blob: 08307aaf293dcd951e22f3dfd510a12f25487b13 [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"
Jim Laskey2f616bf2006-11-16 22:43:37 +000015#include "PPCMachineFunctionInfo.h"
Chris Lattnerdf4ed632006-11-17 22:10:59 +000016#include "PPCPredicates.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000017#include "PPCTargetMachine.h"
Chris Lattner59138102006-04-17 05:28:54 +000018#include "PPCPerfectShuffle.h"
Owen Anderson718cb662007-09-07 04:06:50 +000019#include "llvm/ADT/STLExtras.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000020#include "llvm/ADT/VectorExtras.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"
Dan Gohman69de1932008-02-06 22:27:42 +000026#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000027#include "llvm/CodeGen/SelectionDAG.h"
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +000028#include "llvm/CallingConv.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000029#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000030#include "llvm/Function.h"
Chris Lattner6d92cad2006-03-26 10:06:40 +000031#include "llvm/Intrinsics.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000032#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner4eab7142006-11-10 02:08:47 +000034#include "llvm/Support/CommandLine.h"
Jay Foad8d730fb2009-05-11 19:38:09 +000035#include "llvm/DerivedTypes.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000036using namespace llvm;
37
Tilmann Schellerffd02002009-07-03 06:45:56 +000038static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
39 CCValAssign::LocInfo &LocInfo,
40 ISD::ArgFlagsTy &ArgFlags,
41 CCState &State);
42static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
43 MVT &LocVT,
44 CCValAssign::LocInfo &LocInfo,
45 ISD::ArgFlagsTy &ArgFlags,
46 CCState &State);
47static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
48 MVT &LocVT,
49 CCValAssign::LocInfo &LocInfo,
50 ISD::ArgFlagsTy &ArgFlags,
51 CCState &State);
52
Scott Michelfdc40a02009-02-17 22:15:04 +000053static cl::opt<bool> EnablePPCPreinc("enable-ppc-preinc",
Chris Lattner3ee77402007-06-19 05:46:06 +000054cl::desc("enable preincrement load/store generation on PPC (experimental)"),
55 cl::Hidden);
Chris Lattner4eab7142006-11-10 02:08:47 +000056
Chris Lattner331d1bc2006-11-02 01:44:04 +000057PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Evan Cheng53301922008-07-12 02:23:19 +000058 : TargetLowering(TM), PPCSubTarget(*TM.getSubtargetImpl()) {
Scott Michelfdc40a02009-02-17 22:15:04 +000059
Nate Begeman405e3ec2005-10-21 00:02:42 +000060 setPow2DivIsCheap();
Dale Johannesen72324642008-07-31 18:13:12 +000061
Chris Lattnerd145a612005-09-27 22:18:25 +000062 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000063 setUseUnderscoreSetJmp(true);
64 setUseUnderscoreLongJmp(true);
Scott Michelfdc40a02009-02-17 22:15:04 +000065
Chris Lattner7c5a3d32005-08-16 17:14:42 +000066 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000067 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
68 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
69 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +000070
Evan Chengc5484282006-10-04 00:56:09 +000071 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Evan Cheng03294662008-10-14 21:26:46 +000072 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
73 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000074
Chris Lattnerddf89562008-01-17 19:59:44 +000075 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +000076
Chris Lattner94e509c2006-11-10 23:58:45 +000077 // PowerPC has pre-inc load and store's.
78 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
79 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
80 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000081 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
82 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Chris Lattner94e509c2006-11-10 23:58:45 +000083 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
84 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
85 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000086 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
87 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
88
Dale Johannesen6eaeff22007-10-10 01:01:31 +000089 // This is used in the ppcf128->int sequence. Note it has different semantics
90 // from FP_ROUND: that rounds to nearest, this rounds to zero.
91 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +000092
Chris Lattner7c5a3d32005-08-16 17:14:42 +000093 // PowerPC has no SREM/UREM instructions
94 setOperationAction(ISD::SREM, MVT::i32, Expand);
95 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner563ecfb2006-06-27 18:18:41 +000096 setOperationAction(ISD::SREM, MVT::i64, Expand);
97 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +000098
99 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
100 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
101 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
102 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
103 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
104 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
105 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
106 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
107 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000108
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000109 // We don't support sin/cos/sqrt/fmod/pow
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000110 setOperationAction(ISD::FSIN , MVT::f64, Expand);
111 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +0000112 setOperationAction(ISD::FREM , MVT::f64, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000113 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000114 setOperationAction(ISD::FSIN , MVT::f32, Expand);
115 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +0000116 setOperationAction(ISD::FREM , MVT::f32, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000117 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000118
Dan Gohman1a024862008-01-31 00:41:03 +0000119 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000120
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000121 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000122 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000123 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
124 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
125 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000126
Chris Lattner9601a862006-03-05 05:08:37 +0000127 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
128 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000129
Nate Begemand88fc032006-01-14 03:14:10 +0000130 // PowerPC does not have BSWAP, CTPOP or CTTZ
131 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000132 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
133 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +0000134 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
135 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
136 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000137
Nate Begeman35ef9132006-01-11 21:21:00 +0000138 // PowerPC does not have ROTR
139 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
Bill Wendling3156b622008-08-31 02:53:19 +0000140 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000141
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000142 // PowerPC does not have Select
143 setOperationAction(ISD::SELECT, MVT::i32, Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +0000144 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000145 setOperationAction(ISD::SELECT, MVT::f32, Expand);
146 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000147
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000148 // PowerPC wants to turn select_cc of FP into fsel when possible.
149 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
150 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000151
Nate Begeman750ac1b2006-02-01 07:19:44 +0000152 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +0000153 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000154
Nate Begeman81e80972006-03-17 01:40:33 +0000155 // PowerPC does not have BRCOND which requires SetCC
156 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000157
158 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000159
Chris Lattnerf7605322005-08-31 21:09:52 +0000160 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
161 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000162
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000163 // PowerPC does not have [U|S]INT_TO_FP
164 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
165 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
166
Chris Lattner53e88452005-12-23 05:13:35 +0000167 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
168 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Chris Lattner5f9faea2006-06-27 18:40:08 +0000169 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Expand);
170 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000171
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000172 // We cannot sextinreg(i1). Expand to shifts.
173 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000174
Jim Laskeyabf6d172006-01-05 01:25:28 +0000175 // Support label based line numbers.
Dan Gohman7f460202008-06-30 20:59:49 +0000176 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000177 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000178
Nicolas Geoffray616585b2007-12-21 12:19:44 +0000179 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
180 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
181 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
182 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000183
184
185 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman28a6b022005-12-10 02:36:00 +0000186 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000187 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000188 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000189 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000190 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000191 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000192 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000193 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
194 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000195
Nate Begeman1db3c922008-08-11 17:36:31 +0000196 // RET must be custom lowered, to meet ABI requirements.
Nate Begemanee625572006-01-27 21:09:22 +0000197 setOperationAction(ISD::RET , MVT::Other, Custom);
Duncan Sands36397f52007-07-27 12:58:54 +0000198
Nate Begeman1db3c922008-08-11 17:36:31 +0000199 // TRAP is legal.
200 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling77959322008-09-17 00:30:57 +0000201
202 // TRAMPOLINE is custom lowered.
203 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
204
Nate Begemanacc398c2006-01-25 18:21:52 +0000205 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
206 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000207
Nicolas Geoffray01119992007-04-03 13:59:52 +0000208 // VAARG is custom lowered with ELF 32 ABI
209 if (TM.getSubtarget<PPCSubtarget>().isELF32_ABI())
210 setOperationAction(ISD::VAARG, MVT::Other, Custom);
211 else
212 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000213
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000214 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000215 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
216 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000217 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
Jim Laskeyefc7e522006-12-04 22:04:42 +0000218 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000219 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
220 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000221
Chris Lattner6d92cad2006-03-26 10:06:40 +0000222 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000223 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000224
Dale Johannesen53e4e442008-11-07 22:54:33 +0000225 // Comparisons that require checking two conditions.
226 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
227 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
228 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
229 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
230 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
231 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
232 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
233 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
234 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
235 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
236 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
237 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000238
Chris Lattnera7a58542006-06-16 17:34:12 +0000239 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000240 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000241 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Jim Laskeyca367b42006-12-15 14:32:57 +0000242 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000243 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner85c671b2006-12-07 01:24:16 +0000244 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen4c9369d2009-06-04 20:53:52 +0000245 // This is just the low 32 bits of a (signed) fp->i64 conversion.
246 // We cannot do this with Promote because i64 is not a legal type.
247 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000248
Chris Lattner7fbcef72006-03-24 07:53:47 +0000249 // FIXME: disable this lowered code. This generates 64-bit register values,
250 // and we don't model the fact that the top part is clobbered by calls. We
251 // need to flag these together so that the value isn't live across a call.
252 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000253 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000254 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000255 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000256 }
257
Chris Lattnera7a58542006-06-16 17:34:12 +0000258 if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000259 // 64-bit PowerPC implementations can support i64 types directly
Nate Begeman9d2b8172005-10-18 00:56:42 +0000260 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000261 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
262 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000263 // 64-bit PowerPC wants to expand i128 shifts itself.
264 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
265 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
266 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000267 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000268 // 32-bit PowerPC wants to expand i64 shifts itself.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +0000269 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
270 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
271 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000272 }
Evan Chengd30bf012006-03-01 01:11:20 +0000273
Nate Begeman425a9692005-11-29 08:17:20 +0000274 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000275 // First set operation action for all vector types to expand. Then we
276 // will selectively turn on ones that can be effectively codegen'd.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000277 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
278 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
279 MVT VT = (MVT::SimpleValueType)i;
280
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000281 // add/sub are legal for all supported vector VT's.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000282 setOperationAction(ISD::ADD , VT, Legal);
283 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000284
Chris Lattner7ff7e672006-04-04 17:25:31 +0000285 // We promote all shuffles to v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000286 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
287 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000288
289 // We promote all non-typed operations to v4i32.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000290 setOperationAction(ISD::AND , VT, Promote);
291 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
292 setOperationAction(ISD::OR , VT, Promote);
293 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
294 setOperationAction(ISD::XOR , VT, Promote);
295 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
296 setOperationAction(ISD::LOAD , VT, Promote);
297 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
298 setOperationAction(ISD::SELECT, VT, Promote);
299 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
300 setOperationAction(ISD::STORE, VT, Promote);
301 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000302
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000303 // No other operations are legal.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000304 setOperationAction(ISD::MUL , VT, Expand);
305 setOperationAction(ISD::SDIV, VT, Expand);
306 setOperationAction(ISD::SREM, VT, Expand);
307 setOperationAction(ISD::UDIV, VT, Expand);
308 setOperationAction(ISD::UREM, VT, Expand);
309 setOperationAction(ISD::FDIV, VT, Expand);
310 setOperationAction(ISD::FNEG, VT, Expand);
311 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
312 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
313 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
314 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
315 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
316 setOperationAction(ISD::UDIVREM, VT, Expand);
317 setOperationAction(ISD::SDIVREM, VT, Expand);
318 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
319 setOperationAction(ISD::FPOW, VT, Expand);
320 setOperationAction(ISD::CTPOP, VT, Expand);
321 setOperationAction(ISD::CTLZ, VT, Expand);
322 setOperationAction(ISD::CTTZ, VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000323 }
324
Chris Lattner7ff7e672006-04-04 17:25:31 +0000325 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
326 // with merges, splats, etc.
327 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
328
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000329 setOperationAction(ISD::AND , MVT::v4i32, Legal);
330 setOperationAction(ISD::OR , MVT::v4i32, Legal);
331 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
332 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
333 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
334 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000335
Nate Begeman425a9692005-11-29 08:17:20 +0000336 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000337 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000338 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
339 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +0000340
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000341 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnere7c768e2006-04-18 03:24:30 +0000342 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Chris Lattner72dd9bd2006-04-18 03:43:48 +0000343 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
Chris Lattner19a81522006-04-18 03:57:35 +0000344 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000345
Chris Lattnerb2177b92006-03-19 06:55:52 +0000346 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
347 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000348
Chris Lattner541f91b2006-04-02 00:43:36 +0000349 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
350 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000351 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
352 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000353 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000354
Chris Lattner7b0c58c2006-06-27 17:34:57 +0000355 setShiftAmountType(MVT::i32);
Duncan Sands03228082008-11-23 15:47:28 +0000356 setBooleanContents(ZeroOrOneBooleanContent);
Scott Michelfdc40a02009-02-17 22:15:04 +0000357
Jim Laskey2ad9f172007-02-22 14:56:36 +0000358 if (TM.getSubtarget<PPCSubtarget>().isPPC64()) {
Chris Lattner10da9572006-10-18 01:20:43 +0000359 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000360 setExceptionPointerRegister(PPC::X3);
361 setExceptionSelectorRegister(PPC::X4);
362 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000363 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000364 setExceptionPointerRegister(PPC::R3);
365 setExceptionSelectorRegister(PPC::R4);
366 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000367
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000368 // We have target-specific dag combine patterns for the following nodes:
369 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000370 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000371 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000372 setTargetDAGCombine(ISD::BSWAP);
Scott Michelfdc40a02009-02-17 22:15:04 +0000373
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000374 // Darwin long double math library functions have $LDBL128 appended.
375 if (TM.getSubtarget<PPCSubtarget>().isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000376 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000377 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
378 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000379 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
380 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000381 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
382 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
383 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
384 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
385 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000386 }
387
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000388 computeRegisterProperties();
389}
390
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000391/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
392/// function arguments in the caller parameter area.
393unsigned PPCTargetLowering::getByValTypeAlignment(const Type *Ty) const {
394 TargetMachine &TM = getTargetMachine();
395 // Darwin passes everything on 4 byte boundary.
396 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
397 return 4;
398 // FIXME Elf TBD
399 return 4;
400}
401
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000402const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
403 switch (Opcode) {
404 default: return 0;
Evan Cheng53301922008-07-12 02:23:19 +0000405 case PPCISD::FSEL: return "PPCISD::FSEL";
406 case PPCISD::FCFID: return "PPCISD::FCFID";
407 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
408 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
409 case PPCISD::STFIWX: return "PPCISD::STFIWX";
410 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
411 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
412 case PPCISD::VPERM: return "PPCISD::VPERM";
413 case PPCISD::Hi: return "PPCISD::Hi";
414 case PPCISD::Lo: return "PPCISD::Lo";
415 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
416 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
417 case PPCISD::SRL: return "PPCISD::SRL";
418 case PPCISD::SRA: return "PPCISD::SRA";
419 case PPCISD::SHL: return "PPCISD::SHL";
420 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
421 case PPCISD::STD_32: return "PPCISD::STD_32";
422 case PPCISD::CALL_ELF: return "PPCISD::CALL_ELF";
423 case PPCISD::CALL_Macho: return "PPCISD::CALL_Macho";
424 case PPCISD::MTCTR: return "PPCISD::MTCTR";
425 case PPCISD::BCTRL_Macho: return "PPCISD::BCTRL_Macho";
426 case PPCISD::BCTRL_ELF: return "PPCISD::BCTRL_ELF";
427 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
428 case PPCISD::MFCR: return "PPCISD::MFCR";
429 case PPCISD::VCMP: return "PPCISD::VCMP";
430 case PPCISD::VCMPo: return "PPCISD::VCMPo";
431 case PPCISD::LBRX: return "PPCISD::LBRX";
432 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng53301922008-07-12 02:23:19 +0000433 case PPCISD::LARX: return "PPCISD::LARX";
434 case PPCISD::STCX: return "PPCISD::STCX";
435 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
436 case PPCISD::MFFS: return "PPCISD::MFFS";
437 case PPCISD::MTFSB0: return "PPCISD::MTFSB0";
438 case PPCISD::MTFSB1: return "PPCISD::MTFSB1";
439 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
440 case PPCISD::MTFSF: return "PPCISD::MTFSF";
441 case PPCISD::TAILCALL: return "PPCISD::TAILCALL";
442 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000443 }
444}
445
Duncan Sands5480c042009-01-01 15:52:00 +0000446MVT PPCTargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000447 return MVT::i32;
448}
449
Bill Wendlingb4202b82009-07-01 18:50:55 +0000450/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000451unsigned PPCTargetLowering::getFunctionAlignment(const Function *F) const {
452 if (getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin())
453 return F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4;
454 else
455 return 2;
456}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000457
Chris Lattner1a635d62006-04-14 06:01:58 +0000458//===----------------------------------------------------------------------===//
459// Node matching predicates, for use by the tblgen matching code.
460//===----------------------------------------------------------------------===//
461
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000462/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000463static bool isFloatingPointZero(SDValue Op) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000464 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000465 return CFP->getValueAPF().isZero();
Gabor Greifba36cb52008-08-28 21:40:38 +0000466 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000467 // Maybe this has already been legalized into the constant pool?
468 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Evan Chengc356a572006-09-12 21:04:05 +0000469 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000470 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000471 }
472 return false;
473}
474
Chris Lattnerddb739e2006-04-06 17:23:16 +0000475/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
476/// true if Op is undef or if it matches the specified value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000477static bool isConstantOrUndef(int Op, int Val) {
478 return Op < 0 || Op == Val;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000479}
480
481/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
482/// VPKUHUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000483bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000484 if (!isUnary) {
485 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000486 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000487 return false;
488 } else {
489 for (unsigned i = 0; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000490 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
491 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000492 return false;
493 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000494 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000495}
496
497/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
498/// VPKUWUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000499bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000500 if (!isUnary) {
501 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000502 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
503 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000504 return false;
505 } else {
506 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000507 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
508 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
509 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
510 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000511 return false;
512 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000513 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000514}
515
Chris Lattnercaad1632006-04-06 22:02:42 +0000516/// isVMerge - Common function, used to match vmrg* shuffles.
517///
Nate Begeman9008ca62009-04-27 18:41:29 +0000518static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnercaad1632006-04-06 22:02:42 +0000519 unsigned LHSStart, unsigned RHSStart) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000520 assert(N->getValueType(0) == MVT::v16i8 &&
521 "PPC only supports shuffles by bytes!");
Chris Lattner116cc482006-04-06 21:11:54 +0000522 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
523 "Unsupported merge size!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000524
Chris Lattner116cc482006-04-06 21:11:54 +0000525 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
526 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman9008ca62009-04-27 18:41:29 +0000527 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000528 LHSStart+j+i*UnitSize) ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000529 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000530 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000531 return false;
532 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000533 return true;
Chris Lattnercaad1632006-04-06 22:02:42 +0000534}
535
536/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
537/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Nate Begeman9008ca62009-04-27 18:41:29 +0000538bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
539 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000540 if (!isUnary)
541 return isVMerge(N, UnitSize, 8, 24);
542 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000543}
544
545/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
546/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Nate Begeman9008ca62009-04-27 18:41:29 +0000547bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
548 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000549 if (!isUnary)
550 return isVMerge(N, UnitSize, 0, 16);
551 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000552}
553
554
Chris Lattnerd0608e12006-04-06 18:26:28 +0000555/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
556/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000557int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000558 assert(N->getValueType(0) == MVT::v16i8 &&
559 "PPC only supports shuffles by bytes!");
560
561 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
562
Chris Lattnerd0608e12006-04-06 18:26:28 +0000563 // Find the first non-undef value in the shuffle mask.
564 unsigned i;
Nate Begeman9008ca62009-04-27 18:41:29 +0000565 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattnerd0608e12006-04-06 18:26:28 +0000566 /*search*/;
Scott Michelfdc40a02009-02-17 22:15:04 +0000567
Chris Lattnerd0608e12006-04-06 18:26:28 +0000568 if (i == 16) return -1; // all undef.
Scott Michelfdc40a02009-02-17 22:15:04 +0000569
Nate Begeman9008ca62009-04-27 18:41:29 +0000570 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattnerd0608e12006-04-06 18:26:28 +0000571 // numbered from this value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000572 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattnerd0608e12006-04-06 18:26:28 +0000573 if (ShiftAmt < i) return -1;
574 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000575
Chris Lattnerf24380e2006-04-06 22:28:36 +0000576 if (!isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000577 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000578 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000579 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000580 return -1;
581 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000582 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000583 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000584 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000585 return -1;
586 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000587 return ShiftAmt;
588}
Chris Lattneref819f82006-03-20 06:33:01 +0000589
590/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
591/// specifies a splat of a single element that is suitable for input to
592/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman9008ca62009-04-27 18:41:29 +0000593bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
594 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner7ff7e672006-04-04 17:25:31 +0000595 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelfdc40a02009-02-17 22:15:04 +0000596
Chris Lattner88a99ef2006-03-20 06:37:44 +0000597 // This is a splat operation if each element of the permute is the same, and
598 // if the value doesn't reference the second vector.
Nate Begeman9008ca62009-04-27 18:41:29 +0000599 unsigned ElementBase = N->getMaskElt(0);
600
601 // FIXME: Handle UNDEF elements too!
602 if (ElementBase >= 16)
Chris Lattner7ff7e672006-04-04 17:25:31 +0000603 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000604
Nate Begeman9008ca62009-04-27 18:41:29 +0000605 // Check that the indices are consecutive, in the case of a multi-byte element
606 // splatted with a v16i8 mask.
607 for (unsigned i = 1; i != EltSize; ++i)
608 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000609 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000610
Chris Lattner7ff7e672006-04-04 17:25:31 +0000611 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000612 if (N->getMaskElt(i) < 0) continue;
Chris Lattner7ff7e672006-04-04 17:25:31 +0000613 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman9008ca62009-04-27 18:41:29 +0000614 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000615 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000616 }
Chris Lattner7ff7e672006-04-04 17:25:31 +0000617 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000618}
619
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000620/// isAllNegativeZeroVector - Returns true if all elements of build_vector
621/// are -0.0.
622bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000623 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
624
625 APInt APVal, APUndef;
626 unsigned BitSize;
627 bool HasAnyUndefs;
628
629 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32))
630 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000631 return CFP->getValueAPF().isNegZero();
Nate Begeman9008ca62009-04-27 18:41:29 +0000632
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000633 return false;
634}
635
Chris Lattneref819f82006-03-20 06:33:01 +0000636/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
637/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000638unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000639 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
640 assert(isSplatShuffleMask(SVOp, EltSize));
641 return SVOp->getMaskElt(0) / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000642}
643
Chris Lattnere87192a2006-04-12 17:37:20 +0000644/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000645/// by using a vspltis[bhw] instruction of the specified element size, return
646/// the constant being splatted. The ByteSize field indicates the number of
647/// bytes of each element [124] -> [bhw].
Dan Gohman475871a2008-07-27 21:46:04 +0000648SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
649 SDValue OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000650
651 // If ByteSize of the splat is bigger than the element size of the
652 // build_vector, then we have a case where we are checking for a splat where
653 // multiple elements of the buildvector are folded together into a single
654 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
655 unsigned EltSize = 16/N->getNumOperands();
656 if (EltSize < ByteSize) {
657 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman475871a2008-07-27 21:46:04 +0000658 SDValue UniquedVals[4];
Chris Lattner79d9a882006-04-08 07:14:26 +0000659 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Chris Lattner79d9a882006-04-08 07:14:26 +0000661 // See if all of the elements in the buildvector agree across.
662 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
663 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
664 // If the element isn't a constant, bail fully out.
Dan Gohman475871a2008-07-27 21:46:04 +0000665 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000666
Scott Michelfdc40a02009-02-17 22:15:04 +0000667
Gabor Greifba36cb52008-08-28 21:40:38 +0000668 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000669 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
670 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000671 return SDValue(); // no match.
Chris Lattner79d9a882006-04-08 07:14:26 +0000672 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000673
Chris Lattner79d9a882006-04-08 07:14:26 +0000674 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
675 // either constant or undef values that are identical for each chunk. See
676 // if these chunks can form into a larger vspltis*.
Scott Michelfdc40a02009-02-17 22:15:04 +0000677
Chris Lattner79d9a882006-04-08 07:14:26 +0000678 // Check to see if all of the leading entries are either 0 or -1. If
679 // neither, then this won't fit into the immediate field.
680 bool LeadingZero = true;
681 bool LeadingOnes = true;
682 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000683 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelfdc40a02009-02-17 22:15:04 +0000684
Chris Lattner79d9a882006-04-08 07:14:26 +0000685 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
686 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
687 }
688 // Finally, check the least significant entry.
689 if (LeadingZero) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000690 if (UniquedVals[Multiple-1].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000691 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000692 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000693 if (Val < 16)
694 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
695 }
696 if (LeadingOnes) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000697 if (UniquedVals[Multiple-1].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000698 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman7810bfe2008-09-26 21:54:37 +0000699 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000700 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
701 return DAG.getTargetConstant(Val, MVT::i32);
702 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000703
Dan Gohman475871a2008-07-27 21:46:04 +0000704 return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000705 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000706
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000707 // Check to see if this buildvec has a single non-undef value in its elements.
708 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
709 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +0000710 if (OpVal.getNode() == 0)
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000711 OpVal = N->getOperand(i);
712 else if (OpVal != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000713 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000714 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000715
Gabor Greifba36cb52008-08-28 21:40:38 +0000716 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelfdc40a02009-02-17 22:15:04 +0000717
Eli Friedman1a8229b2009-05-24 02:03:36 +0000718 unsigned ValSizeInBytes = EltSize;
Nate Begeman98e70cc2006-03-28 04:15:58 +0000719 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000720 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000721 Value = CN->getZExtValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000722 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
723 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000724 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000725 }
726
727 // If the splat value is larger than the element value, then we can never do
728 // this splat. The only case that we could fit the replicated bits into our
729 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman475871a2008-07-27 21:46:04 +0000730 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000731
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000732 // If the element value is larger than the splat value, cut it in half and
733 // check to see if the two halves are equal. Continue doing this until we
734 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
735 while (ValSizeInBytes > ByteSize) {
736 ValSizeInBytes >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000737
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000738 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000739 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
740 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman475871a2008-07-27 21:46:04 +0000741 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000742 }
743
744 // Properly sign extend the value.
745 int ShAmt = (4-ByteSize)*8;
746 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
Scott Michelfdc40a02009-02-17 22:15:04 +0000747
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000748 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman475871a2008-07-27 21:46:04 +0000749 if (MaskVal == 0) return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000750
Chris Lattner140a58f2006-04-08 06:46:53 +0000751 // Finally, if this value fits in a 5 bit sext field, return it
752 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
753 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000754 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000755}
756
Chris Lattner1a635d62006-04-14 06:01:58 +0000757//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000758// Addressing Mode Selection
759//===----------------------------------------------------------------------===//
760
761/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
762/// or 64-bit immediate, and if the value can be accurately represented as a
763/// sign extension from a 16-bit value. If so, this returns true and the
764/// immediate.
765static bool isIntS16Immediate(SDNode *N, short &Imm) {
766 if (N->getOpcode() != ISD::Constant)
767 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000768
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000769 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000770 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000771 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000772 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000773 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000774}
Dan Gohman475871a2008-07-27 21:46:04 +0000775static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000776 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000777}
778
779
780/// SelectAddressRegReg - Given the specified addressed, check to see if it
781/// can be represented as an indexed [r+r] operation. Returns false if it
782/// can be more efficiently represented with [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +0000783bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
784 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000785 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000786 short imm = 0;
787 if (N.getOpcode() == ISD::ADD) {
788 if (isIntS16Immediate(N.getOperand(1), imm))
789 return false; // r+i
790 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
791 return false; // r+i
Scott Michelfdc40a02009-02-17 22:15:04 +0000792
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000793 Base = N.getOperand(0);
794 Index = N.getOperand(1);
795 return true;
796 } else if (N.getOpcode() == ISD::OR) {
797 if (isIntS16Immediate(N.getOperand(1), imm))
798 return false; // r+i can fold it if we can.
Scott Michelfdc40a02009-02-17 22:15:04 +0000799
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000800 // If this is an or of disjoint bitfields, we can codegen this as an add
801 // (for better address arithmetic) if the LHS and RHS of the OR are provably
802 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000803 APInt LHSKnownZero, LHSKnownOne;
804 APInt RHSKnownZero, RHSKnownOne;
805 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanec59b952008-02-27 21:12:32 +0000806 APInt::getAllOnesValue(N.getOperand(0)
807 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000808 LHSKnownZero, LHSKnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +0000809
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000810 if (LHSKnownZero.getBoolValue()) {
811 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanec59b952008-02-27 21:12:32 +0000812 APInt::getAllOnesValue(N.getOperand(1)
813 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000814 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000815 // If all of the bits are known zero on the LHS or RHS, the add won't
816 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +0000817 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000818 Base = N.getOperand(0);
819 Index = N.getOperand(1);
820 return true;
821 }
822 }
823 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000824
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000825 return false;
826}
827
828/// Returns true if the address N can be represented by a base register plus
829/// a signed 16-bit displacement [r+imm], and if it is not better
830/// represented as reg+reg.
Dan Gohman475871a2008-07-27 21:46:04 +0000831bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman73e09142009-01-15 16:29:45 +0000832 SDValue &Base,
833 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000834 // FIXME dl should come from parent load or store, not from address
835 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000836 // If this can be more profitably realized as r+r, fail.
837 if (SelectAddressRegReg(N, Disp, Base, DAG))
838 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000839
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000840 if (N.getOpcode() == ISD::ADD) {
841 short imm = 0;
842 if (isIntS16Immediate(N.getOperand(1), imm)) {
843 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
844 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
845 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
846 } else {
847 Base = N.getOperand(0);
848 }
849 return true; // [r+i]
850 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
851 // Match LOAD (ADD (X, Lo(G))).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000852 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000853 && "Cannot handle constant offsets yet!");
854 Disp = N.getOperand(1).getOperand(0); // The global address.
855 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
856 Disp.getOpcode() == ISD::TargetConstantPool ||
857 Disp.getOpcode() == ISD::TargetJumpTable);
858 Base = N.getOperand(0);
859 return true; // [&g+r]
860 }
861 } else if (N.getOpcode() == ISD::OR) {
862 short imm = 0;
863 if (isIntS16Immediate(N.getOperand(1), imm)) {
864 // If this is an or of disjoint bitfields, we can codegen this as an add
865 // (for better address arithmetic) if the LHS and RHS of the OR are
866 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000867 APInt LHSKnownZero, LHSKnownOne;
868 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000869 APInt::getAllOnesValue(N.getOperand(0)
870 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000871 LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +0000872
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000873 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000874 // If all of the bits are known zero on the LHS or RHS, the add won't
875 // carry.
876 Base = N.getOperand(0);
877 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
878 return true;
879 }
880 }
881 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
882 // Loading from a constant address.
Scott Michelfdc40a02009-02-17 22:15:04 +0000883
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000884 // If this address fits entirely in a 16-bit sext immediate field, codegen
885 // this as "d, 0"
886 short Imm;
887 if (isIntS16Immediate(CN, Imm)) {
888 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
889 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
890 return true;
891 }
Chris Lattnerbc681d62007-02-17 06:44:03 +0000892
893 // Handle 32-bit sext immediates with LIS + addr mode.
894 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000895 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
896 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000897
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000898 // Otherwise, break this down into an LIS + disp.
Chris Lattnerbc681d62007-02-17 06:44:03 +0000899 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000900
Chris Lattnerbc681d62007-02-17 06:44:03 +0000901 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
902 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000903 Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000904 return true;
905 }
906 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000907
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000908 Disp = DAG.getTargetConstant(0, getPointerTy());
909 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
910 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
911 else
912 Base = N;
913 return true; // [r+0]
914}
915
916/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
917/// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000918bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
919 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000920 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000921 // Check to see if we can easily represent this as an [r+r] address. This
922 // will fail if it thinks that the address is more profitably represented as
923 // reg+imm, e.g. where imm = 0.
924 if (SelectAddressRegReg(N, Base, Index, DAG))
925 return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000926
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000927 // If the operand is an addition, always emit this as [r+r], since this is
928 // better (for code size, and execution, as the memop does the add for free)
929 // than emitting an explicit add.
930 if (N.getOpcode() == ISD::ADD) {
931 Base = N.getOperand(0);
932 Index = N.getOperand(1);
933 return true;
934 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000935
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000936 // Otherwise, do it the hard way, using R0 as the base register.
937 Base = DAG.getRegister(PPC::R0, N.getValueType());
938 Index = N;
939 return true;
940}
941
942/// SelectAddressRegImmShift - Returns true if the address N can be
943/// represented by a base register plus a signed 14-bit displacement
944/// [r+imm*4]. Suitable for use by STD and friends.
Dan Gohman475871a2008-07-27 21:46:04 +0000945bool PPCTargetLowering::SelectAddressRegImmShift(SDValue N, SDValue &Disp,
946 SDValue &Base,
Dan Gohman73e09142009-01-15 16:29:45 +0000947 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000948 // FIXME dl should come from the parent load or store, not the address
949 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000950 // If this can be more profitably realized as r+r, fail.
951 if (SelectAddressRegReg(N, Disp, Base, DAG))
952 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000953
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000954 if (N.getOpcode() == ISD::ADD) {
955 short imm = 0;
956 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
957 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
958 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
959 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
960 } else {
961 Base = N.getOperand(0);
962 }
963 return true; // [r+i]
964 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
965 // Match LOAD (ADD (X, Lo(G))).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000966 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000967 && "Cannot handle constant offsets yet!");
968 Disp = N.getOperand(1).getOperand(0); // The global address.
969 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
970 Disp.getOpcode() == ISD::TargetConstantPool ||
971 Disp.getOpcode() == ISD::TargetJumpTable);
972 Base = N.getOperand(0);
973 return true; // [&g+r]
974 }
975 } else if (N.getOpcode() == ISD::OR) {
976 short imm = 0;
977 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
978 // If this is an or of disjoint bitfields, we can codegen this as an add
979 // (for better address arithmetic) if the LHS and RHS of the OR are
980 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000981 APInt LHSKnownZero, LHSKnownOne;
982 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000983 APInt::getAllOnesValue(N.getOperand(0)
984 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000985 LHSKnownZero, LHSKnownOne);
986 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000987 // If all of the bits are known zero on the LHS or RHS, the add won't
988 // carry.
989 Base = N.getOperand(0);
990 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
991 return true;
992 }
993 }
994 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000995 // Loading from a constant address. Verify low two bits are clear.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000996 if ((CN->getZExtValue() & 3) == 0) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000997 // If this address fits entirely in a 14-bit sext immediate field, codegen
998 // this as "d, 0"
999 short Imm;
1000 if (isIntS16Immediate(CN, Imm)) {
1001 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
1002 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
1003 return true;
1004 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001005
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001006 // Fold the low-part of 32-bit absolute addresses into addr mode.
1007 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001008 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
1009 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001010
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001011 // Otherwise, break this down into an LIS + disp.
1012 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001013 Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
1014 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dale Johannesenf5f5dce2009-02-06 19:16:40 +00001015 Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base),0);
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001016 return true;
1017 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001018 }
1019 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001020
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001021 Disp = DAG.getTargetConstant(0, getPointerTy());
1022 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1023 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1024 else
1025 Base = N;
1026 return true; // [r+0]
1027}
1028
1029
1030/// getPreIndexedAddressParts - returns true by value, base pointer and
1031/// offset pointer and addressing mode by reference if the node's address
1032/// can be legally represented as pre-indexed load / store address.
Dan Gohman475871a2008-07-27 21:46:04 +00001033bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1034 SDValue &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001035 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001036 SelectionDAG &DAG) const {
Chris Lattner4eab7142006-11-10 02:08:47 +00001037 // Disabled by default for now.
1038 if (!EnablePPCPreinc) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001039
Dan Gohman475871a2008-07-27 21:46:04 +00001040 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001041 MVT VT;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001042 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1043 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001044 VT = LD->getMemoryVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001046 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner4eab7142006-11-10 02:08:47 +00001047 ST = ST;
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001048 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001049 VT = ST->getMemoryVT();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001050 } else
1051 return false;
1052
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001053 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001054 if (VT.isVector())
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001055 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001056
Chris Lattner0851b4f2006-11-15 19:55:13 +00001057 // TODO: Check reg+reg first.
Scott Michelfdc40a02009-02-17 22:15:04 +00001058
Chris Lattner0851b4f2006-11-15 19:55:13 +00001059 // LDU/STU use reg+imm*4, others use reg+imm.
1060 if (VT != MVT::i64) {
1061 // reg + imm
1062 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
1063 return false;
1064 } else {
1065 // reg + imm * 4.
1066 if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
1067 return false;
1068 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001069
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001070 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001071 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1072 // sext i32 to i64 when addr mode is r+i.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001073 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001074 LD->getExtensionType() == ISD::SEXTLOAD &&
1075 isa<ConstantSDNode>(Offset))
1076 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001077 }
1078
Chris Lattner4eab7142006-11-10 02:08:47 +00001079 AM = ISD::PRE_INC;
1080 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001081}
1082
1083//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001084// LowerOperation implementation
1085//===----------------------------------------------------------------------===//
1086
Scott Michelfdc40a02009-02-17 22:15:04 +00001087SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001088 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001089 MVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001090 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +00001091 Constant *C = CP->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +00001092 SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
1093 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001094 // FIXME there isn't really any debug info here
1095 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00001096
1097 const TargetMachine &TM = DAG.getTarget();
Scott Michelfdc40a02009-02-17 22:15:04 +00001098
Dale Johannesende064702009-02-06 21:50:26 +00001099 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, CPI, Zero);
1100 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, CPI, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001101
Chris Lattner1a635d62006-04-14 06:01:58 +00001102 // If this is a non-darwin platform, we don't support non-static relo models
1103 // yet.
1104 if (TM.getRelocationModel() == Reloc::Static ||
1105 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1106 // Generate non-pic code that has direct accesses to the constant pool.
1107 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesende064702009-02-06 21:50:26 +00001108 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001109 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001110
Chris Lattner35d86fe2006-07-26 21:12:04 +00001111 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001112 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesende064702009-02-06 21:50:26 +00001113 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001114 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001115 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001116 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001117
Dale Johannesende064702009-02-06 21:50:26 +00001118 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001119 return Lo;
1120}
1121
Dan Gohman475871a2008-07-27 21:46:04 +00001122SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001123 MVT PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001124 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001125 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1126 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001127 // FIXME there isn't really any debug loc here
1128 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001129
Nate Begeman37efe672006-04-22 18:53:45 +00001130 const TargetMachine &TM = DAG.getTarget();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001131
Dale Johannesende064702009-02-06 21:50:26 +00001132 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, JTI, Zero);
1133 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, JTI, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001134
Nate Begeman37efe672006-04-22 18:53:45 +00001135 // If this is a non-darwin platform, we don't support non-static relo models
1136 // yet.
1137 if (TM.getRelocationModel() == Reloc::Static ||
1138 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1139 // Generate non-pic code that has direct accesses to the constant pool.
1140 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesende064702009-02-06 21:50:26 +00001141 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001143
Chris Lattner35d86fe2006-07-26 21:12:04 +00001144 if (TM.getRelocationModel() == Reloc::PIC_) {
Nate Begeman37efe672006-04-22 18:53:45 +00001145 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesende064702009-02-06 21:50:26 +00001146 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001147 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001148 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Nate Begeman37efe672006-04-22 18:53:45 +00001149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001150
Dale Johannesende064702009-02-06 21:50:26 +00001151 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001152 return Lo;
1153}
1154
Scott Michelfdc40a02009-02-17 22:15:04 +00001155SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001156 SelectionDAG &DAG) {
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001157 assert(0 && "TLS not implemented for PPC.");
Dan Gohman475871a2008-07-27 21:46:04 +00001158 return SDValue(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001159}
1160
Scott Michelfdc40a02009-02-17 22:15:04 +00001161SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
Evan Chengee5c2b82009-01-16 22:57:32 +00001162 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001163 MVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001164 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
1165 GlobalValue *GV = GSDN->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001166 SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00001167 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001168 // FIXME there isn't really any debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001169 DebugLoc dl = GSDN->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001170
Chris Lattner1a635d62006-04-14 06:01:58 +00001171 const TargetMachine &TM = DAG.getTarget();
1172
Dale Johannesen33c960f2009-02-04 20:06:27 +00001173 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, GA, Zero);
1174 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, GA, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001175
Chris Lattner1a635d62006-04-14 06:01:58 +00001176 // If this is a non-darwin platform, we don't support non-static relo models
1177 // yet.
1178 if (TM.getRelocationModel() == Reloc::Static ||
1179 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1180 // Generate non-pic code that has direct accesses to globals.
1181 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesen33c960f2009-02-04 20:06:27 +00001182 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001183 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001184
Chris Lattner35d86fe2006-07-26 21:12:04 +00001185 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001186 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesen33c960f2009-02-04 20:06:27 +00001187 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001188 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001189 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001190 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001191
Dale Johannesen33c960f2009-02-04 20:06:27 +00001192 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00001193
Chris Lattner57fc62c2006-12-11 23:22:45 +00001194 if (!TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV))
Chris Lattner1a635d62006-04-14 06:01:58 +00001195 return Lo;
Scott Michelfdc40a02009-02-17 22:15:04 +00001196
Chris Lattner1a635d62006-04-14 06:01:58 +00001197 // If the global is weak or external, we have to go through the lazy
1198 // resolution stub.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001199 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Lo, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00001200}
1201
Dan Gohman475871a2008-07-27 21:46:04 +00001202SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001203 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001204 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001205
Chris Lattner1a635d62006-04-14 06:01:58 +00001206 // If we're comparing for equality to zero, expose the fact that this is
1207 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1208 // fold the new nodes.
1209 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1210 if (C->isNullValue() && CC == ISD::SETEQ) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001211 MVT VT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001212 SDValue Zext = Op.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00001213 if (VT.bitsLT(MVT::i32)) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001214 VT = MVT::i32;
Dale Johannesenf5d97892009-02-04 01:48:28 +00001215 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001216 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001217 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001218 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1219 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00001220 DAG.getConstant(Log2b, MVT::i32));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001221 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner1a635d62006-04-14 06:01:58 +00001222 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001223 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner1a635d62006-04-14 06:01:58 +00001224 // optimized. FIXME: revisit this when we can custom lower all setcc
1225 // optimizations.
1226 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman475871a2008-07-27 21:46:04 +00001227 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001228 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001229
Chris Lattner1a635d62006-04-14 06:01:58 +00001230 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001231 // by xor'ing the rhs with the lhs, which is faster than setting a
1232 // condition register, reading it back out, and masking the correct bit. The
1233 // normal approach here uses sub to do this instead of xor. Using xor exposes
1234 // the result to other bit-twiddling opportunities.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001235 MVT LHSVT = Op.getOperand(0).getValueType();
1236 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1237 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001238 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001239 Op.getOperand(1));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001240 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner1a635d62006-04-14 06:01:58 +00001241 }
Dan Gohman475871a2008-07-27 21:46:04 +00001242 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001243}
1244
Dan Gohman475871a2008-07-27 21:46:04 +00001245SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001246 int VarArgsFrameIndex,
1247 int VarArgsStackOffset,
1248 unsigned VarArgsNumGPR,
1249 unsigned VarArgsNumFPR,
1250 const PPCSubtarget &Subtarget) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001251
Nicolas Geoffray01119992007-04-03 13:59:52 +00001252 assert(0 && "VAARG in ELF32 ABI not implemented yet!");
Dan Gohman475871a2008-07-27 21:46:04 +00001253 return SDValue(); // Not reached
Nicolas Geoffray01119992007-04-03 13:59:52 +00001254}
1255
Bill Wendling77959322008-09-17 00:30:57 +00001256SDValue PPCTargetLowering::LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
1257 SDValue Chain = Op.getOperand(0);
1258 SDValue Trmp = Op.getOperand(1); // trampoline
1259 SDValue FPtr = Op.getOperand(2); // nested function
1260 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001261 DebugLoc dl = Op.getDebugLoc();
Bill Wendling77959322008-09-17 00:30:57 +00001262
1263 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1264 bool isPPC64 = (PtrVT == MVT::i64);
1265 const Type *IntPtrTy =
1266 DAG.getTargetLoweringInfo().getTargetData()->getIntPtrType();
1267
Scott Michelfdc40a02009-02-17 22:15:04 +00001268 TargetLowering::ArgListTy Args;
Bill Wendling77959322008-09-17 00:30:57 +00001269 TargetLowering::ArgListEntry Entry;
1270
1271 Entry.Ty = IntPtrTy;
1272 Entry.Node = Trmp; Args.push_back(Entry);
1273
1274 // TrampSize == (isPPC64 ? 48 : 40);
1275 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
1276 isPPC64 ? MVT::i64 : MVT::i32);
1277 Args.push_back(Entry);
1278
1279 Entry.Node = FPtr; Args.push_back(Entry);
1280 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Bill Wendling77959322008-09-17 00:30:57 +00001282 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
1283 std::pair<SDValue, SDValue> CallResult =
1284 LowerCallTo(Chain, Op.getValueType().getTypeForMVT(), false, false,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00001285 false, false, 0, CallingConv::C, false,
Bill Wendling77959322008-09-17 00:30:57 +00001286 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001287 Args, DAG, dl);
Bill Wendling77959322008-09-17 00:30:57 +00001288
1289 SDValue Ops[] =
1290 { CallResult.first, CallResult.second };
1291
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001292 return DAG.getMergeValues(Ops, 2, dl);
Bill Wendling77959322008-09-17 00:30:57 +00001293}
1294
Dan Gohman475871a2008-07-27 21:46:04 +00001295SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bill Wendling77959322008-09-17 00:30:57 +00001296 int VarArgsFrameIndex,
1297 int VarArgsStackOffset,
1298 unsigned VarArgsNumGPR,
1299 unsigned VarArgsNumFPR,
1300 const PPCSubtarget &Subtarget) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001301 DebugLoc dl = Op.getDebugLoc();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001302
1303 if (Subtarget.isMachoABI()) {
1304 // vastart just stores the address of the VarArgsFrameIndex slot into the
1305 // memory location argument.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001306 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001307 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001308 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001309 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001310 }
1311
1312 // For ELF 32 ABI we follow the layout of the va_list struct.
1313 // We suppose the given va_list is already allocated.
1314 //
1315 // typedef struct {
1316 // char gpr; /* index into the array of 8 GPRs
1317 // * stored in the register save area
1318 // * gpr=0 corresponds to r3,
1319 // * gpr=1 to r4, etc.
1320 // */
1321 // char fpr; /* index into the array of 8 FPRs
1322 // * stored in the register save area
1323 // * fpr=0 corresponds to f1,
1324 // * fpr=1 to f2, etc.
1325 // */
1326 // char *overflow_arg_area;
1327 // /* location on stack that holds
1328 // * the next overflow argument
1329 // */
1330 // char *reg_save_area;
1331 // /* where r3:r10 and f1:f8 (if saved)
1332 // * are stored
1333 // */
1334 // } va_list[1];
1335
1336
Tilmann Schellerffd02002009-07-03 06:45:56 +00001337 SDValue ArgGPR = DAG.getConstant(VarArgsNumGPR, MVT::i32);
1338 SDValue ArgFPR = DAG.getConstant(VarArgsNumFPR, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001339
Nicolas Geoffray01119992007-04-03 13:59:52 +00001340
Duncan Sands83ec4b62008-06-06 12:08:01 +00001341 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00001342
Dan Gohman475871a2008-07-27 21:46:04 +00001343 SDValue StackOffsetFI = DAG.getFrameIndex(VarArgsStackOffset, PtrVT);
1344 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001345
Duncan Sands83ec4b62008-06-06 12:08:01 +00001346 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman475871a2008-07-27 21:46:04 +00001347 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001348
Duncan Sands83ec4b62008-06-06 12:08:01 +00001349 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001350 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001351
1352 uint64_t FPROffset = 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001354
Dan Gohman69de1932008-02-06 22:27:42 +00001355 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001356
Nicolas Geoffray01119992007-04-03 13:59:52 +00001357 // Store first byte : number of int regs
Tilmann Schellerffd02002009-07-03 06:45:56 +00001358 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
1359 Op.getOperand(1), SV, 0, MVT::i8);
Dan Gohman69de1932008-02-06 22:27:42 +00001360 uint64_t nextOffset = FPROffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001361 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray01119992007-04-03 13:59:52 +00001362 ConstFPROffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001363
Nicolas Geoffray01119992007-04-03 13:59:52 +00001364 // Store second byte : number of float regs
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue secondStore =
Tilmann Schellerffd02002009-07-03 06:45:56 +00001366 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, SV, nextOffset, MVT::i8);
Dan Gohman69de1932008-02-06 22:27:42 +00001367 nextOffset += StackOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001368 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001369
Nicolas Geoffray01119992007-04-03 13:59:52 +00001370 // Store second word : arguments given on stack
Dan Gohman475871a2008-07-27 21:46:04 +00001371 SDValue thirdStore =
Dale Johannesen33c960f2009-02-04 20:06:27 +00001372 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, SV, nextOffset);
Dan Gohman69de1932008-02-06 22:27:42 +00001373 nextOffset += FrameOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001374 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001375
1376 // Store third word : arguments given in registers
Dale Johannesen33c960f2009-02-04 20:06:27 +00001377 return DAG.getStore(thirdStore, dl, FR, nextPtr, SV, nextOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001378
Chris Lattner1a635d62006-04-14 06:01:58 +00001379}
1380
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001381#include "PPCGenCallingConv.inc"
1382
Tilmann Schellerffd02002009-07-03 06:45:56 +00001383static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1384 CCValAssign::LocInfo &LocInfo,
1385 ISD::ArgFlagsTy &ArgFlags,
1386 CCState &State) {
1387 return true;
1388}
1389
1390static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1391 MVT &LocVT,
1392 CCValAssign::LocInfo &LocInfo,
1393 ISD::ArgFlagsTy &ArgFlags,
1394 CCState &State) {
1395 static const unsigned ArgRegs[] = {
1396 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1397 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1398 };
1399 const unsigned NumArgRegs = array_lengthof(ArgRegs);
1400
1401 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1402
1403 // Skip one register if the first unallocated register has an even register
1404 // number and there are still argument registers available which have not been
1405 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1406 // need to skip a register if RegNum is odd.
1407 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1408 State.AllocateReg(ArgRegs[RegNum]);
1409 }
1410
1411 // Always return false here, as this function only makes sure that the first
1412 // unallocated register has an odd register number and does not actually
1413 // allocate a register for the current argument.
1414 return false;
1415}
1416
1417static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1418 MVT &LocVT,
1419 CCValAssign::LocInfo &LocInfo,
1420 ISD::ArgFlagsTy &ArgFlags,
1421 CCState &State) {
1422 static const unsigned ArgRegs[] = {
1423 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1424 PPC::F8
1425 };
1426
1427 const unsigned NumArgRegs = array_lengthof(ArgRegs);
1428
1429 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1430
1431 // If there is only one Floating-point register left we need to put both f64
1432 // values of a split ppc_fp128 value on the stack.
1433 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1434 State.AllocateReg(ArgRegs[RegNum]);
1435 }
1436
1437 // Always return false here, as this function only makes sure that the two f64
1438 // values a ppc_fp128 value is split into are both passed in registers or both
1439 // passed on the stack and does not actually allocate a register for the
1440 // current argument.
1441 return false;
1442}
1443
Chris Lattner9f0bc652007-02-25 05:34:32 +00001444/// GetFPR - Get the set of FP registers that should be allocated for arguments,
1445/// depending on which subtarget is selected.
1446static const unsigned *GetFPR(const PPCSubtarget &Subtarget) {
1447 if (Subtarget.isMachoABI()) {
1448 static const unsigned FPR[] = {
1449 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1450 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1451 };
1452 return FPR;
1453 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001454
1455
Chris Lattner9f0bc652007-02-25 05:34:32 +00001456 static const unsigned FPR[] = {
1457 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001458 PPC::F8
Chris Lattner9f0bc652007-02-25 05:34:32 +00001459 };
1460 return FPR;
1461}
1462
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001463/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1464/// the stack.
Dan Gohman095cc292008-09-13 01:54:27 +00001465static unsigned CalculateStackSlotSize(SDValue Arg, ISD::ArgFlagsTy Flags,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001466 unsigned PtrByteSize) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001467 MVT ArgVT = Arg.getValueType();
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001468 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001469 if (Flags.isByVal())
1470 ArgSize = Flags.getByValSize();
1471 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1472
1473 return ArgSize;
1474}
1475
Dan Gohman475871a2008-07-27 21:46:04 +00001476SDValue
Tilmann Schellerffd02002009-07-03 06:45:56 +00001477PPCTargetLowering::LowerFORMAL_ARGUMENTS_SVR4(SDValue Op,
1478 SelectionDAG &DAG,
1479 int &VarArgsFrameIndex,
1480 int &VarArgsStackOffset,
1481 unsigned &VarArgsNumGPR,
1482 unsigned &VarArgsNumFPR,
1483 const PPCSubtarget &Subtarget) {
1484 // SVR4 ABI Stack Frame Layout:
1485 // +-----------------------------------+
1486 // +--> | Back chain |
1487 // | +-----------------------------------+
1488 // | | Floating-point register save area |
1489 // | +-----------------------------------+
1490 // | | General register save area |
1491 // | +-----------------------------------+
1492 // | | CR save word |
1493 // | +-----------------------------------+
1494 // | | VRSAVE save word |
1495 // | +-----------------------------------+
1496 // | | Alignment padding |
1497 // | +-----------------------------------+
1498 // | | Vector register save area |
1499 // | +-----------------------------------+
1500 // | | Local variable space |
1501 // | +-----------------------------------+
1502 // | | Parameter list area |
1503 // | +-----------------------------------+
1504 // | | LR save word |
1505 // | +-----------------------------------+
1506 // SP--> +--- | Back chain |
1507 // +-----------------------------------+
1508 //
1509 // Specifications:
1510 // System V Application Binary Interface PowerPC Processor Supplement
1511 // AltiVec Technology Programming Interface Manual
1512
1513 MachineFunction &MF = DAG.getMachineFunction();
1514 MachineFrameInfo *MFI = MF.getFrameInfo();
1515 SmallVector<SDValue, 8> ArgValues;
1516 SDValue Root = Op.getOperand(0);
1517 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
1518 DebugLoc dl = Op.getDebugLoc();
1519
1520 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1521 // Potential tail calls could cause overwriting of argument stack slots.
1522 unsigned CC = MF.getFunction()->getCallingConv();
1523 bool isImmutable = !(PerformTailCallOpt && (CC==CallingConv::Fast));
1524 unsigned PtrByteSize = 4;
1525
1526 // Assign locations to all of the incoming arguments.
1527 SmallVector<CCValAssign, 16> ArgLocs;
1528 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1529
1530 // Reserve space for the linkage area on the stack.
1531 CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
1532
1533 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_PPC_SVR4);
1534
1535 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1536 CCValAssign &VA = ArgLocs[i];
1537
1538 // Arguments stored in registers.
1539 if (VA.isRegLoc()) {
1540 TargetRegisterClass *RC;
1541 MVT ValVT = VA.getValVT();
1542
1543 switch (ValVT.getSimpleVT()) {
1544 default:
1545 assert(0 && "ValVT not supported by FORMAL_ARGUMENTS Lowering");
1546 case MVT::i32:
1547 RC = PPC::GPRCRegisterClass;
1548 break;
1549 case MVT::f32:
1550 RC = PPC::F4RCRegisterClass;
1551 break;
1552 case MVT::f64:
1553 RC = PPC::F8RCRegisterClass;
1554 break;
1555 case MVT::v16i8:
1556 case MVT::v8i16:
1557 case MVT::v4i32:
1558 case MVT::v4f32:
1559 RC = PPC::VRRCRegisterClass;
1560 break;
1561 }
1562
1563 // Transform the arguments stored in physical registers into virtual ones.
1564 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1565 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, ValVT);
1566
1567 ArgValues.push_back(ArgValue);
1568 } else {
1569 // Argument stored in memory.
1570 assert(VA.isMemLoc());
1571
1572 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
1573 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1574 isImmutable);
1575
1576 // Create load nodes to retrieve arguments from the stack.
1577 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1578 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
1579 }
1580 }
1581
1582 // Assign locations to all of the incoming aggregate by value arguments.
1583 // Aggregates passed by value are stored in the local variable space of the
1584 // caller's stack frame, right above the parameter list area.
1585 SmallVector<CCValAssign, 16> ByValArgLocs;
1586 CCState CCByValInfo(CC, isVarArg, getTargetMachine(), ByValArgLocs);
1587
1588 // Reserve stack space for the allocations in CCInfo.
1589 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
1590
1591 CCByValInfo.AnalyzeFormalArguments(Op.getNode(), CC_PPC_SVR4_ByVal);
1592
1593 // Area that is at least reserved in the caller of this function.
1594 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
1595
1596 // Set the size that is at least reserved in caller of this function. Tail
1597 // call optimized function's reserved stack space needs to be aligned so that
1598 // taking the difference between two stack areas will result in an aligned
1599 // stack.
1600 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1601
1602 MinReservedArea =
1603 std::max(MinReservedArea,
1604 PPCFrameInfo::getMinCallFrameSize(false, false));
1605
1606 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
1607 getStackAlignment();
1608 unsigned AlignMask = TargetAlign-1;
1609 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
1610
1611 FI->setMinReservedArea(MinReservedArea);
1612
1613 SmallVector<SDValue, 8> MemOps;
1614
1615 // If the function takes variable number of arguments, make a frame index for
1616 // the start of the first vararg value... for expansion of llvm.va_start.
1617 if (isVarArg) {
1618 static const unsigned GPArgRegs[] = {
1619 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1620 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1621 };
1622 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
1623
1624 static const unsigned FPArgRegs[] = {
1625 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1626 PPC::F8
1627 };
1628 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
1629
1630 VarArgsNumGPR = CCInfo.getFirstUnallocated(GPArgRegs, NumGPArgRegs);
1631 VarArgsNumFPR = CCInfo.getFirstUnallocated(FPArgRegs, NumFPArgRegs);
1632
1633 // Make room for NumGPArgRegs and NumFPArgRegs.
1634 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
1635 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
1636
1637 VarArgsStackOffset = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
1638 CCInfo.getNextStackOffset());
1639
1640 VarArgsFrameIndex = MFI->CreateStackObject(Depth, 8);
1641 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1642
1643 // The fixed integer arguments of a variadic function are
1644 // stored to the VarArgsFrameIndex on the stack.
1645 unsigned GPRIndex = 0;
1646 for (; GPRIndex != VarArgsNumGPR; ++GPRIndex) {
1647 SDValue Val = DAG.getRegister(GPArgRegs[GPRIndex], PtrVT);
1648 SDValue Store = DAG.getStore(Root, dl, Val, FIN, NULL, 0);
1649 MemOps.push_back(Store);
1650 // Increment the address by four for the next argument to store
1651 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
1652 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1653 }
1654
1655 // If this function is vararg, store any remaining integer argument regs
1656 // to their spots on the stack so that they may be loaded by deferencing the
1657 // result of va_next.
1658 for (; GPRIndex != NumGPArgRegs; ++GPRIndex) {
1659 unsigned VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
1660
1661 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT);
1662 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1663 MemOps.push_back(Store);
1664 // Increment the address by four for the next argument to store
1665 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
1666 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1667 }
1668
1669 // FIXME SVR4: We only need to save FP argument registers if CR bit 6 is
1670 // set.
1671
1672 // The double arguments are stored to the VarArgsFrameIndex
1673 // on the stack.
1674 unsigned FPRIndex = 0;
1675 for (FPRIndex = 0; FPRIndex != VarArgsNumFPR; ++FPRIndex) {
1676 SDValue Val = DAG.getRegister(FPArgRegs[FPRIndex], MVT::f64);
1677 SDValue Store = DAG.getStore(Root, dl, Val, FIN, NULL, 0);
1678 MemOps.push_back(Store);
1679 // Increment the address by eight for the next argument to store
1680 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
1681 PtrVT);
1682 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1683 }
1684
1685 for (; FPRIndex != NumFPArgRegs; ++FPRIndex) {
1686 unsigned VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
1687
1688 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::f64);
1689 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1690 MemOps.push_back(Store);
1691 // Increment the address by eight for the next argument to store
1692 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
1693 PtrVT);
1694 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1695 }
1696 }
1697
1698 if (!MemOps.empty())
1699 Root = DAG.getNode(ISD::TokenFactor, dl,
1700 MVT::Other, &MemOps[0], MemOps.size());
1701
1702
1703 ArgValues.push_back(Root);
1704
1705 // Return the new list of results.
1706 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
1707 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
1708}
1709
1710SDValue
Scott Michelfdc40a02009-02-17 22:15:04 +00001711PPCTargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001712 SelectionDAG &DAG,
1713 int &VarArgsFrameIndex,
1714 int &VarArgsStackOffset,
1715 unsigned &VarArgsNumGPR,
1716 unsigned &VarArgsNumFPR,
1717 const PPCSubtarget &Subtarget) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001718 // TODO: add description of PPC stack frame format, or at least some docs.
1719 //
1720 MachineFunction &MF = DAG.getMachineFunction();
1721 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001722 SmallVector<SDValue, 8> ArgValues;
1723 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001724 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001725 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001726
Duncan Sands83ec4b62008-06-06 12:08:01 +00001727 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00001728 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattner9f0bc652007-02-25 05:34:32 +00001729 bool isMachoABI = Subtarget.isMachoABI();
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001730 bool isELF32_ABI = Subtarget.isELF32_ABI();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001731 // Potential tail calls could cause overwriting of argument stack slots.
1732 unsigned CC = MF.getFunction()->getCallingConv();
1733 bool isImmutable = !(PerformTailCallOpt && (CC==CallingConv::Fast));
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001734 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00001735
Chris Lattner9f0bc652007-02-25 05:34:32 +00001736 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001737 // Area that is at least reserved in caller of this function.
1738 unsigned MinReservedArea = ArgOffset;
1739
Chris Lattnerc91a4752006-06-26 22:48:35 +00001740 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001741 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1742 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1743 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001744 static const unsigned GPR_64[] = { // 64-bit registers.
1745 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1746 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1747 };
Scott Michelfdc40a02009-02-17 22:15:04 +00001748
Chris Lattner9f0bc652007-02-25 05:34:32 +00001749 static const unsigned *FPR = GetFPR(Subtarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00001750
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001751 static const unsigned VR[] = {
1752 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1753 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1754 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001755
Owen Anderson718cb662007-09-07 04:06:50 +00001756 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001757 const unsigned Num_FPR_Regs = isMachoABI ? 13 : 8;
Owen Anderson718cb662007-09-07 04:06:50 +00001758 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00001759
1760 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00001761
Chris Lattnerc91a4752006-06-26 22:48:35 +00001762 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelfdc40a02009-02-17 22:15:04 +00001763
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001764 // In 32-bit non-varargs functions, the stack space for vectors is after the
1765 // stack space for non-vectors. We do not use this space unless we have
1766 // too many vectors to fit in registers, something that only occurs in
Scott Michelfdc40a02009-02-17 22:15:04 +00001767 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001768 // that out...for the pathological case, compute VecArgOffset as the
1769 // start of the vector parameter area. Computing VecArgOffset is the
1770 // entire point of the following loop.
1771 // Altivec is not mentioned in the ppc32 Elf Supplement, so I'm not trying
1772 // to handle Elf here.
1773 unsigned VecArgOffset = ArgOffset;
1774 if (!isVarArg && !isPPC64) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001775 for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues()-1; ArgNo != e;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001776 ++ArgNo) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001777 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
1778 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001779 ISD::ArgFlagsTy Flags =
1780 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001781
Duncan Sands276dcbd2008-03-21 09:14:45 +00001782 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001783 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001784 ObjSize = Flags.getByValSize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001785 unsigned ArgSize =
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001786 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1787 VecArgOffset += ArgSize;
1788 continue;
1789 }
1790
Duncan Sands83ec4b62008-06-06 12:08:01 +00001791 switch(ObjectVT.getSimpleVT()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001792 default: assert(0 && "Unhandled argument type!");
1793 case MVT::i32:
1794 case MVT::f32:
1795 VecArgOffset += isPPC64 ? 8 : 4;
1796 break;
1797 case MVT::i64: // PPC64
1798 case MVT::f64:
1799 VecArgOffset += 8;
1800 break;
1801 case MVT::v4f32:
1802 case MVT::v4i32:
1803 case MVT::v8i16:
1804 case MVT::v16i8:
1805 // Nothing to do, we're only looking at Nonvector args here.
1806 break;
1807 }
1808 }
1809 }
1810 // We've found where the vector parameter area in memory is. Skip the
1811 // first 12 parameters; these don't use that memory.
1812 VecArgOffset = ((VecArgOffset+15)/16)*16;
1813 VecArgOffset += 12*16;
1814
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001815 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00001816 // entry to a function on PPC, the arguments start after the linkage area,
1817 // although the first ones are often in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00001818 //
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00001819 // In the ELF 32 ABI, GPRs and stack are double word align: an argument
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001820 // represented with two words (long long or double) must be copied to an
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001821 // even GPR_idx value or to an even ArgOffset value.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001822
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001824 unsigned nAltivecParamsAtEnd = 0;
Gabor Greif93c53e52008-08-31 15:37:04 +00001825 for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues() - 1;
1826 ArgNo != e; ++ArgNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001827 SDValue ArgVal;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001828 bool needsLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001829 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
1830 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey619965d2006-11-29 13:37:09 +00001831 unsigned ArgSize = ObjSize;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001832 ISD::ArgFlagsTy Flags =
1833 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags();
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001834 // See if next argument requires stack alignment in ELF
Scott Michelfdc40a02009-02-17 22:15:04 +00001835 bool Align = Flags.isSplit();
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001836
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001837 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001838
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001839 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
1840 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
1841 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
1842 if (isVarArg || isPPC64) {
1843 MinReservedArea = ((MinReservedArea+15)/16)*16;
1844 MinReservedArea += CalculateStackSlotSize(Op.getValue(ArgNo),
Dan Gohman095cc292008-09-13 01:54:27 +00001845 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001846 PtrByteSize);
1847 } else nAltivecParamsAtEnd++;
1848 } else
1849 // Calculate min reserved area.
1850 MinReservedArea += CalculateStackSlotSize(Op.getValue(ArgNo),
Dan Gohman095cc292008-09-13 01:54:27 +00001851 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001852 PtrByteSize);
1853
Dale Johannesen8419dd62008-03-07 20:27:40 +00001854 // FIXME alignment for ELF may not be right
1855 // FIXME the codegen can be much improved in some cases.
1856 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001857 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00001858 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001859 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00001860 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001861 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001862 if (Align && isELF32_ABI) GPR_idx += (GPR_idx % 2);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001863 // Objects of size 1 and 2 are right justified, everything else is
1864 // left justified. This means the memory address is adjusted forwards.
1865 if (ObjSize==1 || ObjSize==2) {
1866 CurArgOffset = CurArgOffset + (4 - ObjSize);
1867 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001868 // The value of the object is its address.
1869 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +00001870 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001871 ArgValues.push_back(FIN);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001872 if (ObjSize==1 || ObjSize==2) {
1873 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001874 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dale Johannesen39355f92009-02-04 02:34:38 +00001875 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001876 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Dale Johannesen7f96f392008-03-08 01:41:42 +00001877 NULL, 0, ObjSize==1 ? MVT::i8 : MVT::i16 );
1878 MemOps.push_back(Store);
1879 ++GPR_idx;
1880 if (isMachoABI) ArgOffset += PtrByteSize;
1881 } else {
1882 ArgOffset += PtrByteSize;
1883 }
1884 continue;
1885 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001886 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
1887 // Store whatever pieces of the object are in registers
1888 // to memory. ArgVal will be address of the beginning of
1889 // the object.
1890 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001891 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001892 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00001894 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT);
1895 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001896 MemOps.push_back(Store);
1897 ++GPR_idx;
1898 if (isMachoABI) ArgOffset += PtrByteSize;
1899 } else {
1900 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
1901 break;
1902 }
1903 }
1904 continue;
1905 }
1906
Duncan Sands83ec4b62008-06-06 12:08:01 +00001907 switch (ObjectVT.getSimpleVT()) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001908 default: assert(0 && "Unhandled argument type!");
1909 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001910 if (!isPPC64) {
1911 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001912 if (Align && isELF32_ABI) GPR_idx += (GPR_idx % 2);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001913
1914 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001915 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dale Johannesen39355f92009-02-04 02:34:38 +00001916 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001917 ++GPR_idx;
1918 } else {
1919 needsLoad = true;
1920 ArgSize = PtrByteSize;
1921 }
1922 // Stack align in ELF
Scott Michelfdc40a02009-02-17 22:15:04 +00001923 if (needsLoad && Align && isELF32_ABI)
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001924 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
1925 // All int arguments reserve stack space in Macho ABI.
1926 if (isMachoABI || needsLoad) ArgOffset += PtrByteSize;
1927 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001928 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001929 // FALLTHROUGH
Chris Lattner9f0bc652007-02-25 05:34:32 +00001930 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00001931 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001932 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Dale Johannesen39355f92009-02-04 02:34:38 +00001933 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001934
1935 if (ObjectVT == MVT::i32) {
1936 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
1937 // value to MVT::i64 and then truncate to the correct register size.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001938 if (Flags.isSExt())
Dale Johannesen39355f92009-02-04 02:34:38 +00001939 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001940 DAG.getValueType(ObjectVT));
Duncan Sands276dcbd2008-03-21 09:14:45 +00001941 else if (Flags.isZExt())
Dale Johannesen39355f92009-02-04 02:34:38 +00001942 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001943 DAG.getValueType(ObjectVT));
1944
Dale Johannesen39355f92009-02-04 02:34:38 +00001945 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001946 }
1947
Chris Lattnerc91a4752006-06-26 22:48:35 +00001948 ++GPR_idx;
1949 } else {
1950 needsLoad = true;
Evan Cheng982a0592008-07-24 08:17:07 +00001951 ArgSize = PtrByteSize;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001952 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00001953 // All int arguments reserve stack space in Macho ABI.
1954 if (isMachoABI || needsLoad) ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001955 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001956
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001957 case MVT::f32:
1958 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001959 // Every 4 bytes of argument space consumes one of the GPRs available for
1960 // argument passing.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001961 if (GPR_idx != Num_GPR_Regs && isMachoABI) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001962 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001963 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001964 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001965 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001966 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001967 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001968
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001969 if (ObjectVT == MVT::f32)
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001970 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001971 else
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001972 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
1973
Dale Johannesen39355f92009-02-04 02:34:38 +00001974 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001975 ++FPR_idx;
1976 } else {
1977 needsLoad = true;
1978 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001979
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001980 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00001981 if (needsLoad && Align && isELF32_ABI)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001982 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
Chris Lattner9f0bc652007-02-25 05:34:32 +00001983 // All FP arguments reserve stack space in Macho ABI.
1984 if (isMachoABI || needsLoad) ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001985 break;
1986 case MVT::v4f32:
1987 case MVT::v4i32:
1988 case MVT::v8i16:
1989 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00001990 // Note that vector arguments in registers don't reserve stack space,
1991 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001992 if (VR_idx != Num_VR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001993 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dale Johannesen39355f92009-02-04 02:34:38 +00001994 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00001995 if (isVarArg) {
1996 while ((ArgOffset % 16) != 0) {
1997 ArgOffset += PtrByteSize;
1998 if (GPR_idx != Num_GPR_Regs)
1999 GPR_idx++;
2000 }
2001 ArgOffset += 16;
2002 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs);
2003 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002004 ++VR_idx;
2005 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002006 if (!isVarArg && !isPPC64) {
2007 // Vectors go after all the nonvectors.
2008 CurArgOffset = VecArgOffset;
2009 VecArgOffset += 16;
2010 } else {
2011 // Vectors are aligned.
2012 ArgOffset = ((ArgOffset+15)/16)*16;
2013 CurArgOffset = ArgOffset;
2014 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00002015 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002016 needsLoad = true;
2017 }
2018 break;
2019 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002020
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002021 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002022 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002023 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002024 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002025 CurArgOffset + (ArgSize - ObjSize),
2026 isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002028 ArgVal = DAG.getLoad(ObjectVT, dl, Root, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002029 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002030
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002031 ArgValues.push_back(ArgVal);
2032 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002033
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002034 // Set the size that is at least reserved in caller of this function. Tail
2035 // call optimized function's reserved stack space needs to be aligned so that
2036 // taking the difference between two stack areas will result in an aligned
2037 // stack.
2038 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2039 // Add the Altivec parameters at the end, if needed.
2040 if (nAltivecParamsAtEnd) {
2041 MinReservedArea = ((MinReservedArea+15)/16)*16;
2042 MinReservedArea += 16*nAltivecParamsAtEnd;
2043 }
2044 MinReservedArea =
2045 std::max(MinReservedArea,
2046 PPCFrameInfo::getMinCallFrameSize(isPPC64, isMachoABI));
2047 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
2048 getStackAlignment();
2049 unsigned AlignMask = TargetAlign-1;
2050 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2051 FI->setMinReservedArea(MinReservedArea);
2052
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002053 // If the function takes variable number of arguments, make a frame index for
2054 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002055 if (isVarArg) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002056
Nicolas Geoffray01119992007-04-03 13:59:52 +00002057 int depth;
2058 if (isELF32_ABI) {
2059 VarArgsNumGPR = GPR_idx;
2060 VarArgsNumFPR = FPR_idx;
Scott Michelfdc40a02009-02-17 22:15:04 +00002061
Nicolas Geoffray01119992007-04-03 13:59:52 +00002062 // Make room for Num_GPR_Regs, Num_FPR_Regs and for a possible frame
2063 // pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002064 depth = -(Num_GPR_Regs * PtrVT.getSizeInBits()/8 +
2065 Num_FPR_Regs * MVT(MVT::f64).getSizeInBits()/8 +
2066 PtrVT.getSizeInBits()/8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002067
Duncan Sands83ec4b62008-06-06 12:08:01 +00002068 VarArgsStackOffset = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Nicolas Geoffray01119992007-04-03 13:59:52 +00002069 ArgOffset);
2070
2071 }
2072 else
2073 depth = ArgOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00002074
Duncan Sands83ec4b62008-06-06 12:08:01 +00002075 VarArgsFrameIndex = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Nicolas Geoffray01119992007-04-03 13:59:52 +00002076 depth);
Dan Gohman475871a2008-07-27 21:46:04 +00002077 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002078
Nicolas Geoffray01119992007-04-03 13:59:52 +00002079 // In ELF 32 ABI, the fixed integer arguments of a variadic function are
2080 // stored to the VarArgsFrameIndex on the stack.
2081 if (isELF32_ABI) {
2082 for (GPR_idx = 0; GPR_idx != VarArgsNumGPR; ++GPR_idx) {
Dan Gohman475871a2008-07-27 21:46:04 +00002083 SDValue Val = DAG.getRegister(GPR[GPR_idx], PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002084 SDValue Store = DAG.getStore(Root, dl, Val, FIN, NULL, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00002085 MemOps.push_back(Store);
2086 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002087 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002088 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Nicolas Geoffray01119992007-04-03 13:59:52 +00002089 }
2090 }
2091
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002092 // If this function is vararg, store any remaining integer argument regs
2093 // to their spots on the stack so that they may be loaded by deferencing the
2094 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002095 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002096 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002097
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002098 if (isPPC64)
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002099 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002100 else
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002101 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002102
Dale Johannesen39355f92009-02-04 02:34:38 +00002103 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT);
2104 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002105 MemOps.push_back(Store);
2106 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002107 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002108 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002109 }
Nicolas Geoffray01119992007-04-03 13:59:52 +00002110
2111 // In ELF 32 ABI, the double arguments are stored to the VarArgsFrameIndex
2112 // on the stack.
2113 if (isELF32_ABI) {
2114 for (FPR_idx = 0; FPR_idx != VarArgsNumFPR; ++FPR_idx) {
Dan Gohman475871a2008-07-27 21:46:04 +00002115 SDValue Val = DAG.getRegister(FPR[FPR_idx], MVT::f64);
Dale Johannesen39355f92009-02-04 02:34:38 +00002116 SDValue Store = DAG.getStore(Root, dl, Val, FIN, NULL, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00002117 MemOps.push_back(Store);
2118 // Increment the address by eight for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002119 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
Nicolas Geoffray01119992007-04-03 13:59:52 +00002120 PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002121 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Nicolas Geoffray01119992007-04-03 13:59:52 +00002122 }
2123
2124 for (; FPR_idx != Num_FPR_Regs; ++FPR_idx) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002125 unsigned VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Nicolas Geoffray01119992007-04-03 13:59:52 +00002126
Dale Johannesen39355f92009-02-04 02:34:38 +00002127 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::f64);
2128 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00002129 MemOps.push_back(Store);
2130 // Increment the address by eight for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002131 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
Nicolas Geoffray01119992007-04-03 13:59:52 +00002132 PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002133 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Nicolas Geoffray01119992007-04-03 13:59:52 +00002134 }
2135 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Dale Johannesen8419dd62008-03-07 20:27:40 +00002138 if (!MemOps.empty())
Scott Michelfdc40a02009-02-17 22:15:04 +00002139 Root = DAG.getNode(ISD::TokenFactor, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +00002140 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002141
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002142 ArgValues.push_back(Root);
Scott Michelfdc40a02009-02-17 22:15:04 +00002143
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002144 // Return the new list of results.
Dale Johannesen39355f92009-02-04 02:34:38 +00002145 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002146 &ArgValues[0], ArgValues.size());
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002147}
2148
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002149/// CalculateParameterAndLinkageAreaSize - Get the size of the paramter plus
2150/// linkage area.
2151static unsigned
2152CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2153 bool isPPC64,
2154 bool isMachoABI,
2155 bool isVarArg,
2156 unsigned CC,
Dan Gohman095cc292008-09-13 01:54:27 +00002157 CallSDNode *TheCall,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002158 unsigned &nAltivecParamsAtEnd) {
2159 // Count how many bytes are to be pushed on the stack, including the linkage
2160 // area, and parameter passing area. We start with 24/48 bytes, which is
2161 // prereserved space for [SP][CR][LR][3 x unused].
2162 unsigned NumBytes = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Dan Gohman095cc292008-09-13 01:54:27 +00002163 unsigned NumOps = TheCall->getNumArgs();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002164 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2165
2166 // Add up all the space actually used.
2167 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2168 // they all go in registers, but we must reserve stack space for them for
2169 // possible use by the caller. In varargs or 64-bit calls, parameters are
2170 // assigned stack space in order, with padding so Altivec parameters are
2171 // 16-byte aligned.
2172 nAltivecParamsAtEnd = 0;
2173 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman095cc292008-09-13 01:54:27 +00002174 SDValue Arg = TheCall->getArg(i);
2175 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002176 MVT ArgVT = Arg.getValueType();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002177 // Varargs Altivec parameters are padded to a 16 byte boundary.
2178 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2179 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
2180 if (!isVarArg && !isPPC64) {
2181 // Non-varargs Altivec parameters go after all the non-Altivec
2182 // parameters; handle those later so we know how much padding we need.
2183 nAltivecParamsAtEnd++;
2184 continue;
2185 }
2186 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2187 NumBytes = ((NumBytes+15)/16)*16;
2188 }
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002189 NumBytes += CalculateStackSlotSize(Arg, Flags, PtrByteSize);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002190 }
2191
2192 // Allow for Altivec parameters at the end, if needed.
2193 if (nAltivecParamsAtEnd) {
2194 NumBytes = ((NumBytes+15)/16)*16;
2195 NumBytes += 16*nAltivecParamsAtEnd;
2196 }
2197
2198 // The prolog code of the callee may store up to 8 GPR argument registers to
2199 // the stack, allowing va_start to index over them in memory if its varargs.
2200 // Because we cannot tell if this is needed on the caller side, we have to
2201 // conservatively assume that it is needed. As such, make sure we have at
2202 // least enough stack space for the caller to store the 8 GPRs.
2203 NumBytes = std::max(NumBytes,
2204 PPCFrameInfo::getMinCallFrameSize(isPPC64, isMachoABI));
2205
2206 // Tail call needs the stack to be aligned.
2207 if (CC==CallingConv::Fast && PerformTailCallOpt) {
2208 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
2209 getStackAlignment();
2210 unsigned AlignMask = TargetAlign-1;
2211 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2212 }
2213
2214 return NumBytes;
2215}
2216
2217/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
2218/// adjusted to accomodate the arguments for the tailcall.
2219static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool IsTailCall,
2220 unsigned ParamSize) {
2221
2222 if (!IsTailCall) return 0;
2223
2224 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2225 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2226 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2227 // Remember only if the new adjustement is bigger.
2228 if (SPDiff < FI->getTailCallSPDelta())
2229 FI->setTailCallSPDelta(SPDiff);
2230
2231 return SPDiff;
2232}
2233
2234/// IsEligibleForTailCallElimination - Check to see whether the next instruction
2235/// following the call is a return. A function is eligible if caller/callee
2236/// calling conventions match, currently only fastcc supports tail calls, and
2237/// the function CALL is immediatly followed by a RET.
2238bool
Dan Gohman095cc292008-09-13 01:54:27 +00002239PPCTargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00002240 SDValue Ret,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002241 SelectionDAG& DAG) const {
2242 // Variable argument functions are not supported.
Dan Gohman095cc292008-09-13 01:54:27 +00002243 if (!PerformTailCallOpt || TheCall->isVarArg())
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002244 return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002245
Dan Gohman095cc292008-09-13 01:54:27 +00002246 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002247 MachineFunction &MF = DAG.getMachineFunction();
2248 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00002249 unsigned CalleeCC = TheCall->getCallingConv();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002250 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2251 // Functions containing by val parameters are not supported.
Dan Gohman095cc292008-09-13 01:54:27 +00002252 for (unsigned i = 0; i != TheCall->getNumArgs(); i++) {
2253 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002254 if (Flags.isByVal()) return false;
2255 }
2256
Dan Gohman095cc292008-09-13 01:54:27 +00002257 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002258 // Non PIC/GOT tail calls are supported.
2259 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2260 return true;
2261
2262 // At the moment we can only do local tail calls (in same module, hidden
2263 // or protected) if we are generating PIC.
2264 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2265 return G->getGlobal()->hasHiddenVisibility()
2266 || G->getGlobal()->hasProtectedVisibility();
2267 }
2268 }
2269
2270 return false;
2271}
2272
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002273/// isCallCompatibleAddress - Return the immediate to use if the specified
2274/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00002275static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002276 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2277 if (!C) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002278
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002279 int Addr = C->getZExtValue();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002280 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
2281 (Addr << 6 >> 6) != Addr)
2282 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002283
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002284 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greifba36cb52008-08-28 21:40:38 +00002285 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002286}
2287
Dan Gohman844731a2008-05-13 00:00:25 +00002288namespace {
2289
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002290struct TailCallArgumentInfo {
Dan Gohman475871a2008-07-27 21:46:04 +00002291 SDValue Arg;
2292 SDValue FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002293 int FrameIdx;
2294
2295 TailCallArgumentInfo() : FrameIdx(0) {}
2296};
2297
Dan Gohman844731a2008-05-13 00:00:25 +00002298}
2299
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002300/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
2301static void
2302StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002303 SDValue Chain,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002304 const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002305 SmallVector<SDValue, 8> &MemOpChains,
2306 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002307 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002308 SDValue Arg = TailCallArgs[i].Arg;
2309 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002310 int FI = TailCallArgs[i].FrameIdx;
2311 // Store relative to framepointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002312 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00002313 PseudoSourceValue::getFixedStack(FI),
2314 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002315 }
2316}
2317
2318/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
2319/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman475871a2008-07-27 21:46:04 +00002320static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002321 MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002322 SDValue Chain,
2323 SDValue OldRetAddr,
2324 SDValue OldFP,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002325 int SPDiff,
2326 bool isPPC64,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002327 bool isMachoABI,
2328 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002329 if (SPDiff) {
2330 // Calculate the new stack slot for the return address.
2331 int SlotSize = isPPC64 ? 8 : 4;
2332 int NewRetAddrLoc = SPDiff + PPCFrameInfo::getReturnSaveOffset(isPPC64,
2333 isMachoABI);
2334 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
2335 NewRetAddrLoc);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002336 MVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002337 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002338 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00002339 PseudoSourceValue::getFixedStack(NewRetAddr), 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002340
2341 // When using the SVR4 ABI there is no need to move the FP stack slot
2342 // as the FP is never overwritten.
2343 if (isMachoABI) {
2344 int NewFPLoc =
2345 SPDiff + PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isMachoABI);
2346 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc);
2347 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
2348 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
2349 PseudoSourceValue::getFixedStack(NewFPIdx), 0);
2350 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002351 }
2352 return Chain;
2353}
2354
2355/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
2356/// the position of the argument.
2357static void
2358CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman475871a2008-07-27 21:46:04 +00002359 SDValue Arg, int SPDiff, unsigned ArgOffset,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002360 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
2361 int Offset = ArgOffset + SPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002362 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002363 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002364 MVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002365 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002366 TailCallArgumentInfo Info;
2367 Info.Arg = Arg;
2368 Info.FrameIdxOp = FIN;
2369 Info.FrameIdx = FI;
2370 TailCallArguments.push_back(Info);
2371}
2372
2373/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
2374/// stack slot. Returns the chain as result and the loaded frame pointers in
2375/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman475871a2008-07-27 21:46:04 +00002376SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002377 int SPDiff,
2378 SDValue Chain,
2379 SDValue &LROpOut,
2380 SDValue &FPOpOut,
Tilmann Schellerffd02002009-07-03 06:45:56 +00002381 bool isMachoABI,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002382 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002383 if (SPDiff) {
2384 // Load the LR and FP stack slot for later adjusting.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002385 MVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002386 LROpOut = getReturnAddrFrameIndex(DAG);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002387 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002388 Chain = SDValue(LROpOut.getNode(), 1);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002389
2390 // When using the SVR4 ABI there is no need to load the FP stack slot
2391 // as the FP is never overwritten.
2392 if (isMachoABI) {
2393 FPOpOut = getFramePointerFrameIndex(DAG);
2394 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, NULL, 0);
2395 Chain = SDValue(FPOpOut.getNode(), 1);
2396 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002397 }
2398 return Chain;
2399}
2400
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002401/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelfdc40a02009-02-17 22:15:04 +00002402/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002403/// specified by the specific parameter attribute. The copy will be passed as
2404/// a byval function parameter.
2405/// Sometimes what we are copying is the end of a larger object, the part that
2406/// does not fit in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00002407static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00002408CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00002409 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002410 DebugLoc dl) {
2411 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen8ad9b432009-02-04 01:17:06 +00002412 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
2413 false, NULL, 0, NULL, 0);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002414}
Chris Lattner9f0bc652007-02-25 05:34:32 +00002415
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002416/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
2417/// tail calls.
2418static void
Dan Gohman475871a2008-07-27 21:46:04 +00002419LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
2420 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002421 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Dan Gohman475871a2008-07-27 21:46:04 +00002422 bool isVector, SmallVector<SDValue, 8> &MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002423 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments,
2424 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002425 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002426 if (!isTailCall) {
2427 if (isVector) {
Dan Gohman475871a2008-07-27 21:46:04 +00002428 SDValue StackPtr;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002429 if (isPPC64)
2430 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
2431 else
2432 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002433 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002434 DAG.getConstant(ArgOffset, PtrVT));
2435 }
Dale Johannesen33c960f2009-02-04 20:06:27 +00002436 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002437 // Calculate and remember argument location.
2438 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
2439 TailCallArguments);
2440}
2441
Tilmann Schellerffd02002009-07-03 06:45:56 +00002442SDValue PPCTargetLowering::LowerCALL_SVR4(SDValue Op, SelectionDAG &DAG,
2443 const PPCSubtarget &Subtarget,
2444 TargetMachine &TM) {
2445 // See PPCTargetLowering::LowerFORMAL_ARGUMENTS_SVR4() for a description
2446 // of the SVR4 ABI stack frame layout.
2447 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
2448 SDValue Chain = TheCall->getChain();
2449 bool isVarArg = TheCall->isVarArg();
2450 unsigned CC = TheCall->getCallingConv();
2451 assert((CC == CallingConv::C ||
2452 CC == CallingConv::Fast) && "Unknown calling convention!");
2453 bool isTailCall = TheCall->isTailCall()
2454 && CC == CallingConv::Fast && PerformTailCallOpt;
2455 SDValue Callee = TheCall->getCallee();
2456 DebugLoc dl = TheCall->getDebugLoc();
2457
2458 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2459 unsigned PtrByteSize = 4;
2460
2461 MachineFunction &MF = DAG.getMachineFunction();
2462
2463 // Mark this function as potentially containing a function that contains a
2464 // tail call. As a consequence the frame pointer will be used for dynamicalloc
2465 // and restoring the callers stack pointer in this functions epilog. This is
2466 // done because by tail calling the called function might overwrite the value
2467 // in this function's (MF) stack pointer stack slot 0(SP).
2468 if (PerformTailCallOpt && CC==CallingConv::Fast)
2469 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
2470
2471 // Count how many bytes are to be pushed on the stack, including the linkage
2472 // area, parameter list area and the part of the local variable space which
2473 // contains copies of aggregates which are passed by value.
2474
2475 // Assign locations to all of the outgoing arguments.
2476 SmallVector<CCValAssign, 16> ArgLocs;
2477 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
2478
2479 // Reserve space for the linkage area on the stack.
2480 CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
2481
2482 if (isVarArg) {
2483 // Handle fixed and variable vector arguments differently.
2484 // Fixed vector arguments go into registers as long as registers are
2485 // available. Variable vector arguments always go into memory.
2486 unsigned NumArgs = TheCall->getNumArgs();
2487 unsigned NumFixedArgs = TheCall->getNumFixedArgs();
2488
2489 for (unsigned i = 0; i != NumArgs; ++i) {
2490 MVT ArgVT = TheCall->getArg(i).getValueType();
2491 ISD::ArgFlagsTy ArgFlags = TheCall->getArgFlags(i);
2492 bool Result;
2493
2494 if (i < NumFixedArgs) {
2495 Result = CC_PPC_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
2496 CCInfo);
2497 } else {
2498 Result = CC_PPC_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
2499 ArgFlags, CCInfo);
2500 }
2501
2502 if (Result) {
2503 cerr << "Call operand #" << i << " has unhandled type "
2504 << ArgVT.getMVTString() << "\n";
2505 abort();
2506 }
2507 }
2508 } else {
2509 // All arguments are treated the same.
2510 CCInfo.AnalyzeCallOperands(TheCall, CC_PPC_SVR4);
2511 }
2512
2513 // Assign locations to all of the outgoing aggregate by value arguments.
2514 SmallVector<CCValAssign, 16> ByValArgLocs;
2515 CCState CCByValInfo(CC, isVarArg, getTargetMachine(), ByValArgLocs);
2516
2517 // Reserve stack space for the allocations in CCInfo.
2518 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2519
2520 CCByValInfo.AnalyzeCallOperands(TheCall, CC_PPC_SVR4_ByVal);
2521
2522 // Size of the linkage area, parameter list area and the part of the local
2523 // space variable where copies of aggregates which are passed by value are
2524 // stored.
2525 unsigned NumBytes = CCByValInfo.getNextStackOffset();
2526
2527 // Calculate by how many bytes the stack has to be adjusted in case of tail
2528 // call optimization.
2529 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
2530
2531 // Adjust the stack pointer for the new arguments...
2532 // These operations are automatically eliminated by the prolog/epilog pass
2533 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
2534 SDValue CallSeqStart = Chain;
2535
2536 // Load the return address and frame pointer so it can be moved somewhere else
2537 // later.
2538 SDValue LROp, FPOp;
2539 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
2540 dl);
2541
2542 // Set up a copy of the stack pointer for use loading and storing any
2543 // arguments that may not fit in the registers available for argument
2544 // passing.
2545 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
2546
2547 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2548 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
2549 SmallVector<SDValue, 8> MemOpChains;
2550
2551 // Walk the register/memloc assignments, inserting copies/loads.
2552 for (unsigned i = 0, j = 0, e = ArgLocs.size();
2553 i != e;
2554 ++i) {
2555 CCValAssign &VA = ArgLocs[i];
2556 SDValue Arg = TheCall->getArg(i);
2557 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
2558
2559 if (Flags.isByVal()) {
2560 // Argument is an aggregate which is passed by value, thus we need to
2561 // create a copy of it in the local variable space of the current stack
2562 // frame (which is the stack frame of the caller) and pass the address of
2563 // this copy to the callee.
2564 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
2565 CCValAssign &ByValVA = ByValArgLocs[j++];
2566 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
2567
2568 // Memory reserved in the local variable space of the callers stack frame.
2569 unsigned LocMemOffset = ByValVA.getLocMemOffset();
2570
2571 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
2572 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
2573
2574 // Create a copy of the argument in the local area of the current
2575 // stack frame.
2576 SDValue MemcpyCall =
2577 CreateCopyOfByValArgument(Arg, PtrOff,
2578 CallSeqStart.getNode()->getOperand(0),
2579 Flags, DAG, dl);
2580
2581 // This must go outside the CALLSEQ_START..END.
2582 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
2583 CallSeqStart.getNode()->getOperand(1));
2584 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
2585 NewCallSeqStart.getNode());
2586 Chain = CallSeqStart = NewCallSeqStart;
2587
2588 // Pass the address of the aggregate copy on the stack either in a
2589 // physical register or in the parameter list area of the current stack
2590 // frame to the callee.
2591 Arg = PtrOff;
2592 }
2593
2594 if (VA.isRegLoc()) {
2595 // Put argument in a physical register.
2596 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2597 } else {
2598 // Put argument in the parameter list area of the current stack frame.
2599 assert(VA.isMemLoc());
2600 unsigned LocMemOffset = VA.getLocMemOffset();
2601
2602 if (!isTailCall) {
2603 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
2604 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
2605
2606 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2607 PseudoSourceValue::getStack(), LocMemOffset));
2608 } else {
2609 // Calculate and remember argument location.
2610 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
2611 TailCallArguments);
2612 }
2613 }
2614 }
2615
2616 if (!MemOpChains.empty())
2617 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2618 &MemOpChains[0], MemOpChains.size());
2619
2620 // Build a sequence of copy-to-reg nodes chained together with token chain
2621 // and flag operands which copy the outgoing args into the appropriate regs.
2622 SDValue InFlag;
2623 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2624 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2625 RegsToPass[i].second, InFlag);
2626 InFlag = Chain.getValue(1);
2627 }
2628
2629 // Set CR6 to true if this is a vararg call.
2630 if (isVarArg) {
2631 SDValue SetCR(DAG.getTargetNode(PPC::CRSET, dl, MVT::i32), 0);
2632 Chain = DAG.getCopyToReg(Chain, dl, PPC::CR1EQ, SetCR, InFlag);
2633 InFlag = Chain.getValue(1);
2634 }
2635
2636 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
2637 // might overwrite each other in case of tail call optimization.
2638 if (isTailCall) {
2639 SmallVector<SDValue, 8> MemOpChains2;
2640 // Do not flag preceeding copytoreg stuff together with the following stuff.
2641 InFlag = SDValue();
2642 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
2643 MemOpChains2, dl);
2644 if (!MemOpChains2.empty())
2645 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2646 &MemOpChains2[0], MemOpChains2.size());
2647
2648 // Store the return address to the appropriate stack slot.
2649 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
2650 false, false, dl);
2651 }
2652
2653 // Emit callseq_end just before tailcall node.
2654 if (isTailCall) {
2655 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2656 DAG.getIntPtrConstant(0, true), InFlag);
2657 InFlag = Chain.getValue(1);
2658 }
2659
2660 std::vector<MVT> NodeTys;
2661 NodeTys.push_back(MVT::Other); // Returns a chain
2662 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2663
2664 SmallVector<SDValue, 8> Ops;
2665 unsigned CallOpc = PPCISD::CALL_ELF;
2666
2667 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2668 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2669 // node so that legalize doesn't hack it.
2670 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2671 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
2672 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
2673 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType());
2674 else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
2675 // If this is an absolute destination address, use the munged value.
2676 Callee = SDValue(Dest, 0);
2677 else {
2678 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
2679 // to do the call, we can't use PPCISD::CALL.
2680 SDValue MTCTROps[] = {Chain, Callee, InFlag};
2681 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
2682 2 + (InFlag.getNode() != 0));
2683 InFlag = Chain.getValue(1);
2684
2685 NodeTys.clear();
2686 NodeTys.push_back(MVT::Other);
2687 NodeTys.push_back(MVT::Flag);
2688 Ops.push_back(Chain);
2689 CallOpc = PPCISD::BCTRL_ELF;
2690 Callee.setNode(0);
2691 // Add CTR register as callee so a bctr can be emitted later.
2692 if (isTailCall)
2693 Ops.push_back(DAG.getRegister(PPC::CTR, getPointerTy()));
2694 }
2695
2696 // If this is a direct call, pass the chain and the callee.
2697 if (Callee.getNode()) {
2698 Ops.push_back(Chain);
2699 Ops.push_back(Callee);
2700 }
2701 // If this is a tail call add stack pointer delta.
2702 if (isTailCall)
2703 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
2704
2705 // Add argument registers to the end of the list so that they are known live
2706 // into the call.
2707 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2708 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2709 RegsToPass[i].second.getValueType()));
2710
2711 // When performing tail call optimization the callee pops its arguments off
2712 // the stack. Account for this here so these bytes can be pushed back on in
2713 // PPCRegisterInfo::eliminateCallFramePseudoInstr.
2714 int BytesCalleePops =
2715 (CC==CallingConv::Fast && PerformTailCallOpt) ? NumBytes : 0;
2716
2717 if (InFlag.getNode())
2718 Ops.push_back(InFlag);
2719
2720 // Emit tail call.
2721 if (isTailCall) {
2722 assert(InFlag.getNode() &&
2723 "Flag must be set. Depend on flag being set in LowerRET");
2724 Chain = DAG.getNode(PPCISD::TAILCALL, dl,
2725 TheCall->getVTList(), &Ops[0], Ops.size());
2726 return SDValue(Chain.getNode(), Op.getResNo());
2727 }
2728
2729 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
2730 InFlag = Chain.getValue(1);
2731
2732 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2733 DAG.getIntPtrConstant(BytesCalleePops, true),
2734 InFlag);
2735 if (TheCall->getValueType(0) != MVT::Other)
2736 InFlag = Chain.getValue(1);
2737
2738 SmallVector<SDValue, 16> ResultVals;
2739 SmallVector<CCValAssign, 16> RVLocs;
2740 unsigned CallerCC = DAG.getMachineFunction().getFunction()->getCallingConv();
2741 CCState CCRetInfo(CallerCC, isVarArg, TM, RVLocs);
2742 CCRetInfo.AnalyzeCallResult(TheCall, RetCC_PPC);
2743
2744 // Copy all of the result registers out of their specified physreg.
2745 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
2746 CCValAssign &VA = RVLocs[i];
2747 MVT VT = VA.getValVT();
2748 assert(VA.isRegLoc() && "Can only return in registers!");
2749 Chain = DAG.getCopyFromReg(Chain, dl,
2750 VA.getLocReg(), VT, InFlag).getValue(1);
2751 ResultVals.push_back(Chain.getValue(0));
2752 InFlag = Chain.getValue(2);
2753 }
2754
2755 // If the function returns void, just return the chain.
2756 if (RVLocs.empty())
2757 return Chain;
2758
2759 // Otherwise, merge everything together with a MERGE_VALUES node.
2760 ResultVals.push_back(Chain);
2761 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
2762 &ResultVals[0], ResultVals.size());
2763 return Res.getValue(Op.getResNo());
2764}
2765
Dan Gohman475871a2008-07-27 21:46:04 +00002766SDValue PPCTargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG,
Dan Gohman7925ed02008-03-19 21:39:28 +00002767 const PPCSubtarget &Subtarget,
2768 TargetMachine &TM) {
Dan Gohman095cc292008-09-13 01:54:27 +00002769 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
2770 SDValue Chain = TheCall->getChain();
2771 bool isVarArg = TheCall->isVarArg();
2772 unsigned CC = TheCall->getCallingConv();
2773 bool isTailCall = TheCall->isTailCall()
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002774 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohman095cc292008-09-13 01:54:27 +00002775 SDValue Callee = TheCall->getCallee();
2776 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00002777 DebugLoc dl = TheCall->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002778
Chris Lattner9f0bc652007-02-25 05:34:32 +00002779 bool isMachoABI = Subtarget.isMachoABI();
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00002780 bool isELF32_ABI = Subtarget.isELF32_ABI();
Evan Cheng4360bdc2006-05-25 00:57:32 +00002781
Duncan Sands83ec4b62008-06-06 12:08:01 +00002782 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Chris Lattnerc91a4752006-06-26 22:48:35 +00002783 bool isPPC64 = PtrVT == MVT::i64;
2784 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002785
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002786 MachineFunction &MF = DAG.getMachineFunction();
2787
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002788 // Mark this function as potentially containing a function that contains a
2789 // tail call. As a consequence the frame pointer will be used for dynamicalloc
2790 // and restoring the callers stack pointer in this functions epilog. This is
2791 // done because by tail calling the called function might overwrite the value
2792 // in this function's (MF) stack pointer stack slot 0(SP).
2793 if (PerformTailCallOpt && CC==CallingConv::Fast)
2794 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
2795
2796 unsigned nAltivecParamsAtEnd = 0;
2797
Chris Lattnerabde4602006-05-16 22:56:08 +00002798 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00002799 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002800 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002801 unsigned NumBytes =
2802 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isMachoABI, isVarArg, CC,
Dan Gohman095cc292008-09-13 01:54:27 +00002803 TheCall, nAltivecParamsAtEnd);
Dale Johannesen75092de2008-03-12 00:22:17 +00002804
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002805 // Calculate by how many bytes the stack has to be adjusted in case of tail
2806 // call optimization.
2807 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelfdc40a02009-02-17 22:15:04 +00002808
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002809 // Adjust the stack pointer for the new arguments...
2810 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +00002811 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohman475871a2008-07-27 21:46:04 +00002812 SDValue CallSeqStart = Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00002813
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002814 // Load the return address and frame pointer so it can be move somewhere else
2815 // later.
Dan Gohman475871a2008-07-27 21:46:04 +00002816 SDValue LROp, FPOp;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002817 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
2818 dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002819
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002820 // Set up a copy of the stack pointer for use loading and storing any
2821 // arguments that may not fit in the registers available for argument
2822 // passing.
Dan Gohman475871a2008-07-27 21:46:04 +00002823 SDValue StackPtr;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002824 if (isPPC64)
2825 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
2826 else
2827 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00002828
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002829 // Figure out which arguments are going to go in registers, and which in
2830 // memory. Also, if this is a vararg function, floating point operations
2831 // must be stored to our stack, and loaded into integer regs as well, if
2832 // any integer regs are available for argument passing.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002833 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, isMachoABI);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002834 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002835
Chris Lattnerc91a4752006-06-26 22:48:35 +00002836 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00002837 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2838 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2839 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00002840 static const unsigned GPR_64[] = { // 64-bit registers.
2841 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2842 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2843 };
Chris Lattner9f0bc652007-02-25 05:34:32 +00002844 static const unsigned *FPR = GetFPR(Subtarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00002845
Chris Lattner9a2a4972006-05-17 06:01:33 +00002846 static const unsigned VR[] = {
2847 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2848 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2849 };
Owen Anderson718cb662007-09-07 04:06:50 +00002850 const unsigned NumGPRs = array_lengthof(GPR_32);
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00002851 const unsigned NumFPRs = isMachoABI ? 13 : 8;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002852 const unsigned NumVRs = array_lengthof(VR);
Scott Michelfdc40a02009-02-17 22:15:04 +00002853
Chris Lattnerc91a4752006-06-26 22:48:35 +00002854 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
2855
Dan Gohman475871a2008-07-27 21:46:04 +00002856 std::vector<std::pair<unsigned, SDValue> > RegsToPass;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002857 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
2858
Dan Gohman475871a2008-07-27 21:46:04 +00002859 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00002860 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner9f0bc652007-02-25 05:34:32 +00002861 bool inMem = false;
Dan Gohman095cc292008-09-13 01:54:27 +00002862 SDValue Arg = TheCall->getArg(i);
2863 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002864 // See if next argument requires stack alignment in ELF
Nicolas Geoffray6ccbbd82008-04-15 08:08:50 +00002865 bool Align = Flags.isSplit();
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002866
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002867 // PtrOff will be used to store the current argument to the stack if a
2868 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00002869 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00002870
Nicolas Geoffrayec58d9f2007-04-03 12:35:28 +00002871 // Stack align in ELF 32
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00002872 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002873 PtrOff = DAG.getConstant(ArgOffset + ((ArgOffset/4) % 2) * PtrByteSize,
2874 StackPtr.getValueType());
2875 else
2876 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
2877
Dale Johannesen39355f92009-02-04 02:34:38 +00002878 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002879
2880 // On PPC64, promote integers to 64-bit values.
2881 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00002882 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
2883 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Dale Johannesen39355f92009-02-04 02:34:38 +00002884 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002885 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002886
2887 // FIXME Elf untested, what are alignment rules?
Dale Johannesen8419dd62008-03-07 20:27:40 +00002888 // FIXME memcpy is used way more than necessary. Correctness first.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002889 if (Flags.isByVal()) {
2890 unsigned Size = Flags.getByValSize();
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00002891 if (isELF32_ABI && Align) GPR_idx += (GPR_idx % 2);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002892 if (Size==1 || Size==2) {
2893 // Very small objects are passed right-justified.
2894 // Everything else is passed left-justified.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002895 MVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002896 if (GPR_idx != NumGPRs) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002897 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Dale Johannesen8419dd62008-03-07 20:27:40 +00002898 NULL, 0, VT);
2899 MemOpChains.push_back(Load.getValue(1));
2900 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
2901 if (isMachoABI)
2902 ArgOffset += PtrByteSize;
2903 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00002904 SDValue Const = DAG.getConstant(4 - Size, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002905 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Dan Gohman475871a2008-07-27 21:46:04 +00002906 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr,
Scott Michelfdc40a02009-02-17 22:15:04 +00002907 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002908 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002909 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00002910 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00002911 CallSeqStart.getNode()->getOperand(1));
Gabor Greif93c53e52008-08-31 15:37:04 +00002912 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
2913 NewCallSeqStart.getNode());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002914 Chain = CallSeqStart = NewCallSeqStart;
2915 ArgOffset += PtrByteSize;
2916 }
2917 continue;
2918 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002919 // Copy entire object into memory. There are cases where gcc-generated
2920 // code assumes it is there, even if it could be put entirely into
2921 // registers. (This is not what the doc says.)
Dan Gohman475871a2008-07-27 21:46:04 +00002922 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00002923 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002924 Flags, DAG, dl);
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002925 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00002926 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00002927 CallSeqStart.getNode()->getOperand(1));
2928 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), NewCallSeqStart.getNode());
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002929 Chain = CallSeqStart = NewCallSeqStart;
2930 // And copy the pieces of it that fit into registers.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002931 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00002932 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002933 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002934 if (GPR_idx != NumGPRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002935 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, NULL, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00002936 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002937 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
2938 if (isMachoABI)
2939 ArgOffset += PtrByteSize;
2940 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002941 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002942 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002943 }
2944 }
2945 continue;
2946 }
2947
Duncan Sands83ec4b62008-06-06 12:08:01 +00002948 switch (Arg.getValueType().getSimpleVT()) {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002949 default: assert(0 && "Unexpected ValueType for argument!");
2950 case MVT::i32:
Chris Lattnerc91a4752006-06-26 22:48:35 +00002951 case MVT::i64:
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002952 // Double word align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00002953 if (isELF32_ABI && Align) GPR_idx += (GPR_idx % 2);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002954 if (GPR_idx != NumGPRs) {
2955 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002956 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002957 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
2958 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002959 TailCallArguments, dl);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002960 inMem = true;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002961 }
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002962 if (inMem || isMachoABI) {
2963 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00002964 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002965 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
2966
2967 ArgOffset += PtrByteSize;
2968 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002969 break;
2970 case MVT::f32:
2971 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00002972 if (FPR_idx != NumFPRs) {
2973 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
2974
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002975 if (isVarArg) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002976 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002977 MemOpChains.push_back(Store);
2978
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002979 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00002980 if (GPR_idx != NumGPRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002981 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002982 MemOpChains.push_back(Load.getValue(1));
Chris Lattner9f0bc652007-02-25 05:34:32 +00002983 if (isMachoABI) RegsToPass.push_back(std::make_pair(GPR[GPR_idx++],
2984 Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002985 }
Jim Laskeyfbb74e62006-12-01 16:30:47 +00002986 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00002987 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002988 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
2989 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002990 MemOpChains.push_back(Load.getValue(1));
Chris Lattner9f0bc652007-02-25 05:34:32 +00002991 if (isMachoABI) RegsToPass.push_back(std::make_pair(GPR[GPR_idx++],
2992 Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00002993 }
2994 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002995 // If we have any FPRs remaining, we may also have GPRs remaining.
2996 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
2997 // GPRs.
Chris Lattner9f0bc652007-02-25 05:34:32 +00002998 if (isMachoABI) {
2999 if (GPR_idx != NumGPRs)
3000 ++GPR_idx;
3001 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
3002 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
3003 ++GPR_idx;
3004 }
Chris Lattnerabde4602006-05-16 22:56:08 +00003005 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003006 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003007 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3008 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003009 TailCallArguments, dl);
Chris Lattner9f0bc652007-02-25 05:34:32 +00003010 inMem = true;
Chris Lattnerabde4602006-05-16 22:56:08 +00003011 }
Chris Lattner9f0bc652007-02-25 05:34:32 +00003012 if (inMem || isMachoABI) {
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00003013 // Stack align in ELF
Nicolas Geoffrayc0cb28f2008-04-13 13:40:22 +00003014 if (isELF32_ABI && Align)
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00003015 ArgOffset += ((ArgOffset/4) % 2) * PtrByteSize;
Chris Lattner9f0bc652007-02-25 05:34:32 +00003016 if (isPPC64)
3017 ArgOffset += 8;
3018 else
3019 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
3020 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003021 break;
3022 case MVT::v4f32:
3023 case MVT::v4i32:
3024 case MVT::v8i16:
3025 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00003026 if (isVarArg) {
3027 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00003028 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00003029 // V registers; in fact gcc does this only for arguments that are
3030 // prototyped, not for those that match the ... We do it for all
3031 // arguments, seems to work.
3032 while (ArgOffset % 16 !=0) {
3033 ArgOffset += PtrByteSize;
3034 if (GPR_idx != NumGPRs)
3035 GPR_idx++;
3036 }
3037 // We could elide this store in the case where the object fits
3038 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00003039 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00003040 DAG.getConstant(ArgOffset, PtrVT));
Dale Johannesen39355f92009-02-04 02:34:38 +00003041 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003042 MemOpChains.push_back(Store);
3043 if (VR_idx != NumVRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00003044 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003045 MemOpChains.push_back(Load.getValue(1));
3046 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
3047 }
3048 ArgOffset += 16;
3049 for (unsigned i=0; i<16; i+=PtrByteSize) {
3050 if (GPR_idx == NumGPRs)
3051 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00003052 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00003053 DAG.getConstant(i, PtrVT));
Dale Johannesen39355f92009-02-04 02:34:38 +00003054 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003055 MemOpChains.push_back(Load.getValue(1));
3056 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3057 }
3058 break;
3059 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003060
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003061 // Non-varargs Altivec params generally go in registers, but have
3062 // stack space allocated at the end.
3063 if (VR_idx != NumVRs) {
3064 // Doesn't have GPR space allocated.
3065 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
3066 } else if (nAltivecParamsAtEnd==0) {
3067 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003068 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3069 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003070 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00003071 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00003072 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003073 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00003074 }
Chris Lattnerabde4602006-05-16 22:56:08 +00003075 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003076 // If all Altivec parameters fit in registers, as they usually do,
3077 // they get stack space following the non-Altivec parameters. We
3078 // don't track this here because nobody below needs it.
3079 // If there are more Altivec parameters than fit in registers emit
3080 // the stores here.
3081 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
3082 unsigned j = 0;
3083 // Offset is aligned; skip 1st 12 params which go in V registers.
3084 ArgOffset = ((ArgOffset+15)/16)*16;
3085 ArgOffset += 12*16;
3086 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman095cc292008-09-13 01:54:27 +00003087 SDValue Arg = TheCall->getArg(i);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003088 MVT ArgType = Arg.getValueType();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003089 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
3090 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
3091 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00003092 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003093 // We are emitting Altivec params in order.
3094 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3095 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003096 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003097 ArgOffset += 16;
3098 }
3099 }
3100 }
3101 }
3102
Chris Lattner9a2a4972006-05-17 06:01:33 +00003103 if (!MemOpChains.empty())
Dale Johannesen39355f92009-02-04 02:34:38 +00003104 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00003105 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00003106
Chris Lattner9a2a4972006-05-17 06:01:33 +00003107 // Build a sequence of copy-to-reg nodes chained together with token chain
3108 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00003109 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00003110 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003111 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00003112 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003113 InFlag = Chain.getValue(1);
3114 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003115
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003116 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
3117 // might overwrite each other in case of tail call optimization.
3118 if (isTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00003119 SmallVector<SDValue, 8> MemOpChains2;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003120 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00003121 InFlag = SDValue();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003122 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003123 MemOpChains2, dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003124 if (!MemOpChains2.empty())
Dale Johannesen39355f92009-02-04 02:34:38 +00003125 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003126 &MemOpChains2[0], MemOpChains2.size());
3127
3128 // Store the return address to the appropriate stack slot.
3129 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003130 isPPC64, isMachoABI, dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003131 }
3132
3133 // Emit callseq_end just before tailcall node.
3134 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00003135 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3136 DAG.getIntPtrConstant(0, true), InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003137 InFlag = Chain.getValue(1);
3138 }
3139
Duncan Sands83ec4b62008-06-06 12:08:01 +00003140 std::vector<MVT> NodeTys;
Chris Lattner4a45abf2006-06-10 01:14:28 +00003141 NodeTys.push_back(MVT::Other); // Returns a chain
3142 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
3143
Dan Gohman475871a2008-07-27 21:46:04 +00003144 SmallVector<SDValue, 8> Ops;
Nicolas Geoffray63f8fb12007-02-27 13:01:19 +00003145 unsigned CallOpc = isMachoABI? PPCISD::CALL_Macho : PPCISD::CALL_ELF;
Scott Michelfdc40a02009-02-17 22:15:04 +00003146
Bill Wendling056292f2008-09-16 21:48:12 +00003147 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3148 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3149 // node so that legalize doesn't hack it.
Nicolas Geoffray5a6c91a2007-12-21 12:22:29 +00003150 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
3151 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
Bill Wendling056292f2008-09-16 21:48:12 +00003152 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
3153 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType());
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003154 else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
3155 // If this is an absolute destination address, use the munged value.
Dan Gohman475871a2008-07-27 21:46:04 +00003156 Callee = SDValue(Dest, 0);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003157 else {
3158 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
3159 // to do the call, we can't use PPCISD::CALL.
Dan Gohman475871a2008-07-27 21:46:04 +00003160 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Dale Johannesen39355f92009-02-04 02:34:38 +00003161 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
Gabor Greif93c53e52008-08-31 15:37:04 +00003162 2 + (InFlag.getNode() != 0));
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003163 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003164
Chris Lattnerdc9971a2008-03-09 20:49:33 +00003165 // Copy the callee address into R12/X12 on darwin.
Chris Lattner9f0bc652007-02-25 05:34:32 +00003166 if (isMachoABI) {
Chris Lattnerdc9971a2008-03-09 20:49:33 +00003167 unsigned Reg = Callee.getValueType() == MVT::i32 ? PPC::R12 : PPC::X12;
Dale Johannesen39355f92009-02-04 02:34:38 +00003168 Chain = DAG.getCopyToReg(Chain, dl, Reg, Callee, InFlag);
Chris Lattner9f0bc652007-02-25 05:34:32 +00003169 InFlag = Chain.getValue(1);
3170 }
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003171
3172 NodeTys.clear();
3173 NodeTys.push_back(MVT::Other);
3174 NodeTys.push_back(MVT::Flag);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003175 Ops.push_back(Chain);
Chris Lattner9f0bc652007-02-25 05:34:32 +00003176 CallOpc = isMachoABI ? PPCISD::BCTRL_Macho : PPCISD::BCTRL_ELF;
Gabor Greifba36cb52008-08-28 21:40:38 +00003177 Callee.setNode(0);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003178 // Add CTR register as callee so a bctr can be emitted later.
3179 if (isTailCall)
3180 Ops.push_back(DAG.getRegister(PPC::CTR, getPointerTy()));
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003181 }
Chris Lattner9a2a4972006-05-17 06:01:33 +00003182
Chris Lattner4a45abf2006-06-10 01:14:28 +00003183 // If this is a direct call, pass the chain and the callee.
Gabor Greifba36cb52008-08-28 21:40:38 +00003184 if (Callee.getNode()) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003185 Ops.push_back(Chain);
3186 Ops.push_back(Callee);
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003187 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003188 // If this is a tail call add stack pointer delta.
3189 if (isTailCall)
3190 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
3191
Chris Lattner4a45abf2006-06-10 01:14:28 +00003192 // Add argument registers to the end of the list so that they are known live
3193 // into the call.
3194 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Scott Michelfdc40a02009-02-17 22:15:04 +00003195 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Chris Lattner4a45abf2006-06-10 01:14:28 +00003196 RegsToPass[i].second.getValueType()));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003197
3198 // When performing tail call optimization the callee pops its arguments off
3199 // the stack. Account for this here so these bytes can be pushed back on in
3200 // PPCRegisterInfo::eliminateCallFramePseudoInstr.
3201 int BytesCalleePops =
3202 (CC==CallingConv::Fast && PerformTailCallOpt) ? NumBytes : 0;
3203
Gabor Greifba36cb52008-08-28 21:40:38 +00003204 if (InFlag.getNode())
Chris Lattner4a45abf2006-06-10 01:14:28 +00003205 Ops.push_back(InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003206
3207 // Emit tail call.
3208 if (isTailCall) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003209 assert(InFlag.getNode() &&
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003210 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesen39355f92009-02-04 02:34:38 +00003211 Chain = DAG.getNode(PPCISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00003212 TheCall->getVTList(), &Ops[0], Ops.size());
Gabor Greifba36cb52008-08-28 21:40:38 +00003213 return SDValue(Chain.getNode(), Op.getResNo());
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003214 }
3215
Dale Johannesen39355f92009-02-04 02:34:38 +00003216 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner4a45abf2006-06-10 01:14:28 +00003217 InFlag = Chain.getValue(1);
3218
Chris Lattnere563bbc2008-10-11 22:08:30 +00003219 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
3220 DAG.getIntPtrConstant(BytesCalleePops, true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00003221 InFlag);
Dan Gohman095cc292008-09-13 01:54:27 +00003222 if (TheCall->getValueType(0) != MVT::Other)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00003223 InFlag = Chain.getValue(1);
3224
Dan Gohman475871a2008-07-27 21:46:04 +00003225 SmallVector<SDValue, 16> ResultVals;
Dan Gohman7925ed02008-03-19 21:39:28 +00003226 SmallVector<CCValAssign, 16> RVLocs;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003227 unsigned CallerCC = DAG.getMachineFunction().getFunction()->getCallingConv();
3228 CCState CCInfo(CallerCC, isVarArg, TM, RVLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00003229 CCInfo.AnalyzeCallResult(TheCall, RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00003230
Dan Gohman7925ed02008-03-19 21:39:28 +00003231 // Copy all of the result registers out of their specified physreg.
3232 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3233 CCValAssign &VA = RVLocs[i];
Duncan Sands83ec4b62008-06-06 12:08:01 +00003234 MVT VT = VA.getValVT();
Dan Gohman7925ed02008-03-19 21:39:28 +00003235 assert(VA.isRegLoc() && "Can only return in registers!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003236 Chain = DAG.getCopyFromReg(Chain, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +00003237 VA.getLocReg(), VT, InFlag).getValue(1);
Dan Gohman7925ed02008-03-19 21:39:28 +00003238 ResultVals.push_back(Chain.getValue(0));
3239 InFlag = Chain.getValue(2);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003240 }
Dan Gohman7925ed02008-03-19 21:39:28 +00003241
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003242 // If the function returns void, just return the chain.
Dan Gohman7925ed02008-03-19 21:39:28 +00003243 if (RVLocs.empty())
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003244 return Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00003245
Chris Lattnerc703a8f2006-05-17 19:00:46 +00003246 // Otherwise, merge everything together with a MERGE_VALUES node.
Dan Gohman7925ed02008-03-19 21:39:28 +00003247 ResultVals.push_back(Chain);
Dale Johannesen39355f92009-02-04 02:34:38 +00003248 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00003249 &ResultVals[0], ResultVals.size());
Gabor Greif99a6cb92008-08-26 22:36:50 +00003250 return Res.getValue(Op.getResNo());
Chris Lattnerabde4602006-05-16 22:56:08 +00003251}
3252
Scott Michelfdc40a02009-02-17 22:15:04 +00003253SDValue PPCTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003254 TargetMachine &TM) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003255 SmallVector<CCValAssign, 16> RVLocs;
3256 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +00003257 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Dale Johannesena05dca42009-02-04 23:02:30 +00003258 DebugLoc dl = Op.getDebugLoc();
Chris Lattner52387be2007-06-19 00:13:10 +00003259 CCState CCInfo(CC, isVarArg, TM, RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +00003260 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00003261
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003262 // If this is the first return lowered for this function, add the regs to the
3263 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00003264 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003265 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00003266 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003267 }
3268
Dan Gohman475871a2008-07-27 21:46:04 +00003269 SDValue Chain = Op.getOperand(0);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003270
3271 Chain = GetPossiblePreceedingTailCall(Chain, PPCISD::TAILCALL);
3272 if (Chain.getOpcode() == PPCISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003273 SDValue TailCall = Chain;
3274 SDValue TargetAddress = TailCall.getOperand(1);
3275 SDValue StackAdjustment = TailCall.getOperand(2);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003276
3277 assert(((TargetAddress.getOpcode() == ISD::Register &&
3278 cast<RegisterSDNode>(TargetAddress)->getReg() == PPC::CTR) ||
Bill Wendling056292f2008-09-16 21:48:12 +00003279 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003280 TargetAddress.getOpcode() == ISD::TargetGlobalAddress ||
3281 isa<ConstantSDNode>(TargetAddress)) &&
3282 "Expecting an global address, external symbol, absolute value or register");
3283
3284 assert(StackAdjustment.getOpcode() == ISD::Constant &&
3285 "Expecting a const value");
3286
Dan Gohman475871a2008-07-27 21:46:04 +00003287 SmallVector<SDValue,8> Operands;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003288 Operands.push_back(Chain.getOperand(0));
3289 Operands.push_back(TargetAddress);
3290 Operands.push_back(StackAdjustment);
3291 // Copy registers used by the call. Last operand is a flag so it is not
3292 // copied.
3293 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
3294 Operands.push_back(Chain.getOperand(i));
3295 }
Dale Johannesena05dca42009-02-04 23:02:30 +00003296 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003297 Operands.size());
3298 }
3299
Dan Gohman475871a2008-07-27 21:46:04 +00003300 SDValue Flag;
Scott Michelfdc40a02009-02-17 22:15:04 +00003301
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003302 // Copy the result values into the output registers.
3303 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3304 CCValAssign &VA = RVLocs[i];
3305 assert(VA.isRegLoc() && "Can only return in registers!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003306 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dale Johannesena05dca42009-02-04 23:02:30 +00003307 Op.getOperand(i*2+1), Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003308 Flag = Chain.getValue(1);
3309 }
3310
Gabor Greifba36cb52008-08-28 21:40:38 +00003311 if (Flag.getNode())
Dale Johannesena05dca42009-02-04 23:02:30 +00003312 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003313 else
Dale Johannesena05dca42009-02-04 23:02:30 +00003314 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner1a635d62006-04-14 06:01:58 +00003315}
3316
Dan Gohman475871a2008-07-27 21:46:04 +00003317SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Jim Laskeyefc7e522006-12-04 22:04:42 +00003318 const PPCSubtarget &Subtarget) {
3319 // When we pop the dynamic allocation we need to restore the SP link.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003320 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003321
Jim Laskeyefc7e522006-12-04 22:04:42 +00003322 // Get the corect type for pointers.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003323 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00003324
3325 // Construct the stack pointer operand.
3326 bool IsPPC64 = Subtarget.isPPC64();
3327 unsigned SP = IsPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00003328 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003329
3330 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00003331 SDValue Chain = Op.getOperand(0);
3332 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003333
Jim Laskeyefc7e522006-12-04 22:04:42 +00003334 // Load the old link SP.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003335 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr, NULL, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003336
Jim Laskeyefc7e522006-12-04 22:04:42 +00003337 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003338 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00003339
Jim Laskeyefc7e522006-12-04 22:04:42 +00003340 // Store the old link SP.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003341 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, NULL, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003342}
3343
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003344
3345
Dan Gohman475871a2008-07-27 21:46:04 +00003346SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003347PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003348 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003349 bool IsPPC64 = PPCSubTarget.isPPC64();
3350 bool isMachoABI = PPCSubTarget.isMachoABI();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003351 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003352
3353 // Get current frame pointer save index. The users of this index will be
3354 // primarily DYNALLOC instructions.
3355 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3356 int RASI = FI->getReturnAddrSaveIndex();
3357
3358 // If the frame pointer save index hasn't been defined yet.
3359 if (!RASI) {
3360 // Find out what the fix offset of the frame pointer save area.
3361 int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64, isMachoABI);
3362 // Allocate the frame index for frame pointer save area.
3363 RASI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, LROffset);
3364 // Save the result.
3365 FI->setReturnAddrSaveIndex(RASI);
3366 }
3367 return DAG.getFrameIndex(RASI, PtrVT);
3368}
3369
Dan Gohman475871a2008-07-27 21:46:04 +00003370SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003371PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
3372 MachineFunction &MF = DAG.getMachineFunction();
3373 bool IsPPC64 = PPCSubTarget.isPPC64();
3374 bool isMachoABI = PPCSubTarget.isMachoABI();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003375 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003376
3377 // Get current frame pointer save index. The users of this index will be
3378 // primarily DYNALLOC instructions.
3379 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3380 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003381
Jim Laskey2f616bf2006-11-16 22:43:37 +00003382 // If the frame pointer save index hasn't been defined yet.
3383 if (!FPSI) {
3384 // Find out what the fix offset of the frame pointer save area.
Chris Lattner9f0bc652007-02-25 05:34:32 +00003385 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64, isMachoABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00003386
Jim Laskey2f616bf2006-11-16 22:43:37 +00003387 // Allocate the frame index for frame pointer save area.
Scott Michelfdc40a02009-02-17 22:15:04 +00003388 FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003389 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00003390 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003391 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003392 return DAG.getFrameIndex(FPSI, PtrVT);
3393}
Jim Laskey2f616bf2006-11-16 22:43:37 +00003394
Dan Gohman475871a2008-07-27 21:46:04 +00003395SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003396 SelectionDAG &DAG,
3397 const PPCSubtarget &Subtarget) {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003398 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00003399 SDValue Chain = Op.getOperand(0);
3400 SDValue Size = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003401 DebugLoc dl = Op.getDebugLoc();
3402
Jim Laskey2f616bf2006-11-16 22:43:37 +00003403 // Get the corect type for pointers.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003404 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003405 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00003406 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00003407 DAG.getConstant(0, PtrVT), Size);
3408 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00003409 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003410 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00003411 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Jim Laskey2f616bf2006-11-16 22:43:37 +00003412 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00003413 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003414}
3415
Chris Lattner1a635d62006-04-14 06:01:58 +00003416/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
3417/// possible.
Dan Gohman475871a2008-07-27 21:46:04 +00003418SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00003419 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003420 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
3421 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00003422 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003423
Chris Lattner1a635d62006-04-14 06:01:58 +00003424 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003425
Chris Lattner1a635d62006-04-14 06:01:58 +00003426 // Cannot handle SETEQ/SETNE.
Eli Friedmanc06441e2009-05-28 04:31:08 +00003427 if (CC == ISD::SETEQ || CC == ISD::SETNE) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003428
Duncan Sands83ec4b62008-06-06 12:08:01 +00003429 MVT ResVT = Op.getValueType();
3430 MVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003431 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3432 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00003433 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003434
Chris Lattner1a635d62006-04-14 06:01:58 +00003435 // If the RHS of the comparison is a 0.0, we don't need to do the
3436 // subtraction at all.
3437 if (isFloatingPointZero(RHS))
3438 switch (CC) {
3439 default: break; // SETUO etc aren't handled by fsel.
3440 case ISD::SETULT:
3441 case ISD::SETLT:
3442 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003443 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003444 case ISD::SETGE:
3445 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003446 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
3447 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003448 case ISD::SETUGT:
3449 case ISD::SETGT:
3450 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003451 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003452 case ISD::SETLE:
3453 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003454 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
3455 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
3456 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003457 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003458
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00003460 switch (CC) {
3461 default: break; // SETUO etc aren't handled by fsel.
3462 case ISD::SETULT:
3463 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00003464 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Chris Lattner1a635d62006-04-14 06:01:58 +00003465 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003466 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
3467 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003468 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003469 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00003470 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Chris Lattner1a635d62006-04-14 06:01:58 +00003471 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003472 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
3473 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003474 case ISD::SETUGT:
3475 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00003476 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Chris Lattner1a635d62006-04-14 06:01:58 +00003477 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003478 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
3479 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003480 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003481 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00003482 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Chris Lattner1a635d62006-04-14 06:01:58 +00003483 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003484 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
3485 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003486 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00003487 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00003488}
3489
Chris Lattner1f873002007-11-28 18:44:47 +00003490// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003491SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00003492 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003493 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00003494 SDValue Src = Op.getOperand(0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003495 if (Src.getValueType() == MVT::f32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003496 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003497
Dan Gohman475871a2008-07-27 21:46:04 +00003498 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003499 switch (Op.getValueType().getSimpleVT()) {
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003500 default: assert(0 && "Unhandled FP_TO_INT type in custom expander!");
Chris Lattner1a635d62006-04-14 06:01:58 +00003501 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003502 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
3503 PPCISD::FCTIDZ,
3504 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003505 break;
3506 case MVT::i64:
Dale Johannesen33c960f2009-02-04 20:06:27 +00003507 Tmp = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003508 break;
3509 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00003510
Chris Lattner1a635d62006-04-14 06:01:58 +00003511 // Convert the FP value to an int value through memory.
Dan Gohman475871a2008-07-27 21:46:04 +00003512 SDValue FIPtr = DAG.CreateStackTemporary(MVT::f64);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003513
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003514 // Emit a store to the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003515 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, NULL, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003516
3517 // Result is a load from the stack slot. If loading 4 bytes, make sure to
3518 // add in a bias.
Chris Lattner1a635d62006-04-14 06:01:58 +00003519 if (Op.getValueType() == MVT::i32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003520 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003521 DAG.getConstant(4, FIPtr.getValueType()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00003522 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003523}
3524
Dan Gohman475871a2008-07-27 21:46:04 +00003525SDValue PPCTargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003526 DebugLoc dl = Op.getDebugLoc();
Dan Gohman034f60e2008-03-11 01:59:03 +00003527 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
3528 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00003529 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00003530
Chris Lattner1a635d62006-04-14 06:01:58 +00003531 if (Op.getOperand(0).getValueType() == MVT::i64) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003532 SDValue Bits = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003533 MVT::f64, Op.getOperand(0));
3534 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Bits);
Chris Lattner1a635d62006-04-14 06:01:58 +00003535 if (Op.getValueType() == MVT::f32)
Scott Michelfdc40a02009-02-17 22:15:04 +00003536 FP = DAG.getNode(ISD::FP_ROUND, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003537 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003538 return FP;
3539 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003540
Chris Lattner1a635d62006-04-14 06:01:58 +00003541 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
3542 "Unhandled SINT_TO_FP type in custom expander!");
3543 // Since we only generate this in 64-bit mode, we can take advantage of
3544 // 64-bit registers. In particular, sign extend the input value into the
3545 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
3546 // then lfd it and fcfid it.
3547 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3548 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003550 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003551
Dale Johannesen33c960f2009-02-04 20:06:27 +00003552 SDValue Ext64 = DAG.getNode(PPCISD::EXTSW_32, dl, MVT::i32,
Chris Lattner1a635d62006-04-14 06:01:58 +00003553 Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003554
Chris Lattner1a635d62006-04-14 06:01:58 +00003555 // STD the extended value into the stack slot.
Dan Gohmana54cf172008-07-11 22:44:52 +00003556 MachineMemOperand MO(PseudoSourceValue::getFixedStack(FrameIdx),
3557 MachineMemOperand::MOStore, 0, 8, 8);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003558 SDValue Store = DAG.getNode(PPCISD::STD_32, dl, MVT::Other,
Chris Lattner1a635d62006-04-14 06:01:58 +00003559 DAG.getEntryNode(), Ext64, FIdx,
Dan Gohman69de1932008-02-06 22:27:42 +00003560 DAG.getMemOperand(MO));
Chris Lattner1a635d62006-04-14 06:01:58 +00003561 // Load the value as a double.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003562 SDValue Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, NULL, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003563
Chris Lattner1a635d62006-04-14 06:01:58 +00003564 // FCFID it and return it.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003565 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Ld);
Chris Lattner1a635d62006-04-14 06:01:58 +00003566 if (Op.getValueType() == MVT::f32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003567 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003568 return FP;
3569}
3570
Dan Gohman475871a2008-07-27 21:46:04 +00003571SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003572 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003573 /*
3574 The rounding mode is in bits 30:31 of FPSR, and has the following
3575 settings:
3576 00 Round to nearest
3577 01 Round to 0
3578 10 Round to +inf
3579 11 Round to -inf
3580
3581 FLT_ROUNDS, on the other hand, expects the following:
3582 -1 Undefined
3583 0 Round to 0
3584 1 Round to nearest
3585 2 Round to +inf
3586 3 Round to -inf
3587
3588 To perform the conversion, we do:
3589 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
3590 */
3591
3592 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003593 MVT VT = Op.getValueType();
3594 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3595 std::vector<MVT> NodeTys;
Dan Gohman475871a2008-07-27 21:46:04 +00003596 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003597
3598 // Save FP Control Word to register
3599 NodeTys.push_back(MVT::f64); // return register
3600 NodeTys.push_back(MVT::Flag); // unused in this context
Dale Johannesen33c960f2009-02-04 20:06:27 +00003601 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003602
3603 // Save FP register to stack slot
3604 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00003605 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003606 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003607 StackSlot, NULL, 0);
3608
3609 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00003610 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003611 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
3612 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, NULL, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003613
3614 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00003615 SDValue CWD1 =
Dale Johannesen33c960f2009-02-04 20:06:27 +00003616 DAG.getNode(ISD::AND, dl, MVT::i32,
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003617 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00003618 SDValue CWD2 =
Dale Johannesen33c960f2009-02-04 20:06:27 +00003619 DAG.getNode(ISD::SRL, dl, MVT::i32,
3620 DAG.getNode(ISD::AND, dl, MVT::i32,
3621 DAG.getNode(ISD::XOR, dl, MVT::i32,
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003622 CWD, DAG.getConstant(3, MVT::i32)),
3623 DAG.getConstant(3, MVT::i32)),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003624 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003625
Dan Gohman475871a2008-07-27 21:46:04 +00003626 SDValue RetVal =
Dale Johannesen33c960f2009-02-04 20:06:27 +00003627 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003628
Duncan Sands83ec4b62008-06-06 12:08:01 +00003629 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00003630 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003631}
3632
Dan Gohman475871a2008-07-27 21:46:04 +00003633SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003634 MVT VT = Op.getValueType();
3635 unsigned BitWidth = VT.getSizeInBits();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003636 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003637 assert(Op.getNumOperands() == 3 &&
3638 VT == Op.getOperand(1).getValueType() &&
3639 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003640
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00003641 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003642 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003643 SDValue Lo = Op.getOperand(0);
3644 SDValue Hi = Op.getOperand(1);
3645 SDValue Amt = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003646 MVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003647
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003648 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003649 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003650 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
3651 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
3652 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
3653 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003654 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003655 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
3656 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3657 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003658 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003659 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003660}
3661
Dan Gohman475871a2008-07-27 21:46:04 +00003662SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003663 MVT VT = Op.getValueType();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003664 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003665 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003666 assert(Op.getNumOperands() == 3 &&
3667 VT == Op.getOperand(1).getValueType() &&
3668 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003669
Dan Gohman9ed06db2008-03-07 20:36:53 +00003670 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003671 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003672 SDValue Lo = Op.getOperand(0);
3673 SDValue Hi = Op.getOperand(1);
3674 SDValue Amt = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003675 MVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003676
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003677 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003678 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003679 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3680 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3681 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3682 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003683 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003684 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
3685 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3686 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003687 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003688 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003689}
3690
Dan Gohman475871a2008-07-27 21:46:04 +00003691SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003692 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003693 MVT VT = Op.getValueType();
3694 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003695 assert(Op.getNumOperands() == 3 &&
3696 VT == Op.getOperand(1).getValueType() &&
3697 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003698
Dan Gohman9ed06db2008-03-07 20:36:53 +00003699 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00003700 SDValue Lo = Op.getOperand(0);
3701 SDValue Hi = Op.getOperand(1);
3702 SDValue Amt = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003703 MVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003704
Dale Johannesenf5d97892009-02-04 01:48:28 +00003705 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003706 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00003707 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3708 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3709 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3710 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003711 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00003712 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
3713 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
3714 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003715 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00003716 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003717 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003718}
3719
3720//===----------------------------------------------------------------------===//
3721// Vector related lowering.
3722//
3723
Chris Lattner4a998b92006-04-17 06:00:21 +00003724/// BuildSplatI - Build a canonical splati of Val with an element size of
3725/// SplatSize. Cast the result to VT.
Dan Gohman475871a2008-07-27 21:46:04 +00003726static SDValue BuildSplatI(int Val, unsigned SplatSize, MVT VT,
Dale Johannesened2eee62009-02-06 01:31:28 +00003727 SelectionDAG &DAG, DebugLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00003728 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00003729
Duncan Sands83ec4b62008-06-06 12:08:01 +00003730 static const MVT VTys[] = { // canonical VT to use for each size.
Chris Lattner4a998b92006-04-17 06:00:21 +00003731 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
3732 };
Chris Lattner70fa4932006-12-01 01:45:39 +00003733
Duncan Sands83ec4b62008-06-06 12:08:01 +00003734 MVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003735
Chris Lattner70fa4932006-12-01 01:45:39 +00003736 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
3737 if (Val == -1)
3738 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00003739
Duncan Sands83ec4b62008-06-06 12:08:01 +00003740 MVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003741
Chris Lattner4a998b92006-04-17 06:00:21 +00003742 // Build a canonical splat for this value.
Eli Friedman1a8229b2009-05-24 02:03:36 +00003743 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003744 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003745 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00003746 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
3747 &Ops[0], Ops.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00003748 return DAG.getNode(ISD::BIT_CONVERT, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003749}
3750
Chris Lattnere7c768e2006-04-18 03:24:30 +00003751/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00003752/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003753static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Dale Johannesened2eee62009-02-06 01:31:28 +00003754 SelectionDAG &DAG, DebugLoc dl,
3755 MVT DestVT = MVT::Other) {
Chris Lattnere7c768e2006-04-18 03:24:30 +00003756 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003757 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Chris Lattner6876e662006-04-17 06:58:41 +00003758 DAG.getConstant(IID, MVT::i32), LHS, RHS);
3759}
3760
Chris Lattnere7c768e2006-04-18 03:24:30 +00003761/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
3762/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003763static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00003764 SDValue Op2, SelectionDAG &DAG,
3765 DebugLoc dl, MVT DestVT = MVT::Other) {
Chris Lattnere7c768e2006-04-18 03:24:30 +00003766 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003767 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Chris Lattnere7c768e2006-04-18 03:24:30 +00003768 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
3769}
3770
3771
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003772/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
3773/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003774static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Dale Johannesened2eee62009-02-06 01:31:28 +00003775 MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003776 // Force LHS/RHS to be the right type.
Dale Johannesened2eee62009-02-06 01:31:28 +00003777 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, LHS);
3778 RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00003779
Nate Begeman9008ca62009-04-27 18:41:29 +00003780 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003781 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003782 Ops[i] = i + Amt;
3783 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Dale Johannesened2eee62009-02-06 01:31:28 +00003784 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003785}
3786
Chris Lattnerf1b47082006-04-14 05:19:18 +00003787// If this is a case we can't handle, return null and let the default
3788// expansion code take care of it. If we CAN select this case, and if it
3789// selects to a single instruction, return Op. Otherwise, if we can codegen
3790// this case more efficiently than a constant pool load, lower it to the
3791// sequence of ops that should be used.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003792SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00003793 DebugLoc dl = Op.getDebugLoc();
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003794 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3795 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00003796
Bob Wilson24e338e2009-03-02 23:24:16 +00003797 // Check if this is a splat of a constant value.
3798 APInt APSplatBits, APSplatUndef;
3799 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003800 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00003801 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
3802 HasAnyUndefs) || SplatBitSize > 32)
3803 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00003804
Bob Wilsonf2950b02009-03-03 19:26:27 +00003805 unsigned SplatBits = APSplatBits.getZExtValue();
3806 unsigned SplatUndef = APSplatUndef.getZExtValue();
3807 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00003808
Bob Wilsonf2950b02009-03-03 19:26:27 +00003809 // First, handle single instruction cases.
3810
3811 // All zeros?
3812 if (SplatBits == 0) {
3813 // Canonicalize all zero vectors to be v4i32.
3814 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
3815 SDValue Z = DAG.getConstant(0, MVT::i32);
3816 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
3817 Op = DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003818 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003819 return Op;
3820 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00003821
Bob Wilsonf2950b02009-03-03 19:26:27 +00003822 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
3823 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
3824 (32-SplatBitSize));
3825 if (SextVal >= -16 && SextVal <= 15)
3826 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003827
3828
Bob Wilsonf2950b02009-03-03 19:26:27 +00003829 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00003830
Bob Wilsonf2950b02009-03-03 19:26:27 +00003831 // If this value is in the range [-32,30] and is even, use:
3832 // tmp = VSPLTI[bhw], result = add tmp, tmp
3833 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
3834 SDValue Res = BuildSplatI(SextVal >> 1, SplatSize, MVT::Other, DAG, dl);
3835 Res = DAG.getNode(ISD::ADD, dl, Res.getValueType(), Res, Res);
3836 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3837 }
3838
3839 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
3840 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
3841 // for fneg/fabs.
3842 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
3843 // Make -1 and vspltisw -1:
3844 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
3845
3846 // Make the VSLW intrinsic, computing 0x8000_0000.
3847 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
3848 OnesV, DAG, dl);
3849
3850 // xor by OnesV to invert it.
3851 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
3852 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3853 }
3854
3855 // Check to see if this is a wide variety of vsplti*, binop self cases.
3856 static const signed char SplatCsts[] = {
3857 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
3858 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
3859 };
3860
3861 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
3862 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
3863 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
3864 int i = SplatCsts[idx];
3865
3866 // Figure out what shift amount will be used by altivec if shifted by i in
3867 // this splat size.
3868 unsigned TypeShiftAmt = i & (SplatBitSize-1);
3869
3870 // vsplti + shl self.
3871 if (SextVal == (i << (int)TypeShiftAmt)) {
3872 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
3873 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3874 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
3875 Intrinsic::ppc_altivec_vslw
3876 };
3877 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Dale Johannesened2eee62009-02-06 01:31:28 +00003878 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003879 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003880
Bob Wilsonf2950b02009-03-03 19:26:27 +00003881 // vsplti + srl self.
3882 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
3883 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
3884 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3885 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
3886 Intrinsic::ppc_altivec_vsrw
3887 };
3888 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Dale Johannesened2eee62009-02-06 01:31:28 +00003889 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003890 }
3891
Bob Wilsonf2950b02009-03-03 19:26:27 +00003892 // vsplti + sra self.
3893 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
3894 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
3895 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3896 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
3897 Intrinsic::ppc_altivec_vsraw
3898 };
3899 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
3900 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003901 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003902
Bob Wilsonf2950b02009-03-03 19:26:27 +00003903 // vsplti + rol self.
3904 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
3905 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
3906 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
3907 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3908 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
3909 Intrinsic::ppc_altivec_vrlw
3910 };
3911 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
3912 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003914
Bob Wilsonf2950b02009-03-03 19:26:27 +00003915 // t = vsplti c, result = vsldoi t, t, 1
3916 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
3917 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
3918 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00003919 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003920 // t = vsplti c, result = vsldoi t, t, 2
3921 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
3922 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
3923 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003924 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003925 // t = vsplti c, result = vsldoi t, t, 3
3926 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
3927 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
3928 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
3929 }
3930 }
3931
3932 // Three instruction sequences.
3933
3934 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
3935 if (SextVal >= 0 && SextVal <= 31) {
3936 SDValue LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG, dl);
3937 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
3938 LHS = DAG.getNode(ISD::SUB, dl, LHS.getValueType(), LHS, RHS);
3939 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
3940 }
3941 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
3942 if (SextVal >= -31 && SextVal <= 0) {
3943 SDValue LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG, dl);
3944 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
3945 LHS = DAG.getNode(ISD::ADD, dl, LHS.getValueType(), LHS, RHS);
3946 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003947 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003948
Dan Gohman475871a2008-07-27 21:46:04 +00003949 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00003950}
3951
Chris Lattner59138102006-04-17 05:28:54 +00003952/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3953/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00003954static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00003955 SDValue RHS, SelectionDAG &DAG,
Dale Johannesened2eee62009-02-06 01:31:28 +00003956 DebugLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00003957 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00003958 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00003959 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003960
Chris Lattner59138102006-04-17 05:28:54 +00003961 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00003962 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 +00003963 OP_VMRGHW,
3964 OP_VMRGLW,
3965 OP_VSPLTISW0,
3966 OP_VSPLTISW1,
3967 OP_VSPLTISW2,
3968 OP_VSPLTISW3,
3969 OP_VSLDOI4,
3970 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00003971 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00003972 };
Scott Michelfdc40a02009-02-17 22:15:04 +00003973
Chris Lattner59138102006-04-17 05:28:54 +00003974 if (OpNum == OP_COPY) {
3975 if (LHSID == (1*9+2)*9+3) return LHS;
3976 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3977 return RHS;
3978 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003979
Dan Gohman475871a2008-07-27 21:46:04 +00003980 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00003981 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3982 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003983
Nate Begeman9008ca62009-04-27 18:41:29 +00003984 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00003985 switch (OpNum) {
3986 default: assert(0 && "Unknown i32 permute!");
3987 case OP_VMRGHW:
3988 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
3989 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
3990 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
3991 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
3992 break;
3993 case OP_VMRGLW:
3994 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
3995 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
3996 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
3997 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
3998 break;
3999 case OP_VSPLTISW0:
4000 for (unsigned i = 0; i != 16; ++i)
4001 ShufIdxs[i] = (i&3)+0;
4002 break;
4003 case OP_VSPLTISW1:
4004 for (unsigned i = 0; i != 16; ++i)
4005 ShufIdxs[i] = (i&3)+4;
4006 break;
4007 case OP_VSPLTISW2:
4008 for (unsigned i = 0; i != 16; ++i)
4009 ShufIdxs[i] = (i&3)+8;
4010 break;
4011 case OP_VSPLTISW3:
4012 for (unsigned i = 0; i != 16; ++i)
4013 ShufIdxs[i] = (i&3)+12;
4014 break;
4015 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00004016 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00004017 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00004018 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00004019 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00004020 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00004021 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004022 MVT VT = OpLHS.getValueType();
4023 OpLHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpLHS);
4024 OpRHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpRHS);
4025 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
4026 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00004027}
4028
Chris Lattnerf1b47082006-04-14 05:19:18 +00004029/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
4030/// is a shuffle we can handle in a single instruction, return it. Otherwise,
4031/// return the code it can be lowered into. Worst case, it can always be
4032/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00004033SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Nate Begeman9008ca62009-04-27 18:41:29 +00004034 SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00004035 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004036 SDValue V1 = Op.getOperand(0);
4037 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004038 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
4039 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004040
Chris Lattnerf1b47082006-04-14 05:19:18 +00004041 // Cases that are handled by instructions that take permute immediates
4042 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
4043 // selected by the instruction selector.
4044 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004045 if (PPC::isSplatShuffleMask(SVOp, 1) ||
4046 PPC::isSplatShuffleMask(SVOp, 2) ||
4047 PPC::isSplatShuffleMask(SVOp, 4) ||
4048 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
4049 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
4050 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
4051 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
4052 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
4053 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
4054 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
4055 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
4056 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00004057 return Op;
4058 }
4059 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004060
Chris Lattnerf1b47082006-04-14 05:19:18 +00004061 // Altivec has a variety of "shuffle immediates" that take two vector inputs
4062 // and produce a fixed permutation. If any of these match, do not lower to
4063 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00004064 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
4065 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
4066 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
4067 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
4068 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
4069 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
4070 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
4071 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
4072 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00004073 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004074
Chris Lattner59138102006-04-17 05:28:54 +00004075 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
4076 // perfect shuffle table to emit an optimal matching sequence.
Nate Begeman9008ca62009-04-27 18:41:29 +00004077 SmallVector<int, 16> PermMask;
4078 SVOp->getMask(PermMask);
4079
Chris Lattner59138102006-04-17 05:28:54 +00004080 unsigned PFIndexes[4];
4081 bool isFourElementShuffle = true;
4082 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
4083 unsigned EltNo = 8; // Start out undef.
4084 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00004085 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00004086 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00004087
Nate Begeman9008ca62009-04-27 18:41:29 +00004088 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00004089 if ((ByteSource & 3) != j) {
4090 isFourElementShuffle = false;
4091 break;
4092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004093
Chris Lattner59138102006-04-17 05:28:54 +00004094 if (EltNo == 8) {
4095 EltNo = ByteSource/4;
4096 } else if (EltNo != ByteSource/4) {
4097 isFourElementShuffle = false;
4098 break;
4099 }
4100 }
4101 PFIndexes[i] = EltNo;
4102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004103
4104 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00004105 // perfect shuffle vector to determine if it is cost effective to do this as
4106 // discrete instructions, or whether we should use a vperm.
4107 if (isFourElementShuffle) {
4108 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00004109 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00004110 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00004111
Chris Lattner59138102006-04-17 05:28:54 +00004112 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4113 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00004114
Chris Lattner59138102006-04-17 05:28:54 +00004115 // Determining when to avoid vperm is tricky. Many things affect the cost
4116 // of vperm, particularly how many times the perm mask needs to be computed.
4117 // For example, if the perm mask can be hoisted out of a loop or is already
4118 // used (perhaps because there are multiple permutes with the same shuffle
4119 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
4120 // the loop requires an extra register.
4121 //
4122 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00004123 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00004124 // available, if this block is within a loop, we should avoid using vperm
4125 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00004126 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00004127 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00004128 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004129
Chris Lattnerf1b47082006-04-14 05:19:18 +00004130 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
4131 // vector that will get spilled to the constant pool.
4132 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00004133
Chris Lattnerf1b47082006-04-14 05:19:18 +00004134 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
4135 // that it is in input element units, not in bytes. Convert now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004136 MVT EltVT = V1.getValueType().getVectorElementType();
4137 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00004138
Dan Gohman475871a2008-07-27 21:46:04 +00004139 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00004140 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
4141 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00004142
Chris Lattnerf1b47082006-04-14 05:19:18 +00004143 for (unsigned j = 0; j != BytesPerElement; ++j)
4144 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Eli Friedman1a8229b2009-05-24 02:03:36 +00004145 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00004146 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004147
Evan Chenga87008d2009-02-25 22:49:59 +00004148 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
4149 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00004150 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00004151}
4152
Chris Lattner90564f22006-04-18 17:59:36 +00004153/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
4154/// altivec comparison. If it is, return true and fill in Opc/isDot with
4155/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00004156static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00004157 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004158 unsigned IntrinsicID =
4159 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00004160 CompareOpc = -1;
4161 isDot = false;
4162 switch (IntrinsicID) {
4163 default: return false;
4164 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00004165 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
4166 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
4167 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
4168 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
4169 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
4170 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
4171 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
4172 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
4173 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
4174 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
4175 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
4176 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
4177 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004178
Chris Lattner1a635d62006-04-14 06:01:58 +00004179 // Normal Comparisons.
4180 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
4181 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
4182 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
4183 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
4184 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
4185 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
4186 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
4187 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
4188 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
4189 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
4190 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
4191 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
4192 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
4193 }
Chris Lattner90564f22006-04-18 17:59:36 +00004194 return true;
4195}
4196
4197/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
4198/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00004199SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004200 SelectionDAG &DAG) {
Chris Lattner90564f22006-04-18 17:59:36 +00004201 // If this is a lowered altivec predicate compare, CompareOpc is set to the
4202 // opcode number of the comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00004203 DebugLoc dl = Op.getDebugLoc();
Chris Lattner90564f22006-04-18 17:59:36 +00004204 int CompareOpc;
4205 bool isDot;
4206 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00004207 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00004208
Chris Lattner90564f22006-04-18 17:59:36 +00004209 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00004210 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004211 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner1a635d62006-04-14 06:01:58 +00004212 Op.getOperand(1), Op.getOperand(2),
4213 DAG.getConstant(CompareOpc, MVT::i32));
Dale Johannesen3484c092009-02-05 22:07:54 +00004214 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00004215 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004216
Chris Lattner1a635d62006-04-14 06:01:58 +00004217 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00004218 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00004219 Op.getOperand(2), // LHS
4220 Op.getOperand(3), // RHS
4221 DAG.getConstant(CompareOpc, MVT::i32)
4222 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00004223 std::vector<MVT> VTs;
Chris Lattner1a635d62006-04-14 06:01:58 +00004224 VTs.push_back(Op.getOperand(2).getValueType());
4225 VTs.push_back(MVT::Flag);
Dale Johannesen3484c092009-02-05 22:07:54 +00004226 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00004227
Chris Lattner1a635d62006-04-14 06:01:58 +00004228 // Now that we have the comparison, emit a copy from the CR to a GPR.
4229 // This is flagged to the above dot comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00004230 SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32,
Chris Lattner1a635d62006-04-14 06:01:58 +00004231 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00004232 CompNode.getValue(1));
4233
Chris Lattner1a635d62006-04-14 06:01:58 +00004234 // Unpack the result based on how the target uses it.
4235 unsigned BitNo; // Bit # of CR6.
4236 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004237 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00004238 default: // Can't happen, don't crash on invalid number though.
4239 case 0: // Return the value of the EQ bit of CR6.
4240 BitNo = 0; InvertBit = false;
4241 break;
4242 case 1: // Return the inverted value of the EQ bit of CR6.
4243 BitNo = 0; InvertBit = true;
4244 break;
4245 case 2: // Return the value of the LT bit of CR6.
4246 BitNo = 2; InvertBit = false;
4247 break;
4248 case 3: // Return the inverted value of the LT bit of CR6.
4249 BitNo = 2; InvertBit = true;
4250 break;
4251 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004252
Chris Lattner1a635d62006-04-14 06:01:58 +00004253 // Shift the bit into the low position.
Dale Johannesen3484c092009-02-05 22:07:54 +00004254 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
Chris Lattner1a635d62006-04-14 06:01:58 +00004255 DAG.getConstant(8-(3-BitNo), MVT::i32));
4256 // Isolate the bit.
Dale Johannesen3484c092009-02-05 22:07:54 +00004257 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
Chris Lattner1a635d62006-04-14 06:01:58 +00004258 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00004259
Chris Lattner1a635d62006-04-14 06:01:58 +00004260 // If we are supposed to, toggle the bit.
4261 if (InvertBit)
Dale Johannesen3484c092009-02-05 22:07:54 +00004262 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
Chris Lattner1a635d62006-04-14 06:01:58 +00004263 DAG.getConstant(1, MVT::i32));
4264 return Flags;
4265}
4266
Scott Michelfdc40a02009-02-17 22:15:04 +00004267SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004268 SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004269 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00004270 // Create a stack slot that is 16-byte aligned.
4271 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4272 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004273 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004274 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004275
Chris Lattner1a635d62006-04-14 06:01:58 +00004276 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004277 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004278 Op.getOperand(0), FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004279 // Load it out.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004280 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004281}
4282
Dan Gohman475871a2008-07-27 21:46:04 +00004283SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00004284 DebugLoc dl = Op.getDebugLoc();
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004285 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00004286 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004287
Dale Johannesened2eee62009-02-06 01:31:28 +00004288 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
4289 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00004290
Dan Gohman475871a2008-07-27 21:46:04 +00004291 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00004292 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00004293
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004294 // Shrinkify inputs to v8i16.
Dale Johannesened2eee62009-02-06 01:31:28 +00004295 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, LHS);
4296 RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHS);
4297 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00004298
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004299 // Low parts multiplied together, generating 32-bit results (we ignore the
4300 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00004301 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Dale Johannesened2eee62009-02-06 01:31:28 +00004302 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004303
Dan Gohman475871a2008-07-27 21:46:04 +00004304 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00004305 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004306 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00004307 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00004308 Neg16, DAG, dl);
4309 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004310 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004311 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004312
Dale Johannesened2eee62009-02-06 01:31:28 +00004313 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004314
Chris Lattnercea2aa72006-04-18 04:28:57 +00004315 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00004316 LHS, RHS, Zero, DAG, dl);
Chris Lattner19a81522006-04-18 03:57:35 +00004317 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004318 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004319
Chris Lattner19a81522006-04-18 03:57:35 +00004320 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004321 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Dale Johannesened2eee62009-02-06 01:31:28 +00004322 LHS, RHS, DAG, dl, MVT::v8i16);
4323 EvenParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004324
Chris Lattner19a81522006-04-18 03:57:35 +00004325 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004326 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Dale Johannesened2eee62009-02-06 01:31:28 +00004327 LHS, RHS, DAG, dl, MVT::v8i16);
4328 OddParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004329
Chris Lattner19a81522006-04-18 03:57:35 +00004330 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00004331 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00004332 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004333 Ops[i*2 ] = 2*i+1;
4334 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00004335 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004336 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004337 } else {
4338 assert(0 && "Unknown mul to lower!");
4339 abort();
4340 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00004341}
4342
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004343/// LowerOperation - Provide custom lowering hooks for some operations.
4344///
Dan Gohman475871a2008-07-27 21:46:04 +00004345SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004346 switch (Op.getOpcode()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004347 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00004348 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4349 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00004350 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00004351 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00004352 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bill Wendling77959322008-09-17 00:30:57 +00004353 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004354 case ISD::VASTART:
Nicolas Geoffray01119992007-04-03 13:59:52 +00004355 return LowerVASTART(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
4356 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00004357
4358 case ISD::VAARG:
Nicolas Geoffray01119992007-04-03 13:59:52 +00004359 return LowerVAARG(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
4360 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
4361
Chris Lattneref957102006-06-21 00:34:03 +00004362 case ISD::FORMAL_ARGUMENTS:
Tilmann Schellerffd02002009-07-03 06:45:56 +00004363 if (PPCSubTarget.isELF32_ABI()) {
4364 return LowerFORMAL_ARGUMENTS_SVR4(Op, DAG, VarArgsFrameIndex,
4365 VarArgsStackOffset, VarArgsNumGPR,
4366 VarArgsNumFPR, PPCSubTarget);
4367 } else {
4368 return LowerFORMAL_ARGUMENTS(Op, DAG, VarArgsFrameIndex,
4369 VarArgsStackOffset, VarArgsNumGPR,
4370 VarArgsNumFPR, PPCSubTarget);
4371 }
Nicolas Geoffray01119992007-04-03 13:59:52 +00004372
Tilmann Schellerffd02002009-07-03 06:45:56 +00004373 case ISD::CALL:
4374 if (PPCSubTarget.isELF32_ABI()) {
4375 return LowerCALL_SVR4(Op, DAG, PPCSubTarget, getTargetMachine());
4376 } else {
4377 return LowerCALL(Op, DAG, PPCSubTarget, getTargetMachine());
4378 }
4379
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004380 case ISD::RET: return LowerRET(Op, DAG, getTargetMachine());
Jim Laskeyefc7e522006-12-04 22:04:42 +00004381 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00004382 case ISD::DYNAMIC_STACKALLOC:
4383 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00004384
Chris Lattner1a635d62006-04-14 06:01:58 +00004385 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004386 case ISD::FP_TO_UINT:
4387 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00004388 Op.getDebugLoc());
Chris Lattner1a635d62006-04-14 06:01:58 +00004389 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00004390 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004391
Chris Lattner1a635d62006-04-14 06:01:58 +00004392 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00004393 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
4394 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
4395 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004396
Chris Lattner1a635d62006-04-14 06:01:58 +00004397 // Vector-related lowering.
4398 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4399 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4400 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4401 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00004402 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004403
Chris Lattner3fc027d2007-12-08 06:59:59 +00004404 // Frame & Return address.
4405 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004406 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00004407 }
Dan Gohman475871a2008-07-27 21:46:04 +00004408 return SDValue();
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004409}
4410
Duncan Sands1607f052008-12-01 11:39:25 +00004411void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
4412 SmallVectorImpl<SDValue>&Results,
4413 SelectionDAG &DAG) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004414 DebugLoc dl = N->getDebugLoc();
Chris Lattner1f873002007-11-28 18:44:47 +00004415 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00004416 default:
Duncan Sands1607f052008-12-01 11:39:25 +00004417 assert(false && "Do not know how to custom type legalize this operation!");
4418 return;
4419 case ISD::FP_ROUND_INREG: {
4420 assert(N->getValueType(0) == MVT::ppcf128);
4421 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00004422 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesen3484c092009-02-05 22:07:54 +00004423 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004424 DAG.getIntPtrConstant(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00004425 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
4426 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004427 DAG.getIntPtrConstant(1));
4428
4429 // This sequence changes FPSCR to do round-to-zero, adds the two halves
4430 // of the long double, and puts FPSCR back the way it was. We do not
4431 // actually model FPSCR.
4432 std::vector<MVT> NodeTys;
4433 SDValue Ops[4], Result, MFFSreg, InFlag, FPreg;
4434
4435 NodeTys.push_back(MVT::f64); // Return register
4436 NodeTys.push_back(MVT::Flag); // Returns a flag for later insns
Dale Johannesen3484c092009-02-05 22:07:54 +00004437 Result = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Duncan Sands1607f052008-12-01 11:39:25 +00004438 MFFSreg = Result.getValue(0);
4439 InFlag = Result.getValue(1);
4440
4441 NodeTys.clear();
4442 NodeTys.push_back(MVT::Flag); // Returns a flag
4443 Ops[0] = DAG.getConstant(31, MVT::i32);
4444 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004445 Result = DAG.getNode(PPCISD::MTFSB1, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004446 InFlag = Result.getValue(0);
4447
4448 NodeTys.clear();
4449 NodeTys.push_back(MVT::Flag); // Returns a flag
4450 Ops[0] = DAG.getConstant(30, MVT::i32);
4451 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004452 Result = DAG.getNode(PPCISD::MTFSB0, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004453 InFlag = Result.getValue(0);
4454
4455 NodeTys.clear();
4456 NodeTys.push_back(MVT::f64); // result of add
4457 NodeTys.push_back(MVT::Flag); // Returns a flag
4458 Ops[0] = Lo;
4459 Ops[1] = Hi;
4460 Ops[2] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004461 Result = DAG.getNode(PPCISD::FADDRTZ, dl, NodeTys, Ops, 3);
Duncan Sands1607f052008-12-01 11:39:25 +00004462 FPreg = Result.getValue(0);
4463 InFlag = Result.getValue(1);
4464
4465 NodeTys.clear();
4466 NodeTys.push_back(MVT::f64);
4467 Ops[0] = DAG.getConstant(1, MVT::i32);
4468 Ops[1] = MFFSreg;
4469 Ops[2] = FPreg;
4470 Ops[3] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004471 Result = DAG.getNode(PPCISD::MTFSF, dl, NodeTys, Ops, 4);
Duncan Sands1607f052008-12-01 11:39:25 +00004472 FPreg = Result.getValue(0);
4473
4474 // We know the low half is about to be thrown away, so just use something
4475 // convenient.
Scott Michelfdc40a02009-02-17 22:15:04 +00004476 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00004477 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00004478 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00004479 }
Duncan Sands1607f052008-12-01 11:39:25 +00004480 case ISD::FP_TO_SINT:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004481 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00004482 return;
Chris Lattner1f873002007-11-28 18:44:47 +00004483 }
4484}
4485
4486
Chris Lattner1a635d62006-04-14 06:01:58 +00004487//===----------------------------------------------------------------------===//
4488// Other Lowering Code
4489//===----------------------------------------------------------------------===//
4490
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004491MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004492PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004493 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004494 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004495 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4496
4497 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4498 MachineFunction *F = BB->getParent();
4499 MachineFunction::iterator It = BB;
4500 ++It;
4501
4502 unsigned dest = MI->getOperand(0).getReg();
4503 unsigned ptrA = MI->getOperand(1).getReg();
4504 unsigned ptrB = MI->getOperand(2).getReg();
4505 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004506 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004507
4508 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4509 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4510 F->insert(It, loopMBB);
4511 F->insert(It, exitMBB);
4512 exitMBB->transferSuccessors(BB);
4513
4514 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00004515 unsigned TmpReg = (!BinOpcode) ? incr :
4516 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00004517 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4518 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004519
4520 // thisMBB:
4521 // ...
4522 // fallthrough --> loopMBB
4523 BB->addSuccessor(loopMBB);
4524
4525 // loopMBB:
4526 // l[wd]arx dest, ptr
4527 // add r0, dest, incr
4528 // st[wd]cx. r0, ptr
4529 // bne- loopMBB
4530 // fallthrough --> exitMBB
4531 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004532 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004533 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004534 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004535 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
4536 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004537 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004538 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004539 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004540 BB->addSuccessor(loopMBB);
4541 BB->addSuccessor(exitMBB);
4542
4543 // exitMBB:
4544 // ...
4545 BB = exitMBB;
4546 return BB;
4547}
4548
4549MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00004550PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00004551 MachineBasicBlock *BB,
4552 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004553 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004554 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00004555 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4556 // In 64 bit mode we have to use 64 bits for addresses, even though the
4557 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
4558 // registers without caring whether they're 32 or 64, but here we're
4559 // doing actual arithmetic on the addresses.
4560 bool is64bit = PPCSubTarget.isPPC64();
4561
4562 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4563 MachineFunction *F = BB->getParent();
4564 MachineFunction::iterator It = BB;
4565 ++It;
4566
4567 unsigned dest = MI->getOperand(0).getReg();
4568 unsigned ptrA = MI->getOperand(1).getReg();
4569 unsigned ptrB = MI->getOperand(2).getReg();
4570 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004571 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00004572
4573 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4574 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4575 F->insert(It, loopMBB);
4576 F->insert(It, exitMBB);
4577 exitMBB->transferSuccessors(BB);
4578
4579 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004580 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004581 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4582 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00004583 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4584 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4585 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4586 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
4587 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4588 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4589 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4590 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4591 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
4592 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004593 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004594 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00004595 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004596
4597 // thisMBB:
4598 // ...
4599 // fallthrough --> loopMBB
4600 BB->addSuccessor(loopMBB);
4601
4602 // The 4-byte load must be aligned, while a char or short may be
4603 // anywhere in the word. Hence all this nasty bookkeeping code.
4604 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4605 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004606 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00004607 // rlwinm ptr, ptr1, 0, 0, 29
4608 // slw incr2, incr, shift
4609 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4610 // slw mask, mask2, shift
4611 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004612 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00004613 // add tmp, tmpDest, incr2
4614 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00004615 // and tmp3, tmp, mask
4616 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004617 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00004618 // bne- loopMBB
4619 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00004620 // srw dest, tmpDest, shift
Dale Johannesen97efa362008-08-28 17:53:09 +00004621
4622 if (ptrA!=PPC::R0) {
4623 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004624 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004625 .addReg(ptrA).addReg(ptrB);
4626 } else {
4627 Ptr1Reg = ptrB;
4628 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004629 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004630 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004631 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004632 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4633 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004634 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004635 .addReg(Ptr1Reg).addImm(0).addImm(61);
4636 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004637 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004638 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004639 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004640 .addReg(incr).addReg(ShiftReg);
4641 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004642 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00004643 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004644 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4645 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00004646 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004647 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004648 .addReg(Mask2Reg).addReg(ShiftReg);
4649
4650 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004651 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004652 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004653 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004654 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004655 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004656 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004657 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004658 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004659 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004660 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004661 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004662 BuildMI(BB, dl, TII->get(PPC::STWCX))
Dale Johannesen97efa362008-08-28 17:53:09 +00004663 .addReg(Tmp4Reg).addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004664 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004665 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00004666 BB->addSuccessor(loopMBB);
4667 BB->addSuccessor(exitMBB);
4668
4669 // exitMBB:
4670 // ...
4671 BB = exitMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004672 BuildMI(BB, dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg).addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00004673 return BB;
4674}
4675
4676MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004677PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004678 MachineBasicBlock *BB) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004679 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00004680
4681 // To "insert" these instructions we actually have to insert their
4682 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004683 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004684 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004685 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00004686
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004687 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00004688
4689 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
4690 MI->getOpcode() == PPC::SELECT_CC_I8 ||
4691 MI->getOpcode() == PPC::SELECT_CC_F4 ||
4692 MI->getOpcode() == PPC::SELECT_CC_F8 ||
4693 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
4694
4695 // The incoming instruction knows the destination vreg to set, the
4696 // condition code register to branch on, the true/false values to
4697 // select between, and a branch opcode to use.
4698
4699 // thisMBB:
4700 // ...
4701 // TrueVal = ...
4702 // cmpTY ccX, r1, r2
4703 // bCC copy1MBB
4704 // fallthrough --> copy0MBB
4705 MachineBasicBlock *thisMBB = BB;
4706 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4707 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4708 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004709 DebugLoc dl = MI->getDebugLoc();
4710 BuildMI(BB, dl, TII->get(PPC::BCC))
Evan Cheng53301922008-07-12 02:23:19 +00004711 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
4712 F->insert(It, copy0MBB);
4713 F->insert(It, sinkMBB);
4714 // Update machine-CFG edges by transferring all successors of the current
4715 // block to the new block which will contain the Phi node for the select.
4716 sinkMBB->transferSuccessors(BB);
4717 // Next, add the true and fallthrough blocks as its successors.
4718 BB->addSuccessor(copy0MBB);
4719 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004720
Evan Cheng53301922008-07-12 02:23:19 +00004721 // copy0MBB:
4722 // %FalseValue = ...
4723 // # fallthrough to sinkMBB
4724 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00004725
Evan Cheng53301922008-07-12 02:23:19 +00004726 // Update machine-CFG edges
4727 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004728
Evan Cheng53301922008-07-12 02:23:19 +00004729 // sinkMBB:
4730 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4731 // ...
4732 BB = sinkMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004733 BuildMI(BB, dl, TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00004734 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
4735 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4736 }
Dale Johannesen97efa362008-08-28 17:53:09 +00004737 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
4738 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
4739 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
4740 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004741 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
4742 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
4743 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
4744 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004745
4746 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
4747 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
4748 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
4749 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004750 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
4751 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
4752 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
4753 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004754
4755 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
4756 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
4757 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
4758 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004759 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
4760 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
4761 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
4762 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004763
4764 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
4765 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
4766 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
4767 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004768 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
4769 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
4770 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
4771 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004772
4773 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00004774 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004775 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00004776 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004777 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00004778 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004779 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00004780 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004781
4782 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
4783 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
4784 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
4785 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004786 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
4787 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
4788 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
4789 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004790
Dale Johannesen0e55f062008-08-29 18:29:46 +00004791 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
4792 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
4793 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
4794 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
4795 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
4796 BB = EmitAtomicBinary(MI, BB, false, 0);
4797 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
4798 BB = EmitAtomicBinary(MI, BB, true, 0);
4799
Evan Cheng53301922008-07-12 02:23:19 +00004800 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
4801 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
4802 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
4803
4804 unsigned dest = MI->getOperand(0).getReg();
4805 unsigned ptrA = MI->getOperand(1).getReg();
4806 unsigned ptrB = MI->getOperand(2).getReg();
4807 unsigned oldval = MI->getOperand(3).getReg();
4808 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004809 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00004810
Dale Johannesen65e39732008-08-25 18:53:26 +00004811 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4812 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4813 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00004814 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004815 F->insert(It, loop1MBB);
4816 F->insert(It, loop2MBB);
4817 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00004818 F->insert(It, exitMBB);
4819 exitMBB->transferSuccessors(BB);
4820
4821 // thisMBB:
4822 // ...
4823 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00004824 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00004825
Dale Johannesen65e39732008-08-25 18:53:26 +00004826 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00004827 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00004828 // cmp[wd] dest, oldval
4829 // bne- midMBB
4830 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00004831 // st[wd]cx. newval, ptr
4832 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00004833 // b exitBB
4834 // midMBB:
4835 // st[wd]cx. dest, ptr
4836 // exitBB:
4837 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004838 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00004839 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004840 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00004841 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004842 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00004843 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
4844 BB->addSuccessor(loop2MBB);
4845 BB->addSuccessor(midMBB);
4846
4847 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004848 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00004849 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004850 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00004851 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004852 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004853 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00004854 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004855
Dale Johannesen65e39732008-08-25 18:53:26 +00004856 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004857 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00004858 .addReg(dest).addReg(ptrA).addReg(ptrB);
4859 BB->addSuccessor(exitMBB);
4860
Evan Cheng53301922008-07-12 02:23:19 +00004861 // exitMBB:
4862 // ...
4863 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004864 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
4865 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
4866 // We must use 64-bit registers for addresses when targeting 64-bit,
4867 // since we're actually doing arithmetic on them. Other registers
4868 // can be 32-bit.
4869 bool is64bit = PPCSubTarget.isPPC64();
4870 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
4871
4872 unsigned dest = MI->getOperand(0).getReg();
4873 unsigned ptrA = MI->getOperand(1).getReg();
4874 unsigned ptrB = MI->getOperand(2).getReg();
4875 unsigned oldval = MI->getOperand(3).getReg();
4876 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004877 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004878
4879 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4880 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4881 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
4882 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4883 F->insert(It, loop1MBB);
4884 F->insert(It, loop2MBB);
4885 F->insert(It, midMBB);
4886 F->insert(It, exitMBB);
4887 exitMBB->transferSuccessors(BB);
4888
4889 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004890 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004891 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4892 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004893 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4894 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4895 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4896 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
4897 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
4898 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
4899 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
4900 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4901 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4902 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4903 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4904 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
4905 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
4906 unsigned Ptr1Reg;
4907 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
4908 // thisMBB:
4909 // ...
4910 // fallthrough --> loopMBB
4911 BB->addSuccessor(loop1MBB);
4912
4913 // The 4-byte load must be aligned, while a char or short may be
4914 // anywhere in the word. Hence all this nasty bookkeeping code.
4915 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4916 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004917 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004918 // rlwinm ptr, ptr1, 0, 0, 29
4919 // slw newval2, newval, shift
4920 // slw oldval2, oldval,shift
4921 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4922 // slw mask, mask2, shift
4923 // and newval3, newval2, mask
4924 // and oldval3, oldval2, mask
4925 // loop1MBB:
4926 // lwarx tmpDest, ptr
4927 // and tmp, tmpDest, mask
4928 // cmpw tmp, oldval3
4929 // bne- midMBB
4930 // loop2MBB:
4931 // andc tmp2, tmpDest, mask
4932 // or tmp4, tmp2, newval3
4933 // stwcx. tmp4, ptr
4934 // bne- loop1MBB
4935 // b exitBB
4936 // midMBB:
4937 // stwcx. tmpDest, ptr
4938 // exitBB:
4939 // srw dest, tmpDest, shift
4940 if (ptrA!=PPC::R0) {
4941 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004942 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004943 .addReg(ptrA).addReg(ptrB);
4944 } else {
4945 Ptr1Reg = ptrB;
4946 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004947 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004948 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004949 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004950 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4951 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004952 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004953 .addReg(Ptr1Reg).addImm(0).addImm(61);
4954 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004955 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004956 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004957 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004958 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004959 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004960 .addReg(oldval).addReg(ShiftReg);
4961 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004962 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004963 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004964 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4965 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
4966 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004967 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004968 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004969 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004970 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004971 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004972 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004973 .addReg(OldVal2Reg).addReg(MaskReg);
4974
4975 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004976 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004977 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004978 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
4979 .addReg(TmpDestReg).addReg(MaskReg);
4980 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004981 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004982 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004983 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
4984 BB->addSuccessor(loop2MBB);
4985 BB->addSuccessor(midMBB);
4986
4987 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004988 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
4989 .addReg(TmpDestReg).addReg(MaskReg);
4990 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
4991 .addReg(Tmp2Reg).addReg(NewVal3Reg);
4992 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004993 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004994 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004995 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004996 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004997 BB->addSuccessor(loop1MBB);
4998 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004999
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005000 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005001 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005002 .addReg(PPC::R0).addReg(PtrReg);
5003 BB->addSuccessor(exitMBB);
5004
5005 // exitMBB:
5006 // ...
5007 BB = exitMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005008 BuildMI(BB, dl, TII->get(PPC::SRW),dest).addReg(TmpReg).addReg(ShiftReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005009 } else {
Evan Cheng53301922008-07-12 02:23:19 +00005010 assert(0 && "Unexpected instr type to insert");
5011 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005012
Dan Gohman8e5f2c62008-07-07 23:14:23 +00005013 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005014 return BB;
5015}
5016
Chris Lattner1a635d62006-04-14 06:01:58 +00005017//===----------------------------------------------------------------------===//
5018// Target Optimization Hooks
5019//===----------------------------------------------------------------------===//
5020
Duncan Sands25cf2272008-11-24 14:53:14 +00005021SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
5022 DAGCombinerInfo &DCI) const {
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005023 TargetMachine &TM = getTargetMachine();
5024 SelectionDAG &DAG = DCI.DAG;
Dale Johannesen3484c092009-02-05 22:07:54 +00005025 DebugLoc dl = N->getDebugLoc();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005026 switch (N->getOpcode()) {
5027 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00005028 case PPCISD::SHL:
5029 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005030 if (C->getZExtValue() == 0) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00005031 return N->getOperand(0);
5032 }
5033 break;
5034 case PPCISD::SRL:
5035 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005036 if (C->getZExtValue() == 0) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00005037 return N->getOperand(0);
5038 }
5039 break;
5040 case PPCISD::SRA:
5041 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005042 if (C->getZExtValue() == 0 || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00005043 C->isAllOnesValue()) // -1 >>s V -> -1.
5044 return N->getOperand(0);
5045 }
5046 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005047
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005048 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00005049 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005050 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
5051 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
5052 // We allow the src/dst to be either f32/f64, but the intermediate
5053 // type must be i64.
Dale Johannesen79217062007-10-23 23:20:14 +00005054 if (N->getOperand(0).getValueType() == MVT::i64 &&
5055 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00005056 SDValue Val = N->getOperand(0).getOperand(0);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005057 if (Val.getValueType() == MVT::f32) {
Dale Johannesen3484c092009-02-05 22:07:54 +00005058 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005059 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005060 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005061
Dale Johannesen3484c092009-02-05 22:07:54 +00005062 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005063 DCI.AddToWorklist(Val.getNode());
Dale Johannesen3484c092009-02-05 22:07:54 +00005064 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005065 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005066 if (N->getValueType(0) == MVT::f32) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005067 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00005068 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005069 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005070 }
5071 return Val;
5072 } else if (N->getOperand(0).getValueType() == MVT::i32) {
5073 // If the intermediate type is i32, we can avoid the load/store here
5074 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005075 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005076 }
5077 }
5078 break;
Chris Lattner51269842006-03-01 05:50:56 +00005079 case ISD::STORE:
5080 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
5081 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00005082 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00005083 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Dale Johannesen79217062007-10-23 23:20:14 +00005084 N->getOperand(1).getValueType() == MVT::i32 &&
5085 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00005086 SDValue Val = N->getOperand(1).getOperand(0);
Chris Lattner51269842006-03-01 05:50:56 +00005087 if (Val.getValueType() == MVT::f32) {
Dale Johannesen3484c092009-02-05 22:07:54 +00005088 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005089 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00005090 }
Dale Johannesen3484c092009-02-05 22:07:54 +00005091 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005092 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00005093
Dale Johannesen3484c092009-02-05 22:07:54 +00005094 Val = DAG.getNode(PPCISD::STFIWX, dl, MVT::Other, N->getOperand(0), Val,
Chris Lattner51269842006-03-01 05:50:56 +00005095 N->getOperand(2), N->getOperand(3));
Gabor Greifba36cb52008-08-28 21:40:38 +00005096 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00005097 return Val;
5098 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005099
Chris Lattnerd9989382006-07-10 20:56:58 +00005100 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
5101 if (N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005102 N->getOperand(1).getNode()->hasOneUse() &&
Chris Lattnerd9989382006-07-10 20:56:58 +00005103 (N->getOperand(1).getValueType() == MVT::i32 ||
5104 N->getOperand(1).getValueType() == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00005105 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00005106 // Do an any-extend to 32-bits if this is a half-word input.
5107 if (BSwapOp.getValueType() == MVT::i16)
Dale Johannesen3484c092009-02-05 22:07:54 +00005108 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00005109
Dale Johannesen3484c092009-02-05 22:07:54 +00005110 return DAG.getNode(PPCISD::STBRX, dl, MVT::Other, N->getOperand(0),
5111 BSwapOp, N->getOperand(2), N->getOperand(3),
Chris Lattnerd9989382006-07-10 20:56:58 +00005112 DAG.getValueType(N->getOperand(1).getValueType()));
5113 }
5114 break;
5115 case ISD::BSWAP:
5116 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00005117 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00005118 N->getOperand(0).hasOneUse() &&
5119 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00005120 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00005121 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00005122 // Create the byte-swapping load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005123 std::vector<MVT> VTs;
Chris Lattnerd9989382006-07-10 20:56:58 +00005124 VTs.push_back(MVT::i32);
5125 VTs.push_back(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005126 SDValue MO = DAG.getMemOperand(LD->getMemOperand());
5127 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00005128 LD->getChain(), // Chain
5129 LD->getBasePtr(), // Ptr
Dan Gohman69de1932008-02-06 22:27:42 +00005130 MO, // MemOperand
Chris Lattner79e490a2006-08-11 17:18:05 +00005131 DAG.getValueType(N->getValueType(0)) // VT
5132 };
Dale Johannesen3484c092009-02-05 22:07:54 +00005133 SDValue BSLoad = DAG.getNode(PPCISD::LBRX, dl, VTs, Ops, 4);
Chris Lattnerd9989382006-07-10 20:56:58 +00005134
Scott Michelfdc40a02009-02-17 22:15:04 +00005135 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00005136 SDValue ResVal = BSLoad;
Chris Lattnerd9989382006-07-10 20:56:58 +00005137 if (N->getValueType(0) == MVT::i16)
Dale Johannesen3484c092009-02-05 22:07:54 +00005138 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00005139
Chris Lattnerd9989382006-07-10 20:56:58 +00005140 // First, combine the bswap away. This makes the value produced by the
5141 // load dead.
5142 DCI.CombineTo(N, ResVal);
5143
5144 // Next, combine the load away, we give it a bogus result value but a real
5145 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00005146 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005147
Chris Lattnerd9989382006-07-10 20:56:58 +00005148 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00005149 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00005150 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005151
Chris Lattner51269842006-03-01 05:50:56 +00005152 break;
Chris Lattner4468c222006-03-31 06:02:07 +00005153 case PPCISD::VCMP: {
5154 // If a VCMPo node already exists with exactly the same operands as this
5155 // node, use its result instead of this node (VCMPo computes both a CR6 and
5156 // a normal output).
5157 //
5158 if (!N->getOperand(0).hasOneUse() &&
5159 !N->getOperand(1).hasOneUse() &&
5160 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005161
Chris Lattner4468c222006-03-31 06:02:07 +00005162 // Scan all of the users of the LHS, looking for VCMPo's that match.
5163 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005164
Gabor Greifba36cb52008-08-28 21:40:38 +00005165 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00005166 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
5167 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00005168 if (UI->getOpcode() == PPCISD::VCMPo &&
5169 UI->getOperand(1) == N->getOperand(1) &&
5170 UI->getOperand(2) == N->getOperand(2) &&
5171 UI->getOperand(0) == N->getOperand(0)) {
5172 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00005173 break;
5174 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005175
Chris Lattner00901202006-04-18 18:28:22 +00005176 // If there is no VCMPo node, or if the flag value has a single use, don't
5177 // transform this.
5178 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
5179 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005180
5181 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00005182 // chain, this transformation is more complex. Note that multiple things
5183 // could use the value result, which we should ignore.
5184 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005185 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00005186 FlagUser == 0; ++UI) {
5187 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00005188 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00005189 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005190 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00005191 FlagUser = User;
5192 break;
5193 }
5194 }
5195 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005196
Chris Lattner00901202006-04-18 18:28:22 +00005197 // If the user is a MFCR instruction, we know this is safe. Otherwise we
5198 // give up for right now.
5199 if (FlagUser->getOpcode() == PPCISD::MFCR)
Dan Gohman475871a2008-07-27 21:46:04 +00005200 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00005201 }
5202 break;
5203 }
Chris Lattner90564f22006-04-18 17:59:36 +00005204 case ISD::BR_CC: {
5205 // If this is a branch on an altivec predicate comparison, lower this so
5206 // that we don't have to do a MFCR: instead, branch directly on CR6. This
5207 // lowering is done pre-legalize, because the legalizer lowers the predicate
5208 // compare down to code that is difficult to reassemble.
5209 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00005210 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Chris Lattner90564f22006-04-18 17:59:36 +00005211 int CompareOpc;
5212 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00005213
Chris Lattner90564f22006-04-18 17:59:36 +00005214 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
5215 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
5216 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
5217 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005218
Chris Lattner90564f22006-04-18 17:59:36 +00005219 // If this is a comparison against something other than 0/1, then we know
5220 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005221 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005222 if (Val != 0 && Val != 1) {
5223 if (CC == ISD::SETEQ) // Cond never true, remove branch.
5224 return N->getOperand(0);
5225 // Always !=, turn it into an unconditional branch.
Dale Johannesen3484c092009-02-05 22:07:54 +00005226 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00005227 N->getOperand(0), N->getOperand(4));
5228 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005229
Chris Lattner90564f22006-04-18 17:59:36 +00005230 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005231
Chris Lattner90564f22006-04-18 17:59:36 +00005232 // Create the PPCISD altivec 'dot' comparison node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005233 std::vector<MVT> VTs;
Dan Gohman475871a2008-07-27 21:46:04 +00005234 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005235 LHS.getOperand(2), // LHS of compare
5236 LHS.getOperand(3), // RHS of compare
5237 DAG.getConstant(CompareOpc, MVT::i32)
5238 };
Chris Lattner90564f22006-04-18 17:59:36 +00005239 VTs.push_back(LHS.getOperand(2).getValueType());
5240 VTs.push_back(MVT::Flag);
Dale Johannesen3484c092009-02-05 22:07:54 +00005241 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005242
Chris Lattner90564f22006-04-18 17:59:36 +00005243 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005244 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005245 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00005246 default: // Can't happen, don't crash on invalid number though.
5247 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005248 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00005249 break;
5250 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005251 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00005252 break;
5253 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005254 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00005255 break;
5256 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005257 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00005258 break;
5259 }
5260
Dale Johannesen3484c092009-02-05 22:07:54 +00005261 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
Chris Lattner90564f22006-04-18 17:59:36 +00005262 DAG.getConstant(CompOpc, MVT::i32),
Chris Lattner18258c62006-11-17 22:37:34 +00005263 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00005264 N->getOperand(4), CompNode.getValue(1));
5265 }
5266 break;
5267 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005268 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005269
Dan Gohman475871a2008-07-27 21:46:04 +00005270 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005271}
5272
Chris Lattner1a635d62006-04-14 06:01:58 +00005273//===----------------------------------------------------------------------===//
5274// Inline Assembly Support
5275//===----------------------------------------------------------------------===//
5276
Dan Gohman475871a2008-07-27 21:46:04 +00005277void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005278 const APInt &Mask,
Scott Michelfdc40a02009-02-17 22:15:04 +00005279 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005280 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005281 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005282 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005283 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005284 switch (Op.getOpcode()) {
5285 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00005286 case PPCISD::LBRX: {
5287 // lhbrx is known to have the top bits cleared out.
5288 if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16)
5289 KnownZero = 0xFFFF0000;
5290 break;
5291 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005292 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005293 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005294 default: break;
5295 case Intrinsic::ppc_altivec_vcmpbfp_p:
5296 case Intrinsic::ppc_altivec_vcmpeqfp_p:
5297 case Intrinsic::ppc_altivec_vcmpequb_p:
5298 case Intrinsic::ppc_altivec_vcmpequh_p:
5299 case Intrinsic::ppc_altivec_vcmpequw_p:
5300 case Intrinsic::ppc_altivec_vcmpgefp_p:
5301 case Intrinsic::ppc_altivec_vcmpgtfp_p:
5302 case Intrinsic::ppc_altivec_vcmpgtsb_p:
5303 case Intrinsic::ppc_altivec_vcmpgtsh_p:
5304 case Intrinsic::ppc_altivec_vcmpgtsw_p:
5305 case Intrinsic::ppc_altivec_vcmpgtub_p:
5306 case Intrinsic::ppc_altivec_vcmpgtuh_p:
5307 case Intrinsic::ppc_altivec_vcmpgtuw_p:
5308 KnownZero = ~1U; // All bits but the low one are known to be zero.
5309 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005310 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005311 }
5312 }
5313}
5314
5315
Chris Lattner4234f572007-03-25 02:14:49 +00005316/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005317/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00005318PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005319PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
5320 if (Constraint.size() == 1) {
5321 switch (Constraint[0]) {
5322 default: break;
5323 case 'b':
5324 case 'r':
5325 case 'f':
5326 case 'v':
5327 case 'y':
5328 return C_RegisterClass;
5329 }
5330 }
5331 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005332}
5333
Scott Michelfdc40a02009-02-17 22:15:04 +00005334std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00005335PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005336 MVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00005337 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00005338 // GCC RS6000 Constraint Letters
5339 switch (Constraint[0]) {
5340 case 'b': // R1-R31
5341 case 'r': // R0-R31
5342 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
5343 return std::make_pair(0U, PPC::G8RCRegisterClass);
5344 return std::make_pair(0U, PPC::GPRCRegisterClass);
5345 case 'f':
5346 if (VT == MVT::f32)
5347 return std::make_pair(0U, PPC::F4RCRegisterClass);
5348 else if (VT == MVT::f64)
5349 return std::make_pair(0U, PPC::F8RCRegisterClass);
5350 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005351 case 'v':
Chris Lattner331d1bc2006-11-02 01:44:04 +00005352 return std::make_pair(0U, PPC::VRRCRegisterClass);
5353 case 'y': // crrc
5354 return std::make_pair(0U, PPC::CRRCRegisterClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005355 }
5356 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005357
Chris Lattner331d1bc2006-11-02 01:44:04 +00005358 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005359}
Chris Lattner763317d2006-02-07 00:47:13 +00005360
Chris Lattner331d1bc2006-11-02 01:44:04 +00005361
Chris Lattner48884cd2007-08-25 00:47:38 +00005362/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Evan Chengda43bcf2008-09-24 00:05:32 +00005363/// vector. If it is invalid, don't add anything to Ops. If hasMemory is true
5364/// it means one of the asm constraint of the inline asm instruction being
5365/// processed is 'm'.
Dan Gohman475871a2008-07-27 21:46:04 +00005366void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, char Letter,
Evan Chengda43bcf2008-09-24 00:05:32 +00005367 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00005368 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00005369 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00005370 SDValue Result(0,0);
Chris Lattner763317d2006-02-07 00:47:13 +00005371 switch (Letter) {
5372 default: break;
5373 case 'I':
5374 case 'J':
5375 case 'K':
5376 case 'L':
5377 case 'M':
5378 case 'N':
5379 case 'O':
5380 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00005381 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00005382 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005383 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00005384 switch (Letter) {
5385 default: assert(0 && "Unknown constraint letter!");
5386 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005387 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005388 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005389 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005390 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
5391 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005392 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005393 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005394 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005395 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005396 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005397 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005398 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005399 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005400 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00005401 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005402 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005403 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005404 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00005405 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005406 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005407 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005408 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005409 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005410 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005411 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005412 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005413 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005414 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005415 }
5416 break;
5417 }
5418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005419
Gabor Greifba36cb52008-08-28 21:40:38 +00005420 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005421 Ops.push_back(Result);
5422 return;
5423 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005424
Chris Lattner763317d2006-02-07 00:47:13 +00005425 // Handle standard constraint letters.
Evan Chengda43bcf2008-09-24 00:05:32 +00005426 TargetLowering::LowerAsmOperandForConstraint(Op, Letter, hasMemory, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00005427}
Evan Chengc4c62572006-03-13 23:20:37 +00005428
Chris Lattnerc9addb72007-03-30 23:15:24 +00005429// isLegalAddressingMode - Return true if the addressing mode represented
5430// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00005431bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005432 const Type *Ty) const {
5433 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00005434
Chris Lattnerc9addb72007-03-30 23:15:24 +00005435 // PPC allows a sign-extended 16-bit immediate field.
5436 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
5437 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005438
Chris Lattnerc9addb72007-03-30 23:15:24 +00005439 // No global is ever allowed as a base.
5440 if (AM.BaseGV)
5441 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005442
5443 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005444 switch (AM.Scale) {
5445 case 0: // "r+i" or just "i", depending on HasBaseReg.
5446 break;
5447 case 1:
5448 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
5449 return false;
5450 // Otherwise we have r+r or r+i.
5451 break;
5452 case 2:
5453 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
5454 return false;
5455 // Allow 2*r as r+r.
5456 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00005457 default:
5458 // No other scales are supported.
5459 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00005460 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005461
Chris Lattnerc9addb72007-03-30 23:15:24 +00005462 return true;
5463}
5464
Evan Chengc4c62572006-03-13 23:20:37 +00005465/// isLegalAddressImmediate - Return true if the integer value can be used
Evan Cheng86193912007-03-12 23:29:01 +00005466/// as the offset of the target addressing mode for load / store of the
5467/// given type.
5468bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
Evan Chengc4c62572006-03-13 23:20:37 +00005469 // PPC allows a sign-extended 16-bit immediate field.
5470 return (V > -(1 << 16) && V < (1 << 16)-1);
5471}
Reid Spencer3a9ec242006-08-28 01:02:49 +00005472
5473bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michelfdc40a02009-02-17 22:15:04 +00005474 return false;
Reid Spencer3a9ec242006-08-28 01:02:49 +00005475}
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005476
Dan Gohman475871a2008-07-27 21:46:04 +00005477SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005478 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005479 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005480 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005481 return SDValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005482
5483 MachineFunction &MF = DAG.getMachineFunction();
5484 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005485
Chris Lattner3fc027d2007-12-08 06:59:59 +00005486 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00005487 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00005488
5489 // Make sure the function really does not optimize away the store of the RA
5490 // to the stack.
5491 FuncInfo->setLRStoreRequired();
Scott Michelfdc40a02009-02-17 22:15:04 +00005492 return DAG.getLoad(getPointerTy(), dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00005493 DAG.getEntryNode(), RetAddrFI, NULL, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00005494}
5495
Dan Gohman475871a2008-07-27 21:46:04 +00005496SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesena05dca42009-02-04 23:02:30 +00005497 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005498 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005499 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005500 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005501
Duncan Sands83ec4b62008-06-06 12:08:01 +00005502 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005503 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00005504
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005505 MachineFunction &MF = DAG.getMachineFunction();
5506 MachineFrameInfo *MFI = MF.getFrameInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005507 bool is31 = (NoFramePointerElim || MFI->hasVarSizedObjects())
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005508 && MFI->getStackSize();
5509
5510 if (isPPC64)
Dale Johannesena05dca42009-02-04 23:02:30 +00005511 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::X31 : PPC::X1,
Bill Wendlingb8a80f02007-08-30 00:59:19 +00005512 MVT::i64);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005513 else
Dale Johannesena05dca42009-02-04 23:02:30 +00005514 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::R31 : PPC::R1,
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005515 MVT::i32);
5516}
Dan Gohman54aeea32008-10-21 03:41:46 +00005517
5518bool
5519PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5520 // The PowerPC target isn't yet aware of offsets.
5521 return false;
5522}
Tilmann Schellerffd02002009-07-03 06:45:56 +00005523
5524MVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
5525 bool isSrcConst, bool isSrcStr,
5526 SelectionDAG &DAG) const {
5527 if (this->PPCSubTarget.isPPC64()) {
5528 return MVT::i64;
5529 } else {
5530 return MVT::i32;
5531 }
5532}