blob: 84cf8f6a57617e36078b1425471a397599585ecb [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattner7c5a3d32005-08-16 17:14:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCPredicates.h"
Jim Laskey2f616bf2006-11-16 22:43:37 +000016#include "PPCMachineFunctionInfo.h"
Bill Wendling53351a12010-03-12 02:00:43 +000017#include "PPCPerfectShuffle.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000018#include "PPCTargetMachine.h"
Bill Schmidt240b9b62013-05-13 19:34:37 +000019#include "PPCTargetObjectFile.h"
Owen Anderson718cb662007-09-07 04:06:50 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattnerb9a7bea2007-03-06 00:59:59 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/CallingConv.h"
29#include "llvm/IR/Constants.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/Intrinsics.h"
Chris Lattner4eab7142006-11-10 02:08:47 +000033#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
Craig Topper79aa3412012-03-17 18:46:09 +000035#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Craig Topper79aa3412012-03-17 18:46:09 +000037#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000038using namespace llvm;
39
Bill Schmidt212af6a2013-02-06 17:33:58 +000040static bool CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
41 CCValAssign::LocInfo &LocInfo,
42 ISD::ArgFlagsTy &ArgFlags,
43 CCState &State);
44static bool CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +000045 MVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +000046 CCValAssign::LocInfo &LocInfo,
47 ISD::ArgFlagsTy &ArgFlags,
48 CCState &State);
Bill Schmidt212af6a2013-02-06 17:33:58 +000049static bool CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
50 MVT &LocVT,
51 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
53 CCState &State);
Tilmann Schellerffd02002009-07-03 06:45:56 +000054
Hal Finkel77838f92012-06-04 02:21:00 +000055static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc",
56cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden);
Chris Lattner4eab7142006-11-10 02:08:47 +000057
Hal Finkel71ffcfe2012-06-10 19:32:29 +000058static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref",
59cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden);
60
Hal Finkel2d37f7b2013-03-15 15:27:13 +000061static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned",
62cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden);
63
Chris Lattnerf0144122009-07-28 03:13:23 +000064static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
65 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +000066 return new TargetLoweringObjectFileMachO();
Bill Wendling53351a12010-03-12 02:00:43 +000067
Bill Schmidt240b9b62013-05-13 19:34:37 +000068 if (TM.getSubtargetImpl()->isSVR4ABI())
69 return new PPC64LinuxTargetObjectFile();
70
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000071 return new TargetLoweringObjectFileELF();
Chris Lattnerf0144122009-07-28 03:13:23 +000072}
73
Chris Lattner331d1bc2006-11-02 01:44:04 +000074PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000075 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Evan Cheng769951f2012-07-02 22:39:56 +000076 const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>();
Hal Finkel7ee74a62013-03-21 21:37:52 +000077 PPCRegInfo = TM.getRegisterInfo();
Hal Finkelff56d1a2013-04-05 23:29:01 +000078 PPCII = TM.getInstrInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +000079
Nate Begeman405e3ec2005-10-21 00:02:42 +000080 setPow2DivIsCheap();
Dale Johannesen72324642008-07-31 18:13:12 +000081
Chris Lattnerd145a612005-09-27 22:18:25 +000082 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000083 setUseUnderscoreSetJmp(true);
84 setUseUnderscoreLongJmp(true);
Scott Michelfdc40a02009-02-17 22:15:04 +000085
Chris Lattner749dc722010-10-10 18:34:00 +000086 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
87 // arguments are at least 4/8 bytes aligned.
Evan Cheng769951f2012-07-02 22:39:56 +000088 bool isPPC64 = Subtarget->isPPC64();
89 setMinStackArgumentAlignment(isPPC64 ? 8:4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000090
Chris Lattner7c5a3d32005-08-16 17:14:42 +000091 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +000092 addRegisterClass(MVT::i32, &PPC::GPRCRegClass);
93 addRegisterClass(MVT::f32, &PPC::F4RCRegClass);
94 addRegisterClass(MVT::f64, &PPC::F8RCRegClass);
Scott Michelfdc40a02009-02-17 22:15:04 +000095
Evan Chengc5484282006-10-04 00:56:09 +000096 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
98 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000099
Owen Anderson825b72b2009-08-11 20:47:22 +0000100 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000101
Chris Lattner94e509c2006-11-10 23:58:45 +0000102 // PowerPC has pre-inc load and store's.
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
104 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
105 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
106 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
107 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
108 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
109 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
110 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
111 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
112 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Chengcd633192006-11-09 19:11:50 +0000113
Dale Johannesen6eaeff22007-10-10 01:01:31 +0000114 // This is used in the ppcf128->int sequence. Note it has different semantics
115 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +0000117
Roman Divacky0016f732012-08-16 18:19:29 +0000118 // We do not currently implement these libm ops for PowerPC.
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000119 setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand);
120 setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand);
121 setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand);
122 setOperationAction(ISD::FRINT, MVT::ppcf128, Expand);
123 setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand);
Bill Schmidtcd7a1552013-04-03 13:05:44 +0000124 setOperationAction(ISD::FREM, MVT::ppcf128, Expand);
Owen Anderson4a4fdf32011-12-08 19:32:14 +0000125
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000126 // PowerPC has no SREM/UREM instructions
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setOperationAction(ISD::SREM, MVT::i32, Expand);
128 setOperationAction(ISD::UREM, MVT::i32, Expand);
129 setOperationAction(ISD::SREM, MVT::i64, Expand);
130 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +0000131
132 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
134 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
135 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
136 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
137 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
138 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
139 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
140 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000141
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000142 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 setOperationAction(ISD::FSIN , MVT::f64, Expand);
144 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000145 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 setOperationAction(ISD::FREM , MVT::f64, Expand);
147 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Hal Finkel070b8db2012-06-22 00:49:52 +0000148 setOperationAction(ISD::FMA , MVT::f64, Legal);
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 setOperationAction(ISD::FSIN , MVT::f32, Expand);
150 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000151 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000152 setOperationAction(ISD::FREM , MVT::f32, Expand);
153 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Hal Finkel070b8db2012-06-22 00:49:52 +0000154 setOperationAction(ISD::FMA , MVT::f32, Legal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000155
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000157
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000158 // If we're enabling GP optimizations, use hardware square root
Hal Finkel827307b2013-04-03 04:01:11 +0000159 if (!Subtarget->hasFSQRT() &&
160 !(TM.Options.UnsafeFPMath &&
161 Subtarget->hasFRSQRTE() && Subtarget->hasFRE()))
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
Hal Finkel827307b2013-04-03 04:01:11 +0000163
164 if (!Subtarget->hasFSQRT() &&
165 !(TM.Options.UnsafeFPMath &&
166 Subtarget->hasFRSQRTES() && Subtarget->hasFRES()))
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000168
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
170 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000171
Hal Finkelf5d5c432013-03-29 08:57:48 +0000172 if (Subtarget->hasFPRND()) {
173 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
174 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
175 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
176
177 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
178 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
179 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
180
181 // frin does not implement "ties to even." Thus, this is safe only in
182 // fast-math mode.
183 if (TM.Options.UnsafeFPMath) {
184 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
185 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
Hal Finkel0882fd62013-03-29 19:41:55 +0000186
187 // These need to set FE_INEXACT, and use a custom inserter.
188 setOperationAction(ISD::FRINT, MVT::f64, Legal);
189 setOperationAction(ISD::FRINT, MVT::f32, Legal);
Hal Finkelf5d5c432013-03-29 08:57:48 +0000190 }
191 }
192
Nate Begemand88fc032006-01-14 03:14:10 +0000193 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000196 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
197 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000200 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
201 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000202
Hal Finkelc53ab4d2013-03-28 13:29:47 +0000203 if (Subtarget->hasPOPCNTD()) {
Hal Finkel1fce8832013-04-01 15:58:15 +0000204 setOperationAction(ISD::CTPOP, MVT::i32 , Legal);
Hal Finkelc53ab4d2013-03-28 13:29:47 +0000205 setOperationAction(ISD::CTPOP, MVT::i64 , Legal);
206 } else {
207 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
208 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
209 }
210
Nate Begeman35ef9132006-01-11 21:21:00 +0000211 // PowerPC does not have ROTR
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
213 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000214
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000215 // PowerPC does not have Select
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::SELECT, MVT::i32, Expand);
217 setOperationAction(ISD::SELECT, MVT::i64, Expand);
218 setOperationAction(ISD::SELECT, MVT::f32, Expand);
219 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000220
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000221 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
223 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000224
Nate Begeman750ac1b2006-02-01 07:19:44 +0000225 // PowerPC wants to optimize integer setcc a bit
Owen Anderson825b72b2009-08-11 20:47:22 +0000226 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000227
Nate Begeman81e80972006-03-17 01:40:33 +0000228 // PowerPC does not have BRCOND which requires SetCC
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000230
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000232
Chris Lattnerf7605322005-08-31 21:09:52 +0000233 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000235
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000236 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
238 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000239
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000240 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
241 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
242 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
243 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000244
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000245 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000247
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
249 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
250 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
251 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000252
Hal Finkele9150472013-03-27 19:10:42 +0000253 // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
Hal Finkel7ee74a62013-03-21 21:37:52 +0000254 // SjLj exception handling but a light-weight setjmp/longjmp replacement to
255 // support continuation, user-level threading, and etc.. As a result, no
256 // other SjLj exception interfaces are implemented and please don't build
257 // your own exception handling based on them.
258 // LLVM/Clang supports zero-cost DWARF exception handling.
259 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
260 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000261
262 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman28a6b022005-12-10 02:36:00 +0000263 // appropriate instructions to materialize the address.
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
265 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000266 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
268 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
269 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
270 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000271 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
273 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000274
Nate Begeman1db3c922008-08-11 17:36:31 +0000275 // TRAP is legal.
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling77959322008-09-17 00:30:57 +0000277
278 // TRAMPOLINE is custom lowered.
Duncan Sands4a544a72011-09-06 13:37:06 +0000279 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
280 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling77959322008-09-17 00:30:57 +0000281
Nate Begemanacc398c2006-01-25 18:21:52 +0000282 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000284
Evan Cheng769951f2012-07-02 22:39:56 +0000285 if (Subtarget->isSVR4ABI()) {
286 if (isPPC64) {
Hal Finkel179a4dd2012-03-24 03:53:55 +0000287 // VAARG always uses double-word chunks, so promote anything smaller.
288 setOperationAction(ISD::VAARG, MVT::i1, Promote);
289 AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64);
290 setOperationAction(ISD::VAARG, MVT::i8, Promote);
291 AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64);
292 setOperationAction(ISD::VAARG, MVT::i16, Promote);
293 AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64);
294 setOperationAction(ISD::VAARG, MVT::i32, Promote);
295 AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64);
296 setOperationAction(ISD::VAARG, MVT::Other, Expand);
297 } else {
298 // VAARG is custom lowered with the 32-bit SVR4 ABI.
299 setOperationAction(ISD::VAARG, MVT::Other, Custom);
300 setOperationAction(ISD::VAARG, MVT::i64, Custom);
301 }
Roman Divackybdb226e2011-06-28 15:30:42 +0000302 } else
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000304
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000305 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
307 setOperationAction(ISD::VAEND , MVT::Other, Expand);
308 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
309 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
310 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
311 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000312
Chris Lattner6d92cad2006-03-26 10:06:40 +0000313 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000316 // To handle counter-based loop conditions.
317 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom);
318
Dale Johannesen53e4e442008-11-07 22:54:33 +0000319 // Comparisons that require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
321 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
322 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
323 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
324 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
325 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
326 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
327 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
328 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
329 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
330 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
331 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000332
Evan Cheng769951f2012-07-02 22:39:56 +0000333 if (Subtarget->has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000334 // They also have instructions for converting between i64 and fp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
336 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
337 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
338 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen4c9369d2009-06-04 20:53:52 +0000339 // This is just the low 32 bits of a (signed) fp->i64 conversion.
340 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000342
Hal Finkel46479192013-04-01 17:52:07 +0000343 if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64())
Hal Finkel9ad0f492013-03-31 01:58:02 +0000344 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000345 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000346 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000348 }
349
Hal Finkel46479192013-04-01 17:52:07 +0000350 // With the instructions enabled under FPCVT, we can do everything.
351 if (PPCSubTarget.hasFPCVT()) {
352 if (Subtarget->has64BitSupport()) {
353 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
354 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
355 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
356 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
357 }
358
359 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
360 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
361 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
362 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
363 }
364
Evan Cheng769951f2012-07-02 22:39:56 +0000365 if (Subtarget->use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000366 // 64-bit PowerPC implementations can support i64 types directly
Craig Topperc9099502012-04-20 06:31:50 +0000367 addRegisterClass(MVT::i64, &PPC::G8RCRegClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000368 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000370 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
372 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
373 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000374 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000375 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
377 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
378 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000379 }
Evan Chengd30bf012006-03-01 01:11:20 +0000380
Evan Cheng769951f2012-07-02 22:39:56 +0000381 if (Subtarget->hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000382 // First set operation action for all vector types to expand. Then we
383 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
385 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
386 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000387
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000388 // add/sub are legal for all supported vector VT's.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000389 setOperationAction(ISD::ADD , VT, Legal);
390 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000391
Chris Lattner7ff7e672006-04-04 17:25:31 +0000392 // We promote all shuffles to v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000393 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000395
396 // We promote all non-typed operations to v4i32.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000397 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000399 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000401 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000403 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000405 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000407 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000409
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000410 // No other operations are legal.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000411 setOperationAction(ISD::MUL , VT, Expand);
412 setOperationAction(ISD::SDIV, VT, Expand);
413 setOperationAction(ISD::SREM, VT, Expand);
414 setOperationAction(ISD::UDIV, VT, Expand);
415 setOperationAction(ISD::UREM, VT, Expand);
416 setOperationAction(ISD::FDIV, VT, Expand);
417 setOperationAction(ISD::FNEG, VT, Expand);
Craig Topper44e394c2012-11-15 08:02:19 +0000418 setOperationAction(ISD::FSQRT, VT, Expand);
419 setOperationAction(ISD::FLOG, VT, Expand);
420 setOperationAction(ISD::FLOG10, VT, Expand);
421 setOperationAction(ISD::FLOG2, VT, Expand);
422 setOperationAction(ISD::FEXP, VT, Expand);
423 setOperationAction(ISD::FEXP2, VT, Expand);
424 setOperationAction(ISD::FSIN, VT, Expand);
425 setOperationAction(ISD::FCOS, VT, Expand);
426 setOperationAction(ISD::FABS, VT, Expand);
427 setOperationAction(ISD::FPOWI, VT, Expand);
Craig Topper1ab489a2012-11-14 08:11:25 +0000428 setOperationAction(ISD::FFLOOR, VT, Expand);
Craig Topper49010472012-11-15 06:51:10 +0000429 setOperationAction(ISD::FCEIL, VT, Expand);
430 setOperationAction(ISD::FTRUNC, VT, Expand);
431 setOperationAction(ISD::FRINT, VT, Expand);
432 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000433 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
434 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
435 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
436 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
437 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
438 setOperationAction(ISD::UDIVREM, VT, Expand);
439 setOperationAction(ISD::SDIVREM, VT, Expand);
440 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
441 setOperationAction(ISD::FPOW, VT, Expand);
442 setOperationAction(ISD::CTPOP, VT, Expand);
443 setOperationAction(ISD::CTLZ, VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000444 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000445 setOperationAction(ISD::CTTZ, VT, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000446 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
Benjamin Kramer91223a42012-12-19 15:49:14 +0000447 setOperationAction(ISD::VSELECT, VT, Expand);
Adhemerval Zanellacfe09ed2012-11-05 17:15:56 +0000448 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
449
450 for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
451 j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) {
452 MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j;
453 setTruncStoreAction(VT, InnerVT, Expand);
454 }
455 setLoadExtAction(ISD::SEXTLOAD, VT, Expand);
456 setLoadExtAction(ISD::ZEXTLOAD, VT, Expand);
457 setLoadExtAction(ISD::EXTLOAD, VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000458 }
459
Chris Lattner7ff7e672006-04-04 17:25:31 +0000460 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
461 // with merges, splats, etc.
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000463
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::AND , MVT::v4i32, Legal);
465 setOperationAction(ISD::OR , MVT::v4i32, Legal);
466 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
467 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
468 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
469 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Adhemerval Zanella51aaadb2012-10-08 17:27:24 +0000470 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
471 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
472 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
473 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
Adhemerval Zanellae95ed2b2012-11-15 20:56:03 +0000474 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
475 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
476 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
477 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000478
Craig Topperc9099502012-04-20 06:31:50 +0000479 addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass);
480 addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass);
481 addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass);
482 addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass);
Scott Michelfdc40a02009-02-17 22:15:04 +0000483
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Hal Finkel070b8db2012-06-22 00:49:52 +0000485 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
Hal Finkel827307b2013-04-03 04:01:11 +0000486
487 if (TM.Options.UnsafeFPMath) {
488 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
489 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
490 }
491
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
493 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
494 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000495
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
497 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000498
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
500 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
501 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
502 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Adhemerval Zanella5f41fd62012-10-30 13:50:19 +0000503
504 // Altivec does not contain unordered floating-point compare instructions
505 setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand);
506 setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand);
507 setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand);
508 setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand);
509 setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand);
510 setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000511 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000512
Hal Finkel8cc34742012-08-04 14:10:46 +0000513 if (Subtarget->has64BitSupport()) {
Hal Finkel19aa2b52012-04-01 20:08:17 +0000514 setOperationAction(ISD::PREFETCH, MVT::Other, Legal);
Hal Finkel8cc34742012-08-04 14:10:46 +0000515 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
516 }
Hal Finkel19aa2b52012-04-01 20:08:17 +0000517
Eli Friedman4db5aca2011-08-29 18:23:02 +0000518 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
519 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
Hal Finkelcd9ea512012-12-25 17:22:53 +0000520 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
521 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
Eli Friedman4db5aca2011-08-29 18:23:02 +0000522
Duncan Sands03228082008-11-23 15:47:28 +0000523 setBooleanContents(ZeroOrOneBooleanContent);
Bill Schmidtfa799112013-04-23 18:49:44 +0000524 // Altivec instructions set fields to all zeros or all ones.
525 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Scott Michelfdc40a02009-02-17 22:15:04 +0000526
Evan Cheng769951f2012-07-02 22:39:56 +0000527 if (isPPC64) {
Chris Lattner10da9572006-10-18 01:20:43 +0000528 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000529 setExceptionPointerRegister(PPC::X3);
530 setExceptionSelectorRegister(PPC::X4);
531 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000532 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000533 setExceptionPointerRegister(PPC::R3);
534 setExceptionSelectorRegister(PPC::R4);
535 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000536
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000537 // We have target-specific dag combine patterns for the following nodes:
538 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000539 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000540 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000541 setTargetDAGCombine(ISD::BSWAP);
Scott Michelfdc40a02009-02-17 22:15:04 +0000542
Hal Finkel827307b2013-04-03 04:01:11 +0000543 // Use reciprocal estimates.
544 if (TM.Options.UnsafeFPMath) {
545 setTargetDAGCombine(ISD::FDIV);
546 setTargetDAGCombine(ISD::FSQRT);
547 }
548
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000549 // Darwin long double math library functions have $LDBL128 appended.
Evan Cheng769951f2012-07-02 22:39:56 +0000550 if (Subtarget->isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000551 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000552 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
553 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000554 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
555 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000556 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
557 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
558 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
559 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
560 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000561 }
562
Hal Finkelc6129162011-10-17 18:53:03 +0000563 setMinFunctionAlignment(2);
564 if (PPCSubTarget.isDarwin())
565 setPrefFunctionAlignment(4);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000566
Evan Cheng769951f2012-07-02 22:39:56 +0000567 if (isPPC64 && Subtarget->isJITCodeModel())
568 // Temporary workaround for the inability of PPC64 JIT to handle jump
569 // tables.
570 setSupportJumpTables(false);
571
Eli Friedman26689ac2011-08-03 21:06:02 +0000572 setInsertFencesForAtomic(true);
573
Hal Finkel768c65f2011-11-22 16:21:04 +0000574 setSchedulingPreference(Sched::Hybrid);
575
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000576 computeRegisterProperties();
Hal Finkel621b77a2012-08-28 16:12:39 +0000577
578 // The Freescale cores does better with aggressive inlining of memcpy and
579 // friends. Gcc uses same threshold of 128 bytes (= 32 word stores).
580 if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc ||
581 Subtarget->getDarwinDirective() == PPC::DIR_E5500) {
Jim Grosbach3450f802013-02-20 21:13:59 +0000582 MaxStoresPerMemset = 32;
583 MaxStoresPerMemsetOptSize = 16;
584 MaxStoresPerMemcpy = 32;
585 MaxStoresPerMemcpyOptSize = 8;
586 MaxStoresPerMemmove = 32;
587 MaxStoresPerMemmoveOptSize = 8;
Hal Finkel621b77a2012-08-28 16:12:39 +0000588
589 setPrefFunctionAlignment(4);
Hal Finkel621b77a2012-08-28 16:12:39 +0000590 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000591}
592
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000593/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
594/// function arguments in the caller parameter area.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000595unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +0000596 const TargetMachine &TM = getTargetMachine();
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000597 // Darwin passes everything on 4 byte boundary.
598 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
599 return 4;
Roman Divacky466958c2012-04-02 15:49:30 +0000600
601 // 16byte and wider vectors are passed on 16byte boundary.
602 if (VectorType *VTy = dyn_cast<VectorType>(Ty))
603 if (VTy->getBitWidth() >= 128)
604 return 16;
605
606 // The rest is 8 on PPC64 and 4 on PPC32 boundary.
607 if (PPCSubTarget.isPPC64())
608 return 8;
609
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000610 return 4;
611}
612
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000613const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
614 switch (Opcode) {
615 default: return 0;
Evan Cheng53301922008-07-12 02:23:19 +0000616 case PPCISD::FSEL: return "PPCISD::FSEL";
617 case PPCISD::FCFID: return "PPCISD::FCFID";
618 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
619 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Hal Finkel827307b2013-04-03 04:01:11 +0000620 case PPCISD::FRE: return "PPCISD::FRE";
621 case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE";
Evan Cheng53301922008-07-12 02:23:19 +0000622 case PPCISD::STFIWX: return "PPCISD::STFIWX";
623 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
624 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
625 case PPCISD::VPERM: return "PPCISD::VPERM";
626 case PPCISD::Hi: return "PPCISD::Hi";
627 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000628 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller3a84dae2009-12-18 13:00:15 +0000629 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
630 case PPCISD::LOAD: return "PPCISD::LOAD";
631 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng53301922008-07-12 02:23:19 +0000632 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
633 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
634 case PPCISD::SRL: return "PPCISD::SRL";
635 case PPCISD::SRA: return "PPCISD::SRA";
636 case PPCISD::SHL: return "PPCISD::SHL";
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000637 case PPCISD::CALL: return "PPCISD::CALL";
638 case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP";
Evan Cheng53301922008-07-12 02:23:19 +0000639 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000640 case PPCISD::BCTRL: return "PPCISD::BCTRL";
Evan Cheng53301922008-07-12 02:23:19 +0000641 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Hal Finkel7ee74a62013-03-21 21:37:52 +0000642 case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP";
643 case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP";
Evan Cheng53301922008-07-12 02:23:19 +0000644 case PPCISD::MFCR: return "PPCISD::MFCR";
645 case PPCISD::VCMP: return "PPCISD::VCMP";
646 case PPCISD::VCMPo: return "PPCISD::VCMPo";
647 case PPCISD::LBRX: return "PPCISD::LBRX";
648 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng53301922008-07-12 02:23:19 +0000649 case PPCISD::LARX: return "PPCISD::LARX";
650 case PPCISD::STCX: return "PPCISD::STCX";
651 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
Hal Finkelb1fd3cd2013-05-15 21:37:41 +0000652 case PPCISD::BDNZ: return "PPCISD::BDNZ";
653 case PPCISD::BDZ: return "PPCISD::BDZ";
Evan Cheng53301922008-07-12 02:23:19 +0000654 case PPCISD::MFFS: return "PPCISD::MFFS";
Evan Cheng53301922008-07-12 02:23:19 +0000655 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
Evan Cheng53301922008-07-12 02:23:19 +0000656 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Hal Finkel82b38212012-08-28 02:10:27 +0000657 case PPCISD::CR6SET: return "PPCISD::CR6SET";
658 case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET";
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000659 case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA";
660 case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L";
661 case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L";
Bill Schmidtb453e162012-12-14 17:02:38 +0000662 case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA";
663 case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L";
Bill Schmidtd7802bf2012-12-04 16:18:08 +0000664 case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS";
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000665 case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA";
666 case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L";
667 case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR";
Bill Schmidt349c2782012-12-12 19:29:35 +0000668 case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA";
669 case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L";
670 case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR";
671 case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA";
672 case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L";
Bill Schmidtb34c79e2013-02-20 15:50:31 +0000673 case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT";
Bill Schmidt5bbdb192013-05-14 19:35:45 +0000674 case PPCISD::SC: return "PPCISD::SC";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000675 }
676}
677
Duncan Sands28b77e92011-09-06 19:07:46 +0000678EVT PPCTargetLowering::getSetCCResultType(EVT VT) const {
Adhemerval Zanella1c7d69b2012-10-08 18:59:53 +0000679 if (!VT.isVector())
680 return MVT::i32;
681 return VT.changeVectorElementTypeToInteger();
Scott Michel5b8f82e2008-03-10 15:42:14 +0000682}
683
Chris Lattner1a635d62006-04-14 06:01:58 +0000684//===----------------------------------------------------------------------===//
685// Node matching predicates, for use by the tblgen matching code.
686//===----------------------------------------------------------------------===//
687
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000688/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000689static bool isFloatingPointZero(SDValue Op) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000690 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000691 return CFP->getValueAPF().isZero();
Gabor Greifba36cb52008-08-28 21:40:38 +0000692 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000693 // Maybe this has already been legalized into the constant pool?
694 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohman46510a72010-04-15 01:51:59 +0000695 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000696 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000697 }
698 return false;
699}
700
Chris Lattnerddb739e2006-04-06 17:23:16 +0000701/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
702/// true if Op is undef or if it matches the specified value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000703static bool isConstantOrUndef(int Op, int Val) {
704 return Op < 0 || Op == Val;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000705}
706
707/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
708/// VPKUHUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000709bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000710 if (!isUnary) {
711 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000712 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000713 return false;
714 } else {
715 for (unsigned i = 0; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000716 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
717 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000718 return false;
719 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000720 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000721}
722
723/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
724/// VPKUWUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000725bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000726 if (!isUnary) {
727 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000728 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
729 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000730 return false;
731 } else {
732 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000733 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
734 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
735 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
736 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000737 return false;
738 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000739 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000740}
741
Chris Lattnercaad1632006-04-06 22:02:42 +0000742/// isVMerge - Common function, used to match vmrg* shuffles.
743///
Nate Begeman9008ca62009-04-27 18:41:29 +0000744static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnercaad1632006-04-06 22:02:42 +0000745 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000747 "PPC only supports shuffles by bytes!");
Chris Lattner116cc482006-04-06 21:11:54 +0000748 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
749 "Unsupported merge size!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000750
Chris Lattner116cc482006-04-06 21:11:54 +0000751 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
752 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman9008ca62009-04-27 18:41:29 +0000753 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000754 LHSStart+j+i*UnitSize) ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000755 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000756 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000757 return false;
758 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000759 return true;
Chris Lattnercaad1632006-04-06 22:02:42 +0000760}
761
762/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
763/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000764bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000765 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000766 if (!isUnary)
767 return isVMerge(N, UnitSize, 8, 24);
768 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000769}
770
771/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
772/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000773bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000774 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000775 if (!isUnary)
776 return isVMerge(N, UnitSize, 0, 16);
777 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000778}
779
780
Chris Lattnerd0608e12006-04-06 18:26:28 +0000781/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
782/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000783int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000785 "PPC only supports shuffles by bytes!");
786
787 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000788
Chris Lattnerd0608e12006-04-06 18:26:28 +0000789 // Find the first non-undef value in the shuffle mask.
790 unsigned i;
Nate Begeman9008ca62009-04-27 18:41:29 +0000791 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattnerd0608e12006-04-06 18:26:28 +0000792 /*search*/;
Scott Michelfdc40a02009-02-17 22:15:04 +0000793
Chris Lattnerd0608e12006-04-06 18:26:28 +0000794 if (i == 16) return -1; // all undef.
Scott Michelfdc40a02009-02-17 22:15:04 +0000795
Nate Begeman9008ca62009-04-27 18:41:29 +0000796 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattnerd0608e12006-04-06 18:26:28 +0000797 // numbered from this value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000798 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattnerd0608e12006-04-06 18:26:28 +0000799 if (ShiftAmt < i) return -1;
800 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000801
Chris Lattnerf24380e2006-04-06 22:28:36 +0000802 if (!isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000803 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000804 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000805 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000806 return -1;
807 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000808 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000809 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000810 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000811 return -1;
812 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000813 return ShiftAmt;
814}
Chris Lattneref819f82006-03-20 06:33:01 +0000815
816/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
817/// specifies a splat of a single element that is suitable for input to
818/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman9008ca62009-04-27 18:41:29 +0000819bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner7ff7e672006-04-04 17:25:31 +0000821 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelfdc40a02009-02-17 22:15:04 +0000822
Chris Lattner88a99ef2006-03-20 06:37:44 +0000823 // This is a splat operation if each element of the permute is the same, and
824 // if the value doesn't reference the second vector.
Nate Begeman9008ca62009-04-27 18:41:29 +0000825 unsigned ElementBase = N->getMaskElt(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000826
Nate Begeman9008ca62009-04-27 18:41:29 +0000827 // FIXME: Handle UNDEF elements too!
828 if (ElementBase >= 16)
Chris Lattner7ff7e672006-04-04 17:25:31 +0000829 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000830
Nate Begeman9008ca62009-04-27 18:41:29 +0000831 // Check that the indices are consecutive, in the case of a multi-byte element
832 // splatted with a v16i8 mask.
833 for (unsigned i = 1; i != EltSize; ++i)
834 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000835 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000836
Chris Lattner7ff7e672006-04-04 17:25:31 +0000837 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000838 if (N->getMaskElt(i) < 0) continue;
Chris Lattner7ff7e672006-04-04 17:25:31 +0000839 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman9008ca62009-04-27 18:41:29 +0000840 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000841 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000842 }
Chris Lattner7ff7e672006-04-04 17:25:31 +0000843 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000844}
845
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000846/// isAllNegativeZeroVector - Returns true if all elements of build_vector
847/// are -0.0.
848bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000849 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
850
851 APInt APVal, APUndef;
852 unsigned BitSize;
853 bool HasAnyUndefs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000854
Dale Johannesen1e608812009-11-13 01:45:18 +0000855 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman9008ca62009-04-27 18:41:29 +0000856 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000857 return CFP->getValueAPF().isNegZero();
Nate Begeman9008ca62009-04-27 18:41:29 +0000858
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000859 return false;
860}
861
Chris Lattneref819f82006-03-20 06:33:01 +0000862/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
863/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000864unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000865 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
866 assert(isSplatShuffleMask(SVOp, EltSize));
867 return SVOp->getMaskElt(0) / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000868}
869
Chris Lattnere87192a2006-04-12 17:37:20 +0000870/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000871/// by using a vspltis[bhw] instruction of the specified element size, return
872/// the constant being splatted. The ByteSize field indicates the number of
873/// bytes of each element [124] -> [bhw].
Dan Gohman475871a2008-07-27 21:46:04 +0000874SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
875 SDValue OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000876
877 // If ByteSize of the splat is bigger than the element size of the
878 // build_vector, then we have a case where we are checking for a splat where
879 // multiple elements of the buildvector are folded together into a single
880 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
881 unsigned EltSize = 16/N->getNumOperands();
882 if (EltSize < ByteSize) {
883 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman475871a2008-07-27 21:46:04 +0000884 SDValue UniquedVals[4];
Chris Lattner79d9a882006-04-08 07:14:26 +0000885 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelfdc40a02009-02-17 22:15:04 +0000886
Chris Lattner79d9a882006-04-08 07:14:26 +0000887 // See if all of the elements in the buildvector agree across.
888 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
889 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
890 // If the element isn't a constant, bail fully out.
Dan Gohman475871a2008-07-27 21:46:04 +0000891 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000892
Scott Michelfdc40a02009-02-17 22:15:04 +0000893
Gabor Greifba36cb52008-08-28 21:40:38 +0000894 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000895 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
896 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000897 return SDValue(); // no match.
Chris Lattner79d9a882006-04-08 07:14:26 +0000898 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000899
Chris Lattner79d9a882006-04-08 07:14:26 +0000900 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
901 // either constant or undef values that are identical for each chunk. See
902 // if these chunks can form into a larger vspltis*.
Scott Michelfdc40a02009-02-17 22:15:04 +0000903
Chris Lattner79d9a882006-04-08 07:14:26 +0000904 // Check to see if all of the leading entries are either 0 or -1. If
905 // neither, then this won't fit into the immediate field.
906 bool LeadingZero = true;
907 bool LeadingOnes = true;
908 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000909 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelfdc40a02009-02-17 22:15:04 +0000910
Chris Lattner79d9a882006-04-08 07:14:26 +0000911 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
912 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
913 }
914 // Finally, check the least significant entry.
915 if (LeadingZero) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000916 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000918 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000919 if (Val < 16)
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattner79d9a882006-04-08 07:14:26 +0000921 }
922 if (LeadingOnes) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000923 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman7810bfe2008-09-26 21:54:37 +0000925 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000926 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattner79d9a882006-04-08 07:14:26 +0000928 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000929
Dan Gohman475871a2008-07-27 21:46:04 +0000930 return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000931 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000932
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000933 // Check to see if this buildvec has a single non-undef value in its elements.
934 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
935 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +0000936 if (OpVal.getNode() == 0)
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000937 OpVal = N->getOperand(i);
938 else if (OpVal != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000939 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000940 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000941
Gabor Greifba36cb52008-08-28 21:40:38 +0000942 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelfdc40a02009-02-17 22:15:04 +0000943
Eli Friedman1a8229b2009-05-24 02:03:36 +0000944 unsigned ValSizeInBytes = EltSize;
Nate Begeman98e70cc2006-03-28 04:15:58 +0000945 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000946 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000947 Value = CN->getZExtValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000948 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000950 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000951 }
952
953 // If the splat value is larger than the element value, then we can never do
954 // this splat. The only case that we could fit the replicated bits into our
955 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman475871a2008-07-27 21:46:04 +0000956 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000957
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000958 // If the element value is larger than the splat value, cut it in half and
959 // check to see if the two halves are equal. Continue doing this until we
960 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
961 while (ValSizeInBytes > ByteSize) {
962 ValSizeInBytes >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000963
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000964 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000965 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
966 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman475871a2008-07-27 21:46:04 +0000967 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000968 }
969
970 // Properly sign extend the value.
Richard Smith1144af32012-08-24 23:29:28 +0000971 int MaskVal = SignExtend32(Value, ByteSize * 8);
Scott Michelfdc40a02009-02-17 22:15:04 +0000972
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000973 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman475871a2008-07-27 21:46:04 +0000974 if (MaskVal == 0) return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000975
Chris Lattner140a58f2006-04-08 06:46:53 +0000976 // Finally, if this value fits in a 5 bit sext field, return it
Richard Smith1144af32012-08-24 23:29:28 +0000977 if (SignExtend32<5>(MaskVal) == MaskVal)
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000979 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000980}
981
Chris Lattner1a635d62006-04-14 06:01:58 +0000982//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000983// Addressing Mode Selection
984//===----------------------------------------------------------------------===//
985
986/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
987/// or 64-bit immediate, and if the value can be accurately represented as a
988/// sign extension from a 16-bit value. If so, this returns true and the
989/// immediate.
990static bool isIntS16Immediate(SDNode *N, short &Imm) {
991 if (N->getOpcode() != ISD::Constant)
992 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000993
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000994 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +0000995 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000996 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000997 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000998 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000999}
Dan Gohman475871a2008-07-27 21:46:04 +00001000static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001001 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001002}
1003
1004
1005/// SelectAddressRegReg - Given the specified addressed, check to see if it
1006/// can be represented as an indexed [r+r] operation. Returns false if it
1007/// can be more efficiently represented with [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +00001008bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
1009 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +00001010 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001011 short imm = 0;
1012 if (N.getOpcode() == ISD::ADD) {
1013 if (isIntS16Immediate(N.getOperand(1), imm))
1014 return false; // r+i
1015 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
1016 return false; // r+i
Scott Michelfdc40a02009-02-17 22:15:04 +00001017
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001018 Base = N.getOperand(0);
1019 Index = N.getOperand(1);
1020 return true;
1021 } else if (N.getOpcode() == ISD::OR) {
1022 if (isIntS16Immediate(N.getOperand(1), imm))
1023 return false; // r+i can fold it if we can.
Scott Michelfdc40a02009-02-17 22:15:04 +00001024
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001025 // If this is an or of disjoint bitfields, we can codegen this as an add
1026 // (for better address arithmetic) if the LHS and RHS of the OR are provably
1027 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001028 APInt LHSKnownZero, LHSKnownOne;
1029 APInt RHSKnownZero, RHSKnownOne;
1030 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001031 LHSKnownZero, LHSKnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001032
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001033 if (LHSKnownZero.getBoolValue()) {
1034 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001035 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001036 // If all of the bits are known zero on the LHS or RHS, the add won't
1037 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +00001038 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001039 Base = N.getOperand(0);
1040 Index = N.getOperand(1);
1041 return true;
1042 }
1043 }
1044 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001046 return false;
1047}
1048
1049/// Returns true if the address N can be represented by a base register plus
1050/// a signed 16-bit displacement [r+imm], and if it is not better
1051/// represented as reg+reg.
Dan Gohman475871a2008-07-27 21:46:04 +00001052bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman73e09142009-01-15 16:29:45 +00001053 SDValue &Base,
1054 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +00001055 // FIXME dl should come from parent load or store, not from address
1056 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001057 // If this can be more profitably realized as r+r, fail.
1058 if (SelectAddressRegReg(N, Disp, Base, DAG))
1059 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001061 if (N.getOpcode() == ISD::ADD) {
1062 short imm = 0;
1063 if (isIntS16Immediate(N.getOperand(1), imm)) {
Ulrich Weigandf0ef8822013-05-16 14:53:05 +00001064 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001065 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1066 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1067 } else {
1068 Base = N.getOperand(0);
1069 }
1070 return true; // [r+i]
1071 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1072 // Match LOAD (ADD (X, Lo(G))).
Gabor Greif413ca0d2012-04-20 11:41:38 +00001073 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001074 && "Cannot handle constant offsets yet!");
1075 Disp = N.getOperand(1).getOperand(0); // The global address.
1076 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
Roman Divackyfd42ed62012-06-04 17:36:38 +00001077 Disp.getOpcode() == ISD::TargetGlobalTLSAddress ||
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001078 Disp.getOpcode() == ISD::TargetConstantPool ||
1079 Disp.getOpcode() == ISD::TargetJumpTable);
1080 Base = N.getOperand(0);
1081 return true; // [&g+r]
1082 }
1083 } else if (N.getOpcode() == ISD::OR) {
1084 short imm = 0;
1085 if (isIntS16Immediate(N.getOperand(1), imm)) {
1086 // If this is an or of disjoint bitfields, we can codegen this as an add
1087 // (for better address arithmetic) if the LHS and RHS of the OR are
1088 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001089 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001090 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +00001091
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001092 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001093 // If all of the bits are known zero on the LHS or RHS, the add won't
1094 // carry.
1095 Base = N.getOperand(0);
Ulrich Weigandf0ef8822013-05-16 14:53:05 +00001096 Disp = DAG.getTargetConstant(imm, N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001097 return true;
1098 }
1099 }
1100 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1101 // Loading from a constant address.
Scott Michelfdc40a02009-02-17 22:15:04 +00001102
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001103 // If this address fits entirely in a 16-bit sext immediate field, codegen
1104 // this as "d, 0"
1105 short Imm;
1106 if (isIntS16Immediate(CN, Imm)) {
1107 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Hal Finkel76973702013-03-21 23:45:03 +00001108 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1109 CN->getValueType(0));
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001110 return true;
1111 }
Chris Lattnerbc681d62007-02-17 06:44:03 +00001112
1113 // Handle 32-bit sext immediates with LIS + addr mode.
Owen Anderson825b72b2009-08-11 20:47:22 +00001114 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001115 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
1116 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001117
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001118 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +00001119 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001120
Owen Anderson825b72b2009-08-11 20:47:22 +00001121 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
1122 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman602b0c82009-09-25 18:54:59 +00001123 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001124 return true;
1125 }
1126 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001127
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001128 Disp = DAG.getTargetConstant(0, getPointerTy());
1129 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1130 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1131 else
1132 Base = N;
1133 return true; // [r+0]
1134}
1135
1136/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
1137/// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +00001138bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
1139 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +00001140 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001141 // Check to see if we can easily represent this as an [r+r] address. This
1142 // will fail if it thinks that the address is more profitably represented as
1143 // reg+imm, e.g. where imm = 0.
1144 if (SelectAddressRegReg(N, Base, Index, DAG))
1145 return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00001146
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001147 // If the operand is an addition, always emit this as [r+r], since this is
1148 // better (for code size, and execution, as the memop does the add for free)
1149 // than emitting an explicit add.
1150 if (N.getOpcode() == ISD::ADD) {
1151 Base = N.getOperand(0);
1152 Index = N.getOperand(1);
1153 return true;
1154 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001155
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001156 // Otherwise, do it the hard way, using R0 as the base register.
Hal Finkel76973702013-03-21 23:45:03 +00001157 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1158 N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001159 Index = N;
1160 return true;
1161}
1162
1163/// SelectAddressRegImmShift - Returns true if the address N can be
1164/// represented by a base register plus a signed 14-bit displacement
1165/// [r+imm*4]. Suitable for use by STD and friends.
Dan Gohman475871a2008-07-27 21:46:04 +00001166bool PPCTargetLowering::SelectAddressRegImmShift(SDValue N, SDValue &Disp,
1167 SDValue &Base,
Dan Gohman73e09142009-01-15 16:29:45 +00001168 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +00001169 // FIXME dl should come from the parent load or store, not the address
1170 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001171 // If this can be more profitably realized as r+r, fail.
1172 if (SelectAddressRegReg(N, Disp, Base, DAG))
1173 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001174
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001175 if (N.getOpcode() == ISD::ADD) {
1176 short imm = 0;
1177 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Gabor Greifc77d6782012-04-20 08:58:49 +00001178 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001179 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
1180 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1181 } else {
1182 Base = N.getOperand(0);
1183 }
1184 return true; // [r+i]
1185 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
1186 // Match LOAD (ADD (X, Lo(G))).
Gabor Greif413ca0d2012-04-20 11:41:38 +00001187 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001188 && "Cannot handle constant offsets yet!");
1189 Disp = N.getOperand(1).getOperand(0); // The global address.
1190 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
1191 Disp.getOpcode() == ISD::TargetConstantPool ||
1192 Disp.getOpcode() == ISD::TargetJumpTable);
1193 Base = N.getOperand(0);
1194 return true; // [&g+r]
1195 }
1196 } else if (N.getOpcode() == ISD::OR) {
1197 short imm = 0;
1198 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
1199 // If this is an or of disjoint bitfields, we can codegen this as an add
1200 // (for better address arithmetic) if the LHS and RHS of the OR are
1201 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001202 APInt LHSKnownZero, LHSKnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001203 DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne);
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001204 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001205 // If all of the bits are known zero on the LHS or RHS, the add won't
1206 // carry.
1207 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001208 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001209 return true;
1210 }
1211 }
1212 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001213 // Loading from a constant address. Verify low two bits are clear.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001214 if ((CN->getZExtValue() & 3) == 0) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001215 // If this address fits entirely in a 14-bit sext immediate field, codegen
1216 // this as "d, 0"
1217 short Imm;
1218 if (isIntS16Immediate(CN, Imm)) {
1219 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
Hal Finkel76973702013-03-21 23:45:03 +00001220 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO,
1221 CN->getValueType(0));
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001222 return true;
1223 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001224
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001225 // Fold the low-part of 32-bit absolute addresses into addr mode.
Owen Anderson825b72b2009-08-11 20:47:22 +00001226 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001227 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
1228 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001229
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001230 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +00001231 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
1232 Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
1233 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman602b0c82009-09-25 18:54:59 +00001234 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base),0);
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001235 return true;
1236 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001237 }
1238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001239
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001240 Disp = DAG.getTargetConstant(0, getPointerTy());
1241 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1242 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1243 else
1244 Base = N;
1245 return true; // [r+0]
1246}
1247
1248
1249/// getPreIndexedAddressParts - returns true by value, base pointer and
1250/// offset pointer and addressing mode by reference if the node's address
1251/// can be legally represented as pre-indexed load / store address.
Dan Gohman475871a2008-07-27 21:46:04 +00001252bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1253 SDValue &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001254 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001255 SelectionDAG &DAG) const {
Hal Finkel77838f92012-06-04 02:21:00 +00001256 if (DisablePPCPreinc) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001257
Ulrich Weigand881a7152013-03-22 14:58:48 +00001258 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001259 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00001260 EVT VT;
Hal Finkel08a215c2013-03-18 23:00:58 +00001261 unsigned Alignment;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001262 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1263 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001264 VT = LD->getMemoryVT();
Hal Finkel08a215c2013-03-18 23:00:58 +00001265 Alignment = LD->getAlignment();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001266 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001267 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001268 VT = ST->getMemoryVT();
Hal Finkel08a215c2013-03-18 23:00:58 +00001269 Alignment = ST->getAlignment();
Ulrich Weigand881a7152013-03-22 14:58:48 +00001270 isLoad = false;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001271 } else
1272 return false;
1273
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001274 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001275 if (VT.isVector())
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001276 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001277
Ulrich Weigand881a7152013-03-22 14:58:48 +00001278 if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) {
1279
1280 // Common code will reject creating a pre-inc form if the base pointer
1281 // is a frame index, or if N is a store and the base pointer is either
1282 // the same as or a predecessor of the value being stored. Check for
1283 // those situations here, and try with swapped Base/Offset instead.
1284 bool Swap = false;
1285
1286 if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base))
1287 Swap = true;
1288 else if (!isLoad) {
1289 SDValue Val = cast<StoreSDNode>(N)->getValue();
1290 if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode()))
1291 Swap = true;
1292 }
1293
1294 if (Swap)
1295 std::swap(Base, Offset);
1296
Hal Finkel0fcdd8b2012-06-20 15:43:03 +00001297 AM = ISD::PRE_INC;
1298 return true;
Hal Finkelac81cc32012-06-19 02:34:32 +00001299 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001300
Chris Lattner0851b4f2006-11-15 19:55:13 +00001301 // LDU/STU use reg+imm*4, others use reg+imm.
Owen Anderson825b72b2009-08-11 20:47:22 +00001302 if (VT != MVT::i64) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001303 // reg + imm
1304 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
1305 return false;
1306 } else {
Hal Finkel08a215c2013-03-18 23:00:58 +00001307 // LDU/STU need an address with at least 4-byte alignment.
1308 if (Alignment < 4)
1309 return false;
1310
Chris Lattner0851b4f2006-11-15 19:55:13 +00001311 // reg + imm * 4.
1312 if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
1313 return false;
1314 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001315
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001316 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001317 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1318 // sext i32 to i64 when addr mode is r+i.
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001320 LD->getExtensionType() == ISD::SEXTLOAD &&
1321 isa<ConstantSDNode>(Offset))
1322 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001323 }
1324
Chris Lattner4eab7142006-11-10 02:08:47 +00001325 AM = ISD::PRE_INC;
1326 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001327}
1328
1329//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001330// LowerOperation implementation
1331//===----------------------------------------------------------------------===//
1332
Chris Lattner1e61e692010-11-15 02:46:57 +00001333/// GetLabelAccessInfo - Return true if we should reference labels using a
1334/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1335static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattner6d2ff122010-11-15 03:13:19 +00001336 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
1337 HiOpFlags = PPCII::MO_HA16;
1338 LoOpFlags = PPCII::MO_LO16;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001339
Chris Lattner1e61e692010-11-15 02:46:57 +00001340 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1341 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001342 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattner1e61e692010-11-15 02:46:57 +00001343 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattner6d2ff122010-11-15 03:13:19 +00001344 if (isPIC) {
1345 HiOpFlags |= PPCII::MO_PIC_FLAG;
1346 LoOpFlags |= PPCII::MO_PIC_FLAG;
1347 }
1348
1349 // If this is a reference to a global value that requires a non-lazy-ptr, make
1350 // sure that instruction lowering adds it.
1351 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1352 HiOpFlags |= PPCII::MO_NLP_FLAG;
1353 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001354
Chris Lattner6d2ff122010-11-15 03:13:19 +00001355 if (GV->hasHiddenVisibility()) {
1356 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1357 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1358 }
1359 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001360
Chris Lattner1e61e692010-11-15 02:46:57 +00001361 return isPIC;
1362}
1363
1364static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1365 SelectionDAG &DAG) {
1366 EVT PtrVT = HiPart.getValueType();
1367 SDValue Zero = DAG.getConstant(0, PtrVT);
1368 DebugLoc DL = HiPart.getDebugLoc();
1369
1370 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1371 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001372
Chris Lattner1e61e692010-11-15 02:46:57 +00001373 // With PIC, the first instruction is actually "GR+hi(&G)".
1374 if (isPIC)
1375 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1376 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001377
Chris Lattner1e61e692010-11-15 02:46:57 +00001378 // Generate non-pic code that has direct accesses to the constant pool.
1379 // The address of the global is just (hi(&g)+lo(&g)).
1380 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1381}
1382
Scott Michelfdc40a02009-02-17 22:15:04 +00001383SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001384 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001385 EVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001386 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001387 const Constant *C = CP->getConstVal();
Chris Lattner1a635d62006-04-14 06:01:58 +00001388
Roman Divacky9fb8b492012-08-24 16:26:02 +00001389 // 64-bit SVR4 ABI code is always position-independent.
1390 // The actual address of the GlobalValue is stored in the TOC.
1391 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1392 SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0);
1393 return DAG.getNode(PPCISD::TOC_ENTRY, CP->getDebugLoc(), MVT::i64, GA,
1394 DAG.getRegister(PPC::X2, MVT::i64));
1395 }
1396
Chris Lattner1e61e692010-11-15 02:46:57 +00001397 unsigned MOHiFlag, MOLoFlag;
1398 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1399 SDValue CPIHi =
1400 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1401 SDValue CPILo =
1402 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1403 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00001404}
1405
Dan Gohmand858e902010-04-17 15:26:15 +00001406SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001407 EVT PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001408 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409
Roman Divacky9fb8b492012-08-24 16:26:02 +00001410 // 64-bit SVR4 ABI code is always position-independent.
1411 // The actual address of the GlobalValue is stored in the TOC.
1412 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1413 SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1414 return DAG.getNode(PPCISD::TOC_ENTRY, JT->getDebugLoc(), MVT::i64, GA,
1415 DAG.getRegister(PPC::X2, MVT::i64));
1416 }
1417
Chris Lattner1e61e692010-11-15 02:46:57 +00001418 unsigned MOHiFlag, MOLoFlag;
1419 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1420 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1421 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1422 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001423}
1424
Dan Gohmand858e902010-04-17 15:26:15 +00001425SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1426 SelectionDAG &DAG) const {
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001427 EVT PtrVT = Op.getValueType();
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001428
Dan Gohman46510a72010-04-15 01:51:59 +00001429 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001430
Chris Lattner1e61e692010-11-15 02:46:57 +00001431 unsigned MOHiFlag, MOLoFlag;
1432 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
Michael Liao6c7ccaa2012-09-12 21:43:09 +00001433 SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag);
1434 SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag);
Chris Lattner1e61e692010-11-15 02:46:57 +00001435 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1436}
1437
Roman Divackyfd42ed62012-06-04 17:36:38 +00001438SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1439 SelectionDAG &DAG) const {
1440
1441 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1442 DebugLoc dl = GA->getDebugLoc();
1443 const GlobalValue *GV = GA->getGlobal();
1444 EVT PtrVT = getPointerTy();
1445 bool is64bit = PPCSubTarget.isPPC64();
1446
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001447 TLSModel::Model Model = getTargetMachine().getTLSModel(GV);
Roman Divackyfd42ed62012-06-04 17:36:38 +00001448
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001449 if (Model == TLSModel::LocalExec) {
1450 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1451 PPCII::MO_TPREL16_HA);
1452 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
1453 PPCII::MO_TPREL16_LO);
1454 SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2,
1455 is64bit ? MVT::i64 : MVT::i32);
1456 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg);
1457 return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi);
1458 }
Roman Divackyfd42ed62012-06-04 17:36:38 +00001459
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001460 if (!is64bit)
1461 llvm_unreachable("only local-exec is currently supported for ppc32");
1462
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001463 if (Model == TLSModel::InitialExec) {
Bill Schmidtdfebc4c2012-12-13 18:45:54 +00001464 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1465 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
Bill Schmidtb453e162012-12-14 17:02:38 +00001466 SDValue TPOffsetHi = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl,
1467 PtrVT, GOTReg, TGA);
1468 SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl,
1469 PtrVT, TGA, TPOffsetHi);
Bill Schmidtdfebc4c2012-12-13 18:45:54 +00001470 return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGA);
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001471 }
Bill Schmidtd7802bf2012-12-04 16:18:08 +00001472
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001473 if (Model == TLSModel::GeneralDynamic) {
1474 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1475 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1476 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT,
1477 GOTReg, TGA);
1478 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT,
1479 GOTEntryHi, TGA);
1480
1481 // We need a chain node, and don't have one handy. The underlying
1482 // call has no side effects, so using the function entry node
1483 // suffices.
1484 SDValue Chain = DAG.getEntryNode();
1485 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1486 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1487 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl,
1488 PtrVT, ParmReg, TGA);
Bill Schmidt349c2782012-12-12 19:29:35 +00001489 // The return value from GET_TLS_ADDR really is in X3 already, but
1490 // some hacks are needed here to tie everything together. The extra
1491 // copies dissolve during subsequent transforms.
Bill Schmidt57ac1f42012-12-11 20:30:11 +00001492 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1493 return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT);
1494 }
1495
Bill Schmidt349c2782012-12-12 19:29:35 +00001496 if (Model == TLSModel::LocalDynamic) {
1497 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0);
1498 SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64);
1499 SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT,
1500 GOTReg, TGA);
1501 SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT,
1502 GOTEntryHi, TGA);
1503
1504 // We need a chain node, and don't have one handy. The underlying
1505 // call has no side effects, so using the function entry node
1506 // suffices.
1507 SDValue Chain = DAG.getEntryNode();
1508 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry);
1509 SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64);
1510 SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl,
1511 PtrVT, ParmReg, TGA);
1512 // The return value from GET_TLSLD_ADDR really is in X3 already, but
1513 // some hacks are needed here to tie everything together. The extra
1514 // copies dissolve during subsequent transforms.
1515 Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr);
1516 SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT,
Bill Schmidt1e18b862012-12-13 20:57:10 +00001517 Chain, ParmReg, TGA);
Bill Schmidt349c2782012-12-12 19:29:35 +00001518 return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA);
1519 }
1520
1521 llvm_unreachable("Unknown TLS model!");
Roman Divackyfd42ed62012-06-04 17:36:38 +00001522}
1523
Chris Lattner1e61e692010-11-15 02:46:57 +00001524SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1525 SelectionDAG &DAG) const {
1526 EVT PtrVT = Op.getValueType();
1527 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
1528 DebugLoc DL = GSDN->getDebugLoc();
1529 const GlobalValue *GV = GSDN->getGlobal();
1530
Chris Lattner1e61e692010-11-15 02:46:57 +00001531 // 64-bit SVR4 ABI code is always position-independent.
1532 // The actual address of the GlobalValue is stored in the TOC.
1533 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1534 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1535 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1536 DAG.getRegister(PPC::X2, MVT::i64));
1537 }
1538
Chris Lattner6d2ff122010-11-15 03:13:19 +00001539 unsigned MOHiFlag, MOLoFlag;
1540 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattner1e61e692010-11-15 02:46:57 +00001541
Chris Lattner6d2ff122010-11-15 03:13:19 +00001542 SDValue GAHi =
1543 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1544 SDValue GALo =
1545 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001546
Chris Lattner6d2ff122010-11-15 03:13:19 +00001547 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001548
Chris Lattner6d2ff122010-11-15 03:13:19 +00001549 // If the global reference is actually to a non-lazy-pointer, we have to do an
1550 // extra load to get the address of the global.
1551 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1552 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001553 false, false, false, 0);
Chris Lattner6d2ff122010-11-15 03:13:19 +00001554 return Ptr;
Chris Lattner1a635d62006-04-14 06:01:58 +00001555}
1556
Dan Gohmand858e902010-04-17 15:26:15 +00001557SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00001558 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001559 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001560
Chris Lattner1a635d62006-04-14 06:01:58 +00001561 // If we're comparing for equality to zero, expose the fact that this is
1562 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1563 // fold the new nodes.
1564 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1565 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Andersone50ed302009-08-10 22:56:29 +00001566 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001567 SDValue Zext = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 if (VT.bitsLT(MVT::i32)) {
1569 VT = MVT::i32;
Dale Johannesenf5d97892009-02-04 01:48:28 +00001570 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001571 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001572 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001573 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1574 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson825b72b2009-08-11 20:47:22 +00001575 DAG.getConstant(Log2b, MVT::i32));
1576 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner1a635d62006-04-14 06:01:58 +00001577 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001578 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner1a635d62006-04-14 06:01:58 +00001579 // optimized. FIXME: revisit this when we can custom lower all setcc
1580 // optimizations.
1581 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman475871a2008-07-27 21:46:04 +00001582 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001583 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001584
Chris Lattner1a635d62006-04-14 06:01:58 +00001585 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001586 // by xor'ing the rhs with the lhs, which is faster than setting a
1587 // condition register, reading it back out, and masking the correct bit. The
1588 // normal approach here uses sub to do this instead of xor. Using xor exposes
1589 // the result to other bit-twiddling opportunities.
Owen Andersone50ed302009-08-10 22:56:29 +00001590 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001591 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001592 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001593 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001594 Op.getOperand(1));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001595 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner1a635d62006-04-14 06:01:58 +00001596 }
Dan Gohman475871a2008-07-27 21:46:04 +00001597 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001598}
1599
Dan Gohman475871a2008-07-27 21:46:04 +00001600SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001601 const PPCSubtarget &Subtarget) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00001602 SDNode *Node = Op.getNode();
1603 EVT VT = Node->getValueType(0);
1604 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1605 SDValue InChain = Node->getOperand(0);
1606 SDValue VAListPtr = Node->getOperand(1);
1607 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1608 DebugLoc dl = Node->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001609
Roman Divackybdb226e2011-06-28 15:30:42 +00001610 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1611
1612 // gpr_index
1613 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1614 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1615 false, false, 0);
1616 InChain = GprIndex.getValue(1);
1617
1618 if (VT == MVT::i64) {
1619 // Check if GprIndex is even
1620 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1621 DAG.getConstant(1, MVT::i32));
1622 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1623 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1624 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1625 DAG.getConstant(1, MVT::i32));
1626 // Align GprIndex to be even if it isn't
1627 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1628 GprIndex);
1629 }
1630
1631 // fpr index is 1 byte after gpr
1632 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1633 DAG.getConstant(1, MVT::i32));
1634
1635 // fpr
1636 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1637 FprPtr, MachinePointerInfo(SV), MVT::i8,
1638 false, false, 0);
1639 InChain = FprIndex.getValue(1);
1640
1641 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1642 DAG.getConstant(8, MVT::i32));
1643
1644 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1645 DAG.getConstant(4, MVT::i32));
1646
1647 // areas
1648 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001649 MachinePointerInfo(), false, false,
1650 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001651 InChain = OverflowArea.getValue(1);
1652
1653 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001654 MachinePointerInfo(), false, false,
1655 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001656 InChain = RegSaveArea.getValue(1);
1657
1658 // select overflow_area if index > 8
1659 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1660 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1661
Roman Divackybdb226e2011-06-28 15:30:42 +00001662 // adjustment constant gpr_index * 4/8
1663 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1664 VT.isInteger() ? GprIndex : FprIndex,
1665 DAG.getConstant(VT.isInteger() ? 4 : 8,
1666 MVT::i32));
1667
1668 // OurReg = RegSaveArea + RegConstant
1669 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1670 RegConstant);
1671
1672 // Floating types are 32 bytes into RegSaveArea
1673 if (VT.isFloatingPoint())
1674 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1675 DAG.getConstant(32, MVT::i32));
1676
1677 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1678 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1679 VT.isInteger() ? GprIndex : FprIndex,
1680 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1681 MVT::i32));
1682
1683 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1684 VT.isInteger() ? VAListPtr : FprPtr,
1685 MachinePointerInfo(SV),
1686 MVT::i8, false, false, 0);
1687
1688 // determine if we should load from reg_save_area or overflow_area
1689 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1690
1691 // increase overflow_area by 4/8 if gpr/fpr > 8
1692 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1693 DAG.getConstant(VT.isInteger() ? 4 : 8,
1694 MVT::i32));
1695
1696 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1697 OverflowAreaPlusN);
1698
1699 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1700 OverflowAreaPtr,
1701 MachinePointerInfo(),
1702 MVT::i32, false, false, 0);
1703
NAKAMURA Takumi25f6b5a2012-08-30 15:52:23 +00001704 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001705 false, false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001706}
1707
Duncan Sands4a544a72011-09-06 13:37:06 +00001708SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1709 SelectionDAG &DAG) const {
1710 return Op.getOperand(0);
1711}
1712
1713SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1714 SelectionDAG &DAG) const {
Bill Wendling77959322008-09-17 00:30:57 +00001715 SDValue Chain = Op.getOperand(0);
1716 SDValue Trmp = Op.getOperand(1); // trampoline
1717 SDValue FPtr = Op.getOperand(2); // nested function
1718 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001719 DebugLoc dl = Op.getDebugLoc();
Bill Wendling77959322008-09-17 00:30:57 +00001720
Owen Andersone50ed302009-08-10 22:56:29 +00001721 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001723 Type *IntPtrTy =
Micah Villmow3574eca2012-10-08 16:38:25 +00001724 DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType(
Chandler Carruthece6c6b2012-11-01 08:07:29 +00001725 *DAG.getContext());
Bill Wendling77959322008-09-17 00:30:57 +00001726
Scott Michelfdc40a02009-02-17 22:15:04 +00001727 TargetLowering::ArgListTy Args;
Bill Wendling77959322008-09-17 00:30:57 +00001728 TargetLowering::ArgListEntry Entry;
1729
1730 Entry.Ty = IntPtrTy;
1731 Entry.Node = Trmp; Args.push_back(Entry);
1732
1733 // TrampSize == (isPPC64 ? 48 : 40);
1734 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson825b72b2009-08-11 20:47:22 +00001735 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling77959322008-09-17 00:30:57 +00001736 Args.push_back(Entry);
1737
1738 Entry.Node = FPtr; Args.push_back(Entry);
1739 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
Bill Wendling77959322008-09-17 00:30:57 +00001741 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001742 TargetLowering::CallLoweringInfo CLI(Chain,
1743 Type::getVoidTy(*DAG.getContext()),
1744 false, false, false, false, 0,
1745 CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001746 /*isTailCall=*/false,
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001747 /*doesNotRet=*/false,
1748 /*isReturnValueUsed=*/true,
Bill Wendling77959322008-09-17 00:30:57 +00001749 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling46ada192010-03-02 01:55:18 +00001750 Args, DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00001751 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Bill Wendling77959322008-09-17 00:30:57 +00001752
Duncan Sands4a544a72011-09-06 13:37:06 +00001753 return CallResult.second;
Bill Wendling77959322008-09-17 00:30:57 +00001754}
1755
Dan Gohman475871a2008-07-27 21:46:04 +00001756SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001757 const PPCSubtarget &Subtarget) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001758 MachineFunction &MF = DAG.getMachineFunction();
1759 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1760
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001761 DebugLoc dl = Op.getDebugLoc();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001762
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001763 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001764 // vastart just stores the address of the VarArgsFrameIndex slot into the
1765 // memory location argument.
Owen Andersone50ed302009-08-10 22:56:29 +00001766 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001767 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001768 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +00001769 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1770 MachinePointerInfo(SV),
David Greene534502d12010-02-15 16:56:53 +00001771 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001772 }
1773
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001774 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray01119992007-04-03 13:59:52 +00001775 // We suppose the given va_list is already allocated.
1776 //
1777 // typedef struct {
1778 // char gpr; /* index into the array of 8 GPRs
1779 // * stored in the register save area
1780 // * gpr=0 corresponds to r3,
1781 // * gpr=1 to r4, etc.
1782 // */
1783 // char fpr; /* index into the array of 8 FPRs
1784 // * stored in the register save area
1785 // * fpr=0 corresponds to f1,
1786 // * fpr=1 to f2, etc.
1787 // */
1788 // char *overflow_arg_area;
1789 // /* location on stack that holds
1790 // * the next overflow argument
1791 // */
1792 // char *reg_save_area;
1793 // /* where r3:r10 and f1:f8 (if saved)
1794 // * are stored
1795 // */
1796 // } va_list[1];
1797
1798
Dan Gohman1e93df62010-04-17 14:41:14 +00001799 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1800 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001801
Nicolas Geoffray01119992007-04-03 13:59:52 +00001802
Owen Andersone50ed302009-08-10 22:56:29 +00001803 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00001804
Dan Gohman1e93df62010-04-17 14:41:14 +00001805 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1806 PtrVT);
1807 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1808 PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001809
Duncan Sands83ec4b62008-06-06 12:08:01 +00001810 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman475871a2008-07-27 21:46:04 +00001811 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001812
Duncan Sands83ec4b62008-06-06 12:08:01 +00001813 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001815
1816 uint64_t FPROffset = 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001817 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001818
Dan Gohman69de1932008-02-06 22:27:42 +00001819 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001820
Nicolas Geoffray01119992007-04-03 13:59:52 +00001821 // Store first byte : number of int regs
Tilmann Schellerffd02002009-07-03 06:45:56 +00001822 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001823 Op.getOperand(1),
1824 MachinePointerInfo(SV),
1825 MVT::i8, false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001826 uint64_t nextOffset = FPROffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001827 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray01119992007-04-03 13:59:52 +00001828 ConstFPROffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001829
Nicolas Geoffray01119992007-04-03 13:59:52 +00001830 // Store second byte : number of float regs
Dan Gohman475871a2008-07-27 21:46:04 +00001831 SDValue secondStore =
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001832 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1833 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene534502d12010-02-15 16:56:53 +00001834 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001835 nextOffset += StackOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001836 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001837
Nicolas Geoffray01119992007-04-03 13:59:52 +00001838 // Store second word : arguments given on stack
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue thirdStore =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001840 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1841 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001842 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001843 nextOffset += FrameOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001844 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001845
1846 // Store third word : arguments given in registers
Chris Lattner6229d0a2010-09-21 18:41:36 +00001847 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1848 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001849 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001850
Chris Lattner1a635d62006-04-14 06:01:58 +00001851}
1852
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001853#include "PPCGenCallingConv.inc"
1854
Bill Schmidt212af6a2013-02-06 17:33:58 +00001855static bool CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1856 CCValAssign::LocInfo &LocInfo,
1857 ISD::ArgFlagsTy &ArgFlags,
1858 CCState &State) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001859 return true;
1860}
1861
Bill Schmidt212af6a2013-02-06 17:33:58 +00001862static bool CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1863 MVT &LocVT,
1864 CCValAssign::LocInfo &LocInfo,
1865 ISD::ArgFlagsTy &ArgFlags,
1866 CCState &State) {
Craig Topperc5eaae42012-03-11 07:57:25 +00001867 static const uint16_t ArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001868 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1869 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1870 };
1871 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001872
Tilmann Schellerffd02002009-07-03 06:45:56 +00001873 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1874
1875 // Skip one register if the first unallocated register has an even register
1876 // number and there are still argument registers available which have not been
1877 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1878 // need to skip a register if RegNum is odd.
1879 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1880 State.AllocateReg(ArgRegs[RegNum]);
1881 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001882
Tilmann Schellerffd02002009-07-03 06:45:56 +00001883 // Always return false here, as this function only makes sure that the first
1884 // unallocated register has an odd register number and does not actually
1885 // allocate a register for the current argument.
1886 return false;
1887}
1888
Bill Schmidt212af6a2013-02-06 17:33:58 +00001889static bool CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1890 MVT &LocVT,
1891 CCValAssign::LocInfo &LocInfo,
1892 ISD::ArgFlagsTy &ArgFlags,
1893 CCState &State) {
Craig Topperc5eaae42012-03-11 07:57:25 +00001894 static const uint16_t ArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001895 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1896 PPC::F8
1897 };
1898
1899 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001900
Tilmann Schellerffd02002009-07-03 06:45:56 +00001901 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1902
1903 // If there is only one Floating-point register left we need to put both f64
1904 // values of a split ppc_fp128 value on the stack.
1905 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1906 State.AllocateReg(ArgRegs[RegNum]);
1907 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001908
Tilmann Schellerffd02002009-07-03 06:45:56 +00001909 // Always return false here, as this function only makes sure that the two f64
1910 // values a ppc_fp128 value is split into are both passed in registers or both
1911 // passed on the stack and does not actually allocate a register for the
1912 // current argument.
1913 return false;
1914}
1915
Chris Lattner9f0bc652007-02-25 05:34:32 +00001916/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001917/// on Darwin.
Craig Topperb78ca422012-03-11 07:16:55 +00001918static const uint16_t *GetFPR() {
1919 static const uint16_t FPR[] = {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001920 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001921 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner9f0bc652007-02-25 05:34:32 +00001922 };
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001923
Chris Lattner9f0bc652007-02-25 05:34:32 +00001924 return FPR;
1925}
1926
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001927/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1928/// the stack.
Owen Andersone50ed302009-08-10 22:56:29 +00001929static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001930 unsigned PtrByteSize) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001931 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001932 if (Flags.isByVal())
1933 ArgSize = Flags.getByValSize();
1934 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1935
1936 return ArgSize;
1937}
1938
Dan Gohman475871a2008-07-27 21:46:04 +00001939SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001940PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001941 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001942 const SmallVectorImpl<ISD::InputArg>
1943 &Ins,
1944 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001945 SmallVectorImpl<SDValue> &InVals)
1946 const {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00001947 if (PPCSubTarget.isSVR4ABI()) {
1948 if (PPCSubTarget.isPPC64())
1949 return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins,
1950 dl, DAG, InVals);
1951 else
1952 return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins,
1953 dl, DAG, InVals);
Bill Schmidt419f3762012-09-19 15:42:13 +00001954 } else {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00001955 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1956 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001957 }
1958}
1959
1960SDValue
Bill Schmidt419f3762012-09-19 15:42:13 +00001961PPCTargetLowering::LowerFormalArguments_32SVR4(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001962 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001963 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 const SmallVectorImpl<ISD::InputArg>
1965 &Ins,
1966 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001967 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001969 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001970 // +-----------------------------------+
1971 // +--> | Back chain |
1972 // | +-----------------------------------+
1973 // | | Floating-point register save area |
1974 // | +-----------------------------------+
1975 // | | General register save area |
1976 // | +-----------------------------------+
1977 // | | CR save word |
1978 // | +-----------------------------------+
1979 // | | VRSAVE save word |
1980 // | +-----------------------------------+
1981 // | | Alignment padding |
1982 // | +-----------------------------------+
1983 // | | Vector register save area |
1984 // | +-----------------------------------+
1985 // | | Local variable space |
1986 // | +-----------------------------------+
1987 // | | Parameter list area |
1988 // | +-----------------------------------+
1989 // | | LR save word |
1990 // | +-----------------------------------+
1991 // SP--> +--- | Back chain |
1992 // +-----------------------------------+
1993 //
1994 // Specifications:
1995 // System V Application Binary Interface PowerPC Processor Supplement
1996 // AltiVec Technology Programming Interface Manual
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001997
Tilmann Schellerffd02002009-07-03 06:45:56 +00001998 MachineFunction &MF = DAG.getMachineFunction();
1999 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00002000 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerffd02002009-07-03 06:45:56 +00002001
Owen Andersone50ed302009-08-10 22:56:29 +00002002 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerffd02002009-07-03 06:45:56 +00002003 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002004 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2005 (CallConv == CallingConv::Fast));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002006 unsigned PtrByteSize = 4;
2007
2008 // Assign locations to all of the incoming arguments.
2009 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002010 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00002011 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002012
2013 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002014 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002015
Bill Schmidt212af6a2013-02-06 17:33:58 +00002016 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002017
Tilmann Schellerffd02002009-07-03 06:45:56 +00002018 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2019 CCValAssign &VA = ArgLocs[i];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002020
Tilmann Schellerffd02002009-07-03 06:45:56 +00002021 // Arguments stored in registers.
2022 if (VA.isRegLoc()) {
Craig Topper44d23822012-02-22 05:59:10 +00002023 const TargetRegisterClass *RC;
Owen Andersone50ed302009-08-10 22:56:29 +00002024 EVT ValVT = VA.getValVT();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002025
Owen Anderson825b72b2009-08-11 20:47:22 +00002026 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002027 default:
Dan Gohman98ca4f22009-08-05 01:29:28 +00002028 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson825b72b2009-08-11 20:47:22 +00002029 case MVT::i32:
Craig Topperc9099502012-04-20 06:31:50 +00002030 RC = &PPC::GPRCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002031 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 case MVT::f32:
Craig Topperc9099502012-04-20 06:31:50 +00002033 RC = &PPC::F4RCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002034 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002035 case MVT::f64:
Craig Topperc9099502012-04-20 06:31:50 +00002036 RC = &PPC::F8RCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002037 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 case MVT::v16i8:
2039 case MVT::v8i16:
2040 case MVT::v4i32:
2041 case MVT::v4f32:
Craig Topperc9099502012-04-20 06:31:50 +00002042 RC = &PPC::VRRCRegClass;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002043 break;
2044 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002045
Tilmann Schellerffd02002009-07-03 06:45:56 +00002046 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00002047 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002048 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002049
Dan Gohman98ca4f22009-08-05 01:29:28 +00002050 InVals.push_back(ArgValue);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002051 } else {
2052 // Argument stored in memory.
2053 assert(VA.isMemLoc());
2054
2055 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
2056 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Chenged2ae132010-07-03 00:40:23 +00002057 isImmutable);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002058
2059 // Create load nodes to retrieve arguments from the stack.
2060 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002061 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2062 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002063 false, false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002064 }
2065 }
2066
2067 // Assign locations to all of the incoming aggregate by value arguments.
2068 // Aggregates passed by value are stored in the local variable space of the
2069 // caller's stack frame, right above the parameter list area.
2070 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002071 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00002072 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002073
2074 // Reserve stack space for the allocations in CCInfo.
2075 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2076
Bill Schmidt212af6a2013-02-06 17:33:58 +00002077 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002078
2079 // Area that is at least reserved in the caller of this function.
2080 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002081
Tilmann Schellerffd02002009-07-03 06:45:56 +00002082 // Set the size that is at least reserved in caller of this function. Tail
2083 // call optimized function's reserved stack space needs to be aligned so that
2084 // taking the difference between two stack areas will result in an aligned
2085 // stack.
2086 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2087
2088 MinReservedArea =
2089 std::max(MinReservedArea,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002090 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002091
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002092 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerffd02002009-07-03 06:45:56 +00002093 getStackAlignment();
2094 unsigned AlignMask = TargetAlign-1;
2095 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002096
Tilmann Schellerffd02002009-07-03 06:45:56 +00002097 FI->setMinReservedArea(MinReservedArea);
2098
2099 SmallVector<SDValue, 8> MemOps;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002100
Tilmann Schellerffd02002009-07-03 06:45:56 +00002101 // If the function takes variable number of arguments, make a frame index for
2102 // the start of the first vararg value... for expansion of llvm.va_start.
2103 if (isVarArg) {
Craig Topperc5eaae42012-03-11 07:57:25 +00002104 static const uint16_t GPArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002105 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2106 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2107 };
2108 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
2109
Craig Topperc5eaae42012-03-11 07:57:25 +00002110 static const uint16_t FPArgRegs[] = {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002111 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
2112 PPC::F8
2113 };
2114 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
2115
Dan Gohman1e93df62010-04-17 14:41:14 +00002116 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
2117 NumGPArgRegs));
2118 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
2119 NumFPArgRegs));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002120
2121 // Make room for NumGPArgRegs and NumFPArgRegs.
2122 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson825b72b2009-08-11 20:47:22 +00002123 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002124
Dan Gohman1e93df62010-04-17 14:41:14 +00002125 FuncInfo->setVarArgsStackOffset(
2126 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002127 CCInfo.getNextStackOffset(), true));
Tilmann Schellerffd02002009-07-03 06:45:56 +00002128
Dan Gohman1e93df62010-04-17 14:41:14 +00002129 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
2130 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002131
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00002132 // The fixed integer arguments of a variadic function are stored to the
2133 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
2134 // the result of va_next.
2135 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
2136 // Get an existing live-in vreg, or add a new one.
2137 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
2138 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00002139 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002140
Dan Gohman98ca4f22009-08-05 01:29:28 +00002141 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002142 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2143 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002144 MemOps.push_back(Store);
2145 // Increment the address by four for the next argument to store
2146 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
2147 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2148 }
2149
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002150 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
2151 // is set.
Tilmann Schellerffd02002009-07-03 06:45:56 +00002152 // The double arguments are stored to the VarArgsFrameIndex
2153 // on the stack.
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00002154 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
2155 // Get an existing live-in vreg, or add a new one.
2156 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
2157 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00002158 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002159
Owen Anderson825b72b2009-08-11 20:47:22 +00002160 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002161 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2162 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002163 MemOps.push_back(Store);
2164 // Increment the address by eight for the next argument to store
Owen Anderson825b72b2009-08-11 20:47:22 +00002165 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerffd02002009-07-03 06:45:56 +00002166 PtrVT);
2167 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2168 }
2169 }
2170
2171 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002173 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002174
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 return Chain;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002176}
2177
Bill Schmidt726c2372012-10-23 15:51:16 +00002178// PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2179// value to MVT::i64 and then truncate to the correct register size.
2180SDValue
2181PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT,
2182 SelectionDAG &DAG, SDValue ArgVal,
2183 DebugLoc dl) const {
2184 if (Flags.isSExt())
2185 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
2186 DAG.getValueType(ObjectVT));
2187 else if (Flags.isZExt())
2188 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
2189 DAG.getValueType(ObjectVT));
2190
2191 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
2192}
2193
2194// Set the size that is at least reserved in caller of this function. Tail
2195// call optimized functions' reserved stack space needs to be aligned so that
2196// taking the difference between two stack areas will result in an aligned
2197// stack.
2198void
2199PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG,
2200 unsigned nAltivecParamsAtEnd,
2201 unsigned MinReservedArea,
2202 bool isPPC64) const {
2203 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2204 // Add the Altivec parameters at the end, if needed.
2205 if (nAltivecParamsAtEnd) {
2206 MinReservedArea = ((MinReservedArea+15)/16)*16;
2207 MinReservedArea += 16*nAltivecParamsAtEnd;
2208 }
2209 MinReservedArea =
2210 std::max(MinReservedArea,
2211 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2212 unsigned TargetAlign
2213 = DAG.getMachineFunction().getTarget().getFrameLowering()->
2214 getStackAlignment();
2215 unsigned AlignMask = TargetAlign-1;
2216 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2217 FI->setMinReservedArea(MinReservedArea);
2218}
2219
Tilmann Schellerffd02002009-07-03 06:45:56 +00002220SDValue
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002221PPCTargetLowering::LowerFormalArguments_64SVR4(
2222 SDValue Chain,
2223 CallingConv::ID CallConv, bool isVarArg,
2224 const SmallVectorImpl<ISD::InputArg>
2225 &Ins,
2226 DebugLoc dl, SelectionDAG &DAG,
2227 SmallVectorImpl<SDValue> &InVals) const {
2228 // TODO: add description of PPC stack frame format, or at least some docs.
2229 //
2230 MachineFunction &MF = DAG.getMachineFunction();
2231 MachineFrameInfo *MFI = MF.getFrameInfo();
2232 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
2233
2234 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2235 // Potential tail calls could cause overwriting of argument stack slots.
2236 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2237 (CallConv == CallingConv::Fast));
2238 unsigned PtrByteSize = 8;
2239
2240 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
2241 // Area that is at least reserved in caller of this function.
2242 unsigned MinReservedArea = ArgOffset;
2243
2244 static const uint16_t GPR[] = {
2245 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2246 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2247 };
2248
2249 static const uint16_t *FPR = GetFPR();
2250
2251 static const uint16_t VR[] = {
2252 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2253 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2254 };
2255
2256 const unsigned Num_GPR_Regs = array_lengthof(GPR);
2257 const unsigned Num_FPR_Regs = 13;
2258 const unsigned Num_VR_Regs = array_lengthof(VR);
2259
2260 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
2261
2262 // Add DAG nodes to load the arguments or copy them out of registers. On
2263 // entry to a function on PPC, the arguments start after the linkage area,
2264 // although the first ones are often in registers.
2265
2266 SmallVector<SDValue, 8> MemOps;
2267 unsigned nAltivecParamsAtEnd = 0;
2268 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidt49deebb2013-02-20 17:31:41 +00002269 unsigned CurArgIdx = 0;
2270 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002271 SDValue ArgVal;
2272 bool needsLoad = false;
2273 EVT ObjectVT = Ins[ArgNo].VT;
2274 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
2275 unsigned ArgSize = ObjSize;
2276 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidt49deebb2013-02-20 17:31:41 +00002277 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2278 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002279
2280 unsigned CurArgOffset = ArgOffset;
2281
2282 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
2283 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2284 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
2285 if (isVarArg) {
2286 MinReservedArea = ((MinReservedArea+15)/16)*16;
2287 MinReservedArea += CalculateStackSlotSize(ObjectVT,
2288 Flags,
2289 PtrByteSize);
2290 } else
2291 nAltivecParamsAtEnd++;
2292 } else
2293 // Calculate min reserved area.
2294 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
2295 Flags,
2296 PtrByteSize);
2297
2298 // FIXME the codegen can be much improved in some cases.
2299 // We do not have to keep everything in memory.
2300 if (Flags.isByVal()) {
2301 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
2302 ObjSize = Flags.getByValSize();
2303 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidt42d43352012-10-31 01:15:05 +00002304 // Empty aggregate parameters do not take up registers. Examples:
2305 // struct { } a;
2306 // union { } b;
2307 // int c[0];
2308 // etc. However, we have to provide a place-holder in InVals, so
2309 // pretend we have an 8-byte item at the current address for that
2310 // purpose.
2311 if (!ObjSize) {
2312 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2313 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2314 InVals.push_back(FIN);
2315 continue;
2316 }
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002317 // All aggregates smaller than 8 bytes must be passed right-justified.
Bill Schmidt7a6cb152012-10-16 13:30:53 +00002318 if (ObjSize < PtrByteSize)
2319 CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002320 // The value of the object is its address.
2321 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
2322 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2323 InVals.push_back(FIN);
Bill Schmidt37900c52012-10-25 13:38:09 +00002324
2325 if (ObjSize < 8) {
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002326 if (GPR_idx != Num_GPR_Regs) {
Bill Schmidt37900c52012-10-25 13:38:09 +00002327 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002328 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt37900c52012-10-25 13:38:09 +00002329 SDValue Store;
2330
2331 if (ObjSize==1 || ObjSize==2 || ObjSize==4) {
2332 EVT ObjType = (ObjSize == 1 ? MVT::i8 :
2333 (ObjSize == 2 ? MVT::i16 : MVT::i32));
2334 Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
2335 MachinePointerInfo(FuncArg, CurArgOffset),
2336 ObjType, false, false, 0);
2337 } else {
2338 // For sizes that don't fit a truncating store (3, 5, 6, 7),
2339 // store the whole register as-is to the parameter save area
2340 // slot. The address of the parameter was already calculated
2341 // above (InVals.push_back(FIN)) to be the right-justified
2342 // offset within the slot. For this store, we need a new
2343 // frame index that points at the beginning of the slot.
2344 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2345 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2346 Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2347 MachinePointerInfo(FuncArg, ArgOffset),
2348 false, false, 0);
2349 }
2350
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002351 MemOps.push_back(Store);
2352 ++GPR_idx;
2353 }
Bill Schmidt37900c52012-10-25 13:38:09 +00002354 // Whether we copied from a register or not, advance the offset
2355 // into the parameter save area by a full doubleword.
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002356 ArgOffset += PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002357 continue;
2358 }
Bill Schmidt37900c52012-10-25 13:38:09 +00002359
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002360 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2361 // Store whatever pieces of the object are in registers
2362 // to memory. ArgOffset will be the address of the beginning
2363 // of the object.
2364 if (GPR_idx != Num_GPR_Regs) {
2365 unsigned VReg;
2366 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2367 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
2368 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2369 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt37900c52012-10-25 13:38:09 +00002370 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002371 MachinePointerInfo(FuncArg, ArgOffset),
2372 false, false, 0);
2373 MemOps.push_back(Store);
2374 ++GPR_idx;
2375 ArgOffset += PtrByteSize;
2376 } else {
Bill Schmidt7a6cb152012-10-16 13:30:53 +00002377 ArgOffset += ArgSize - j;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002378 break;
2379 }
2380 }
2381 continue;
2382 }
2383
2384 switch (ObjectVT.getSimpleVT().SimpleTy) {
2385 default: llvm_unreachable("Unhandled argument type!");
2386 case MVT::i32:
2387 case MVT::i64:
2388 if (GPR_idx != Num_GPR_Regs) {
2389 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2390 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
2391
Bill Schmidt726c2372012-10-23 15:51:16 +00002392 if (ObjectVT == MVT::i32)
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002393 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
2394 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt726c2372012-10-23 15:51:16 +00002395 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002396
2397 ++GPR_idx;
2398 } else {
2399 needsLoad = true;
2400 ArgSize = PtrByteSize;
2401 }
2402 ArgOffset += 8;
2403 break;
2404
2405 case MVT::f32:
2406 case MVT::f64:
2407 // Every 8 bytes of argument space consumes one of the GPRs available for
2408 // argument passing.
2409 if (GPR_idx != Num_GPR_Regs) {
2410 ++GPR_idx;
2411 }
2412 if (FPR_idx != Num_FPR_Regs) {
2413 unsigned VReg;
2414
2415 if (ObjectVT == MVT::f32)
2416 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
2417 else
2418 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
2419
2420 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2421 ++FPR_idx;
2422 } else {
2423 needsLoad = true;
Bill Schmidta867f372012-10-11 15:38:20 +00002424 ArgSize = PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002425 }
2426
2427 ArgOffset += 8;
2428 break;
2429 case MVT::v4f32:
2430 case MVT::v4i32:
2431 case MVT::v8i16:
2432 case MVT::v16i8:
2433 // Note that vector arguments in registers don't reserve stack space,
2434 // except in varargs functions.
2435 if (VR_idx != Num_VR_Regs) {
2436 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
2437 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
2438 if (isVarArg) {
2439 while ((ArgOffset % 16) != 0) {
2440 ArgOffset += PtrByteSize;
2441 if (GPR_idx != Num_GPR_Regs)
2442 GPR_idx++;
2443 }
2444 ArgOffset += 16;
2445 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
2446 }
2447 ++VR_idx;
2448 } else {
2449 // Vectors are aligned.
2450 ArgOffset = ((ArgOffset+15)/16)*16;
2451 CurArgOffset = ArgOffset;
2452 ArgOffset += 16;
2453 needsLoad = true;
2454 }
2455 break;
2456 }
2457
2458 // We need to load the argument to a virtual register if we determined
2459 // above that we ran out of physical registers of the appropriate type.
2460 if (needsLoad) {
2461 int FI = MFI->CreateFixedObject(ObjSize,
2462 CurArgOffset + (ArgSize - ObjSize),
2463 isImmutable);
2464 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
2465 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
2466 false, false, false, 0);
2467 }
2468
2469 InVals.push_back(ArgVal);
2470 }
2471
2472 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt726c2372012-10-23 15:51:16 +00002473 // call optimized functions' reserved stack space needs to be aligned so that
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002474 // taking the difference between two stack areas will result in an aligned
2475 // stack.
Bill Schmidt726c2372012-10-23 15:51:16 +00002476 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002477
2478 // If the function takes variable number of arguments, make a frame index for
2479 // the start of the first vararg value... for expansion of llvm.va_start.
2480 if (isVarArg) {
2481 int Depth = ArgOffset;
2482
2483 FuncInfo->setVarArgsFrameIndex(
Bill Schmidt726c2372012-10-23 15:51:16 +00002484 MFI->CreateFixedObject(PtrByteSize, Depth, true));
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002485 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
2486
2487 // If this function is vararg, store any remaining integer argument regs
2488 // to their spots on the stack so that they may be loaded by deferencing the
2489 // result of va_next.
2490 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
2491 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2492 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
2493 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2494 MachinePointerInfo(), false, false, 0);
2495 MemOps.push_back(Store);
2496 // Increment the address by four for the next argument to store
Bill Schmidt726c2372012-10-23 15:51:16 +00002497 SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002498 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
2499 }
2500 }
2501
2502 if (!MemOps.empty())
2503 Chain = DAG.getNode(ISD::TokenFactor, dl,
2504 MVT::Other, &MemOps[0], MemOps.size());
2505
2506 return Chain;
2507}
2508
2509SDValue
2510PPCTargetLowering::LowerFormalArguments_Darwin(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002511 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002512 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002513 const SmallVectorImpl<ISD::InputArg>
2514 &Ins,
2515 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002516 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002517 // TODO: add description of PPC stack frame format, or at least some docs.
2518 //
2519 MachineFunction &MF = DAG.getMachineFunction();
2520 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00002521 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00002522
Owen Andersone50ed302009-08-10 22:56:29 +00002523 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00002524 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002525 // Potential tail calls could cause overwriting of argument stack slots.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002526 bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt &&
2527 (CallConv == CallingConv::Fast));
Jim Laskeye9bd7b22006-11-28 14:53:52 +00002528 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00002529
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002530 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002531 // Area that is at least reserved in caller of this function.
2532 unsigned MinReservedArea = ArgOffset;
2533
Craig Topperb78ca422012-03-11 07:16:55 +00002534 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002535 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2536 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2537 };
Craig Topperb78ca422012-03-11 07:16:55 +00002538 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerc91a4752006-06-26 22:48:35 +00002539 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2540 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2541 };
Scott Michelfdc40a02009-02-17 22:15:04 +00002542
Craig Topperb78ca422012-03-11 07:16:55 +00002543 static const uint16_t *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00002544
Craig Topperb78ca422012-03-11 07:16:55 +00002545 static const uint16_t VR[] = {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002546 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2547 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2548 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00002549
Owen Anderson718cb662007-09-07 04:06:50 +00002550 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002551 const unsigned Num_FPR_Regs = 13;
Owen Anderson718cb662007-09-07 04:06:50 +00002552 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00002553
2554 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002555
Craig Topperb78ca422012-03-11 07:16:55 +00002556 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelfdc40a02009-02-17 22:15:04 +00002557
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002558 // In 32-bit non-varargs functions, the stack space for vectors is after the
2559 // stack space for non-vectors. We do not use this space unless we have
2560 // too many vectors to fit in registers, something that only occurs in
Scott Michelfdc40a02009-02-17 22:15:04 +00002561 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002562 // that out...for the pathological case, compute VecArgOffset as the
2563 // start of the vector parameter area. Computing VecArgOffset is the
2564 // entire point of the following loop.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002565 unsigned VecArgOffset = ArgOffset;
2566 if (!isVarArg && !isPPC64) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002567 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002568 ++ArgNo) {
Owen Andersone50ed302009-08-10 22:56:29 +00002569 EVT ObjectVT = Ins[ArgNo].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002570 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002571
Duncan Sands276dcbd2008-03-21 09:14:45 +00002572 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002573 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Benjamin Kramer263109d2012-01-20 14:42:32 +00002574 unsigned ObjSize = Flags.getByValSize();
Scott Michelfdc40a02009-02-17 22:15:04 +00002575 unsigned ArgSize =
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002576 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
2577 VecArgOffset += ArgSize;
2578 continue;
2579 }
2580
Owen Anderson825b72b2009-08-11 20:47:22 +00002581 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002582 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002583 case MVT::i32:
2584 case MVT::f32:
Bill Schmidt419f3762012-09-19 15:42:13 +00002585 VecArgOffset += 4;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002586 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002587 case MVT::i64: // PPC64
2588 case MVT::f64:
Bill Schmidt419f3762012-09-19 15:42:13 +00002589 // FIXME: We are guaranteed to be !isPPC64 at this point.
2590 // Does MVT::i64 apply?
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002591 VecArgOffset += 8;
2592 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002593 case MVT::v4f32:
2594 case MVT::v4i32:
2595 case MVT::v8i16:
2596 case MVT::v16i8:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002597 // Nothing to do, we're only looking at Nonvector args here.
2598 break;
2599 }
2600 }
2601 }
2602 // We've found where the vector parameter area in memory is. Skip the
2603 // first 12 parameters; these don't use that memory.
2604 VecArgOffset = ((VecArgOffset+15)/16)*16;
2605 VecArgOffset += 12*16;
2606
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002607 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00002608 // entry to a function on PPC, the arguments start after the linkage area,
2609 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002610
Dan Gohman475871a2008-07-27 21:46:04 +00002611 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002612 unsigned nAltivecParamsAtEnd = 0;
Roman Divacky5236ab32012-09-24 20:47:19 +00002613 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin();
Bill Schmidta7f2ce82013-05-08 17:22:33 +00002614 unsigned CurArgIdx = 0;
2615 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00002616 SDValue ArgVal;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002617 bool needsLoad = false;
Owen Andersone50ed302009-08-10 22:56:29 +00002618 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002619 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey619965d2006-11-29 13:37:09 +00002620 unsigned ArgSize = ObjSize;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002621 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Bill Schmidta7f2ce82013-05-08 17:22:33 +00002622 std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx);
2623 CurArgIdx = Ins[ArgNo].OrigArgIndex;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002624
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002625 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002626
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002627 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002628 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
2629 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002630 if (isVarArg || isPPC64) {
2631 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002632 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohman095cc292008-09-13 01:54:27 +00002633 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002634 PtrByteSize);
2635 } else nAltivecParamsAtEnd++;
2636 } else
2637 // Calculate min reserved area.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002638 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohman095cc292008-09-13 01:54:27 +00002639 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002640 PtrByteSize);
2641
Dale Johannesen8419dd62008-03-07 20:27:40 +00002642 // FIXME the codegen can be much improved in some cases.
2643 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002644 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00002645 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002646 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00002647 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002648 // Objects of size 1 and 2 are right justified, everything else is
2649 // left justified. This means the memory address is adjusted forwards.
Dale Johannesen7f96f392008-03-08 01:41:42 +00002650 if (ObjSize==1 || ObjSize==2) {
2651 CurArgOffset = CurArgOffset + (4 - ObjSize);
2652 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002653 // The value of the object is its address.
Evan Chenged2ae132010-07-03 00:40:23 +00002654 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002655 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002656 InVals.push_back(FIN);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002657 if (ObjSize==1 || ObjSize==2) {
Dale Johannesen7f96f392008-03-08 01:41:42 +00002658 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002659 unsigned VReg;
2660 if (isPPC64)
2661 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2662 else
2663 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002664 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidt726c2372012-10-23 15:51:16 +00002665 EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16;
Scott Michelfdc40a02009-02-17 22:15:04 +00002666 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Roman Divacky5236ab32012-09-24 20:47:19 +00002667 MachinePointerInfo(FuncArg,
2668 CurArgOffset),
Bill Schmidt419f3762012-09-19 15:42:13 +00002669 ObjType, false, false, 0);
Dale Johannesen7f96f392008-03-08 01:41:42 +00002670 MemOps.push_back(Store);
2671 ++GPR_idx;
Dale Johannesen7f96f392008-03-08 01:41:42 +00002672 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002673
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002674 ArgOffset += PtrByteSize;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002675
Dale Johannesen7f96f392008-03-08 01:41:42 +00002676 continue;
2677 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002678 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2679 // Store whatever pieces of the object are in registers
Bill Schmidt419f3762012-09-19 15:42:13 +00002680 // to memory. ArgOffset will be the address of the beginning
2681 // of the object.
Dale Johannesen8419dd62008-03-07 20:27:40 +00002682 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002683 unsigned VReg;
2684 if (isPPC64)
2685 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2686 else
2687 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Chenged2ae132010-07-03 00:40:23 +00002688 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002689 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002690 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Bill Schmidtb2544ec2012-10-05 21:27:08 +00002691 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
Roman Divacky5236ab32012-09-24 20:47:19 +00002692 MachinePointerInfo(FuncArg, ArgOffset),
David Greene534502d12010-02-15 16:56:53 +00002693 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002694 MemOps.push_back(Store);
2695 ++GPR_idx;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002696 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002697 } else {
2698 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2699 break;
2700 }
2701 }
2702 continue;
2703 }
2704
Owen Anderson825b72b2009-08-11 20:47:22 +00002705 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002706 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002707 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002708 if (!isPPC64) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002709 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002710 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002711 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002712 ++GPR_idx;
2713 } else {
2714 needsLoad = true;
2715 ArgSize = PtrByteSize;
2716 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002717 // All int arguments reserve stack space in the Darwin ABI.
2718 ArgOffset += PtrByteSize;
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002719 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002720 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002721 // FALLTHROUGH
Owen Anderson825b72b2009-08-11 20:47:22 +00002722 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00002723 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002724 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002725 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002726
Bill Schmidt726c2372012-10-23 15:51:16 +00002727 if (ObjectVT == MVT::i32)
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002728 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson825b72b2009-08-11 20:47:22 +00002729 // value to MVT::i64 and then truncate to the correct register size.
Bill Schmidt726c2372012-10-23 15:51:16 +00002730 ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002731
Chris Lattnerc91a4752006-06-26 22:48:35 +00002732 ++GPR_idx;
2733 } else {
2734 needsLoad = true;
Evan Cheng982a0592008-07-24 08:17:07 +00002735 ArgSize = PtrByteSize;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002736 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002737 // All int arguments reserve stack space in the Darwin ABI.
2738 ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002739 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002740
Owen Anderson825b72b2009-08-11 20:47:22 +00002741 case MVT::f32:
2742 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002743 // Every 4 bytes of argument space consumes one of the GPRs available for
2744 // argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002745 if (GPR_idx != Num_GPR_Regs) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002746 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002747 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002748 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002749 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002750 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002751 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002752
Owen Anderson825b72b2009-08-11 20:47:22 +00002753 if (ObjectVT == MVT::f32)
Devang Patel68e6bee2011-02-21 23:21:26 +00002754 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002755 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002756 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002757
Dan Gohman98ca4f22009-08-05 01:29:28 +00002758 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002759 ++FPR_idx;
2760 } else {
2761 needsLoad = true;
2762 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002763
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002764 // All FP arguments reserve stack space in the Darwin ABI.
2765 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002766 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002767 case MVT::v4f32:
2768 case MVT::v4i32:
2769 case MVT::v8i16:
2770 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00002771 // Note that vector arguments in registers don't reserve stack space,
2772 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002773 if (VR_idx != Num_VR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002774 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002775 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00002776 if (isVarArg) {
2777 while ((ArgOffset % 16) != 0) {
2778 ArgOffset += PtrByteSize;
2779 if (GPR_idx != Num_GPR_Regs)
2780 GPR_idx++;
2781 }
2782 ArgOffset += 16;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002783 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesen75092de2008-03-12 00:22:17 +00002784 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002785 ++VR_idx;
2786 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002787 if (!isVarArg && !isPPC64) {
2788 // Vectors go after all the nonvectors.
2789 CurArgOffset = VecArgOffset;
2790 VecArgOffset += 16;
2791 } else {
2792 // Vectors are aligned.
2793 ArgOffset = ((ArgOffset+15)/16)*16;
2794 CurArgOffset = ArgOffset;
2795 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00002796 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002797 needsLoad = true;
2798 }
2799 break;
2800 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002801
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002802 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002803 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002804 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002805 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002806 CurArgOffset + (ArgSize - ObjSize),
Evan Chenged2ae132010-07-03 00:40:23 +00002807 isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00002808 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002809 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002810 false, false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002811 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002812
Dan Gohman98ca4f22009-08-05 01:29:28 +00002813 InVals.push_back(ArgVal);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002814 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002815
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002816 // Set the size that is at least reserved in caller of this function. Tail
Bill Schmidt726c2372012-10-23 15:51:16 +00002817 // call optimized functions' reserved stack space needs to be aligned so that
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002818 // taking the difference between two stack areas will result in an aligned
2819 // stack.
Bill Schmidt726c2372012-10-23 15:51:16 +00002820 setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002821
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002822 // If the function takes variable number of arguments, make a frame index for
2823 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002824 if (isVarArg) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002825 int Depth = ArgOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00002826
Dan Gohman1e93df62010-04-17 14:41:14 +00002827 FuncInfo->setVarArgsFrameIndex(
2828 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002829 Depth, true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002830 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002831
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002832 // If this function is vararg, store any remaining integer argument regs
2833 // to their spots on the stack so that they may be loaded by deferencing the
2834 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002835 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002836 unsigned VReg;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002837
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002838 if (isPPC64)
Devang Patel68e6bee2011-02-21 23:21:26 +00002839 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002840 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002841 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002842
Dan Gohman98ca4f22009-08-05 01:29:28 +00002843 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002844 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2845 MachinePointerInfo(), false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002846 MemOps.push_back(Store);
2847 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002848 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002849 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002850 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002851 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002852
Dale Johannesen8419dd62008-03-07 20:27:40 +00002853 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002854 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002855 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002856
Dan Gohman98ca4f22009-08-05 01:29:28 +00002857 return Chain;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002858}
2859
Bill Schmidt419f3762012-09-19 15:42:13 +00002860/// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus
2861/// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002862static unsigned
2863CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2864 bool isPPC64,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002865 bool isVarArg,
2866 unsigned CC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002867 const SmallVectorImpl<ISD::OutputArg>
2868 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002869 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002870 unsigned &nAltivecParamsAtEnd) {
2871 // Count how many bytes are to be pushed on the stack, including the linkage
2872 // area, and parameter passing area. We start with 24/48 bytes, which is
2873 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002874 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002875 unsigned NumOps = Outs.size();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002876 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2877
2878 // Add up all the space actually used.
2879 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2880 // they all go in registers, but we must reserve stack space for them for
2881 // possible use by the caller. In varargs or 64-bit calls, parameters are
2882 // assigned stack space in order, with padding so Altivec parameters are
2883 // 16-byte aligned.
2884 nAltivecParamsAtEnd = 0;
2885 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002886 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanc9403652010-07-07 15:54:55 +00002887 EVT ArgVT = Outs[i].VT;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002888 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002889 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2890 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002891 if (!isVarArg && !isPPC64) {
2892 // Non-varargs Altivec parameters go after all the non-Altivec
2893 // parameters; handle those later so we know how much padding we need.
2894 nAltivecParamsAtEnd++;
2895 continue;
2896 }
2897 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2898 NumBytes = ((NumBytes+15)/16)*16;
2899 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002900 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002901 }
2902
2903 // Allow for Altivec parameters at the end, if needed.
2904 if (nAltivecParamsAtEnd) {
2905 NumBytes = ((NumBytes+15)/16)*16;
2906 NumBytes += 16*nAltivecParamsAtEnd;
2907 }
2908
2909 // The prolog code of the callee may store up to 8 GPR argument registers to
2910 // the stack, allowing va_start to index over them in memory if its varargs.
2911 // Because we cannot tell if this is needed on the caller side, we have to
2912 // conservatively assume that it is needed. As such, make sure we have at
2913 // least enough stack space for the caller to store the 8 GPRs.
2914 NumBytes = std::max(NumBytes,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002915 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002916
2917 // Tail call needs the stack to be aligned.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002918 if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){
2919 unsigned TargetAlign = DAG.getMachineFunction().getTarget().
2920 getFrameLowering()->getStackAlignment();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002921 unsigned AlignMask = TargetAlign-1;
2922 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2923 }
2924
2925 return NumBytes;
2926}
2927
2928/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002929/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesenb60d5192009-11-24 01:09:07 +00002930static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002931 unsigned ParamSize) {
2932
Dale Johannesenb60d5192009-11-24 01:09:07 +00002933 if (!isTailCall) return 0;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002934
2935 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2936 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2937 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2938 // Remember only if the new adjustement is bigger.
2939 if (SPDiff < FI->getTailCallSPDelta())
2940 FI->setTailCallSPDelta(SPDiff);
2941
2942 return SPDiff;
2943}
2944
Dan Gohman98ca4f22009-08-05 01:29:28 +00002945/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2946/// for tail call optimization. Targets which want to do tail call
2947/// optimization should implement this function.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002948bool
Dan Gohman98ca4f22009-08-05 01:29:28 +00002949PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002950 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002951 bool isVarArg,
2952 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002953 SelectionDAG& DAG) const {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002954 if (!getTargetMachine().Options.GuaranteedTailCallOpt)
Evan Cheng6c2e8a92010-01-29 23:05:56 +00002955 return false;
2956
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002957 // Variable argument functions are not supported.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002958 if (isVarArg)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002959 return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002960
Dan Gohman98ca4f22009-08-05 01:29:28 +00002961 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002962 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002963 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2964 // Functions containing by val parameters are not supported.
2965 for (unsigned i = 0; i != Ins.size(); i++) {
2966 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2967 if (Flags.isByVal()) return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002968 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002969
2970 // Non PIC/GOT tail calls are supported.
2971 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2972 return true;
2973
2974 // At the moment we can only do local tail calls (in same module, hidden
2975 // or protected) if we are generating PIC.
2976 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2977 return G->getGlobal()->hasHiddenVisibility()
2978 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002979 }
2980
2981 return false;
2982}
2983
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002984/// isCallCompatibleAddress - Return the immediate to use if the specified
2985/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00002986static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002987 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2988 if (!C) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002989
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002990 int Addr = C->getZExtValue();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002991 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
Richard Smith1144af32012-08-24 23:29:28 +00002992 SignExtend32<26>(Addr) != Addr)
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002993 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002994
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002995 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greifba36cb52008-08-28 21:40:38 +00002996 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002997}
2998
Dan Gohman844731a2008-05-13 00:00:25 +00002999namespace {
3000
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003001struct TailCallArgumentInfo {
Dan Gohman475871a2008-07-27 21:46:04 +00003002 SDValue Arg;
3003 SDValue FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003004 int FrameIdx;
3005
3006 TailCallArgumentInfo() : FrameIdx(0) {}
3007};
3008
Dan Gohman844731a2008-05-13 00:00:25 +00003009}
3010
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003011/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
3012static void
3013StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Chengff89dcb2009-10-18 18:16:27 +00003014 SDValue Chain,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003015 const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003016 SmallVector<SDValue, 8> &MemOpChains,
3017 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003018 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003019 SDValue Arg = TailCallArgs[i].Arg;
3020 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003021 int FI = TailCallArgs[i].FrameIdx;
3022 // Store relative to framepointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003023 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003024 MachinePointerInfo::getFixedStack(FI),
3025 false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003026 }
3027}
3028
3029/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
3030/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman475871a2008-07-27 21:46:04 +00003031static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003032 MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00003033 SDValue Chain,
3034 SDValue OldRetAddr,
3035 SDValue OldFP,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003036 int SPDiff,
3037 bool isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003038 bool isDarwinABI,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003039 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003040 if (SPDiff) {
3041 // Calculate the new stack slot for the return address.
3042 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003043 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003044 isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003045 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00003046 NewRetAddrLoc, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00003047 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003049 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003050 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene534502d12010-02-15 16:56:53 +00003051 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003052
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003053 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
3054 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003055 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00003056 int NewFPLoc =
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003057 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene3f2bf852009-11-12 20:49:22 +00003058 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Chenged2ae132010-07-03 00:40:23 +00003059 true);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003060 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
3061 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003062 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene534502d12010-02-15 16:56:53 +00003063 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003064 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003065 }
3066 return Chain;
3067}
3068
3069/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
3070/// the position of the argument.
3071static void
3072CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman475871a2008-07-27 21:46:04 +00003073 SDValue Arg, int SPDiff, unsigned ArgOffset,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003074 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
3075 int Offset = ArgOffset + SPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003076 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00003077 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00003078 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00003079 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003080 TailCallArgumentInfo Info;
3081 Info.Arg = Arg;
3082 Info.FrameIdxOp = FIN;
3083 Info.FrameIdx = FI;
3084 TailCallArguments.push_back(Info);
3085}
3086
3087/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
3088/// stack slot. Returns the chain as result and the loaded frame pointers in
3089/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman475871a2008-07-27 21:46:04 +00003090SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003091 int SPDiff,
3092 SDValue Chain,
3093 SDValue &LROpOut,
3094 SDValue &FPOpOut,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003095 bool isDarwinABI,
Dan Gohmand858e902010-04-17 15:26:15 +00003096 DebugLoc dl) const {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003097 if (SPDiff) {
3098 // Load the LR and FP stack slot for later adjusting.
Owen Anderson825b72b2009-08-11 20:47:22 +00003099 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003100 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003101 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003102 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00003103 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003104
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003105 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
3106 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003107 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00003108 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003109 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003110 false, false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003111 Chain = SDValue(FPOpOut.getNode(), 1);
3112 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003113 }
3114 return Chain;
3115}
3116
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003117/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelfdc40a02009-02-17 22:15:04 +00003118/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003119/// specified by the specific parameter attribute. The copy will be passed as
3120/// a byval function parameter.
3121/// Sometimes what we are copying is the end of a larger object, the part that
3122/// does not fit in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00003123static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003124CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00003125 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00003126 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen8ad9b432009-02-04 01:17:06 +00003128 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattnere72f2022010-09-21 05:40:29 +00003129 false, false, MachinePointerInfo(0),
3130 MachinePointerInfo(0));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003131}
Chris Lattner9f0bc652007-02-25 05:34:32 +00003132
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003133/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
3134/// tail calls.
3135static void
Dan Gohman475871a2008-07-27 21:46:04 +00003136LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
3137 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003138 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Dan Gohman475871a2008-07-27 21:46:04 +00003139 bool isVector, SmallVector<SDValue, 8> &MemOpChains,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003140 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003141 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00003142 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003143 if (!isTailCall) {
3144 if (isVector) {
Dan Gohman475871a2008-07-27 21:46:04 +00003145 SDValue StackPtr;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003146 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003148 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003150 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003151 DAG.getConstant(ArgOffset, PtrVT));
3152 }
Chris Lattner6229d0a2010-09-21 18:41:36 +00003153 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
3154 MachinePointerInfo(), false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003155 // Calculate and remember argument location.
3156 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
3157 TailCallArguments);
3158}
3159
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003160static
3161void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
3162 DebugLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
3163 SDValue LROp, SDValue FPOp, bool isDarwinABI,
3164 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) {
3165 MachineFunction &MF = DAG.getMachineFunction();
3166
3167 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3168 // might overwrite each other in case of tail call optimization.
3169 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00003170 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003171 InFlag = SDValue();
3172 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
3173 MemOpChains2, dl);
3174 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003176 &MemOpChains2[0], MemOpChains2.size());
3177
3178 // Store the return address to the appropriate stack slot.
3179 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
3180 isPPC64, isDarwinABI, dl);
3181
3182 // Emit callseq_end just before tailcall node.
3183 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3184 DAG.getIntPtrConstant(0, true), InFlag);
3185 InFlag = Chain.getValue(1);
3186}
3187
3188static
3189unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
3190 SDValue &Chain, DebugLoc dl, int SPDiff, bool isTailCall,
3191 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
Owen Andersone50ed302009-08-10 22:56:29 +00003192 SmallVector<SDValue, 8> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00003193 const PPCSubtarget &PPCSubTarget) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003194
Chris Lattnerb9082582010-11-14 23:42:06 +00003195 bool isPPC64 = PPCSubTarget.isPPC64();
3196 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
3197
Owen Andersone50ed302009-08-10 22:56:29 +00003198 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003200 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003201
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003202 unsigned CallOpc = PPCISD::CALL;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003203
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003204 bool needIndirectCall = true;
3205 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003206 // If this is an absolute destination address, use the munged value.
3207 Callee = SDValue(Dest, 0);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003208 needIndirectCall = false;
3209 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003210
Chris Lattnerb9082582010-11-14 23:42:06 +00003211 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3212 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
3213 // Use indirect calls for ALL functions calls in JIT mode, since the
3214 // far-call stubs may be outside relocation limits for a BL instruction.
3215 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
3216 unsigned OpFlags = 0;
3217 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00003218 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00003219 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerb9082582010-11-14 23:42:06 +00003220 (G->getGlobal()->isDeclaration() ||
3221 G->getGlobal()->isWeakForLinker())) {
3222 // PC-relative references to external symbols should go through $stub,
3223 // unless we're building with the leopard linker or later, which
3224 // automatically synthesizes these stubs.
3225 OpFlags = PPCII::MO_DARWIN_STUB;
3226 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003227
Chris Lattnerb9082582010-11-14 23:42:06 +00003228 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
3229 // every direct call is) turn it into a TargetGlobalAddress /
3230 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003231 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerb9082582010-11-14 23:42:06 +00003232 Callee.getValueType(),
3233 0, OpFlags);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003234 needIndirectCall = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003235 }
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003236 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003237
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003238 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerb9082582010-11-14 23:42:06 +00003239 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003240
Chris Lattnerb9082582010-11-14 23:42:06 +00003241 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00003242 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00003243 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerb9082582010-11-14 23:42:06 +00003244 // PC-relative references to external symbols should go through $stub,
3245 // unless we're building with the leopard linker or later, which
3246 // automatically synthesizes these stubs.
3247 OpFlags = PPCII::MO_DARWIN_STUB;
3248 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003249
Chris Lattnerb9082582010-11-14 23:42:06 +00003250 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
3251 OpFlags);
3252 needIndirectCall = false;
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003253 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003254
Torok Edwin0e3a1a82010-08-04 20:47:44 +00003255 if (needIndirectCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003256 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3257 // to do the call, we can't use PPCISD::CALL.
3258 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003259
3260 if (isSVR4ABI && isPPC64) {
3261 // Function pointers in the 64-bit SVR4 ABI do not point to the function
3262 // entry point, but to the function descriptor (the function entry point
3263 // address is part of the function descriptor though).
3264 // The function descriptor is a three doubleword structure with the
3265 // following fields: function entry point, TOC base address and
3266 // environment pointer.
3267 // Thus for a call through a function pointer, the following actions need
3268 // to be performed:
3269 // 1. Save the TOC of the caller in the TOC save area of its stack
Bill Schmidt726c2372012-10-23 15:51:16 +00003270 // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()).
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003271 // 2. Load the address of the function entry point from the function
3272 // descriptor.
3273 // 3. Load the TOC of the callee from the function descriptor into r2.
3274 // 4. Load the environment pointer from the function descriptor into
3275 // r11.
3276 // 5. Branch to the function entry point address.
3277 // 6. On return of the callee, the TOC of the caller needs to be
3278 // restored (this is done in FinishCall()).
3279 //
3280 // All those operations are flagged together to ensure that no other
3281 // operations can be scheduled in between. E.g. without flagging the
3282 // operations together, a TOC access in the caller could be scheduled
3283 // between the load of the callee TOC and the branch to the callee, which
3284 // results in the TOC access going through the TOC of the callee instead
3285 // of going through the TOC of the caller, which leads to incorrect code.
3286
3287 // Load the address of the function entry point from the function
3288 // descriptor.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003289 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003290 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
3291 InFlag.getNode() ? 3 : 2);
3292 Chain = LoadFuncPtr.getValue(1);
3293 InFlag = LoadFuncPtr.getValue(2);
3294
3295 // Load environment pointer into r11.
3296 // Offset of the environment pointer within the function descriptor.
3297 SDValue PtrOff = DAG.getIntPtrConstant(16);
3298
3299 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
3300 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
3301 InFlag);
3302 Chain = LoadEnvPtr.getValue(1);
3303 InFlag = LoadEnvPtr.getValue(2);
3304
3305 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
3306 InFlag);
3307 Chain = EnvVal.getValue(0);
3308 InFlag = EnvVal.getValue(1);
3309
3310 // Load TOC of the callee into r2. We are using a target-specific load
3311 // with r2 hard coded, because the result of a target-independent load
3312 // would never go directly into r2, since r2 is a reserved register (which
3313 // prevents the register allocator from allocating it), resulting in an
3314 // additional register being allocated and an unnecessary move instruction
3315 // being generated.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003316 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003317 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
3318 Callee, InFlag);
3319 Chain = LoadTOCPtr.getValue(0);
3320 InFlag = LoadTOCPtr.getValue(1);
3321
3322 MTCTROps[0] = Chain;
3323 MTCTROps[1] = LoadFuncPtr;
3324 MTCTROps[2] = InFlag;
3325 }
3326
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003327 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
3328 2 + (InFlag.getNode() != 0));
3329 InFlag = Chain.getValue(1);
3330
3331 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 NodeTys.push_back(MVT::Other);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003333 NodeTys.push_back(MVT::Glue);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003334 Ops.push_back(Chain);
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003335 CallOpc = PPCISD::BCTRL;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003336 Callee.setNode(0);
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003337 // Add use of X11 (holding environment pointer)
3338 if (isSVR4ABI && isPPC64)
3339 Ops.push_back(DAG.getRegister(PPC::X11, PtrVT));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003340 // Add CTR register as callee so a bctr can be emitted later.
3341 if (isTailCall)
Roman Divacky0c9b5592011-06-03 15:47:49 +00003342 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003343 }
3344
3345 // If this is a direct call, pass the chain and the callee.
3346 if (Callee.getNode()) {
3347 Ops.push_back(Chain);
3348 Ops.push_back(Callee);
3349 }
3350 // If this is a tail call add stack pointer delta.
3351 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003353
3354 // Add argument registers to the end of the list so that they are known live
3355 // into the call.
3356 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3357 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3358 RegsToPass[i].second.getValueType()));
3359
3360 return CallOpc;
3361}
3362
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003363static
3364bool isLocalCall(const SDValue &Callee)
3365{
3366 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Roman Divacky6fc3ea22012-09-18 18:27:49 +00003367 return !G->getGlobal()->isDeclaration() &&
3368 !G->getGlobal()->isWeakForLinker();
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003369 return false;
3370}
3371
Dan Gohman98ca4f22009-08-05 01:29:28 +00003372SDValue
3373PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003374 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003375 const SmallVectorImpl<ISD::InputArg> &Ins,
3376 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003377 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003378
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003379 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003380 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003381 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00003382 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003383
3384 // Copy all of the result registers out of their specified physreg.
3385 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3386 CCValAssign &VA = RVLocs[i];
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003387 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand86aef0a2012-11-05 19:39:45 +00003388
3389 SDValue Val = DAG.getCopyFromReg(Chain, dl,
3390 VA.getLocReg(), VA.getLocVT(), InFlag);
3391 Chain = Val.getValue(1);
3392 InFlag = Val.getValue(2);
3393
3394 switch (VA.getLocInfo()) {
3395 default: llvm_unreachable("Unknown loc info!");
3396 case CCValAssign::Full: break;
3397 case CCValAssign::AExt:
3398 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3399 break;
3400 case CCValAssign::ZExt:
3401 Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val,
3402 DAG.getValueType(VA.getValVT()));
3403 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3404 break;
3405 case CCValAssign::SExt:
3406 Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val,
3407 DAG.getValueType(VA.getValVT()));
3408 Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
3409 break;
3410 }
3411
3412 InVals.push_back(Val);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003413 }
3414
Dan Gohman98ca4f22009-08-05 01:29:28 +00003415 return Chain;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003416}
3417
Dan Gohman98ca4f22009-08-05 01:29:28 +00003418SDValue
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003419PPCTargetLowering::FinishCall(CallingConv::ID CallConv, DebugLoc dl,
3420 bool isTailCall, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003421 SelectionDAG &DAG,
3422 SmallVector<std::pair<unsigned, SDValue>, 8>
3423 &RegsToPass,
3424 SDValue InFlag, SDValue Chain,
3425 SDValue &Callee,
3426 int SPDiff, unsigned NumBytes,
3427 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohmand858e902010-04-17 15:26:15 +00003428 SmallVectorImpl<SDValue> &InVals) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003429 std::vector<EVT> NodeTys;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003430 SmallVector<SDValue, 8> Ops;
3431 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
3432 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00003433 PPCSubTarget);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003434
Hal Finkel82b38212012-08-28 02:10:27 +00003435 // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls
3436 if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
3437 Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32));
3438
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003439 // When performing tail call optimization the callee pops its arguments off
3440 // the stack. Account for this here so these bytes can be pushed back on in
Eli Bendersky700ed802013-02-21 20:05:00 +00003441 // PPCFrameLowering::eliminateCallFramePseudoInstr.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003442 int BytesCalleePops =
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003443 (CallConv == CallingConv::Fast &&
3444 getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003445
Roman Divackye46137f2012-03-06 16:41:49 +00003446 // Add a register mask operand representing the call-preserved registers.
3447 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3448 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3449 assert(Mask && "Missing call preserved mask for calling convention");
3450 Ops.push_back(DAG.getRegisterMask(Mask));
3451
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003452 if (InFlag.getNode())
3453 Ops.push_back(InFlag);
3454
3455 // Emit tail call.
3456 if (isTailCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003457 assert(((Callee.getOpcode() == ISD::Register &&
3458 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
3459 Callee.getOpcode() == ISD::TargetExternalSymbol ||
3460 Callee.getOpcode() == ISD::TargetGlobalAddress ||
3461 isa<ConstantSDNode>(Callee)) &&
3462 "Expecting an global address, external symbol, absolute value or register");
3463
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003465 }
3466
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003467 // Add a NOP immediately after the branch instruction when using the 64-bit
3468 // SVR4 ABI. At link time, if caller and callee are in a different module and
3469 // thus have a different TOC, the call will be replaced with a call to a stub
3470 // function which saves the current TOC, loads the TOC of the callee and
3471 // branches to the callee. The NOP will be replaced with a load instruction
3472 // which restores the TOC of the caller from the TOC save slot of the current
3473 // stack frame. If caller and callee belong to the same module (and have the
3474 // same TOC), the NOP will remain unchanged.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003475
3476 bool needsTOCRestore = false;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003477 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003478 if (CallOpc == PPCISD::BCTRL) {
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003479 // This is a call through a function pointer.
3480 // Restore the caller TOC from the save area into R2.
3481 // See PrepareCall() for more information about calls through function
3482 // pointers in the 64-bit SVR4 ABI.
3483 // We are using a target-specific load with r2 hard coded, because the
3484 // result of a target-independent load would never go directly into r2,
3485 // since r2 is a reserved register (which prevents the register allocator
3486 // from allocating it), resulting in an additional register being
3487 // allocated and an unnecessary move instruction being generated.
Hal Finkel5b00cea2012-03-31 14:45:15 +00003488 needsTOCRestore = true;
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003489 } else if ((CallOpc == PPCISD::CALL) && !isLocalCall(Callee)) {
Roman Divackyeb8b7dc2012-09-18 16:47:58 +00003490 // Otherwise insert NOP for non-local calls.
Ulrich Weigand86765fb2013-03-22 15:24:13 +00003491 CallOpc = PPCISD::CALL_NOP;
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003492 }
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003493 }
3494
Hal Finkel5b00cea2012-03-31 14:45:15 +00003495 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
3496 InFlag = Chain.getValue(1);
3497
3498 if (needsTOCRestore) {
3499 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3500 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
3501 InFlag = Chain.getValue(1);
3502 }
3503
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003504 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3505 DAG.getIntPtrConstant(BytesCalleePops, true),
3506 InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003507 if (!Ins.empty())
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003508 InFlag = Chain.getValue(1);
3509
Dan Gohman98ca4f22009-08-05 01:29:28 +00003510 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3511 Ins, dl, DAG, InVals);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003512}
3513
Dan Gohman98ca4f22009-08-05 01:29:28 +00003514SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003515PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +00003516 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00003517 SelectionDAG &DAG = CLI.DAG;
3518 DebugLoc &dl = CLI.DL;
3519 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
3520 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
3521 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
3522 SDValue Chain = CLI.Chain;
3523 SDValue Callee = CLI.Callee;
3524 bool &isTailCall = CLI.IsTailCall;
3525 CallingConv::ID CallConv = CLI.CallConv;
3526 bool isVarArg = CLI.IsVarArg;
3527
Evan Cheng0c439eb2010-01-27 00:07:07 +00003528 if (isTailCall)
3529 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
3530 Ins, DAG);
3531
Bill Schmidt726c2372012-10-23 15:51:16 +00003532 if (PPCSubTarget.isSVR4ABI()) {
3533 if (PPCSubTarget.isPPC64())
3534 return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg,
3535 isTailCall, Outs, OutVals, Ins,
3536 dl, DAG, InVals);
3537 else
3538 return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg,
3539 isTailCall, Outs, OutVals, Ins,
3540 dl, DAG, InVals);
3541 }
Chris Lattnerb9082582010-11-14 23:42:06 +00003542
Bill Schmidt726c2372012-10-23 15:51:16 +00003543 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
3544 isTailCall, Outs, OutVals, Ins,
3545 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00003546}
3547
3548SDValue
Bill Schmidt419f3762012-09-19 15:42:13 +00003549PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee,
3550 CallingConv::ID CallConv, bool isVarArg,
3551 bool isTailCall,
3552 const SmallVectorImpl<ISD::OutputArg> &Outs,
3553 const SmallVectorImpl<SDValue> &OutVals,
3554 const SmallVectorImpl<ISD::InputArg> &Ins,
3555 DebugLoc dl, SelectionDAG &DAG,
3556 SmallVectorImpl<SDValue> &InVals) const {
3557 // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003558 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003559
Dan Gohman98ca4f22009-08-05 01:29:28 +00003560 assert((CallConv == CallingConv::C ||
3561 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerffd02002009-07-03 06:45:56 +00003562
Tilmann Schellerffd02002009-07-03 06:45:56 +00003563 unsigned PtrByteSize = 4;
3564
3565 MachineFunction &MF = DAG.getMachineFunction();
3566
3567 // Mark this function as potentially containing a function that contains a
3568 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3569 // and restoring the callers stack pointer in this functions epilog. This is
3570 // done because by tail calling the called function might overwrite the value
3571 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky8a8d4792011-12-02 22:16:29 +00003572 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3573 CallConv == CallingConv::Fast)
Tilmann Schellerffd02002009-07-03 06:45:56 +00003574 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003575
Tilmann Schellerffd02002009-07-03 06:45:56 +00003576 // Count how many bytes are to be pushed on the stack, including the linkage
3577 // area, parameter list area and the part of the local variable space which
3578 // contains copies of aggregates which are passed by value.
3579
3580 // Assign locations to all of the outgoing arguments.
3581 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003582 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003583 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00003584
3585 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003586 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003587
3588 if (isVarArg) {
3589 // Handle fixed and variable vector arguments differently.
3590 // Fixed vector arguments go into registers as long as registers are
3591 // available. Variable vector arguments always go into memory.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003592 unsigned NumArgs = Outs.size();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003593
Tilmann Schellerffd02002009-07-03 06:45:56 +00003594 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00003595 MVT ArgVT = Outs[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00003596 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003597 bool Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003598
Dan Gohman98ca4f22009-08-05 01:29:28 +00003599 if (Outs[i].IsFixed) {
Bill Schmidt212af6a2013-02-06 17:33:58 +00003600 Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
3601 CCInfo);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003602 } else {
Bill Schmidt212af6a2013-02-06 17:33:58 +00003603 Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
3604 ArgFlags, CCInfo);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003605 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003606
Tilmann Schellerffd02002009-07-03 06:45:56 +00003607 if (Result) {
Torok Edwindac237e2009-07-08 20:53:28 +00003608#ifndef NDEBUG
Chris Lattner45cfe542009-08-23 06:03:38 +00003609 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sands1440e8b2010-11-03 11:35:31 +00003610 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +00003611#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00003612 llvm_unreachable(0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003613 }
3614 }
3615 } else {
3616 // All arguments are treated the same.
Bill Schmidt212af6a2013-02-06 17:33:58 +00003617 CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003618 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003619
Tilmann Schellerffd02002009-07-03 06:45:56 +00003620 // Assign locations to all of the outgoing aggregate by value arguments.
3621 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003622 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00003623 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00003624
3625 // Reserve stack space for the allocations in CCInfo.
3626 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
3627
Bill Schmidt212af6a2013-02-06 17:33:58 +00003628 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003629
3630 // Size of the linkage area, parameter list area and the part of the local
3631 // space variable where copies of aggregates which are passed by value are
3632 // stored.
3633 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003634
Tilmann Schellerffd02002009-07-03 06:45:56 +00003635 // Calculate by how many bytes the stack has to be adjusted in case of tail
3636 // call optimization.
3637 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3638
3639 // Adjust the stack pointer for the new arguments...
3640 // These operations are automatically eliminated by the prolog/epilog pass
3641 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
3642 SDValue CallSeqStart = Chain;
3643
3644 // Load the return address and frame pointer so it can be moved somewhere else
3645 // later.
3646 SDValue LROp, FPOp;
3647 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
3648 dl);
3649
3650 // Set up a copy of the stack pointer for use loading and storing any
3651 // arguments that may not fit in the registers available for argument
3652 // passing.
Owen Anderson825b72b2009-08-11 20:47:22 +00003653 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003654
Tilmann Schellerffd02002009-07-03 06:45:56 +00003655 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3656 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3657 SmallVector<SDValue, 8> MemOpChains;
3658
Roman Divacky0aaa9192011-08-30 17:04:16 +00003659 bool seenFloatArg = false;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003660 // Walk the register/memloc assignments, inserting copies/loads.
3661 for (unsigned i = 0, j = 0, e = ArgLocs.size();
3662 i != e;
3663 ++i) {
3664 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00003665 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00003666 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003667
Tilmann Schellerffd02002009-07-03 06:45:56 +00003668 if (Flags.isByVal()) {
3669 // Argument is an aggregate which is passed by value, thus we need to
3670 // create a copy of it in the local variable space of the current stack
3671 // frame (which is the stack frame of the caller) and pass the address of
3672 // this copy to the callee.
3673 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
3674 CCValAssign &ByValVA = ByValArgLocs[j++];
3675 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003676
Tilmann Schellerffd02002009-07-03 06:45:56 +00003677 // Memory reserved in the local variable space of the callers stack frame.
3678 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003679
Tilmann Schellerffd02002009-07-03 06:45:56 +00003680 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3681 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003682
Tilmann Schellerffd02002009-07-03 06:45:56 +00003683 // Create a copy of the argument in the local area of the current
3684 // stack frame.
3685 SDValue MemcpyCall =
3686 CreateCopyOfByValArgument(Arg, PtrOff,
3687 CallSeqStart.getNode()->getOperand(0),
3688 Flags, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003689
Tilmann Schellerffd02002009-07-03 06:45:56 +00003690 // This must go outside the CALLSEQ_START..END.
3691 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
3692 CallSeqStart.getNode()->getOperand(1));
3693 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3694 NewCallSeqStart.getNode());
3695 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003696
Tilmann Schellerffd02002009-07-03 06:45:56 +00003697 // Pass the address of the aggregate copy on the stack either in a
3698 // physical register or in the parameter list area of the current stack
3699 // frame to the callee.
3700 Arg = PtrOff;
3701 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003702
Tilmann Schellerffd02002009-07-03 06:45:56 +00003703 if (VA.isRegLoc()) {
Roman Divacky0aaa9192011-08-30 17:04:16 +00003704 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerffd02002009-07-03 06:45:56 +00003705 // Put argument in a physical register.
3706 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3707 } else {
3708 // Put argument in the parameter list area of the current stack frame.
3709 assert(VA.isMemLoc());
3710 unsigned LocMemOffset = VA.getLocMemOffset();
3711
3712 if (!isTailCall) {
3713 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3714 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3715
3716 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003717 MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00003718 false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00003719 } else {
3720 // Calculate and remember argument location.
3721 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3722 TailCallArguments);
3723 }
3724 }
3725 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003726
Tilmann Schellerffd02002009-07-03 06:45:56 +00003727 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003728 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerffd02002009-07-03 06:45:56 +00003729 &MemOpChains[0], MemOpChains.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003730
Tilmann Schellerffd02002009-07-03 06:45:56 +00003731 // Build a sequence of copy-to-reg nodes chained together with token chain
3732 // and flag operands which copy the outgoing args into the appropriate regs.
3733 SDValue InFlag;
3734 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3735 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3736 RegsToPass[i].second, InFlag);
3737 InFlag = Chain.getValue(1);
3738 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003739
Hal Finkel82b38212012-08-28 02:10:27 +00003740 // Set CR bit 6 to true if this is a vararg call with floating args passed in
3741 // registers.
3742 if (isVarArg) {
NAKAMURA Takumid2a35f22012-08-30 15:52:29 +00003743 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
3744 SDValue Ops[] = { Chain, InFlag };
3745
Hal Finkel82b38212012-08-28 02:10:27 +00003746 Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET,
NAKAMURA Takumid2a35f22012-08-30 15:52:29 +00003747 dl, VTs, Ops, InFlag.getNode() ? 2 : 1);
3748
Hal Finkel82b38212012-08-28 02:10:27 +00003749 InFlag = Chain.getValue(1);
3750 }
3751
Chris Lattnerb9082582010-11-14 23:42:06 +00003752 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003753 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3754 false, TailCallArguments);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003755
Dan Gohman98ca4f22009-08-05 01:29:28 +00003756 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3757 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3758 Ins, InVals);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003759}
3760
Bill Schmidt726c2372012-10-23 15:51:16 +00003761// Copy an argument into memory, being careful to do this outside the
3762// call sequence for the call to which the argument belongs.
Dan Gohman98ca4f22009-08-05 01:29:28 +00003763SDValue
Bill Schmidt726c2372012-10-23 15:51:16 +00003764PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff,
3765 SDValue CallSeqStart,
3766 ISD::ArgFlagsTy Flags,
3767 SelectionDAG &DAG,
3768 DebugLoc dl) const {
3769 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
3770 CallSeqStart.getNode()->getOperand(0),
3771 Flags, DAG, dl);
3772 // The MEMCPY must go outside the CALLSEQ_START..END.
3773 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
3774 CallSeqStart.getNode()->getOperand(1));
3775 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3776 NewCallSeqStart.getNode());
3777 return NewCallSeqStart;
3778}
3779
3780SDValue
3781PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003782 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003783 bool isTailCall,
3784 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003785 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003786 const SmallVectorImpl<ISD::InputArg> &Ins,
3787 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003788 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003789
Bill Schmidt726c2372012-10-23 15:51:16 +00003790 unsigned NumOps = Outs.size();
Bill Schmidt419f3762012-09-19 15:42:13 +00003791
Bill Schmidt726c2372012-10-23 15:51:16 +00003792 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3793 unsigned PtrByteSize = 8;
3794
3795 MachineFunction &MF = DAG.getMachineFunction();
3796
3797 // Mark this function as potentially containing a function that contains a
3798 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3799 // and restoring the callers stack pointer in this functions epilog. This is
3800 // done because by tail calling the called function might overwrite the value
3801 // in this function's (MF) stack pointer stack slot 0(SP).
3802 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
3803 CallConv == CallingConv::Fast)
3804 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3805
3806 unsigned nAltivecParamsAtEnd = 0;
3807
3808 // Count how many bytes are to be pushed on the stack, including the linkage
3809 // area, and parameter passing area. We start with at least 48 bytes, which
3810 // is reserved space for [SP][CR][LR][3 x unused].
3811 // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result
3812 // of this call.
3813 unsigned NumBytes =
3814 CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv,
3815 Outs, OutVals, nAltivecParamsAtEnd);
3816
3817 // Calculate by how many bytes the stack has to be adjusted in case of tail
3818 // call optimization.
3819 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
3820
3821 // To protect arguments on the stack from being clobbered in a tail call,
3822 // force all the loads to happen before doing any other lowering.
3823 if (isTailCall)
3824 Chain = DAG.getStackArgumentTokenFactor(Chain);
3825
3826 // Adjust the stack pointer for the new arguments...
3827 // These operations are automatically eliminated by the prolog/epilog pass
3828 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
3829 SDValue CallSeqStart = Chain;
3830
3831 // Load the return address and frame pointer so it can be move somewhere else
3832 // later.
3833 SDValue LROp, FPOp;
3834 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3835 dl);
3836
3837 // Set up a copy of the stack pointer for use loading and storing any
3838 // arguments that may not fit in the registers available for argument
3839 // passing.
3840 SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
3841
3842 // Figure out which arguments are going to go in registers, and which in
3843 // memory. Also, if this is a vararg function, floating point operations
3844 // must be stored to our stack, and loaded into integer regs as well, if
3845 // any integer regs are available for argument passing.
3846 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true);
3847 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
3848
3849 static const uint16_t GPR[] = {
3850 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3851 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3852 };
3853 static const uint16_t *FPR = GetFPR();
3854
3855 static const uint16_t VR[] = {
3856 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3857 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3858 };
3859 const unsigned NumGPRs = array_lengthof(GPR);
3860 const unsigned NumFPRs = 13;
3861 const unsigned NumVRs = array_lengthof(VR);
3862
3863 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3864 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3865
3866 SmallVector<SDValue, 8> MemOpChains;
3867 for (unsigned i = 0; i != NumOps; ++i) {
3868 SDValue Arg = OutVals[i];
3869 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3870
3871 // PtrOff will be used to store the current argument to the stack if a
3872 // register cannot be found for it.
3873 SDValue PtrOff;
3874
3875 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
3876
3877 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
3878
3879 // Promote integers to 64-bit values.
3880 if (Arg.getValueType() == MVT::i32) {
3881 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3882 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3883 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
3884 }
3885
3886 // FIXME memcpy is used way more than necessary. Correctness first.
3887 // Note: "by value" is code for passing a structure by value, not
3888 // basic types.
3889 if (Flags.isByVal()) {
3890 // Note: Size includes alignment padding, so
3891 // struct x { short a; char b; }
3892 // will have Size = 4. With #pragma pack(1), it will have Size = 3.
3893 // These are the proper values we need for right-justifying the
3894 // aggregate in a parameter register.
3895 unsigned Size = Flags.getByValSize();
Bill Schmidt42d43352012-10-31 01:15:05 +00003896
3897 // An empty aggregate parameter takes up no storage and no
3898 // registers.
3899 if (Size == 0)
3900 continue;
3901
Bill Schmidt726c2372012-10-23 15:51:16 +00003902 // All aggregates smaller than 8 bytes must be passed right-justified.
3903 if (Size==1 || Size==2 || Size==4) {
3904 EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32);
3905 if (GPR_idx != NumGPRs) {
3906 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
3907 MachinePointerInfo(), VT,
3908 false, false, 0);
3909 MemOpChains.push_back(Load.getValue(1));
3910 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3911
3912 ArgOffset += PtrByteSize;
3913 continue;
3914 }
3915 }
3916
3917 if (GPR_idx == NumGPRs && Size < 8) {
3918 SDValue Const = DAG.getConstant(PtrByteSize - Size,
3919 PtrOff.getValueType());
3920 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3921 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3922 CallSeqStart,
3923 Flags, DAG, dl);
3924 ArgOffset += PtrByteSize;
3925 continue;
3926 }
3927 // Copy entire object into memory. There are cases where gcc-generated
3928 // code assumes it is there, even if it could be put entirely into
3929 // registers. (This is not what the doc says.)
3930
3931 // FIXME: The above statement is likely due to a misunderstanding of the
3932 // documents. All arguments must be copied into the parameter area BY
3933 // THE CALLEE in the event that the callee takes the address of any
3934 // formal argument. That has not yet been implemented. However, it is
3935 // reasonable to use the stack area as a staging area for the register
3936 // load.
3937
3938 // Skip this for small aggregates, as we will use the same slot for a
3939 // right-justified copy, below.
3940 if (Size >= 8)
3941 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
3942 CallSeqStart,
3943 Flags, DAG, dl);
3944
3945 // When a register is available, pass a small aggregate right-justified.
3946 if (Size < 8 && GPR_idx != NumGPRs) {
3947 // The easiest way to get this right-justified in a register
3948 // is to copy the structure into the rightmost portion of a
3949 // local variable slot, then load the whole slot into the
3950 // register.
3951 // FIXME: The memcpy seems to produce pretty awful code for
3952 // small aggregates, particularly for packed ones.
3953 // FIXME: It would be preferable to use the slot in the
3954 // parameter save area instead of a new local variable.
3955 SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType());
3956 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
3957 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
3958 CallSeqStart,
3959 Flags, DAG, dl);
3960
3961 // Load the slot into the register.
3962 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff,
3963 MachinePointerInfo(),
3964 false, false, false, 0);
3965 MemOpChains.push_back(Load.getValue(1));
3966 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3967
3968 // Done with this argument.
3969 ArgOffset += PtrByteSize;
3970 continue;
3971 }
3972
3973 // For aggregates larger than PtrByteSize, copy the pieces of the
3974 // object that fit into registers from the parameter save area.
3975 for (unsigned j=0; j<Size; j+=PtrByteSize) {
3976 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
3977 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
3978 if (GPR_idx != NumGPRs) {
3979 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
3980 MachinePointerInfo(),
3981 false, false, false, 0);
3982 MemOpChains.push_back(Load.getValue(1));
3983 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3984 ArgOffset += PtrByteSize;
3985 } else {
3986 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
3987 break;
3988 }
3989 }
3990 continue;
3991 }
3992
3993 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
3994 default: llvm_unreachable("Unexpected ValueType for argument!");
3995 case MVT::i32:
3996 case MVT::i64:
3997 if (GPR_idx != NumGPRs) {
3998 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
3999 } else {
4000 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4001 true, isTailCall, false, MemOpChains,
4002 TailCallArguments, dl);
4003 }
4004 ArgOffset += PtrByteSize;
4005 break;
4006 case MVT::f32:
4007 case MVT::f64:
4008 if (FPR_idx != NumFPRs) {
4009 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4010
4011 if (isVarArg) {
Bill Schmidte6c56432012-10-29 21:18:16 +00004012 // A single float or an aggregate containing only a single float
4013 // must be passed right-justified in the stack doubleword, and
4014 // in the GPR, if one is available.
4015 SDValue StoreOff;
4016 if (Arg.getValueType().getSimpleVT().SimpleTy == MVT::f32) {
4017 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4018 StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4019 } else
4020 StoreOff = PtrOff;
4021
4022 SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff,
Bill Schmidt726c2372012-10-23 15:51:16 +00004023 MachinePointerInfo(), false, false, 0);
4024 MemOpChains.push_back(Store);
4025
4026 // Float varargs are always shadowed in available integer registers
4027 if (GPR_idx != NumGPRs) {
4028 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4029 MachinePointerInfo(), false, false,
4030 false, 0);
4031 MemOpChains.push_back(Load.getValue(1));
4032 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4033 }
4034 } else if (GPR_idx != NumGPRs)
4035 // If we have any FPRs remaining, we may also have GPRs remaining.
4036 ++GPR_idx;
4037 } else {
4038 // Single-precision floating-point values are mapped to the
4039 // second (rightmost) word of the stack doubleword.
4040 if (Arg.getValueType() == MVT::f32) {
4041 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
4042 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
4043 }
4044
4045 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4046 true, isTailCall, false, MemOpChains,
4047 TailCallArguments, dl);
4048 }
4049 ArgOffset += 8;
4050 break;
4051 case MVT::v4f32:
4052 case MVT::v4i32:
4053 case MVT::v8i16:
4054 case MVT::v16i8:
4055 if (isVarArg) {
4056 // These go aligned on the stack, or in the corresponding R registers
4057 // when within range. The Darwin PPC ABI doc claims they also go in
4058 // V registers; in fact gcc does this only for arguments that are
4059 // prototyped, not for those that match the ... We do it for all
4060 // arguments, seems to work.
4061 while (ArgOffset % 16 !=0) {
4062 ArgOffset += PtrByteSize;
4063 if (GPR_idx != NumGPRs)
4064 GPR_idx++;
4065 }
4066 // We could elide this store in the case where the object fits
4067 // entirely in R registers. Maybe later.
4068 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
4069 DAG.getConstant(ArgOffset, PtrVT));
4070 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4071 MachinePointerInfo(), false, false, 0);
4072 MemOpChains.push_back(Store);
4073 if (VR_idx != NumVRs) {
4074 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
4075 MachinePointerInfo(),
4076 false, false, false, 0);
4077 MemOpChains.push_back(Load.getValue(1));
4078 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4079 }
4080 ArgOffset += 16;
4081 for (unsigned i=0; i<16; i+=PtrByteSize) {
4082 if (GPR_idx == NumGPRs)
4083 break;
4084 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
4085 DAG.getConstant(i, PtrVT));
4086 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
4087 false, false, false, 0);
4088 MemOpChains.push_back(Load.getValue(1));
4089 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4090 }
4091 break;
4092 }
4093
4094 // Non-varargs Altivec params generally go in registers, but have
4095 // stack space allocated at the end.
4096 if (VR_idx != NumVRs) {
4097 // Doesn't have GPR space allocated.
4098 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4099 } else {
4100 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4101 true, isTailCall, true, MemOpChains,
4102 TailCallArguments, dl);
4103 ArgOffset += 16;
4104 }
4105 break;
4106 }
4107 }
4108
4109 if (!MemOpChains.empty())
4110 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4111 &MemOpChains[0], MemOpChains.size());
4112
4113 // Check if this is an indirect call (MTCTR/BCTRL).
4114 // See PrepareCall() for more information about calls through function
4115 // pointers in the 64-bit SVR4 ABI.
4116 if (!isTailCall &&
4117 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4118 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4119 !isBLACompatibleAddress(Callee, DAG)) {
4120 // Load r2 into a virtual register and store it to the TOC save area.
4121 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
4122 // TOC save area offset.
4123 SDValue PtrOff = DAG.getIntPtrConstant(40);
4124 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
4125 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
4126 false, false, 0);
4127 // R12 must contain the address of an indirect callee. This does not
4128 // mean the MTCTR instruction must use R12; it's easier to model this
4129 // as an extra parameter, so do that.
4130 RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee));
4131 }
4132
4133 // Build a sequence of copy-to-reg nodes chained together with token chain
4134 // and flag operands which copy the outgoing args into the appropriate regs.
4135 SDValue InFlag;
4136 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
4137 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
4138 RegsToPass[i].second, InFlag);
4139 InFlag = Chain.getValue(1);
4140 }
4141
4142 if (isTailCall)
4143 PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp,
4144 FPOp, true, TailCallArguments);
4145
4146 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4147 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4148 Ins, InVals);
4149}
4150
4151SDValue
4152PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
4153 CallingConv::ID CallConv, bool isVarArg,
4154 bool isTailCall,
4155 const SmallVectorImpl<ISD::OutputArg> &Outs,
4156 const SmallVectorImpl<SDValue> &OutVals,
4157 const SmallVectorImpl<ISD::InputArg> &Ins,
4158 DebugLoc dl, SelectionDAG &DAG,
4159 SmallVectorImpl<SDValue> &InVals) const {
4160
4161 unsigned NumOps = Outs.size();
Scott Michelfdc40a02009-02-17 22:15:04 +00004162
Owen Andersone50ed302009-08-10 22:56:29 +00004163 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004165 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004166
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004167 MachineFunction &MF = DAG.getMachineFunction();
4168
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004169 // Mark this function as potentially containing a function that contains a
4170 // tail call. As a consequence the frame pointer will be used for dynamicalloc
4171 // and restoring the callers stack pointer in this functions epilog. This is
4172 // done because by tail calling the called function might overwrite the value
4173 // in this function's (MF) stack pointer stack slot 0(SP).
Nick Lewycky8a8d4792011-12-02 22:16:29 +00004174 if (getTargetMachine().Options.GuaranteedTailCallOpt &&
4175 CallConv == CallingConv::Fast)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004176 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
4177
4178 unsigned nAltivecParamsAtEnd = 0;
4179
Chris Lattnerabde4602006-05-16 22:56:08 +00004180 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00004181 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004182 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004183 unsigned NumBytes =
Dan Gohman98ca4f22009-08-05 01:29:28 +00004184 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanc9403652010-07-07 15:54:55 +00004185 Outs, OutVals,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004186 nAltivecParamsAtEnd);
Dale Johannesen75092de2008-03-12 00:22:17 +00004187
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004188 // Calculate by how many bytes the stack has to be adjusted in case of tail
4189 // call optimization.
4190 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelfdc40a02009-02-17 22:15:04 +00004191
Dan Gohman98ca4f22009-08-05 01:29:28 +00004192 // To protect arguments on the stack from being clobbered in a tail call,
4193 // force all the loads to happen before doing any other lowering.
4194 if (isTailCall)
4195 Chain = DAG.getStackArgumentTokenFactor(Chain);
4196
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004197 // Adjust the stack pointer for the new arguments...
4198 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +00004199 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohman475871a2008-07-27 21:46:04 +00004200 SDValue CallSeqStart = Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00004201
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004202 // Load the return address and frame pointer so it can be move somewhere else
4203 // later.
Dan Gohman475871a2008-07-27 21:46:04 +00004204 SDValue LROp, FPOp;
Tilmann Schellerffd02002009-07-03 06:45:56 +00004205 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
4206 dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004207
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004208 // Set up a copy of the stack pointer for use loading and storing any
4209 // arguments that may not fit in the registers available for argument
4210 // passing.
Dan Gohman475871a2008-07-27 21:46:04 +00004211 SDValue StackPtr;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004212 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004214 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004216
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004217 // Figure out which arguments are going to go in registers, and which in
4218 // memory. Also, if this is a vararg function, floating point operations
4219 // must be stored to our stack, and loaded into integer regs as well, if
4220 // any integer regs are available for argument passing.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004221 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004222 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004223
Craig Topperb78ca422012-03-11 07:16:55 +00004224 static const uint16_t GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00004225 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
4226 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
4227 };
Craig Topperb78ca422012-03-11 07:16:55 +00004228 static const uint16_t GPR_64[] = { // 64-bit registers.
Chris Lattnerc91a4752006-06-26 22:48:35 +00004229 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
4230 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
4231 };
Craig Topperb78ca422012-03-11 07:16:55 +00004232 static const uint16_t *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00004233
Craig Topperb78ca422012-03-11 07:16:55 +00004234 static const uint16_t VR[] = {
Chris Lattner9a2a4972006-05-17 06:01:33 +00004235 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
4236 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
4237 };
Owen Anderson718cb662007-09-07 04:06:50 +00004238 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004239 const unsigned NumFPRs = 13;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00004240 const unsigned NumVRs = array_lengthof(VR);
Scott Michelfdc40a02009-02-17 22:15:04 +00004241
Craig Topperb78ca422012-03-11 07:16:55 +00004242 const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32;
Chris Lattnerc91a4752006-06-26 22:48:35 +00004243
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004244 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004245 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
4246
Dan Gohman475871a2008-07-27 21:46:04 +00004247 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00004248 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00004249 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00004250 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00004251
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004252 // PtrOff will be used to store the current argument to the stack if a
4253 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00004254 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00004255
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004256 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00004257
Dale Johannesen39355f92009-02-04 02:34:38 +00004258 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004259
4260 // On PPC64, promote integers to 64-bit values.
Owen Anderson825b72b2009-08-11 20:47:22 +00004261 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00004262 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
4263 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson825b72b2009-08-11 20:47:22 +00004264 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00004265 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004266
Dale Johannesen8419dd62008-03-07 20:27:40 +00004267 // FIXME memcpy is used way more than necessary. Correctness first.
Bill Schmidt419f3762012-09-19 15:42:13 +00004268 // Note: "by value" is code for passing a structure by value, not
4269 // basic types.
Duncan Sands276dcbd2008-03-21 09:14:45 +00004270 if (Flags.isByVal()) {
4271 unsigned Size = Flags.getByValSize();
Bill Schmidt726c2372012-10-23 15:51:16 +00004272 // Very small objects are passed right-justified. Everything else is
4273 // passed left-justified.
4274 if (Size==1 || Size==2) {
4275 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004276 if (GPR_idx != NumGPRs) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004277 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004278 MachinePointerInfo(), VT,
4279 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00004280 MemOpChains.push_back(Load.getValue(1));
4281 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004282
4283 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004284 } else {
Bill Schmidt7a6cb152012-10-16 13:30:53 +00004285 SDValue Const = DAG.getConstant(PtrByteSize - Size,
4286 PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004287 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Bill Schmidt726c2372012-10-23 15:51:16 +00004288 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr,
4289 CallSeqStart,
4290 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00004291 ArgOffset += PtrByteSize;
4292 }
4293 continue;
4294 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00004295 // Copy entire object into memory. There are cases where gcc-generated
4296 // code assumes it is there, even if it could be put entirely into
4297 // registers. (This is not what the doc says.)
Bill Schmidt726c2372012-10-23 15:51:16 +00004298 Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff,
4299 CallSeqStart,
4300 Flags, DAG, dl);
Bill Schmidt419f3762012-09-19 15:42:13 +00004301
4302 // For small aggregates (Darwin only) and aggregates >= PtrByteSize,
4303 // copy the pieces of the object that fit into registers from the
4304 // parameter save area.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004305 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00004306 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004307 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004308 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004309 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
4310 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004311 false, false, false, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00004312 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004313 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004314 ArgOffset += PtrByteSize;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004315 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00004316 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00004317 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004318 }
4319 }
4320 continue;
4321 }
4322
Owen Anderson825b72b2009-08-11 20:47:22 +00004323 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004324 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 case MVT::i32:
4326 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00004327 if (GPR_idx != NumGPRs) {
4328 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004329 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004330 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4331 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004332 TailCallArguments, dl);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004333 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004334 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004335 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004336 case MVT::f32:
4337 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00004338 if (FPR_idx != NumFPRs) {
4339 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
4340
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004341 if (isVarArg) {
Chris Lattner6229d0a2010-09-21 18:41:36 +00004342 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4343 MachinePointerInfo(), false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004344 MemOpChains.push_back(Store);
4345
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004346 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00004347 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004348 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004349 MachinePointerInfo(), false, false,
4350 false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004351 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004352 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004353 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004354 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00004355 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00004356 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004357 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
4358 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004359 false, false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004360 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004361 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00004362 }
4363 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004364 // If we have any FPRs remaining, we may also have GPRs remaining.
4365 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
4366 // GPRs.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004367 if (GPR_idx != NumGPRs)
4368 ++GPR_idx;
Owen Anderson825b72b2009-08-11 20:47:22 +00004369 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004370 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
4371 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00004372 }
Bill Schmidt726c2372012-10-23 15:51:16 +00004373 } else
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004374 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4375 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004376 TailCallArguments, dl);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004377 if (isPPC64)
4378 ArgOffset += 8;
4379 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004380 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004381 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004382 case MVT::v4f32:
4383 case MVT::v4i32:
4384 case MVT::v8i16:
4385 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00004386 if (isVarArg) {
4387 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00004388 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00004389 // V registers; in fact gcc does this only for arguments that are
4390 // prototyped, not for those that match the ... We do it for all
4391 // arguments, seems to work.
4392 while (ArgOffset % 16 !=0) {
4393 ArgOffset += PtrByteSize;
4394 if (GPR_idx != NumGPRs)
4395 GPR_idx++;
4396 }
4397 // We could elide this store in the case where the object fits
4398 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00004399 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00004400 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner6229d0a2010-09-21 18:41:36 +00004401 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
4402 MachinePointerInfo(), false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004403 MemOpChains.push_back(Store);
4404 if (VR_idx != NumVRs) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004405 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004406 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004407 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004408 MemOpChains.push_back(Load.getValue(1));
4409 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
4410 }
4411 ArgOffset += 16;
4412 for (unsigned i=0; i<16; i+=PtrByteSize) {
4413 if (GPR_idx == NumGPRs)
4414 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00004415 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00004416 DAG.getConstant(i, PtrVT));
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004417 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004418 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00004419 MemOpChains.push_back(Load.getValue(1));
4420 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
4421 }
4422 break;
4423 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004424
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004425 // Non-varargs Altivec params generally go in registers, but have
4426 // stack space allocated at the end.
4427 if (VR_idx != NumVRs) {
4428 // Doesn't have GPR space allocated.
4429 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
4430 } else if (nAltivecParamsAtEnd==0) {
4431 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004432 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4433 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004434 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00004435 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00004436 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00004437 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00004438 }
Chris Lattnerabde4602006-05-16 22:56:08 +00004439 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004440 // If all Altivec parameters fit in registers, as they usually do,
4441 // they get stack space following the non-Altivec parameters. We
4442 // don't track this here because nobody below needs it.
4443 // If there are more Altivec parameters than fit in registers emit
4444 // the stores here.
4445 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
4446 unsigned j = 0;
4447 // Offset is aligned; skip 1st 12 params which go in V registers.
4448 ArgOffset = ((ArgOffset+15)/16)*16;
4449 ArgOffset += 12*16;
4450 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00004451 SDValue Arg = OutVals[i];
4452 EVT ArgType = Outs[i].VT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004453 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
4454 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004455 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00004456 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004457 // We are emitting Altivec params in order.
4458 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
4459 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00004460 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00004461 ArgOffset += 16;
4462 }
4463 }
4464 }
4465 }
4466
Chris Lattner9a2a4972006-05-17 06:01:33 +00004467 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00004468 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00004469 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00004470
Dale Johannesenf7b73042010-03-09 20:15:42 +00004471 // On Darwin, R12 must contain the address of an indirect callee. This does
4472 // not mean the MTCTR instruction must use R12; it's easier to model this as
4473 // an extra parameter, so do that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004474 if (!isTailCall &&
Dale Johannesenf7b73042010-03-09 20:15:42 +00004475 !dyn_cast<GlobalAddressSDNode>(Callee) &&
4476 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
4477 !isBLACompatibleAddress(Callee, DAG))
4478 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
4479 PPC::R12), Callee));
4480
Chris Lattner9a2a4972006-05-17 06:01:33 +00004481 // Build a sequence of copy-to-reg nodes chained together with token chain
4482 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00004483 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00004484 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004485 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00004486 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00004487 InFlag = Chain.getValue(1);
4488 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004489
Chris Lattnerb9082582010-11-14 23:42:06 +00004490 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004491 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
4492 FPOp, true, TailCallArguments);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004493
Dan Gohman98ca4f22009-08-05 01:29:28 +00004494 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
4495 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
4496 Ins, InVals);
Chris Lattnerabde4602006-05-16 22:56:08 +00004497}
4498
Hal Finkeld712f932011-10-14 19:51:36 +00004499bool
4500PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
4501 MachineFunction &MF, bool isVarArg,
4502 const SmallVectorImpl<ISD::OutputArg> &Outs,
4503 LLVMContext &Context) const {
4504 SmallVector<CCValAssign, 16> RVLocs;
4505 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
4506 RVLocs, Context);
4507 return CCInfo.CheckReturn(Outs, RetCC_PPC);
4508}
4509
Dan Gohman98ca4f22009-08-05 01:29:28 +00004510SDValue
4511PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00004512 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004513 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00004514 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00004515 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00004516
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004517 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00004518 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Gabor Greifa4b00b22012-04-19 15:16:31 +00004519 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004520 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00004521
Dan Gohman475871a2008-07-27 21:46:04 +00004522 SDValue Flag;
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004523 SmallVector<SDValue, 4> RetOps(1, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00004524
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004525 // Copy the result values into the output registers.
4526 for (unsigned i = 0; i != RVLocs.size(); ++i) {
4527 CCValAssign &VA = RVLocs[i];
4528 assert(VA.isRegLoc() && "Can only return in registers!");
Ulrich Weigand86aef0a2012-11-05 19:39:45 +00004529
4530 SDValue Arg = OutVals[i];
4531
4532 switch (VA.getLocInfo()) {
4533 default: llvm_unreachable("Unknown loc info!");
4534 case CCValAssign::Full: break;
4535 case CCValAssign::AExt:
4536 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
4537 break;
4538 case CCValAssign::ZExt:
4539 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
4540 break;
4541 case CCValAssign::SExt:
4542 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
4543 break;
4544 }
4545
4546 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004547 Flag = Chain.getValue(1);
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004548 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004549 }
4550
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004551 RetOps[0] = Chain; // Update chain.
4552
4553 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00004554 if (Flag.getNode())
Jakob Stoklund Olesen6ab50612013-02-05 18:12:00 +00004555 RetOps.push_back(Flag);
4556
4557 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other,
4558 &RetOps[0], RetOps.size());
Chris Lattner1a635d62006-04-14 06:01:58 +00004559}
4560
Dan Gohman475871a2008-07-27 21:46:04 +00004561SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004562 const PPCSubtarget &Subtarget) const {
Jim Laskeyefc7e522006-12-04 22:04:42 +00004563 // When we pop the dynamic allocation we need to restore the SP link.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004564 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00004565
Jim Laskeyefc7e522006-12-04 22:04:42 +00004566 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00004567 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00004568
4569 // Construct the stack pointer operand.
Dale Johannesenb60d5192009-11-24 01:09:07 +00004570 bool isPPC64 = Subtarget.isPPC64();
4571 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00004572 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00004573
4574 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00004575 SDValue Chain = Op.getOperand(0);
4576 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004577
Jim Laskeyefc7e522006-12-04 22:04:42 +00004578 // Load the old link SP.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004579 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
4580 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004581 false, false, false, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004582
Jim Laskeyefc7e522006-12-04 22:04:42 +00004583 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004584 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00004585
Jim Laskeyefc7e522006-12-04 22:04:42 +00004586 // Store the old link SP.
Chris Lattner6229d0a2010-09-21 18:41:36 +00004587 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00004588 false, false, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00004589}
4590
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004591
4592
Dan Gohman475871a2008-07-27 21:46:04 +00004593SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004594PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00004595 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00004596 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004597 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00004598 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004599
4600 // Get current frame pointer save index. The users of this index will be
4601 // primarily DYNALLOC instructions.
4602 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4603 int RASI = FI->getReturnAddrSaveIndex();
4604
4605 // If the frame pointer save index hasn't been defined yet.
4606 if (!RASI) {
4607 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004608 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004609 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00004610 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004611 // Save the result.
4612 FI->setReturnAddrSaveIndex(RASI);
4613 }
4614 return DAG.getFrameIndex(RASI, PtrVT);
4615}
4616
Dan Gohman475871a2008-07-27 21:46:04 +00004617SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004618PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
4619 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00004620 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004621 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00004622 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00004623
4624 // Get current frame pointer save index. The users of this index will be
4625 // primarily DYNALLOC instructions.
4626 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
4627 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004628
Jim Laskey2f616bf2006-11-16 22:43:37 +00004629 // If the frame pointer save index hasn't been defined yet.
4630 if (!FPSI) {
4631 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00004632 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004633 isDarwinABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00004634
Jim Laskey2f616bf2006-11-16 22:43:37 +00004635 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00004636 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004637 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00004638 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004639 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004640 return DAG.getFrameIndex(FPSI, PtrVT);
4641}
Jim Laskey2f616bf2006-11-16 22:43:37 +00004642
Dan Gohman475871a2008-07-27 21:46:04 +00004643SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00004644 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004645 const PPCSubtarget &Subtarget) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00004646 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004647 SDValue Chain = Op.getOperand(0);
4648 SDValue Size = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004649 DebugLoc dl = Op.getDebugLoc();
4650
Jim Laskey2f616bf2006-11-16 22:43:37 +00004651 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00004652 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00004653 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00004654 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00004655 DAG.getConstant(0, PtrVT), Size);
4656 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00004657 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004658 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00004659 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson825b72b2009-08-11 20:47:22 +00004660 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00004661 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00004662}
4663
Hal Finkel7ee74a62013-03-21 21:37:52 +00004664SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
4665 SelectionDAG &DAG) const {
4666 DebugLoc DL = Op.getDebugLoc();
4667 return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL,
4668 DAG.getVTList(MVT::i32, MVT::Other),
4669 Op.getOperand(0), Op.getOperand(1));
4670}
4671
4672SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
4673 SelectionDAG &DAG) const {
4674 DebugLoc DL = Op.getDebugLoc();
4675 return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
4676 Op.getOperand(0), Op.getOperand(1));
4677}
4678
Chris Lattner1a635d62006-04-14 06:01:58 +00004679/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
4680/// possible.
Dan Gohmand858e902010-04-17 15:26:15 +00004681SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00004682 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004683 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
4684 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00004685 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004686
Hal Finkel59889f72013-04-07 22:11:09 +00004687 // We might be able to do better than this under some circumstances, but in
4688 // general, fsel-based lowering of select is a finite-math-only optimization.
4689 // For more information, see section F.3 of the 2.06 ISA specification.
4690 if (!DAG.getTarget().Options.NoInfsFPMath ||
4691 !DAG.getTarget().Options.NoNaNsFPMath)
4692 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004693
Hal Finkel59889f72013-04-07 22:11:09 +00004694 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004695
Owen Andersone50ed302009-08-10 22:56:29 +00004696 EVT ResVT = Op.getValueType();
4697 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00004698 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
4699 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00004700 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00004701
Chris Lattner1a635d62006-04-14 06:01:58 +00004702 // If the RHS of the comparison is a 0.0, we don't need to do the
4703 // subtraction at all.
Hal Finkel59889f72013-04-07 22:11:09 +00004704 SDValue Sel1;
Chris Lattner1a635d62006-04-14 06:01:58 +00004705 if (isFloatingPointZero(RHS))
4706 switch (CC) {
4707 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel59889f72013-04-07 22:11:09 +00004708 case ISD::SETNE:
4709 std::swap(TV, FV);
4710 case ISD::SETEQ:
4711 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4712 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
4713 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
4714 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4715 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4716 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4717 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004718 case ISD::SETULT:
4719 case ISD::SETLT:
4720 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00004721 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004722 case ISD::SETGE:
Owen Anderson825b72b2009-08-11 20:47:22 +00004723 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4724 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00004725 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004726 case ISD::SETUGT:
4727 case ISD::SETGT:
4728 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00004729 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004730 case ISD::SETLE:
Owen Anderson825b72b2009-08-11 20:47:22 +00004731 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
4732 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00004733 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00004734 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004735 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004736
Dan Gohman475871a2008-07-27 21:46:04 +00004737 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00004738 switch (CC) {
4739 default: break; // SETUO etc aren't handled by fsel.
Hal Finkel59889f72013-04-07 22:11:09 +00004740 case ISD::SETNE:
4741 std::swap(TV, FV);
4742 case ISD::SETEQ:
4743 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
4744 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4745 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
4746 Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
4747 if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits
4748 Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1);
4749 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
4750 DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004751 case ISD::SETULT:
4752 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00004753 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004754 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4755 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004756 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00004757 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004758 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00004759 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004760 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4761 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004762 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004763 case ISD::SETUGT:
4764 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00004765 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004766 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4767 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004768 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00004769 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00004770 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00004771 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004772 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
4773 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Hal Finkel59889f72013-04-07 22:11:09 +00004774 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00004775 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00004776 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00004777}
4778
Chris Lattner1f873002007-11-28 18:44:47 +00004779// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004780SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004781 DebugLoc dl) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004782 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00004783 SDValue Src = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 if (Src.getValueType() == MVT::f32)
4785 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00004786
Dan Gohman475871a2008-07-27 21:46:04 +00004787 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00004788 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004789 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004791 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Hal Finkel46479192013-04-01 17:52:07 +00004792 (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ :
4793 PPCISD::FCTIDZ),
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00004795 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004796 case MVT::i64:
Hal Finkela1646ce2013-04-01 18:42:58 +00004797 assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) &&
4798 "i64 FP_TO_UINT is supported only with FPCVT");
Hal Finkel46479192013-04-01 17:52:07 +00004799 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ :
4800 PPCISD::FCTIDUZ,
4801 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00004802 break;
4803 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00004804
Chris Lattner1a635d62006-04-14 06:01:58 +00004805 // Convert the FP value to an int value through memory.
Hal Finkel46479192013-04-01 17:52:07 +00004806 bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() &&
4807 (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT());
4808 SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64);
4809 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
4810 MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI);
Duncan Sandsa7360f02008-07-19 16:26:02 +00004811
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004812 // Emit a store to the stack slot.
Hal Finkel46479192013-04-01 17:52:07 +00004813 SDValue Chain;
4814 if (i32Stack) {
4815 MachineFunction &MF = DAG.getMachineFunction();
4816 MachineMemOperand *MMO =
4817 MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4);
4818 SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr };
4819 Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
4820 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
4821 MVT::i32, MMO);
4822 } else
4823 Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
4824 MPI, false, false, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004825
4826 // Result is a load from the stack slot. If loading 4 bytes, make sure to
4827 // add in a bias.
Hal Finkel46479192013-04-01 17:52:07 +00004828 if (Op.getValueType() == MVT::i32 && !i32Stack) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004829 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00004830 DAG.getConstant(4, FIPtr.getValueType()));
Hal Finkel46479192013-04-01 17:52:07 +00004831 MPI = MachinePointerInfo();
4832 }
4833
4834 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI,
Pete Cooperd752e0f2011-11-08 18:42:53 +00004835 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004836}
4837
Hal Finkel46479192013-04-01 17:52:07 +00004838SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004839 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004840 DebugLoc dl = Op.getDebugLoc();
Dan Gohman034f60e2008-03-11 01:59:03 +00004841 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00004843 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00004844
Hal Finkel46479192013-04-01 17:52:07 +00004845 assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) &&
4846 "UINT_TO_FP is supported only with FPCVT");
4847
4848 // If we have FCFIDS, then use it when converting to single-precision.
Hal Finkel2a401952013-04-02 03:29:51 +00004849 // Otherwise, convert to double-precision and then round.
Hal Finkel46479192013-04-01 17:52:07 +00004850 unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4851 (Op.getOpcode() == ISD::UINT_TO_FP ?
4852 PPCISD::FCFIDUS : PPCISD::FCFIDS) :
4853 (Op.getOpcode() == ISD::UINT_TO_FP ?
4854 PPCISD::FCFIDU : PPCISD::FCFID);
4855 MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ?
4856 MVT::f32 : MVT::f64;
4857
Owen Anderson825b72b2009-08-11 20:47:22 +00004858 if (Op.getOperand(0).getValueType() == MVT::i64) {
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004859 SDValue SINT = Op.getOperand(0);
4860 // When converting to single-precision, we actually need to convert
4861 // to double-precision first and then round to single-precision.
4862 // To avoid double-rounding effects during that operation, we have
4863 // to prepare the input operand. Bits that might be truncated when
4864 // converting to double-precision are replaced by a bit that won't
4865 // be lost at this stage, but is below the single-precision rounding
4866 // position.
4867 //
4868 // However, if -enable-unsafe-fp-math is in effect, accept double
4869 // rounding to avoid the extra overhead.
4870 if (Op.getValueType() == MVT::f32 &&
Hal Finkel46479192013-04-01 17:52:07 +00004871 !PPCSubTarget.hasFPCVT() &&
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004872 !DAG.getTarget().Options.UnsafeFPMath) {
4873
4874 // Twiddle input to make sure the low 11 bits are zero. (If this
4875 // is the case, we are guaranteed the value will fit into the 53 bit
4876 // mantissa of an IEEE double-precision value without rounding.)
4877 // If any of those low 11 bits were not zero originally, make sure
4878 // bit 12 (value 2048) is set instead, so that the final rounding
4879 // to single-precision gets the correct result.
4880 SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4881 SINT, DAG.getConstant(2047, MVT::i64));
4882 Round = DAG.getNode(ISD::ADD, dl, MVT::i64,
4883 Round, DAG.getConstant(2047, MVT::i64));
4884 Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT);
4885 Round = DAG.getNode(ISD::AND, dl, MVT::i64,
4886 Round, DAG.getConstant(-2048, MVT::i64));
4887
4888 // However, we cannot use that value unconditionally: if the magnitude
4889 // of the input value is small, the bit-twiddling we did above might
4890 // end up visibly changing the output. Fortunately, in that case, we
4891 // don't need to twiddle bits since the original input will convert
4892 // exactly to double-precision floating-point already. Therefore,
4893 // construct a conditional to use the original value if the top 11
4894 // bits are all sign-bit copies, and use the rounded value computed
4895 // above otherwise.
4896 SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64,
4897 SINT, DAG.getConstant(53, MVT::i32));
4898 Cond = DAG.getNode(ISD::ADD, dl, MVT::i64,
4899 Cond, DAG.getConstant(1, MVT::i64));
4900 Cond = DAG.getSetCC(dl, MVT::i32,
4901 Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT);
4902
4903 SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT);
4904 }
Hal Finkel46479192013-04-01 17:52:07 +00004905
Ulrich Weigand6c28a7e2012-10-18 13:16:11 +00004906 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT);
Hal Finkel46479192013-04-01 17:52:07 +00004907 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits);
4908
4909 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Scott Michelfdc40a02009-02-17 22:15:04 +00004910 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004911 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00004912 return FP;
4913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004914
Owen Anderson825b72b2009-08-11 20:47:22 +00004915 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Hal Finkel46479192013-04-01 17:52:07 +00004916 "Unhandled INT_TO_FP type in custom expander!");
Chris Lattner1a635d62006-04-14 06:01:58 +00004917 // Since we only generate this in 64-bit mode, we can take advantage of
4918 // 64-bit registers. In particular, sign extend the input value into the
4919 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
4920 // then lfd it and fcfid it.
Dan Gohmanc76909a2009-09-25 20:36:54 +00004921 MachineFunction &MF = DAG.getMachineFunction();
4922 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004923 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00004924
Hal Finkel8049ab12013-03-31 10:12:51 +00004925 SDValue Ld;
Hal Finkel46479192013-04-01 17:52:07 +00004926 if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) {
Hal Finkel8049ab12013-03-31 10:12:51 +00004927 int FrameIdx = FrameInfo->CreateStackObject(4, 4, false);
4928 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004929
Hal Finkel8049ab12013-03-31 10:12:51 +00004930 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx,
4931 MachinePointerInfo::getFixedStack(FrameIdx),
4932 false, false, 0);
Hal Finkel9ad0f492013-03-31 01:58:02 +00004933
Hal Finkel8049ab12013-03-31 10:12:51 +00004934 assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 &&
4935 "Expected an i32 store");
4936 MachineMemOperand *MMO =
4937 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
4938 MachineMemOperand::MOLoad, 4, 4);
4939 SDValue Ops[] = { Store, FIdx };
Hal Finkel46479192013-04-01 17:52:07 +00004940 Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ?
4941 PPCISD::LFIWZX : PPCISD::LFIWAX,
4942 dl, DAG.getVTList(MVT::f64, MVT::Other),
4943 Ops, 2, MVT::i32, MMO);
Hal Finkel8049ab12013-03-31 10:12:51 +00004944 } else {
Hal Finkel46479192013-04-01 17:52:07 +00004945 assert(PPCSubTarget.isPPC64() &&
4946 "i32->FP without LFIWAX supported only on PPC64");
4947
Hal Finkel8049ab12013-03-31 10:12:51 +00004948 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
4949 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
4950
4951 SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64,
4952 Op.getOperand(0));
4953
4954 // STD the extended value into the stack slot.
4955 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx,
4956 MachinePointerInfo::getFixedStack(FrameIdx),
4957 false, false, 0);
4958
4959 // Load the value as a double.
4960 Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx,
4961 MachinePointerInfo::getFixedStack(FrameIdx),
4962 false, false, false, 0);
4963 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004964
Chris Lattner1a635d62006-04-14 06:01:58 +00004965 // FCFID it and return it.
Hal Finkel46479192013-04-01 17:52:07 +00004966 SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld);
4967 if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT())
Owen Anderson825b72b2009-08-11 20:47:22 +00004968 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00004969 return FP;
4970}
4971
Dan Gohmand858e902010-04-17 15:26:15 +00004972SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
4973 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004974 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004975 /*
4976 The rounding mode is in bits 30:31 of FPSR, and has the following
4977 settings:
4978 00 Round to nearest
4979 01 Round to 0
4980 10 Round to +inf
4981 11 Round to -inf
4982
4983 FLT_ROUNDS, on the other hand, expects the following:
4984 -1 Undefined
4985 0 Round to 0
4986 1 Round to nearest
4987 2 Round to +inf
4988 3 Round to -inf
4989
4990 To perform the conversion, we do:
4991 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
4992 */
4993
4994 MachineFunction &MF = DAG.getMachineFunction();
Owen Andersone50ed302009-08-10 22:56:29 +00004995 EVT VT = Op.getValueType();
4996 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004997 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00004998
4999 // Save FP Control Word to register
Benjamin Kramer3853f742013-03-07 20:33:29 +00005000 EVT NodeTys[] = {
5001 MVT::f64, // return register
5002 MVT::Glue // unused in this context
5003 };
Dale Johannesen33c960f2009-02-04 20:06:27 +00005004 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00005005
5006 // Save FP register to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00005007 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005008 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00005009 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner6229d0a2010-09-21 18:41:36 +00005010 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00005011
5012 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005013 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00005014 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00005015 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005016 false, false, false, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00005017
5018 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00005019 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00005020 DAG.getNode(ISD::AND, dl, MVT::i32,
5021 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00005022 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00005023 DAG.getNode(ISD::SRL, dl, MVT::i32,
5024 DAG.getNode(ISD::AND, dl, MVT::i32,
5025 DAG.getNode(ISD::XOR, dl, MVT::i32,
5026 CWD, DAG.getConstant(3, MVT::i32)),
5027 DAG.getConstant(3, MVT::i32)),
5028 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00005029
Dan Gohman475871a2008-07-27 21:46:04 +00005030 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00005031 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00005032
Duncan Sands83ec4b62008-06-06 12:08:01 +00005033 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00005034 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00005035}
5036
Dan Gohmand858e902010-04-17 15:26:15 +00005037SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005038 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005039 unsigned BitWidth = VT.getSizeInBits();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005040 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9ed06db2008-03-07 20:36:53 +00005041 assert(Op.getNumOperands() == 3 &&
5042 VT == Op.getOperand(1).getValueType() &&
5043 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005044
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00005045 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00005046 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00005047 SDValue Lo = Op.getOperand(0);
5048 SDValue Hi = Op.getOperand(1);
5049 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005050 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005051
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005052 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005053 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005054 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
5055 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
5056 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
5057 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005058 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005059 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
5060 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5061 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00005062 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005063 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00005064}
5065
Dan Gohmand858e902010-04-17 15:26:15 +00005066SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005067 EVT VT = Op.getValueType();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005068 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005069 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00005070 assert(Op.getNumOperands() == 3 &&
5071 VT == Op.getOperand(1).getValueType() &&
5072 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005073
Dan Gohman9ed06db2008-03-07 20:36:53 +00005074 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00005075 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00005076 SDValue Lo = Op.getOperand(0);
5077 SDValue Hi = Op.getOperand(1);
5078 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005079 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005080
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005081 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005082 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005083 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5084 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5085 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5086 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005087 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005088 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
5089 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
5090 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00005091 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005092 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00005093}
5094
Dan Gohmand858e902010-04-17 15:26:15 +00005095SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005096 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005097 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005098 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00005099 assert(Op.getNumOperands() == 3 &&
5100 VT == Op.getOperand(1).getValueType() &&
5101 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005102
Dan Gohman9ed06db2008-03-07 20:36:53 +00005103 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00005104 SDValue Lo = Op.getOperand(0);
5105 SDValue Hi = Op.getOperand(1);
5106 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005107 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005108
Dale Johannesenf5d97892009-02-04 01:48:28 +00005109 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005110 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00005111 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
5112 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
5113 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
5114 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005115 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00005116 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
5117 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
5118 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00005119 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00005120 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00005121 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00005122}
5123
5124//===----------------------------------------------------------------------===//
5125// Vector related lowering.
5126//
5127
Chris Lattner4a998b92006-04-17 06:00:21 +00005128/// BuildSplatI - Build a canonical splati of Val with an element size of
5129/// SplatSize. Cast the result to VT.
Owen Andersone50ed302009-08-10 22:56:29 +00005130static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Dale Johannesened2eee62009-02-06 01:31:28 +00005131 SelectionDAG &DAG, DebugLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00005132 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00005133
Owen Andersone50ed302009-08-10 22:56:29 +00005134 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson825b72b2009-08-11 20:47:22 +00005135 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner4a998b92006-04-17 06:00:21 +00005136 };
Chris Lattner70fa4932006-12-01 01:45:39 +00005137
Owen Anderson825b72b2009-08-11 20:47:22 +00005138 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00005139
Chris Lattner70fa4932006-12-01 01:45:39 +00005140 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
5141 if (Val == -1)
5142 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00005143
Owen Andersone50ed302009-08-10 22:56:29 +00005144 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00005145
Chris Lattner4a998b92006-04-17 06:00:21 +00005146 // Build a canonical splat for this value.
Owen Anderson825b72b2009-08-11 20:47:22 +00005147 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00005148 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005149 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00005150 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
5151 &Ops[0], Ops.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005152 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00005153}
5154
Chris Lattnere7c768e2006-04-18 03:24:30 +00005155/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00005156/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00005157static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Dale Johannesened2eee62009-02-06 01:31:28 +00005158 SelectionDAG &DAG, DebugLoc dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005159 EVT DestVT = MVT::Other) {
5160 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00005161 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005162 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner6876e662006-04-17 06:58:41 +00005163}
5164
Chris Lattnere7c768e2006-04-18 03:24:30 +00005165/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
5166/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00005167static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00005168 SDValue Op2, SelectionDAG &DAG,
Owen Anderson825b72b2009-08-11 20:47:22 +00005169 DebugLoc dl, EVT DestVT = MVT::Other) {
5170 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00005171 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005172 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnere7c768e2006-04-18 03:24:30 +00005173}
5174
5175
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005176/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
5177/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005178static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Owen Andersone50ed302009-08-10 22:56:29 +00005179 EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005180 // Force LHS/RHS to be the right type.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005181 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
5182 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00005183
Nate Begeman9008ca62009-04-27 18:41:29 +00005184 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005185 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00005186 Ops[i] = i + Amt;
Owen Anderson825b72b2009-08-11 20:47:22 +00005187 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005188 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00005189}
5190
Chris Lattnerf1b47082006-04-14 05:19:18 +00005191// If this is a case we can't handle, return null and let the default
5192// expansion code take care of it. If we CAN select this case, and if it
5193// selects to a single instruction, return Op. Otherwise, if we can codegen
5194// this case more efficiently than a constant pool load, lower it to the
5195// sequence of ops that should be used.
Dan Gohmand858e902010-04-17 15:26:15 +00005196SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
5197 SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00005198 DebugLoc dl = Op.getDebugLoc();
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005199 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
5200 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00005201
Bob Wilson24e338e2009-03-02 23:24:16 +00005202 // Check if this is a splat of a constant value.
5203 APInt APSplatBits, APSplatUndef;
5204 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00005205 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00005206 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen1e608812009-11-13 01:45:18 +00005207 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilsonf2950b02009-03-03 19:26:27 +00005208 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00005209
Bob Wilsonf2950b02009-03-03 19:26:27 +00005210 unsigned SplatBits = APSplatBits.getZExtValue();
5211 unsigned SplatUndef = APSplatUndef.getZExtValue();
5212 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00005213
Bob Wilsonf2950b02009-03-03 19:26:27 +00005214 // First, handle single instruction cases.
5215
5216 // All zeros?
5217 if (SplatBits == 0) {
5218 // Canonicalize all zero vectors to be v4i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005219 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
5220 SDValue Z = DAG.getConstant(0, MVT::i32);
5221 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005222 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005223 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005224 return Op;
5225 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00005226
Bob Wilsonf2950b02009-03-03 19:26:27 +00005227 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
5228 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
5229 (32-SplatBitSize));
5230 if (SextVal >= -16 && SextVal <= 15)
5231 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005232
5233
Bob Wilsonf2950b02009-03-03 19:26:27 +00005234 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00005235
Bob Wilsonf2950b02009-03-03 19:26:27 +00005236 // If this value is in the range [-32,30] and is even, use:
Bill Schmidtabc40282013-02-20 20:41:42 +00005237 // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2)
5238 // If this value is in the range [17,31] and is odd, use:
5239 // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16)
5240 // If this value is in the range [-31,-17] and is odd, use:
5241 // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16)
5242 // Note the last two are three-instruction sequences.
5243 if (SextVal >= -32 && SextVal <= 31) {
5244 // To avoid having these optimizations undone by constant folding,
5245 // we convert to a pseudo that will be expanded later into one of
5246 // the above forms.
5247 SDValue Elt = DAG.getConstant(SextVal, MVT::i32);
Bill Schmidtb34c79e2013-02-20 15:50:31 +00005248 EVT VT = Op.getValueType();
5249 int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4);
5250 SDValue EltSize = DAG.getConstant(Size, MVT::i32);
5251 return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005252 }
5253
5254 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
5255 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
5256 // for fneg/fabs.
5257 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
5258 // Make -1 and vspltisw -1:
Owen Anderson825b72b2009-08-11 20:47:22 +00005259 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005260
5261 // Make the VSLW intrinsic, computing 0x8000_0000.
5262 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
5263 OnesV, DAG, dl);
5264
5265 // xor by OnesV to invert it.
Owen Anderson825b72b2009-08-11 20:47:22 +00005266 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005267 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005268 }
5269
5270 // Check to see if this is a wide variety of vsplti*, binop self cases.
5271 static const signed char SplatCsts[] = {
5272 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
5273 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
5274 };
5275
5276 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
5277 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
5278 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
5279 int i = SplatCsts[idx];
5280
5281 // Figure out what shift amount will be used by altivec if shifted by i in
5282 // this splat size.
5283 unsigned TypeShiftAmt = i & (SplatBitSize-1);
5284
5285 // vsplti + shl self.
Richard Smith1144af32012-08-24 23:29:28 +00005286 if (SextVal == (int)((unsigned)i << TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005288 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5289 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
5290 Intrinsic::ppc_altivec_vslw
5291 };
5292 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005293 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00005294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005295
Bob Wilsonf2950b02009-03-03 19:26:27 +00005296 // vsplti + srl self.
5297 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005298 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005299 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5300 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
5301 Intrinsic::ppc_altivec_vsrw
5302 };
5303 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005304 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00005305 }
5306
Bob Wilsonf2950b02009-03-03 19:26:27 +00005307 // vsplti + sra self.
5308 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005309 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005310 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5311 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
5312 Intrinsic::ppc_altivec_vsraw
5313 };
5314 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005315 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00005316 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005317
Bob Wilsonf2950b02009-03-03 19:26:27 +00005318 // vsplti + rol self.
5319 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
5320 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005321 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005322 static const unsigned IIDs[] = { // Intrinsic to use for each size.
5323 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
5324 Intrinsic::ppc_altivec_vrlw
5325 };
5326 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005327 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005328 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005329
Bob Wilsonf2950b02009-03-03 19:26:27 +00005330 // t = vsplti c, result = vsldoi t, t, 1
Richard Smith1144af32012-08-24 23:29:28 +00005331 if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005332 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005333 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00005334 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005335 // t = vsplti c, result = vsldoi t, t, 2
Richard Smith1144af32012-08-24 23:29:28 +00005336 if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005337 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005338 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005339 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00005340 // t = vsplti c, result = vsldoi t, t, 3
Richard Smith1144af32012-08-24 23:29:28 +00005341 if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005342 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00005343 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
5344 }
5345 }
5346
Dan Gohman475871a2008-07-27 21:46:04 +00005347 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00005348}
5349
Chris Lattner59138102006-04-17 05:28:54 +00005350/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5351/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00005352static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00005353 SDValue RHS, SelectionDAG &DAG,
Dale Johannesened2eee62009-02-06 01:31:28 +00005354 DebugLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00005355 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00005356 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00005357 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005358
Chris Lattner59138102006-04-17 05:28:54 +00005359 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00005360 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
Chris Lattner59138102006-04-17 05:28:54 +00005361 OP_VMRGHW,
5362 OP_VMRGLW,
5363 OP_VSPLTISW0,
5364 OP_VSPLTISW1,
5365 OP_VSPLTISW2,
5366 OP_VSPLTISW3,
5367 OP_VSLDOI4,
5368 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00005369 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00005370 };
Scott Michelfdc40a02009-02-17 22:15:04 +00005371
Chris Lattner59138102006-04-17 05:28:54 +00005372 if (OpNum == OP_COPY) {
5373 if (LHSID == (1*9+2)*9+3) return LHS;
5374 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5375 return RHS;
5376 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005377
Dan Gohman475871a2008-07-27 21:46:04 +00005378 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00005379 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5380 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005381
Nate Begeman9008ca62009-04-27 18:41:29 +00005382 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00005383 switch (OpNum) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005384 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner59138102006-04-17 05:28:54 +00005385 case OP_VMRGHW:
5386 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
5387 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
5388 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
5389 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
5390 break;
5391 case OP_VMRGLW:
5392 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
5393 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
5394 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
5395 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
5396 break;
5397 case OP_VSPLTISW0:
5398 for (unsigned i = 0; i != 16; ++i)
5399 ShufIdxs[i] = (i&3)+0;
5400 break;
5401 case OP_VSPLTISW1:
5402 for (unsigned i = 0; i != 16; ++i)
5403 ShufIdxs[i] = (i&3)+4;
5404 break;
5405 case OP_VSPLTISW2:
5406 for (unsigned i = 0; i != 16; ++i)
5407 ShufIdxs[i] = (i&3)+8;
5408 break;
5409 case OP_VSPLTISW3:
5410 for (unsigned i = 0; i != 16; ++i)
5411 ShufIdxs[i] = (i&3)+12;
5412 break;
5413 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00005414 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005415 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00005416 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005417 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00005418 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005419 }
Owen Andersone50ed302009-08-10 22:56:29 +00005420 EVT VT = OpLHS.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005421 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
5422 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00005423 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005424 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00005425}
5426
Chris Lattnerf1b47082006-04-14 05:19:18 +00005427/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
5428/// is a shuffle we can handle in a single instruction, return it. Otherwise,
5429/// return the code it can be lowered into. Worst case, it can always be
5430/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00005431SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005432 SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00005433 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005434 SDValue V1 = Op.getOperand(0);
5435 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005436 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00005437 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005438
Chris Lattnerf1b47082006-04-14 05:19:18 +00005439 // Cases that are handled by instructions that take permute immediates
5440 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
5441 // selected by the instruction selector.
5442 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005443 if (PPC::isSplatShuffleMask(SVOp, 1) ||
5444 PPC::isSplatShuffleMask(SVOp, 2) ||
5445 PPC::isSplatShuffleMask(SVOp, 4) ||
5446 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
5447 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
5448 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
5449 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
5450 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
5451 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
5452 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
5453 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
5454 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00005455 return Op;
5456 }
5457 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005458
Chris Lattnerf1b47082006-04-14 05:19:18 +00005459 // Altivec has a variety of "shuffle immediates" that take two vector inputs
5460 // and produce a fixed permutation. If any of these match, do not lower to
5461 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00005462 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
5463 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
5464 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
5465 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
5466 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
5467 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
5468 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
5469 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
5470 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00005471 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005472
Chris Lattner59138102006-04-17 05:28:54 +00005473 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
5474 // perfect shuffle table to emit an optimal matching sequence.
Benjamin Kramered4c8c62012-01-15 13:16:05 +00005475 ArrayRef<int> PermMask = SVOp->getMask();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005476
Chris Lattner59138102006-04-17 05:28:54 +00005477 unsigned PFIndexes[4];
5478 bool isFourElementShuffle = true;
5479 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
5480 unsigned EltNo = 8; // Start out undef.
5481 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00005482 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00005483 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00005484
Nate Begeman9008ca62009-04-27 18:41:29 +00005485 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00005486 if ((ByteSource & 3) != j) {
5487 isFourElementShuffle = false;
5488 break;
5489 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005490
Chris Lattner59138102006-04-17 05:28:54 +00005491 if (EltNo == 8) {
5492 EltNo = ByteSource/4;
5493 } else if (EltNo != ByteSource/4) {
5494 isFourElementShuffle = false;
5495 break;
5496 }
5497 }
5498 PFIndexes[i] = EltNo;
5499 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005500
5501 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00005502 // perfect shuffle vector to determine if it is cost effective to do this as
5503 // discrete instructions, or whether we should use a vperm.
5504 if (isFourElementShuffle) {
5505 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00005506 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00005507 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00005508
Chris Lattner59138102006-04-17 05:28:54 +00005509 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5510 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00005511
Chris Lattner59138102006-04-17 05:28:54 +00005512 // Determining when to avoid vperm is tricky. Many things affect the cost
5513 // of vperm, particularly how many times the perm mask needs to be computed.
5514 // For example, if the perm mask can be hoisted out of a loop or is already
5515 // used (perhaps because there are multiple permutes with the same shuffle
5516 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
5517 // the loop requires an extra register.
5518 //
5519 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00005520 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00005521 // available, if this block is within a loop, we should avoid using vperm
5522 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00005523 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00005524 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00005525 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005526
Chris Lattnerf1b47082006-04-14 05:19:18 +00005527 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
5528 // vector that will get spilled to the constant pool.
5529 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00005530
Chris Lattnerf1b47082006-04-14 05:19:18 +00005531 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
5532 // that it is in input element units, not in bytes. Convert now.
Owen Andersone50ed302009-08-10 22:56:29 +00005533 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005534 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00005535
Dan Gohman475871a2008-07-27 21:46:04 +00005536 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00005537 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
5538 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00005539
Chris Lattnerf1b47082006-04-14 05:19:18 +00005540 for (unsigned j = 0; j != BytesPerElement; ++j)
5541 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson825b72b2009-08-11 20:47:22 +00005542 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00005543 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005544
Owen Anderson825b72b2009-08-11 20:47:22 +00005545 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga87008d2009-02-25 22:49:59 +00005546 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00005547 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00005548}
5549
Chris Lattner90564f22006-04-18 17:59:36 +00005550/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
5551/// altivec comparison. If it is, return true and fill in Opc/isDot with
5552/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00005553static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00005554 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005555 unsigned IntrinsicID =
5556 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005557 CompareOpc = -1;
5558 isDot = false;
5559 switch (IntrinsicID) {
5560 default: return false;
5561 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00005562 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
5563 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
5564 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
5565 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
5566 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
5567 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
5568 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
5569 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
5570 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
5571 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
5572 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
5573 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
5574 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005575
Chris Lattner1a635d62006-04-14 06:01:58 +00005576 // Normal Comparisons.
5577 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
5578 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
5579 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
5580 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
5581 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
5582 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
5583 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
5584 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
5585 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
5586 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
5587 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
5588 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
5589 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
5590 }
Chris Lattner90564f22006-04-18 17:59:36 +00005591 return true;
5592}
5593
5594/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
5595/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00005596SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005597 SelectionDAG &DAG) const {
Chris Lattner90564f22006-04-18 17:59:36 +00005598 // If this is a lowered altivec predicate compare, CompareOpc is set to the
5599 // opcode number of the comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00005600 DebugLoc dl = Op.getDebugLoc();
Chris Lattner90564f22006-04-18 17:59:36 +00005601 int CompareOpc;
5602 bool isDot;
5603 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00005604 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00005605
Chris Lattner90564f22006-04-18 17:59:36 +00005606 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00005607 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00005608 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner149add02010-03-14 22:44:11 +00005609 Op.getOperand(1), Op.getOperand(2),
5610 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005611 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00005612 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005613
Chris Lattner1a635d62006-04-14 06:01:58 +00005614 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00005615 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005616 Op.getOperand(2), // LHS
5617 Op.getOperand(3), // RHS
Owen Anderson825b72b2009-08-11 20:47:22 +00005618 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00005619 };
Benjamin Kramer3853f742013-03-07 20:33:29 +00005620 EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesen3484c092009-02-05 22:07:54 +00005621 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005622
Chris Lattner1a635d62006-04-14 06:01:58 +00005623 // Now that we have the comparison, emit a copy from the CR to a GPR.
5624 // This is flagged to the above dot comparison.
Owen Anderson825b72b2009-08-11 20:47:22 +00005625 SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32,
5626 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00005627 CompNode.getValue(1));
5628
Chris Lattner1a635d62006-04-14 06:01:58 +00005629 // Unpack the result based on how the target uses it.
5630 unsigned BitNo; // Bit # of CR6.
5631 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005632 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00005633 default: // Can't happen, don't crash on invalid number though.
5634 case 0: // Return the value of the EQ bit of CR6.
5635 BitNo = 0; InvertBit = false;
5636 break;
5637 case 1: // Return the inverted value of the EQ bit of CR6.
5638 BitNo = 0; InvertBit = true;
5639 break;
5640 case 2: // Return the value of the LT bit of CR6.
5641 BitNo = 2; InvertBit = false;
5642 break;
5643 case 3: // Return the inverted value of the LT bit of CR6.
5644 BitNo = 2; InvertBit = true;
5645 break;
5646 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005647
Chris Lattner1a635d62006-04-14 06:01:58 +00005648 // Shift the bit into the low position.
Owen Anderson825b72b2009-08-11 20:47:22 +00005649 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
5650 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00005651 // Isolate the bit.
Owen Anderson825b72b2009-08-11 20:47:22 +00005652 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
5653 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00005654
Chris Lattner1a635d62006-04-14 06:01:58 +00005655 // If we are supposed to, toggle the bit.
5656 if (InvertBit)
Owen Anderson825b72b2009-08-11 20:47:22 +00005657 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
5658 DAG.getConstant(1, MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00005659 return Flags;
5660}
5661
Scott Michelfdc40a02009-02-17 22:15:04 +00005662SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005663 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005664 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00005665 // Create a stack slot that is 16-byte aligned.
5666 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00005667 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen08673d22010-05-03 22:59:34 +00005668 EVT PtrVT = getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005669 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005670
Chris Lattner1a635d62006-04-14 06:01:58 +00005671 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005672 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00005673 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00005674 false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00005675 // Load it out.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00005676 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005677 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00005678}
5679
Dan Gohmand858e902010-04-17 15:26:15 +00005680SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00005681 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005682 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00005683 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005684
Owen Anderson825b72b2009-08-11 20:47:22 +00005685 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
5686 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00005687
Dan Gohman475871a2008-07-27 21:46:04 +00005688 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00005689 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00005690
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005691 // Shrinkify inputs to v8i16.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005692 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
5693 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
5694 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00005695
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005696 // Low parts multiplied together, generating 32-bit results (we ignore the
5697 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00005698 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson825b72b2009-08-11 20:47:22 +00005699 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00005700
Dan Gohman475871a2008-07-27 21:46:04 +00005701 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson825b72b2009-08-11 20:47:22 +00005702 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005703 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00005704 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00005705 Neg16, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00005706 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
5707 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005708 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005709
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005711
Chris Lattnercea2aa72006-04-18 04:28:57 +00005712 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00005713 LHS, RHS, Zero, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00005714 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00005715 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005716
Chris Lattner19a81522006-04-18 03:57:35 +00005717 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00005718 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson825b72b2009-08-11 20:47:22 +00005719 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005720 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00005721
Chris Lattner19a81522006-04-18 03:57:35 +00005722 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00005723 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson825b72b2009-08-11 20:47:22 +00005724 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005725 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00005726
Chris Lattner19a81522006-04-18 03:57:35 +00005727 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00005728 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00005729 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005730 Ops[i*2 ] = 2*i+1;
5731 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00005732 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005733 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005734 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005735 llvm_unreachable("Unknown mul to lower!");
Chris Lattner72dd9bd2006-04-18 03:43:48 +00005736 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00005737}
5738
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005739/// LowerOperation - Provide custom lowering hooks for some operations.
5740///
Dan Gohmand858e902010-04-17 15:26:15 +00005741SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005742 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005743 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00005744 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00005745 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00005746 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Roman Divackyfd42ed62012-06-04 17:36:38 +00005747 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00005748 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00005749 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +00005750 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
5751 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005752 case ISD::VASTART:
Dan Gohman1e93df62010-04-17 14:41:14 +00005753 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00005754
5755 case ISD::VAARG:
Dan Gohman1e93df62010-04-17 14:41:14 +00005756 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray01119992007-04-03 13:59:52 +00005757
Jim Laskeyefc7e522006-12-04 22:04:42 +00005758 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00005759 case ISD::DYNAMIC_STACKALLOC:
5760 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00005761
Hal Finkel7ee74a62013-03-21 21:37:52 +00005762 case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG);
5763 case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG);
5764
Chris Lattner1a635d62006-04-14 06:01:58 +00005765 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00005766 case ISD::FP_TO_UINT:
5767 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00005768 Op.getDebugLoc());
Hal Finkel46479192013-04-01 17:52:07 +00005769 case ISD::UINT_TO_FP:
5770 case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005771 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005772
Chris Lattner1a635d62006-04-14 06:01:58 +00005773 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00005774 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
5775 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
5776 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005777
Chris Lattner1a635d62006-04-14 06:01:58 +00005778 // Vector-related lowering.
5779 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5780 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5781 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5782 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00005783 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005784
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00005785 // For counter-based loop handling.
5786 case ISD::INTRINSIC_W_CHAIN: return SDValue();
5787
Chris Lattner3fc027d2007-12-08 06:59:59 +00005788 // Frame & Return address.
5789 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005790 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00005791 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00005792}
5793
Duncan Sands1607f052008-12-01 11:39:25 +00005794void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
5795 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005796 SelectionDAG &DAG) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00005797 const TargetMachine &TM = getTargetMachine();
Dale Johannesen3484c092009-02-05 22:07:54 +00005798 DebugLoc dl = N->getDebugLoc();
Chris Lattner1f873002007-11-28 18:44:47 +00005799 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00005800 default:
Craig Topperbc219812012-02-07 02:50:20 +00005801 llvm_unreachable("Do not know how to custom type legalize this operation!");
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00005802 case ISD::INTRINSIC_W_CHAIN: {
5803 if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() !=
5804 Intrinsic::ppc_is_decremented_ctr_nonzero)
5805 break;
5806
5807 assert(N->getValueType(0) == MVT::i1 &&
5808 "Unexpected result type for CTR decrement intrinsic");
5809 EVT SVT = getSetCCResultType(N->getValueType(0));
5810 SDVTList VTs = DAG.getVTList(SVT, MVT::Other);
5811 SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0),
5812 N->getOperand(1));
5813
5814 Results.push_back(NewInt);
5815 Results.push_back(NewInt.getValue(1));
5816 break;
5817 }
Roman Divackybdb226e2011-06-28 15:30:42 +00005818 case ISD::VAARG: {
5819 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
5820 || TM.getSubtarget<PPCSubtarget>().isPPC64())
5821 return;
5822
5823 EVT VT = N->getValueType(0);
5824
5825 if (VT == MVT::i64) {
5826 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
5827
5828 Results.push_back(NewNode);
5829 Results.push_back(NewNode.getValue(1));
5830 }
5831 return;
5832 }
Duncan Sands1607f052008-12-01 11:39:25 +00005833 case ISD::FP_ROUND_INREG: {
Owen Anderson825b72b2009-08-11 20:47:22 +00005834 assert(N->getValueType(0) == MVT::ppcf128);
5835 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00005836 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005837 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00005838 DAG.getIntPtrConstant(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00005839 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005840 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00005841 DAG.getIntPtrConstant(1));
5842
Ulrich Weigand7d35d3f2013-03-26 10:56:22 +00005843 // Add the two halves of the long double in round-to-zero mode.
5844 SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi);
Duncan Sands1607f052008-12-01 11:39:25 +00005845
5846 // We know the low half is about to be thrown away, so just use something
5847 // convenient.
Owen Anderson825b72b2009-08-11 20:47:22 +00005848 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00005849 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00005850 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00005851 }
Duncan Sands1607f052008-12-01 11:39:25 +00005852 case ISD::FP_TO_SINT:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00005853 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00005854 return;
Chris Lattner1f873002007-11-28 18:44:47 +00005855 }
5856}
5857
5858
Chris Lattner1a635d62006-04-14 06:01:58 +00005859//===----------------------------------------------------------------------===//
5860// Other Lowering Code
5861//===----------------------------------------------------------------------===//
5862
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005863MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005864PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00005865 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00005866 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005867 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5868
5869 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5870 MachineFunction *F = BB->getParent();
5871 MachineFunction::iterator It = BB;
5872 ++It;
5873
5874 unsigned dest = MI->getOperand(0).getReg();
5875 unsigned ptrA = MI->getOperand(1).getReg();
5876 unsigned ptrB = MI->getOperand(2).getReg();
5877 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005878 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005879
5880 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5881 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5882 F->insert(It, loopMBB);
5883 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005884 exitMBB->splice(exitMBB->begin(), BB,
5885 llvm::next(MachineBasicBlock::iterator(MI)),
5886 BB->end());
5887 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005888
5889 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00005890 unsigned TmpReg = (!BinOpcode) ? incr :
5891 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00005892 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5893 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005894
5895 // thisMBB:
5896 // ...
5897 // fallthrough --> loopMBB
5898 BB->addSuccessor(loopMBB);
5899
5900 // loopMBB:
5901 // l[wd]arx dest, ptr
5902 // add r0, dest, incr
5903 // st[wd]cx. r0, ptr
5904 // bne- loopMBB
5905 // fallthrough --> exitMBB
5906 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005907 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005908 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005909 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005910 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
5911 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005912 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005913 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00005914 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00005915 BB->addSuccessor(loopMBB);
5916 BB->addSuccessor(exitMBB);
5917
5918 // exitMBB:
5919 // ...
5920 BB = exitMBB;
5921 return BB;
5922}
5923
5924MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00005925PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00005926 MachineBasicBlock *BB,
5927 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00005928 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00005929 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00005930 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5931 // In 64 bit mode we have to use 64 bits for addresses, even though the
5932 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
5933 // registers without caring whether they're 32 or 64, but here we're
5934 // doing actual arithmetic on the addresses.
5935 bool is64bit = PPCSubTarget.isPPC64();
Hal Finkel76973702013-03-21 23:45:03 +00005936 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesen97efa362008-08-28 17:53:09 +00005937
5938 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5939 MachineFunction *F = BB->getParent();
5940 MachineFunction::iterator It = BB;
5941 ++It;
5942
5943 unsigned dest = MI->getOperand(0).getReg();
5944 unsigned ptrA = MI->getOperand(1).getReg();
5945 unsigned ptrB = MI->getOperand(2).getReg();
5946 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005947 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00005948
5949 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
5950 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5951 F->insert(It, loopMBB);
5952 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005953 exitMBB->splice(exitMBB->begin(), BB,
5954 llvm::next(MachineBasicBlock::iterator(MI)),
5955 BB->end());
5956 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen97efa362008-08-28 17:53:09 +00005957
5958 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005959 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00005960 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5961 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00005962 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
5963 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
5964 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
5965 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
5966 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
5967 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
5968 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
5969 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
5970 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
5971 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00005972 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00005973 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00005974 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00005975
5976 // thisMBB:
5977 // ...
5978 // fallthrough --> loopMBB
5979 BB->addSuccessor(loopMBB);
5980
5981 // The 4-byte load must be aligned, while a char or short may be
5982 // anywhere in the word. Hence all this nasty bookkeeping code.
5983 // add ptr1, ptrA, ptrB [copy if ptrA==0]
5984 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00005985 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00005986 // rlwinm ptr, ptr1, 0, 0, 29
5987 // slw incr2, incr, shift
5988 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
5989 // slw mask, mask2, shift
5990 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005991 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00005992 // add tmp, tmpDest, incr2
5993 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00005994 // and tmp3, tmp, mask
5995 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005996 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00005997 // bne- loopMBB
5998 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00005999 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006000 if (ptrA != ZeroReg) {
Dale Johannesen97efa362008-08-28 17:53:09 +00006001 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006002 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006003 .addReg(ptrA).addReg(ptrB);
6004 } else {
6005 Ptr1Reg = ptrB;
6006 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006007 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006008 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006009 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006010 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6011 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006012 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006013 .addReg(Ptr1Reg).addImm(0).addImm(61);
6014 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00006015 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006016 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006017 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006018 .addReg(incr).addReg(ShiftReg);
6019 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006020 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00006021 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00006022 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6023 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00006024 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006025 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006026 .addReg(Mask2Reg).addReg(ShiftReg);
6027
6028 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006029 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006030 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00006031 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006032 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00006033 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006034 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00006035 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006036 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006037 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006038 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00006039 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Bill Schmidtdebf7d32013-04-02 18:37:08 +00006040 BuildMI(BB, dl, TII->get(PPC::STWCX))
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006041 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006042 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00006043 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00006044 BB->addSuccessor(loopMBB);
6045 BB->addSuccessor(exitMBB);
6046
6047 // exitMBB:
6048 // ...
6049 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00006050 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
6051 .addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00006052 return BB;
6053}
6054
Hal Finkel7ee74a62013-03-21 21:37:52 +00006055llvm::MachineBasicBlock*
6056PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
6057 MachineBasicBlock *MBB) const {
6058 DebugLoc DL = MI->getDebugLoc();
6059 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6060
6061 MachineFunction *MF = MBB->getParent();
6062 MachineRegisterInfo &MRI = MF->getRegInfo();
6063
6064 const BasicBlock *BB = MBB->getBasicBlock();
6065 MachineFunction::iterator I = MBB;
6066 ++I;
6067
6068 // Memory Reference
6069 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6070 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6071
6072 unsigned DstReg = MI->getOperand(0).getReg();
6073 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
6074 assert(RC->hasType(MVT::i32) && "Invalid destination!");
6075 unsigned mainDstReg = MRI.createVirtualRegister(RC);
6076 unsigned restoreDstReg = MRI.createVirtualRegister(RC);
6077
6078 MVT PVT = getPointerTy();
6079 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6080 "Invalid Pointer Size!");
6081 // For v = setjmp(buf), we generate
6082 //
6083 // thisMBB:
6084 // SjLjSetup mainMBB
6085 // bl mainMBB
6086 // v_restore = 1
6087 // b sinkMBB
6088 //
6089 // mainMBB:
6090 // buf[LabelOffset] = LR
6091 // v_main = 0
6092 //
6093 // sinkMBB:
6094 // v = phi(main, restore)
6095 //
6096
6097 MachineBasicBlock *thisMBB = MBB;
6098 MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
6099 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
6100 MF->insert(I, mainMBB);
6101 MF->insert(I, sinkMBB);
6102
6103 MachineInstrBuilder MIB;
6104
6105 // Transfer the remainder of BB and its successor edges to sinkMBB.
6106 sinkMBB->splice(sinkMBB->begin(), MBB,
6107 llvm::next(MachineBasicBlock::iterator(MI)), MBB->end());
6108 sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
6109
6110 // Note that the structure of the jmp_buf used here is not compatible
6111 // with that used by libc, and is not designed to be. Specifically, it
6112 // stores only those 'reserved' registers that LLVM does not otherwise
6113 // understand how to spill. Also, by convention, by the time this
6114 // intrinsic is called, Clang has already stored the frame address in the
6115 // first slot of the buffer and stack address in the third. Following the
6116 // X86 target code, we'll store the jump address in the second slot. We also
6117 // need to save the TOC pointer (R2) to handle jumps between shared
6118 // libraries, and that will be stored in the fourth slot. The thread
6119 // identifier (R13) is not affected.
6120
6121 // thisMBB:
6122 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6123 const int64_t TOCOffset = 3 * PVT.getStoreSize();
6124
6125 // Prepare IP either in reg.
6126 const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
6127 unsigned LabelReg = MRI.createVirtualRegister(PtrRC);
6128 unsigned BufReg = MI->getOperand(1).getReg();
6129
6130 if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) {
6131 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD))
6132 .addReg(PPC::X2)
6133 .addImm(TOCOffset / 4)
6134 .addReg(BufReg);
6135
6136 MIB.setMemRefs(MMOBegin, MMOEnd);
6137 }
6138
6139 // Setup
Hal Finkelcaeeb182013-04-04 22:55:54 +00006140 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB);
Hal Finkel7ee74a62013-03-21 21:37:52 +00006141 MIB.addRegMask(PPCRegInfo->getNoPreservedMask());
6142
6143 BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1);
6144
6145 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup))
6146 .addMBB(mainMBB);
6147 MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB);
6148
6149 thisMBB->addSuccessor(mainMBB, /* weight */ 0);
6150 thisMBB->addSuccessor(sinkMBB, /* weight */ 1);
6151
6152 // mainMBB:
6153 // mainDstReg = 0
6154 MIB = BuildMI(mainMBB, DL,
6155 TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg);
6156
6157 // Store IP
6158 if (PPCSubTarget.isPPC64()) {
6159 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD))
6160 .addReg(LabelReg)
6161 .addImm(LabelOffset / 4)
6162 .addReg(BufReg);
6163 } else {
6164 MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW))
6165 .addReg(LabelReg)
6166 .addImm(LabelOffset)
6167 .addReg(BufReg);
6168 }
6169
6170 MIB.setMemRefs(MMOBegin, MMOEnd);
6171
6172 BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0);
6173 mainMBB->addSuccessor(sinkMBB);
6174
6175 // sinkMBB:
6176 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
6177 TII->get(PPC::PHI), DstReg)
6178 .addReg(mainDstReg).addMBB(mainMBB)
6179 .addReg(restoreDstReg).addMBB(thisMBB);
6180
6181 MI->eraseFromParent();
6182 return sinkMBB;
6183}
6184
6185MachineBasicBlock *
6186PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
6187 MachineBasicBlock *MBB) const {
6188 DebugLoc DL = MI->getDebugLoc();
6189 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6190
6191 MachineFunction *MF = MBB->getParent();
6192 MachineRegisterInfo &MRI = MF->getRegInfo();
6193
6194 // Memory Reference
6195 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
6196 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
6197
6198 MVT PVT = getPointerTy();
6199 assert((PVT == MVT::i64 || PVT == MVT::i32) &&
6200 "Invalid Pointer Size!");
6201
6202 const TargetRegisterClass *RC =
6203 (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
6204 unsigned Tmp = MRI.createVirtualRegister(RC);
6205 // Since FP is only updated here but NOT referenced, it's treated as GPR.
6206 unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31;
6207 unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1;
6208
6209 MachineInstrBuilder MIB;
6210
6211 const int64_t LabelOffset = 1 * PVT.getStoreSize();
6212 const int64_t SPOffset = 2 * PVT.getStoreSize();
6213 const int64_t TOCOffset = 3 * PVT.getStoreSize();
6214
6215 unsigned BufReg = MI->getOperand(0).getReg();
6216
6217 // Reload FP (the jumped-to function may not have had a
6218 // frame pointer, and if so, then its r31 will be restored
6219 // as necessary).
6220 if (PVT == MVT::i64) {
6221 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP)
6222 .addImm(0)
6223 .addReg(BufReg);
6224 } else {
6225 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP)
6226 .addImm(0)
6227 .addReg(BufReg);
6228 }
6229 MIB.setMemRefs(MMOBegin, MMOEnd);
6230
6231 // Reload IP
6232 if (PVT == MVT::i64) {
6233 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp)
6234 .addImm(LabelOffset / 4)
6235 .addReg(BufReg);
6236 } else {
6237 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp)
6238 .addImm(LabelOffset)
6239 .addReg(BufReg);
6240 }
6241 MIB.setMemRefs(MMOBegin, MMOEnd);
6242
6243 // Reload SP
6244 if (PVT == MVT::i64) {
6245 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP)
6246 .addImm(SPOffset / 4)
6247 .addReg(BufReg);
6248 } else {
6249 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP)
6250 .addImm(SPOffset)
6251 .addReg(BufReg);
6252 }
6253 MIB.setMemRefs(MMOBegin, MMOEnd);
6254
6255 // FIXME: When we also support base pointers, that register must also be
6256 // restored here.
6257
6258 // Reload TOC
6259 if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) {
6260 MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2)
6261 .addImm(TOCOffset / 4)
6262 .addReg(BufReg);
6263
6264 MIB.setMemRefs(MMOBegin, MMOEnd);
6265 }
6266
6267 // Jump
6268 BuildMI(*MBB, MI, DL,
6269 TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp);
6270 BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR));
6271
6272 MI->eraseFromParent();
6273 return MBB;
6274}
6275
Dale Johannesen97efa362008-08-28 17:53:09 +00006276MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006277PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00006278 MachineBasicBlock *BB) const {
Hal Finkel7ee74a62013-03-21 21:37:52 +00006279 if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 ||
6280 MI->getOpcode() == PPC::EH_SjLj_SetJmp64) {
6281 return emitEHSjLjSetJmp(MI, BB);
6282 } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 ||
6283 MI->getOpcode() == PPC::EH_SjLj_LongJmp64) {
6284 return emitEHSjLjLongJmp(MI, BB);
6285 }
6286
Evan Chengc0f64ff2006-11-27 23:37:22 +00006287 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00006288
6289 // To "insert" these instructions we actually have to insert their
6290 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006291 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006292 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006293 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00006294
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006295 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00006296
Hal Finkel009f7af2012-06-22 23:10:08 +00006297 if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6298 MI->getOpcode() == PPC::SELECT_CC_I8)) {
Hal Finkelff56d1a2013-04-05 23:29:01 +00006299 SmallVector<MachineOperand, 2> Cond;
6300 Cond.push_back(MI->getOperand(4));
6301 Cond.push_back(MI->getOperand(1));
6302
Hal Finkel009f7af2012-06-22 23:10:08 +00006303 DebugLoc dl = MI->getDebugLoc();
Hal Finkelff56d1a2013-04-05 23:29:01 +00006304 PPCII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(), Cond,
6305 MI->getOperand(2).getReg(), MI->getOperand(3).getReg());
Hal Finkel009f7af2012-06-22 23:10:08 +00006306 } else if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
6307 MI->getOpcode() == PPC::SELECT_CC_I8 ||
6308 MI->getOpcode() == PPC::SELECT_CC_F4 ||
6309 MI->getOpcode() == PPC::SELECT_CC_F8 ||
6310 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
6311
Evan Cheng53301922008-07-12 02:23:19 +00006312
6313 // The incoming instruction knows the destination vreg to set, the
6314 // condition code register to branch on, the true/false values to
6315 // select between, and a branch opcode to use.
6316
6317 // thisMBB:
6318 // ...
6319 // TrueVal = ...
6320 // cmpTY ccX, r1, r2
6321 // bCC copy1MBB
6322 // fallthrough --> copy0MBB
6323 MachineBasicBlock *thisMBB = BB;
6324 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6325 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
6326 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006327 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00006328 F->insert(It, copy0MBB);
6329 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006330
6331 // Transfer the remainder of BB and its successor edges to sinkMBB.
6332 sinkMBB->splice(sinkMBB->begin(), BB,
6333 llvm::next(MachineBasicBlock::iterator(MI)),
6334 BB->end());
6335 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
6336
Evan Cheng53301922008-07-12 02:23:19 +00006337 // Next, add the true and fallthrough blocks as its successors.
6338 BB->addSuccessor(copy0MBB);
6339 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006340
Dan Gohman14152b42010-07-06 20:24:04 +00006341 BuildMI(BB, dl, TII->get(PPC::BCC))
6342 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
6343
Evan Cheng53301922008-07-12 02:23:19 +00006344 // copy0MBB:
6345 // %FalseValue = ...
6346 // # fallthrough to sinkMBB
6347 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00006348
Evan Cheng53301922008-07-12 02:23:19 +00006349 // Update machine-CFG edges
6350 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006351
Evan Cheng53301922008-07-12 02:23:19 +00006352 // sinkMBB:
6353 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6354 // ...
6355 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00006356 BuildMI(*BB, BB->begin(), dl,
6357 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00006358 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
6359 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6360 }
Dale Johannesen97efa362008-08-28 17:53:09 +00006361 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
6362 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
6363 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
6364 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006365 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
6366 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
6367 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
6368 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006369
6370 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
6371 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
6372 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
6373 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006374 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
6375 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
6376 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
6377 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006378
6379 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
6380 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
6381 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
6382 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006383 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
6384 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
6385 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
6386 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006387
6388 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
6389 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
6390 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
6391 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006392 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
6393 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
6394 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
6395 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006396
6397 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00006398 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00006399 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00006400 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006401 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00006402 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006403 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00006404 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006405
6406 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
6407 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
6408 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
6409 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00006410 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
6411 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
6412 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
6413 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00006414
Dale Johannesen0e55f062008-08-29 18:29:46 +00006415 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
6416 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
6417 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
6418 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
6419 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
6420 BB = EmitAtomicBinary(MI, BB, false, 0);
6421 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
6422 BB = EmitAtomicBinary(MI, BB, true, 0);
6423
Evan Cheng53301922008-07-12 02:23:19 +00006424 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
6425 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
6426 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
6427
6428 unsigned dest = MI->getOperand(0).getReg();
6429 unsigned ptrA = MI->getOperand(1).getReg();
6430 unsigned ptrB = MI->getOperand(2).getReg();
6431 unsigned oldval = MI->getOperand(3).getReg();
6432 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006433 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00006434
Dale Johannesen65e39732008-08-25 18:53:26 +00006435 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6436 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6437 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00006438 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00006439 F->insert(It, loop1MBB);
6440 F->insert(It, loop2MBB);
6441 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00006442 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006443 exitMBB->splice(exitMBB->begin(), BB,
6444 llvm::next(MachineBasicBlock::iterator(MI)),
6445 BB->end());
6446 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng53301922008-07-12 02:23:19 +00006447
6448 // thisMBB:
6449 // ...
6450 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00006451 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00006452
Dale Johannesen65e39732008-08-25 18:53:26 +00006453 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00006454 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00006455 // cmp[wd] dest, oldval
6456 // bne- midMBB
6457 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00006458 // st[wd]cx. newval, ptr
6459 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00006460 // b exitBB
6461 // midMBB:
6462 // st[wd]cx. dest, ptr
6463 // exitBB:
6464 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006465 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00006466 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006467 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00006468 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006469 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00006470 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6471 BB->addSuccessor(loop2MBB);
6472 BB->addSuccessor(midMBB);
6473
6474 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006475 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00006476 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006477 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00006478 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006479 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00006480 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00006481 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006482
Dale Johannesen65e39732008-08-25 18:53:26 +00006483 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006484 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00006485 .addReg(dest).addReg(ptrA).addReg(ptrB);
6486 BB->addSuccessor(exitMBB);
6487
Evan Cheng53301922008-07-12 02:23:19 +00006488 // exitMBB:
6489 // ...
6490 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006491 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
6492 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
6493 // We must use 64-bit registers for addresses when targeting 64-bit,
6494 // since we're actually doing arithmetic on them. Other registers
6495 // can be 32-bit.
6496 bool is64bit = PPCSubTarget.isPPC64();
6497 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
6498
6499 unsigned dest = MI->getOperand(0).getReg();
6500 unsigned ptrA = MI->getOperand(1).getReg();
6501 unsigned ptrB = MI->getOperand(2).getReg();
6502 unsigned oldval = MI->getOperand(3).getReg();
6503 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00006504 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006505
6506 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
6507 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
6508 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6509 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6510 F->insert(It, loop1MBB);
6511 F->insert(It, loop2MBB);
6512 F->insert(It, midMBB);
6513 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00006514 exitMBB->splice(exitMBB->begin(), BB,
6515 llvm::next(MachineBasicBlock::iterator(MI)),
6516 BB->end());
6517 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006518
6519 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00006520 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00006521 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
6522 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006523 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
6524 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
6525 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
6526 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
6527 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
6528 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
6529 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
6530 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
6531 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
6532 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
6533 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
6534 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
6535 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
6536 unsigned Ptr1Reg;
6537 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Hal Finkel76973702013-03-21 23:45:03 +00006538 unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006539 // thisMBB:
6540 // ...
6541 // fallthrough --> loopMBB
6542 BB->addSuccessor(loop1MBB);
6543
6544 // The 4-byte load must be aligned, while a char or short may be
6545 // anywhere in the word. Hence all this nasty bookkeeping code.
6546 // add ptr1, ptrA, ptrB [copy if ptrA==0]
6547 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00006548 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006549 // rlwinm ptr, ptr1, 0, 0, 29
6550 // slw newval2, newval, shift
6551 // slw oldval2, oldval,shift
6552 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
6553 // slw mask, mask2, shift
6554 // and newval3, newval2, mask
6555 // and oldval3, oldval2, mask
6556 // loop1MBB:
6557 // lwarx tmpDest, ptr
6558 // and tmp, tmpDest, mask
6559 // cmpw tmp, oldval3
6560 // bne- midMBB
6561 // loop2MBB:
6562 // andc tmp2, tmpDest, mask
6563 // or tmp4, tmp2, newval3
6564 // stwcx. tmp4, ptr
6565 // bne- loop1MBB
6566 // b exitBB
6567 // midMBB:
6568 // stwcx. tmpDest, ptr
6569 // exitBB:
6570 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006571 if (ptrA != ZeroReg) {
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006572 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006573 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006574 .addReg(ptrA).addReg(ptrB);
6575 } else {
6576 Ptr1Reg = ptrB;
6577 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006578 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006579 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006580 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006581 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
6582 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006583 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006584 .addReg(Ptr1Reg).addImm(0).addImm(61);
6585 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00006586 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006587 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006588 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006589 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006590 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006591 .addReg(oldval).addReg(ShiftReg);
6592 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00006593 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006594 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00006595 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
6596 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
6597 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006598 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00006599 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006600 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006601 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006602 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006603 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006604 .addReg(OldVal2Reg).addReg(MaskReg);
6605
6606 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006607 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006608 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006609 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
6610 .addReg(TmpDestReg).addReg(MaskReg);
6611 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006612 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006613 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006614 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
6615 BB->addSuccessor(loop2MBB);
6616 BB->addSuccessor(midMBB);
6617
6618 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006619 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
6620 .addReg(TmpDestReg).addReg(MaskReg);
6621 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
6622 .addReg(Tmp2Reg).addReg(NewVal3Reg);
6623 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006624 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006625 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006626 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00006627 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006628 BB->addSuccessor(loop1MBB);
6629 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006630
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006631 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00006632 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00006633 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006634 BB->addSuccessor(exitMBB);
6635
6636 // exitMBB:
6637 // ...
6638 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00006639 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
6640 .addReg(ShiftReg);
Ulrich Weigand7d35d3f2013-03-26 10:56:22 +00006641 } else if (MI->getOpcode() == PPC::FADDrtz) {
6642 // This pseudo performs an FADD with rounding mode temporarily forced
6643 // to round-to-zero. We emit this via custom inserter since the FPSCR
6644 // is not modeled at the SelectionDAG level.
6645 unsigned Dest = MI->getOperand(0).getReg();
6646 unsigned Src1 = MI->getOperand(1).getReg();
6647 unsigned Src2 = MI->getOperand(2).getReg();
6648 DebugLoc dl = MI->getDebugLoc();
6649
6650 MachineRegisterInfo &RegInfo = F->getRegInfo();
6651 unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass);
6652
6653 // Save FPSCR value.
6654 BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg);
6655
6656 // Set rounding mode to round-to-zero.
6657 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31);
6658 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30);
6659
6660 // Perform addition.
6661 BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2);
6662
6663 // Restore FPSCR value.
6664 BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg);
Hal Finkel0882fd62013-03-29 19:41:55 +00006665 } else if (MI->getOpcode() == PPC::FRINDrint ||
6666 MI->getOpcode() == PPC::FRINSrint) {
6667 bool isf32 = MI->getOpcode() == PPC::FRINSrint;
6668 unsigned Dest = MI->getOperand(0).getReg();
6669 unsigned Src = MI->getOperand(1).getReg();
6670 DebugLoc dl = MI->getDebugLoc();
6671
6672 MachineRegisterInfo &RegInfo = F->getRegInfo();
6673 unsigned CRReg = RegInfo.createVirtualRegister(&PPC::CRRCRegClass);
6674
6675 // Perform the rounding.
6676 BuildMI(*BB, MI, dl, TII->get(isf32 ? PPC::FRINS : PPC::FRIND), Dest)
6677 .addReg(Src);
6678
6679 // Compare the results.
6680 BuildMI(*BB, MI, dl, TII->get(isf32 ? PPC::FCMPUS : PPC::FCMPUD), CRReg)
6681 .addReg(Dest).addReg(Src);
6682
6683 // If the results were not equal, then set the FPSCR XX bit.
6684 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
6685 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
6686 F->insert(It, midMBB);
6687 F->insert(It, exitMBB);
6688 exitMBB->splice(exitMBB->begin(), BB,
6689 llvm::next(MachineBasicBlock::iterator(MI)),
6690 BB->end());
6691 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
6692
6693 BuildMI(*BB, MI, dl, TII->get(PPC::BCC))
6694 .addImm(PPC::PRED_EQ).addReg(CRReg).addMBB(exitMBB);
6695
6696 BB->addSuccessor(midMBB);
6697 BB->addSuccessor(exitMBB);
6698
6699 BB = midMBB;
6700
6701 // Set the FPSCR XX bit (FE_INEXACT). Note that we cannot just set
6702 // the FI bit here because that will not automatically set XX also,
6703 // and XX is what libm interprets as the FE_INEXACT flag.
6704 BuildMI(BB, dl, TII->get(PPC::MTFSB1)).addImm(/* 38 - 32 = */ 6);
6705 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
6706
6707 BB->addSuccessor(exitMBB);
6708
6709 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00006710 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00006711 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng53301922008-07-12 02:23:19 +00006712 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006713
Dan Gohman14152b42010-07-06 20:24:04 +00006714 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00006715 return BB;
6716}
6717
Chris Lattner1a635d62006-04-14 06:01:58 +00006718//===----------------------------------------------------------------------===//
6719// Target Optimization Hooks
6720//===----------------------------------------------------------------------===//
6721
Hal Finkel63c32a72013-04-03 17:44:56 +00006722SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op,
6723 DAGCombinerInfo &DCI) const {
Hal Finkel827307b2013-04-03 04:01:11 +00006724 if (DCI.isAfterLegalizeVectorOps())
6725 return SDValue();
6726
Hal Finkel63c32a72013-04-03 17:44:56 +00006727 EVT VT = Op.getValueType();
6728
6729 if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) ||
6730 (VT == MVT::f64 && PPCSubTarget.hasFRE()) ||
6731 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel827307b2013-04-03 04:01:11 +00006732
6733 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6734 // For the reciprocal, we need to find the zero of the function:
6735 // F(X) = A X - 1 [which has a zero at X = 1/A]
6736 // =>
6737 // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form
6738 // does not require additional intermediate precision]
6739
6740 // Convergence is quadratic, so we essentially double the number of digits
6741 // correct after every iteration. The minimum architected relative
6742 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6743 // 23 digits and double has 52 digits.
6744 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkel63c32a72013-04-03 17:44:56 +00006745 if (VT.getScalarType() == MVT::f64)
Hal Finkel827307b2013-04-03 04:01:11 +00006746 ++Iterations;
6747
6748 SelectionDAG &DAG = DCI.DAG;
Hal Finkel63c32a72013-04-03 17:44:56 +00006749 DebugLoc dl = Op.getDebugLoc();
Hal Finkel827307b2013-04-03 04:01:11 +00006750
6751 SDValue FPOne =
Hal Finkel63c32a72013-04-03 17:44:56 +00006752 DAG.getConstantFP(1.0, VT.getScalarType());
6753 if (VT.isVector()) {
6754 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel827307b2013-04-03 04:01:11 +00006755 "Unknown vector type");
Hal Finkel63c32a72013-04-03 17:44:56 +00006756 FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
Hal Finkel827307b2013-04-03 04:01:11 +00006757 FPOne, FPOne, FPOne, FPOne);
6758 }
6759
Hal Finkel63c32a72013-04-03 17:44:56 +00006760 SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006761 DCI.AddToWorklist(Est.getNode());
6762
6763 // Newton iterations: Est = Est + Est (1 - Arg * Est)
6764 for (int i = 0; i < Iterations; ++i) {
Hal Finkel63c32a72013-04-03 17:44:56 +00006765 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est);
Hal Finkel827307b2013-04-03 04:01:11 +00006766 DCI.AddToWorklist(NewEst.getNode());
6767
Hal Finkel63c32a72013-04-03 17:44:56 +00006768 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006769 DCI.AddToWorklist(NewEst.getNode());
6770
Hal Finkel63c32a72013-04-03 17:44:56 +00006771 NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006772 DCI.AddToWorklist(NewEst.getNode());
6773
Hal Finkel63c32a72013-04-03 17:44:56 +00006774 Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006775 DCI.AddToWorklist(Est.getNode());
6776 }
6777
6778 return Est;
6779 }
6780
6781 return SDValue();
6782}
6783
Hal Finkel63c32a72013-04-03 17:44:56 +00006784SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op,
Hal Finkel827307b2013-04-03 04:01:11 +00006785 DAGCombinerInfo &DCI) const {
6786 if (DCI.isAfterLegalizeVectorOps())
6787 return SDValue();
6788
Hal Finkel63c32a72013-04-03 17:44:56 +00006789 EVT VT = Op.getValueType();
6790
6791 if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) ||
6792 (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) ||
6793 (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) {
Hal Finkel827307b2013-04-03 04:01:11 +00006794
6795 // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i)
6796 // For the reciprocal sqrt, we need to find the zero of the function:
6797 // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)]
6798 // =>
6799 // X_{i+1} = X_i (1.5 - A X_i^2 / 2)
6800 // As a result, we precompute A/2 prior to the iteration loop.
6801
6802 // Convergence is quadratic, so we essentially double the number of digits
6803 // correct after every iteration. The minimum architected relative
6804 // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has
6805 // 23 digits and double has 52 digits.
6806 int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3;
Hal Finkel63c32a72013-04-03 17:44:56 +00006807 if (VT.getScalarType() == MVT::f64)
Hal Finkel827307b2013-04-03 04:01:11 +00006808 ++Iterations;
6809
6810 SelectionDAG &DAG = DCI.DAG;
Hal Finkel63c32a72013-04-03 17:44:56 +00006811 DebugLoc dl = Op.getDebugLoc();
Hal Finkel827307b2013-04-03 04:01:11 +00006812
Hal Finkel63c32a72013-04-03 17:44:56 +00006813 SDValue FPThreeHalves =
6814 DAG.getConstantFP(1.5, VT.getScalarType());
6815 if (VT.isVector()) {
6816 assert(VT.getVectorNumElements() == 4 &&
Hal Finkel827307b2013-04-03 04:01:11 +00006817 "Unknown vector type");
Hal Finkel63c32a72013-04-03 17:44:56 +00006818 FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
6819 FPThreeHalves, FPThreeHalves,
6820 FPThreeHalves, FPThreeHalves);
Hal Finkel827307b2013-04-03 04:01:11 +00006821 }
6822
Hal Finkel63c32a72013-04-03 17:44:56 +00006823 SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006824 DCI.AddToWorklist(Est.getNode());
6825
6826 // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that
6827 // this entire sequence requires only one FP constant.
Hal Finkel63c32a72013-04-03 17:44:56 +00006828 SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006829 DCI.AddToWorklist(HalfArg.getNode());
6830
Hal Finkel63c32a72013-04-03 17:44:56 +00006831 HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op);
Hal Finkel827307b2013-04-03 04:01:11 +00006832 DCI.AddToWorklist(HalfArg.getNode());
6833
6834 // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est)
6835 for (int i = 0; i < Iterations; ++i) {
Hal Finkel63c32a72013-04-03 17:44:56 +00006836 SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est);
Hal Finkel827307b2013-04-03 04:01:11 +00006837 DCI.AddToWorklist(NewEst.getNode());
6838
Hal Finkel63c32a72013-04-03 17:44:56 +00006839 NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006840 DCI.AddToWorklist(NewEst.getNode());
6841
Hal Finkel63c32a72013-04-03 17:44:56 +00006842 NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006843 DCI.AddToWorklist(NewEst.getNode());
6844
Hal Finkel63c32a72013-04-03 17:44:56 +00006845 Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst);
Hal Finkel827307b2013-04-03 04:01:11 +00006846 DCI.AddToWorklist(Est.getNode());
6847 }
6848
6849 return Est;
6850 }
6851
6852 return SDValue();
6853}
6854
Duncan Sands25cf2272008-11-24 14:53:14 +00006855SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
6856 DAGCombinerInfo &DCI) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +00006857 const TargetMachine &TM = getTargetMachine();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006858 SelectionDAG &DAG = DCI.DAG;
Dale Johannesen3484c092009-02-05 22:07:54 +00006859 DebugLoc dl = N->getDebugLoc();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006860 switch (N->getOpcode()) {
6861 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006862 case PPCISD::SHL:
6863 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006864 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006865 return N->getOperand(0);
6866 }
6867 break;
6868 case PPCISD::SRL:
6869 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006870 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006871 return N->getOperand(0);
6872 }
6873 break;
6874 case PPCISD::SRA:
6875 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00006876 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00006877 C->isAllOnesValue()) // -1 >>s V -> -1.
6878 return N->getOperand(0);
6879 }
6880 break;
Hal Finkel827307b2013-04-03 04:01:11 +00006881 case ISD::FDIV: {
6882 assert(TM.Options.UnsafeFPMath &&
6883 "Reciprocal estimates require UnsafeFPMath");
Scott Michelfdc40a02009-02-17 22:15:04 +00006884
Hal Finkel827307b2013-04-03 04:01:11 +00006885 if (N->getOperand(1).getOpcode() == ISD::FSQRT) {
Hal Finkel63c32a72013-04-03 17:44:56 +00006886 SDValue RV =
6887 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00006888 if (RV.getNode() != 0) {
6889 DCI.AddToWorklist(RV.getNode());
6890 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6891 N->getOperand(0), RV);
6892 }
Hal Finkel7530a9f2013-04-04 22:44:12 +00006893 } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND &&
6894 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
6895 SDValue RV =
6896 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
6897 DCI);
6898 if (RV.getNode() != 0) {
6899 DCI.AddToWorklist(RV.getNode());
6900 RV = DAG.getNode(ISD::FP_EXTEND, N->getOperand(1).getDebugLoc(),
6901 N->getValueType(0), RV);
6902 DCI.AddToWorklist(RV.getNode());
6903 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6904 N->getOperand(0), RV);
6905 }
6906 } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND &&
6907 N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) {
6908 SDValue RV =
6909 DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0),
6910 DCI);
6911 if (RV.getNode() != 0) {
6912 DCI.AddToWorklist(RV.getNode());
6913 RV = DAG.getNode(ISD::FP_ROUND, N->getOperand(1).getDebugLoc(),
6914 N->getValueType(0), RV,
6915 N->getOperand(1).getOperand(1));
6916 DCI.AddToWorklist(RV.getNode());
6917 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6918 N->getOperand(0), RV);
6919 }
Hal Finkel827307b2013-04-03 04:01:11 +00006920 }
6921
Hal Finkel63c32a72013-04-03 17:44:56 +00006922 SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00006923 if (RV.getNode() != 0) {
6924 DCI.AddToWorklist(RV.getNode());
6925 return DAG.getNode(ISD::FMUL, dl, N->getValueType(0),
6926 N->getOperand(0), RV);
6927 }
6928
6929 }
6930 break;
6931 case ISD::FSQRT: {
6932 assert(TM.Options.UnsafeFPMath &&
6933 "Reciprocal estimates require UnsafeFPMath");
6934
6935 // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the
6936 // reciprocal sqrt.
Hal Finkel63c32a72013-04-03 17:44:56 +00006937 SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00006938 if (RV.getNode() != 0) {
6939 DCI.AddToWorklist(RV.getNode());
Hal Finkel63c32a72013-04-03 17:44:56 +00006940 RV = DAGCombineFastRecip(RV, DCI);
Hal Finkel827307b2013-04-03 04:01:11 +00006941 if (RV.getNode() != 0)
6942 return RV;
6943 }
6944
6945 }
6946 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006947 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00006948 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006949 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
6950 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
6951 // We allow the src/dst to be either f32/f64, but the intermediate
6952 // type must be i64.
Owen Anderson825b72b2009-08-11 20:47:22 +00006953 if (N->getOperand(0).getValueType() == MVT::i64 &&
6954 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006955 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006956 if (Val.getValueType() == MVT::f32) {
6957 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006958 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006959 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006960
Owen Anderson825b72b2009-08-11 20:47:22 +00006961 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006962 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00006963 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006964 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00006965 if (N->getValueType(0) == MVT::f32) {
6966 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00006967 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00006968 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006969 }
6970 return Val;
Owen Anderson825b72b2009-08-11 20:47:22 +00006971 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00006972 // If the intermediate type is i32, we can avoid the load/store here
6973 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006974 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00006975 }
6976 }
6977 break;
Chris Lattner51269842006-03-01 05:50:56 +00006978 case ISD::STORE:
6979 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
6980 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00006981 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00006982 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006983 N->getOperand(1).getValueType() == MVT::i32 &&
6984 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006985 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006986 if (Val.getValueType() == MVT::f32) {
6987 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006988 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00006989 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006990 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00006991 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00006992
Hal Finkelf170cc92013-04-01 15:37:53 +00006993 SDValue Ops[] = {
6994 N->getOperand(0), Val, N->getOperand(2),
6995 DAG.getValueType(N->getOperand(1).getValueType())
6996 };
6997
6998 Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl,
6999 DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops),
7000 cast<StoreSDNode>(N)->getMemoryVT(),
7001 cast<StoreSDNode>(N)->getMemOperand());
Gabor Greifba36cb52008-08-28 21:40:38 +00007002 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00007003 return Val;
7004 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007005
Chris Lattnerd9989382006-07-10 20:56:58 +00007006 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman6acaaa82009-09-25 00:57:30 +00007007 if (cast<StoreSDNode>(N)->isUnindexed() &&
7008 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00007009 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00007010 (N->getOperand(1).getValueType() == MVT::i32 ||
Hal Finkelefdd4672013-03-28 19:25:55 +00007011 N->getOperand(1).getValueType() == MVT::i16 ||
7012 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel2544f222013-03-28 20:23:46 +00007013 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007014 N->getOperand(1).getValueType() == MVT::i64))) {
Dan Gohman475871a2008-07-27 21:46:04 +00007015 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00007016 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson825b72b2009-08-11 20:47:22 +00007017 if (BSwapOp.getValueType() == MVT::i16)
7018 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00007019
Dan Gohmanc76909a2009-09-25 20:36:54 +00007020 SDValue Ops[] = {
7021 N->getOperand(0), BSwapOp, N->getOperand(2),
7022 DAG.getValueType(N->getOperand(1).getValueType())
7023 };
7024 return
7025 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
7026 Ops, array_lengthof(Ops),
7027 cast<StoreSDNode>(N)->getMemoryVT(),
7028 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00007029 }
7030 break;
7031 case ISD::BSWAP:
7032 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00007033 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00007034 N->getOperand(0).hasOneUse() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007035 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 ||
7036 (TM.getSubtarget<PPCSubtarget>().hasLDBRX() &&
Hal Finkel2544f222013-03-28 20:23:46 +00007037 TM.getSubtarget<PPCSubtarget>().isPPC64() &&
Hal Finkelefdd4672013-03-28 19:25:55 +00007038 N->getValueType(0) == MVT::i64))) {
Dan Gohman475871a2008-07-27 21:46:04 +00007039 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00007040 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00007041 // Create the byte-swapping load.
Dan Gohman475871a2008-07-27 21:46:04 +00007042 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00007043 LD->getChain(), // Chain
7044 LD->getBasePtr(), // Ptr
Chris Lattner79e490a2006-08-11 17:18:05 +00007045 DAG.getValueType(N->getValueType(0)) // VT
7046 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00007047 SDValue BSLoad =
7048 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
Hal Finkelefdd4672013-03-28 19:25:55 +00007049 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
7050 MVT::i64 : MVT::i32, MVT::Other),
Hal Finkelb52980b2013-03-28 19:43:12 +00007051 Ops, 3, LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00007052
Scott Michelfdc40a02009-02-17 22:15:04 +00007053 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00007054 SDValue ResVal = BSLoad;
Owen Anderson825b72b2009-08-11 20:47:22 +00007055 if (N->getValueType(0) == MVT::i16)
7056 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00007057
Chris Lattnerd9989382006-07-10 20:56:58 +00007058 // First, combine the bswap away. This makes the value produced by the
7059 // load dead.
7060 DCI.CombineTo(N, ResVal);
7061
7062 // Next, combine the load away, we give it a bogus result value but a real
7063 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00007064 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00007065
Chris Lattnerd9989382006-07-10 20:56:58 +00007066 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00007067 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00007068 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007069
Chris Lattner51269842006-03-01 05:50:56 +00007070 break;
Chris Lattner4468c222006-03-31 06:02:07 +00007071 case PPCISD::VCMP: {
7072 // If a VCMPo node already exists with exactly the same operands as this
7073 // node, use its result instead of this node (VCMPo computes both a CR6 and
7074 // a normal output).
7075 //
7076 if (!N->getOperand(0).hasOneUse() &&
7077 !N->getOperand(1).hasOneUse() &&
7078 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00007079
Chris Lattner4468c222006-03-31 06:02:07 +00007080 // Scan all of the users of the LHS, looking for VCMPo's that match.
7081 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00007082
Gabor Greifba36cb52008-08-28 21:40:38 +00007083 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00007084 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
7085 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00007086 if (UI->getOpcode() == PPCISD::VCMPo &&
7087 UI->getOperand(1) == N->getOperand(1) &&
7088 UI->getOperand(2) == N->getOperand(2) &&
7089 UI->getOperand(0) == N->getOperand(0)) {
7090 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00007091 break;
7092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007093
Chris Lattner00901202006-04-18 18:28:22 +00007094 // If there is no VCMPo node, or if the flag value has a single use, don't
7095 // transform this.
7096 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
7097 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007098
7099 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00007100 // chain, this transformation is more complex. Note that multiple things
7101 // could use the value result, which we should ignore.
7102 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00007103 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00007104 FlagUser == 0; ++UI) {
7105 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00007106 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00007107 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007108 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00007109 FlagUser = User;
7110 break;
7111 }
7112 }
7113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007114
Chris Lattner00901202006-04-18 18:28:22 +00007115 // If the user is a MFCR instruction, we know this is safe. Otherwise we
7116 // give up for right now.
7117 if (FlagUser->getOpcode() == PPCISD::MFCR)
Dan Gohman475871a2008-07-27 21:46:04 +00007118 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00007119 }
7120 break;
7121 }
Chris Lattner90564f22006-04-18 17:59:36 +00007122 case ISD::BR_CC: {
7123 // If this is a branch on an altivec predicate comparison, lower this so
7124 // that we don't have to do a MFCR: instead, branch directly on CR6. This
7125 // lowering is done pre-legalize, because the legalizer lowers the predicate
7126 // compare down to code that is difficult to reassemble.
7127 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00007128 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Hal Finkelb1fd3cd2013-05-15 21:37:41 +00007129
7130 // Sometimes the promoted value of the intrinsic is ANDed by some non-zero
7131 // value. If so, pass-through the AND to get to the intrinsic.
7132 if (LHS.getOpcode() == ISD::AND &&
7133 LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7134 cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() ==
7135 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7136 isa<ConstantSDNode>(LHS.getOperand(1)) &&
7137 !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()->
7138 isZero())
7139 LHS = LHS.getOperand(0);
7140
7141 if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
7142 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() ==
7143 Intrinsic::ppc_is_decremented_ctr_nonzero &&
7144 isa<ConstantSDNode>(RHS)) {
7145 assert((CC == ISD::SETEQ || CC == ISD::SETNE) &&
7146 "Counter decrement comparison is not EQ or NE");
7147
7148 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
7149 bool isBDNZ = (CC == ISD::SETEQ && Val) ||
7150 (CC == ISD::SETNE && !Val);
7151
7152 // We now need to make the intrinsic dead (it cannot be instruction
7153 // selected).
7154 DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0));
7155 assert(LHS.getNode()->hasOneUse() &&
7156 "Counter decrement has more than one use");
7157
7158 return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other,
7159 N->getOperand(0), N->getOperand(4));
7160 }
7161
Chris Lattner90564f22006-04-18 17:59:36 +00007162 int CompareOpc;
7163 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00007164
Chris Lattner90564f22006-04-18 17:59:36 +00007165 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
7166 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
7167 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
7168 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00007169
Chris Lattner90564f22006-04-18 17:59:36 +00007170 // If this is a comparison against something other than 0/1, then we know
7171 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007172 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00007173 if (Val != 0 && Val != 1) {
7174 if (CC == ISD::SETEQ) // Cond never true, remove branch.
7175 return N->getOperand(0);
7176 // Always !=, turn it into an unconditional branch.
Owen Anderson825b72b2009-08-11 20:47:22 +00007177 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00007178 N->getOperand(0), N->getOperand(4));
7179 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007180
Chris Lattner90564f22006-04-18 17:59:36 +00007181 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00007182
Chris Lattner90564f22006-04-18 17:59:36 +00007183 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00007184 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00007185 LHS.getOperand(2), // LHS of compare
7186 LHS.getOperand(3), // RHS of compare
Owen Anderson825b72b2009-08-11 20:47:22 +00007187 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00007188 };
Benjamin Kramer3853f742013-03-07 20:33:29 +00007189 EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue };
Dale Johannesen3484c092009-02-05 22:07:54 +00007190 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00007191
Chris Lattner90564f22006-04-18 17:59:36 +00007192 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007193 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007194 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00007195 default: // Can't happen, don't crash on invalid number though.
7196 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007197 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00007198 break;
7199 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007200 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00007201 break;
7202 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007203 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00007204 break;
7205 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00007206 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00007207 break;
7208 }
7209
Owen Anderson825b72b2009-08-11 20:47:22 +00007210 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
7211 DAG.getConstant(CompOpc, MVT::i32),
7212 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00007213 N->getOperand(4), CompNode.getValue(1));
7214 }
7215 break;
7216 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007217 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007218
Dan Gohman475871a2008-07-27 21:46:04 +00007219 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00007220}
7221
Chris Lattner1a635d62006-04-14 06:01:58 +00007222//===----------------------------------------------------------------------===//
7223// Inline Assembly Support
7224//===----------------------------------------------------------------------===//
7225
Dan Gohman475871a2008-07-27 21:46:04 +00007226void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Scott Michelfdc40a02009-02-17 22:15:04 +00007227 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007228 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007229 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007230 unsigned Depth) const {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00007231 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007232 switch (Op.getOpcode()) {
7233 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00007234 case PPCISD::LBRX: {
7235 // lhbrx is known to have the top bits cleared out.
Dan Gohmanae03af22009-09-27 23:17:47 +00007236 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnerd9989382006-07-10 20:56:58 +00007237 KnownZero = 0xFFFF0000;
7238 break;
7239 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007240 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007241 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007242 default: break;
7243 case Intrinsic::ppc_altivec_vcmpbfp_p:
7244 case Intrinsic::ppc_altivec_vcmpeqfp_p:
7245 case Intrinsic::ppc_altivec_vcmpequb_p:
7246 case Intrinsic::ppc_altivec_vcmpequh_p:
7247 case Intrinsic::ppc_altivec_vcmpequw_p:
7248 case Intrinsic::ppc_altivec_vcmpgefp_p:
7249 case Intrinsic::ppc_altivec_vcmpgtfp_p:
7250 case Intrinsic::ppc_altivec_vcmpgtsb_p:
7251 case Intrinsic::ppc_altivec_vcmpgtsh_p:
7252 case Intrinsic::ppc_altivec_vcmpgtsw_p:
7253 case Intrinsic::ppc_altivec_vcmpgtub_p:
7254 case Intrinsic::ppc_altivec_vcmpgtuh_p:
7255 case Intrinsic::ppc_altivec_vcmpgtuw_p:
7256 KnownZero = ~1U; // All bits but the low one are known to be zero.
7257 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007258 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00007259 }
7260 }
7261}
7262
7263
Chris Lattner4234f572007-03-25 02:14:49 +00007264/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00007265/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00007266PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007267PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
7268 if (Constraint.size() == 1) {
7269 switch (Constraint[0]) {
7270 default: break;
7271 case 'b':
7272 case 'r':
7273 case 'f':
7274 case 'v':
7275 case 'y':
7276 return C_RegisterClass;
Hal Finkel827b7a02012-11-05 18:18:42 +00007277 case 'Z':
7278 // FIXME: While Z does indicate a memory constraint, it specifically
7279 // indicates an r+r address (used in conjunction with the 'y' modifier
7280 // in the replacement string). Currently, we're forcing the base
7281 // register to be r0 in the asm printer (which is interpreted as zero)
7282 // and forming the complete address in the second register. This is
7283 // suboptimal.
7284 return C_Memory;
Chris Lattner4234f572007-03-25 02:14:49 +00007285 }
7286 }
7287 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00007288}
7289
John Thompson44ab89e2010-10-29 17:29:13 +00007290/// Examine constraint type and operand type and determine a weight value.
7291/// This object must already have been set up with the operand type
7292/// and the current alternative constraint selected.
7293TargetLowering::ConstraintWeight
7294PPCTargetLowering::getSingleConstraintMatchWeight(
7295 AsmOperandInfo &info, const char *constraint) const {
7296 ConstraintWeight weight = CW_Invalid;
7297 Value *CallOperandVal = info.CallOperandVal;
7298 // If we don't have a value, we can't do a match,
7299 // but allow it at the lowest weight.
7300 if (CallOperandVal == NULL)
7301 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007302 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00007303 // Look at the constraint type.
7304 switch (*constraint) {
7305 default:
7306 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
7307 break;
7308 case 'b':
7309 if (type->isIntegerTy())
7310 weight = CW_Register;
7311 break;
7312 case 'f':
7313 if (type->isFloatTy())
7314 weight = CW_Register;
7315 break;
7316 case 'd':
7317 if (type->isDoubleTy())
7318 weight = CW_Register;
7319 break;
7320 case 'v':
7321 if (type->isVectorTy())
7322 weight = CW_Register;
7323 break;
7324 case 'y':
7325 weight = CW_Register;
7326 break;
Hal Finkel827b7a02012-11-05 18:18:42 +00007327 case 'Z':
7328 weight = CW_Memory;
7329 break;
John Thompson44ab89e2010-10-29 17:29:13 +00007330 }
7331 return weight;
7332}
7333
Scott Michelfdc40a02009-02-17 22:15:04 +00007334std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00007335PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00007336 EVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00007337 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00007338 // GCC RS6000 Constraint Letters
7339 switch (Constraint[0]) {
7340 case 'b': // R1-R31
Hal Finkela548afc2013-03-19 18:51:05 +00007341 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
7342 return std::make_pair(0U, &PPC::G8RC_NOX0RegClass);
7343 return std::make_pair(0U, &PPC::GPRC_NOR0RegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007344 case 'r': // R0-R31
Owen Anderson825b72b2009-08-11 20:47:22 +00007345 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Craig Topperc9099502012-04-20 06:31:50 +00007346 return std::make_pair(0U, &PPC::G8RCRegClass);
7347 return std::make_pair(0U, &PPC::GPRCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007348 case 'f':
Ulrich Weigand78dab642012-10-29 17:49:34 +00007349 if (VT == MVT::f32 || VT == MVT::i32)
Craig Topperc9099502012-04-20 06:31:50 +00007350 return std::make_pair(0U, &PPC::F4RCRegClass);
Ulrich Weigand78dab642012-10-29 17:49:34 +00007351 if (VT == MVT::f64 || VT == MVT::i64)
Craig Topperc9099502012-04-20 06:31:50 +00007352 return std::make_pair(0U, &PPC::F8RCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007353 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007354 case 'v':
Craig Topperc9099502012-04-20 06:31:50 +00007355 return std::make_pair(0U, &PPC::VRRCRegClass);
Chris Lattner331d1bc2006-11-02 01:44:04 +00007356 case 'y': // crrc
Craig Topperc9099502012-04-20 06:31:50 +00007357 return std::make_pair(0U, &PPC::CRRCRegClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00007358 }
7359 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007360
Chris Lattner331d1bc2006-11-02 01:44:04 +00007361 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00007362}
Chris Lattner763317d2006-02-07 00:47:13 +00007363
Chris Lattner331d1bc2006-11-02 01:44:04 +00007364
Chris Lattner48884cd2007-08-25 00:47:38 +00007365/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesen1784d162010-06-25 21:55:36 +00007366/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher471e4222011-06-08 23:55:35 +00007367void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00007368 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00007369 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00007370 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007371 SDValue Result(0,0);
Eric Christopher471e4222011-06-08 23:55:35 +00007372
Eric Christopher100c8332011-06-02 23:16:42 +00007373 // Only support length 1 constraints.
7374 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00007375
Eric Christopher100c8332011-06-02 23:16:42 +00007376 char Letter = Constraint[0];
Chris Lattner763317d2006-02-07 00:47:13 +00007377 switch (Letter) {
7378 default: break;
7379 case 'I':
7380 case 'J':
7381 case 'K':
7382 case 'L':
7383 case 'M':
7384 case 'N':
7385 case 'O':
7386 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00007387 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00007388 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007389 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00007390 switch (Letter) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007391 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner763317d2006-02-07 00:47:13 +00007392 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007393 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00007394 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007395 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007396 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
7397 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007398 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007399 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007400 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007401 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007402 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007403 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007404 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007405 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007406 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00007407 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007408 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007409 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007410 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00007411 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007412 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007413 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007414 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00007415 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007416 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007417 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00007418 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00007419 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00007420 break;
Chris Lattner763317d2006-02-07 00:47:13 +00007421 }
7422 break;
7423 }
7424 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007425
Gabor Greifba36cb52008-08-28 21:40:38 +00007426 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00007427 Ops.push_back(Result);
7428 return;
7429 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007430
Chris Lattner763317d2006-02-07 00:47:13 +00007431 // Handle standard constraint letters.
Eric Christopher100c8332011-06-02 23:16:42 +00007432 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00007433}
Evan Chengc4c62572006-03-13 23:20:37 +00007434
Chris Lattnerc9addb72007-03-30 23:15:24 +00007435// isLegalAddressingMode - Return true if the addressing mode represented
7436// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007437bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007438 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00007439 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00007440
Chris Lattnerc9addb72007-03-30 23:15:24 +00007441 // PPC allows a sign-extended 16-bit immediate field.
7442 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
7443 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007444
Chris Lattnerc9addb72007-03-30 23:15:24 +00007445 // No global is ever allowed as a base.
7446 if (AM.BaseGV)
7447 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007448
7449 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007450 switch (AM.Scale) {
7451 case 0: // "r+i" or just "i", depending on HasBaseReg.
7452 break;
7453 case 1:
7454 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
7455 return false;
7456 // Otherwise we have r+r or r+i.
7457 break;
7458 case 2:
7459 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
7460 return false;
7461 // Allow 2*r as r+r.
7462 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00007463 default:
7464 // No other scales are supported.
7465 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007466 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007467
Chris Lattnerc9addb72007-03-30 23:15:24 +00007468 return true;
7469}
7470
Dan Gohmand858e902010-04-17 15:26:15 +00007471SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
7472 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00007473 MachineFunction &MF = DAG.getMachineFunction();
7474 MachineFrameInfo *MFI = MF.getFrameInfo();
7475 MFI->setReturnAddressIsTaken(true);
7476
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007477 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen08673d22010-05-03 22:59:34 +00007478 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00007479
Dale Johannesen08673d22010-05-03 22:59:34 +00007480 // Make sure the function does not optimize away the store of the RA to
7481 // the stack.
Chris Lattner3fc027d2007-12-08 06:59:59 +00007482 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen08673d22010-05-03 22:59:34 +00007483 FuncInfo->setLRStoreRequired();
7484 bool isPPC64 = PPCSubTarget.isPPC64();
7485 bool isDarwinABI = PPCSubTarget.isDarwinABI();
7486
7487 if (Depth > 0) {
7488 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7489 SDValue Offset =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007490
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00007491 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen08673d22010-05-03 22:59:34 +00007492 isPPC64? MVT::i64 : MVT::i32);
7493 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
7494 DAG.getNode(ISD::ADD, dl, getPointerTy(),
7495 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007496 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00007497 }
Chris Lattner3fc027d2007-12-08 06:59:59 +00007498
Chris Lattner3fc027d2007-12-08 06:59:59 +00007499 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00007500 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen08673d22010-05-03 22:59:34 +00007501 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007502 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00007503}
7504
Dan Gohmand858e902010-04-17 15:26:15 +00007505SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
7506 SelectionDAG &DAG) const {
Dale Johannesena05dca42009-02-04 23:02:30 +00007507 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen08673d22010-05-03 22:59:34 +00007508 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007509
Owen Andersone50ed302009-08-10 22:56:29 +00007510 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00007511 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00007512
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00007513 MachineFunction &MF = DAG.getMachineFunction();
7514 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen08673d22010-05-03 22:59:34 +00007515 MFI->setFrameAddressIsTaken(true);
Hal Finkele9cc0a02013-03-21 19:03:19 +00007516
7517 // Naked functions never have a frame pointer, and so we use r1. For all
7518 // other functions, this decision must be delayed until during PEI.
7519 unsigned FrameReg;
7520 if (MF.getFunction()->getAttributes().hasAttribute(
7521 AttributeSet::FunctionIndex, Attribute::Naked))
7522 FrameReg = isPPC64 ? PPC::X1 : PPC::R1;
7523 else
7524 FrameReg = isPPC64 ? PPC::FP8 : PPC::FP;
7525
Dale Johannesen08673d22010-05-03 22:59:34 +00007526 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
7527 PtrVT);
7528 while (Depth--)
7529 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007530 FrameAddr, MachinePointerInfo(), false, false,
7531 false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00007532 return FrameAddr;
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00007533}
Dan Gohman54aeea32008-10-21 03:41:46 +00007534
7535bool
7536PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
7537 // The PowerPC target isn't yet aware of offsets.
7538 return false;
7539}
Tilmann Schellerffd02002009-07-03 06:45:56 +00007540
Evan Cheng42642d02010-04-01 20:10:42 +00007541/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengf28f8bc2010-04-02 19:36:14 +00007542/// and store operations as a result of memset, memcpy, and memmove
7543/// lowering. If DstAlign is zero that means it's safe to destination
7544/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
7545/// means there isn't a need to check it against alignment requirement,
Evan Cheng946a3a92012-12-12 02:34:41 +00007546/// probably because the source does not need to be loaded. If 'IsMemset' is
7547/// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
7548/// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
7549/// source is constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00007550/// It returns EVT::Other if the type should be determined using generic
7551/// target-independent logic.
Evan Cheng255f20f2010-04-01 06:04:33 +00007552EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
7553 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng946a3a92012-12-12 02:34:41 +00007554 bool IsMemset, bool ZeroMemset,
Evan Chengc3b0c342010-04-08 07:37:57 +00007555 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00007556 MachineFunction &MF) const {
Tilmann Schellerffd02002009-07-03 06:45:56 +00007557 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007558 return MVT::i64;
Tilmann Schellerffd02002009-07-03 06:45:56 +00007559 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00007560 return MVT::i32;
Tilmann Schellerffd02002009-07-03 06:45:56 +00007561 }
7562}
Hal Finkel3f31d492012-04-01 19:23:08 +00007563
Hal Finkel2d37f7b2013-03-15 15:27:13 +00007564bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
7565 bool *Fast) const {
7566 if (DisablePPCUnaligned)
7567 return false;
7568
7569 // PowerPC supports unaligned memory access for simple non-vector types.
7570 // Although accessing unaligned addresses is not as efficient as accessing
7571 // aligned addresses, it is generally more efficient than manual expansion,
7572 // and generally only traps for software emulation when crossing page
7573 // boundaries.
7574
7575 if (!VT.isSimple())
7576 return false;
7577
7578 if (VT.getSimpleVT().isVector())
7579 return false;
7580
7581 if (VT == MVT::ppcf128)
7582 return false;
7583
7584 if (Fast)
7585 *Fast = true;
7586
7587 return true;
7588}
7589
Hal Finkel070b8db2012-06-22 00:49:52 +00007590/// isFMAFasterThanMulAndAdd - Return true if an FMA operation is faster than
7591/// a pair of mul and add instructions. fmuladd intrinsics will be expanded to
7592/// FMAs when this method returns true (and FMAs are legal), otherwise fmuladd
7593/// is expanded to mul + add.
7594bool PPCTargetLowering::isFMAFasterThanMulAndAdd(EVT VT) const {
7595 if (!VT.isSimple())
7596 return false;
7597
7598 switch (VT.getSimpleVT().SimpleTy) {
7599 case MVT::f32:
7600 case MVT::f64:
7601 case MVT::v4f32:
7602 return true;
7603 default:
7604 break;
7605 }
7606
7607 return false;
7608}
7609
Hal Finkel3f31d492012-04-01 19:23:08 +00007610Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const {
Hal Finkel71ffcfe2012-06-10 19:32:29 +00007611 if (DisableILPPref)
7612 return TargetLowering::getSchedulingPreference(N);
Hal Finkel3f31d492012-04-01 19:23:08 +00007613
Hal Finkel71ffcfe2012-06-10 19:32:29 +00007614 return Sched::ILP;
Hal Finkel3f31d492012-04-01 19:23:08 +00007615}
7616