blob: 5b0c9357c0e15ff036302305bb44dfd1d95545e3 [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"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Jay Foad8d730fb2009-05-11 19:38:09 +000037#include "llvm/DerivedTypes.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000038using namespace llvm;
39
Tilmann Schellerffd02002009-07-03 06:45:56 +000040static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
41 CCValAssign::LocInfo &LocInfo,
42 ISD::ArgFlagsTy &ArgFlags,
43 CCState &State);
44static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
45 MVT &LocVT,
46 CCValAssign::LocInfo &LocInfo,
47 ISD::ArgFlagsTy &ArgFlags,
48 CCState &State);
49static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
50 MVT &LocVT,
51 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
53 CCState &State);
54
Scott Michelfdc40a02009-02-17 22:15:04 +000055static cl::opt<bool> EnablePPCPreinc("enable-ppc-preinc",
Chris Lattner3ee77402007-06-19 05:46:06 +000056cl::desc("enable preincrement load/store generation on PPC (experimental)"),
57 cl::Hidden);
Chris Lattner4eab7142006-11-10 02:08:47 +000058
Chris Lattner331d1bc2006-11-02 01:44:04 +000059PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Evan Cheng53301922008-07-12 02:23:19 +000060 : TargetLowering(TM), PPCSubTarget(*TM.getSubtargetImpl()) {
Scott Michelfdc40a02009-02-17 22:15:04 +000061
Nate Begeman405e3ec2005-10-21 00:02:42 +000062 setPow2DivIsCheap();
Dale Johannesen72324642008-07-31 18:13:12 +000063
Chris Lattnerd145a612005-09-27 22:18:25 +000064 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000065 setUseUnderscoreSetJmp(true);
66 setUseUnderscoreLongJmp(true);
Scott Michelfdc40a02009-02-17 22:15:04 +000067
Chris Lattner7c5a3d32005-08-16 17:14:42 +000068 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000069 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
70 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
71 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +000072
Evan Chengc5484282006-10-04 00:56:09 +000073 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Evan Cheng03294662008-10-14 21:26:46 +000074 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
75 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000076
Chris Lattnerddf89562008-01-17 19:59:44 +000077 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +000078
Chris Lattner94e509c2006-11-10 23:58:45 +000079 // PowerPC has pre-inc load and store's.
80 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
81 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
82 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000083 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
84 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
Chris Lattner94e509c2006-11-10 23:58:45 +000085 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
86 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
87 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000088 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
89 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
90
Dale Johannesen6eaeff22007-10-10 01:01:31 +000091 // This is used in the ppcf128->int sequence. Note it has different semantics
92 // from FP_ROUND: that rounds to nearest, this rounds to zero.
93 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +000094
Chris Lattner7c5a3d32005-08-16 17:14:42 +000095 // PowerPC has no SREM/UREM instructions
96 setOperationAction(ISD::SREM, MVT::i32, Expand);
97 setOperationAction(ISD::UREM, MVT::i32, Expand);
Chris Lattner563ecfb2006-06-27 18:18:41 +000098 setOperationAction(ISD::SREM, MVT::i64, Expand);
99 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +0000100
101 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
102 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
103 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
104 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
105 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
106 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
107 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
108 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
109 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000110
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000111 // We don't support sin/cos/sqrt/fmod/pow
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000112 setOperationAction(ISD::FSIN , MVT::f64, Expand);
113 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +0000114 setOperationAction(ISD::FREM , MVT::f64, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000115 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000116 setOperationAction(ISD::FSIN , MVT::f32, Expand);
117 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +0000118 setOperationAction(ISD::FREM , MVT::f32, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000119 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000120
Dan Gohman1a024862008-01-31 00:41:03 +0000121 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000123 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000124 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000125 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
127 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000128
Chris Lattner9601a862006-03-05 05:08:37 +0000129 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
130 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
Nate Begemand88fc032006-01-14 03:14:10 +0000132 // PowerPC does not have BSWAP, CTPOP or CTTZ
133 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000134 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
135 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +0000136 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
137 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
138 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000139
Nate Begeman35ef9132006-01-11 21:21:00 +0000140 // PowerPC does not have ROTR
141 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
Bill Wendling3156b622008-08-31 02:53:19 +0000142 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000143
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000144 // PowerPC does not have Select
145 setOperationAction(ISD::SELECT, MVT::i32, Expand);
Chris Lattnerf89437d2006-06-27 20:14:52 +0000146 setOperationAction(ISD::SELECT, MVT::i64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000147 setOperationAction(ISD::SELECT, MVT::f32, Expand);
148 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000149
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000150 // PowerPC wants to turn select_cc of FP into fsel when possible.
151 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
152 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000153
Nate Begeman750ac1b2006-02-01 07:19:44 +0000154 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +0000155 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000156
Nate Begeman81e80972006-03-17 01:40:33 +0000157 // PowerPC does not have BRCOND which requires SetCC
158 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000159
160 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000161
Chris Lattnerf7605322005-08-31 21:09:52 +0000162 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
163 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000164
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000165 // PowerPC does not have [U|S]INT_TO_FP
166 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
167 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
168
Chris Lattner53e88452005-12-23 05:13:35 +0000169 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
170 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Chris Lattner5f9faea2006-06-27 18:40:08 +0000171 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Expand);
172 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000173
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000174 // We cannot sextinreg(i1). Expand to shifts.
175 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000176
Jim Laskeyabf6d172006-01-05 01:25:28 +0000177 // Support label based line numbers.
Dan Gohman7f460202008-06-30 20:59:49 +0000178 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000179 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000180
Nicolas Geoffray616585b2007-12-21 12:19:44 +0000181 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
182 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
183 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
184 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000185
186
187 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman28a6b022005-12-10 02:36:00 +0000188 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000189 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000190 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000191 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000192 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000193 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +0000194 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Chris Lattner059ca0f2006-06-16 21:01:35 +0000195 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
196 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000197
Nate Begeman1db3c922008-08-11 17:36:31 +0000198 // RET must be custom lowered, to meet ABI requirements.
Nate Begemanee625572006-01-27 21:09:22 +0000199 setOperationAction(ISD::RET , MVT::Other, Custom);
Duncan Sands36397f52007-07-27 12:58:54 +0000200
Nate Begeman1db3c922008-08-11 17:36:31 +0000201 // TRAP is legal.
202 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling77959322008-09-17 00:30:57 +0000203
204 // TRAMPOLINE is custom lowered.
205 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
206
Nate Begemanacc398c2006-01-25 18:21:52 +0000207 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
208 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000209
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000210 // VAARG is custom lowered with the SVR4 ABI
211 if (TM.getSubtarget<PPCSubtarget>().isSVR4ABI())
Nicolas Geoffray01119992007-04-03 13:59:52 +0000212 setOperationAction(ISD::VAARG, MVT::Other, Custom);
213 else
214 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000215
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000216 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000217 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
218 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000219 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
Jim Laskeyefc7e522006-12-04 22:04:42 +0000220 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
Jim Laskey2f616bf2006-11-16 22:43:37 +0000221 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
222 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000223
Chris Lattner6d92cad2006-03-26 10:06:40 +0000224 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000225 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000226
Dale Johannesen53e4e442008-11-07 22:54:33 +0000227 // Comparisons that require checking two conditions.
228 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
229 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
230 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
231 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
232 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
233 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
234 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
235 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
236 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
237 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
238 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
239 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000240
Chris Lattnera7a58542006-06-16 17:34:12 +0000241 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000242 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000243 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
Jim Laskeyca367b42006-12-15 14:32:57 +0000244 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000245 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner85c671b2006-12-07 01:24:16 +0000246 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen4c9369d2009-06-04 20:53:52 +0000247 // This is just the low 32 bits of a (signed) fp->i64 conversion.
248 // We cannot do this with Promote because i64 is not a legal type.
249 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000250
Chris Lattner7fbcef72006-03-24 07:53:47 +0000251 // FIXME: disable this lowered code. This generates 64-bit register values,
252 // and we don't model the fact that the top part is clobbered by calls. We
253 // need to flag these together so that the value isn't live across a call.
254 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000255 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000256 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000257 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000258 }
259
Chris Lattnera7a58542006-06-16 17:34:12 +0000260 if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000261 // 64-bit PowerPC implementations can support i64 types directly
Nate Begeman9d2b8172005-10-18 00:56:42 +0000262 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000263 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
264 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000265 // 64-bit PowerPC wants to expand i128 shifts itself.
266 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
267 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
268 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000269 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000270 // 32-bit PowerPC wants to expand i64 shifts itself.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +0000271 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
272 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
273 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000274 }
Evan Chengd30bf012006-03-01 01:11:20 +0000275
Nate Begeman425a9692005-11-29 08:17:20 +0000276 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000277 // First set operation action for all vector types to expand. Then we
278 // will selectively turn on ones that can be effectively codegen'd.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000279 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
280 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
281 MVT VT = (MVT::SimpleValueType)i;
282
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000283 // add/sub are legal for all supported vector VT's.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000284 setOperationAction(ISD::ADD , VT, Legal);
285 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000286
Chris Lattner7ff7e672006-04-04 17:25:31 +0000287 // We promote all shuffles to v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000288 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
289 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000290
291 // We promote all non-typed operations to v4i32.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000292 setOperationAction(ISD::AND , VT, Promote);
293 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
294 setOperationAction(ISD::OR , VT, Promote);
295 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
296 setOperationAction(ISD::XOR , VT, Promote);
297 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
298 setOperationAction(ISD::LOAD , VT, Promote);
299 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
300 setOperationAction(ISD::SELECT, VT, Promote);
301 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
302 setOperationAction(ISD::STORE, VT, Promote);
303 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000304
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000305 // No other operations are legal.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000306 setOperationAction(ISD::MUL , VT, Expand);
307 setOperationAction(ISD::SDIV, VT, Expand);
308 setOperationAction(ISD::SREM, VT, Expand);
309 setOperationAction(ISD::UDIV, VT, Expand);
310 setOperationAction(ISD::UREM, VT, Expand);
311 setOperationAction(ISD::FDIV, VT, Expand);
312 setOperationAction(ISD::FNEG, VT, Expand);
313 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
314 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
315 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
316 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
317 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
318 setOperationAction(ISD::UDIVREM, VT, Expand);
319 setOperationAction(ISD::SDIVREM, VT, Expand);
320 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
321 setOperationAction(ISD::FPOW, VT, Expand);
322 setOperationAction(ISD::CTPOP, VT, Expand);
323 setOperationAction(ISD::CTLZ, VT, Expand);
324 setOperationAction(ISD::CTTZ, VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000325 }
326
Chris Lattner7ff7e672006-04-04 17:25:31 +0000327 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
328 // with merges, splats, etc.
329 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
330
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000331 setOperationAction(ISD::AND , MVT::v4i32, Legal);
332 setOperationAction(ISD::OR , MVT::v4i32, Legal);
333 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
334 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
335 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
336 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000337
Nate Begeman425a9692005-11-29 08:17:20 +0000338 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000339 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000340 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
341 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +0000342
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000343 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnere7c768e2006-04-18 03:24:30 +0000344 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
Chris Lattner72dd9bd2006-04-18 03:43:48 +0000345 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
Chris Lattner19a81522006-04-18 03:57:35 +0000346 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000347
Chris Lattnerb2177b92006-03-19 06:55:52 +0000348 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
349 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000350
Chris Lattner541f91b2006-04-02 00:43:36 +0000351 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
352 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000353 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
354 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000355 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000356
Chris Lattner7b0c58c2006-06-27 17:34:57 +0000357 setShiftAmountType(MVT::i32);
Duncan Sands03228082008-11-23 15:47:28 +0000358 setBooleanContents(ZeroOrOneBooleanContent);
Scott Michelfdc40a02009-02-17 22:15:04 +0000359
Jim Laskey2ad9f172007-02-22 14:56:36 +0000360 if (TM.getSubtarget<PPCSubtarget>().isPPC64()) {
Chris Lattner10da9572006-10-18 01:20:43 +0000361 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000362 setExceptionPointerRegister(PPC::X3);
363 setExceptionSelectorRegister(PPC::X4);
364 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000365 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000366 setExceptionPointerRegister(PPC::R3);
367 setExceptionSelectorRegister(PPC::R4);
368 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000369
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000370 // We have target-specific dag combine patterns for the following nodes:
371 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000372 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000373 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000374 setTargetDAGCombine(ISD::BSWAP);
Scott Michelfdc40a02009-02-17 22:15:04 +0000375
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000376 // Darwin long double math library functions have $LDBL128 appended.
377 if (TM.getSubtarget<PPCSubtarget>().isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000378 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000379 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
380 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000381 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
382 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000383 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
384 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
385 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
386 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
387 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000388 }
389
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000390 computeRegisterProperties();
391}
392
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000393/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
394/// function arguments in the caller parameter area.
395unsigned PPCTargetLowering::getByValTypeAlignment(const Type *Ty) const {
396 TargetMachine &TM = getTargetMachine();
397 // Darwin passes everything on 4 byte boundary.
398 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
399 return 4;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000400 // FIXME SVR4 TBD
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000401 return 4;
402}
403
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000404const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
405 switch (Opcode) {
406 default: return 0;
Evan Cheng53301922008-07-12 02:23:19 +0000407 case PPCISD::FSEL: return "PPCISD::FSEL";
408 case PPCISD::FCFID: return "PPCISD::FCFID";
409 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
410 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
411 case PPCISD::STFIWX: return "PPCISD::STFIWX";
412 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
413 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
414 case PPCISD::VPERM: return "PPCISD::VPERM";
415 case PPCISD::Hi: return "PPCISD::Hi";
416 case PPCISD::Lo: return "PPCISD::Lo";
417 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
418 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
419 case PPCISD::SRL: return "PPCISD::SRL";
420 case PPCISD::SRA: return "PPCISD::SRA";
421 case PPCISD::SHL: return "PPCISD::SHL";
422 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
423 case PPCISD::STD_32: return "PPCISD::STD_32";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000424 case PPCISD::CALL_SVR4: return "PPCISD::CALL_SVR4";
425 case PPCISD::CALL_Darwin: return "PPCISD::CALL_Darwin";
Evan Cheng53301922008-07-12 02:23:19 +0000426 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000427 case PPCISD::BCTRL_Darwin: return "PPCISD::BCTRL_Darwin";
428 case PPCISD::BCTRL_SVR4: return "PPCISD::BCTRL_SVR4";
Evan Cheng53301922008-07-12 02:23:19 +0000429 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
430 case PPCISD::MFCR: return "PPCISD::MFCR";
431 case PPCISD::VCMP: return "PPCISD::VCMP";
432 case PPCISD::VCMPo: return "PPCISD::VCMPo";
433 case PPCISD::LBRX: return "PPCISD::LBRX";
434 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng53301922008-07-12 02:23:19 +0000435 case PPCISD::LARX: return "PPCISD::LARX";
436 case PPCISD::STCX: return "PPCISD::STCX";
437 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
438 case PPCISD::MFFS: return "PPCISD::MFFS";
439 case PPCISD::MTFSB0: return "PPCISD::MTFSB0";
440 case PPCISD::MTFSB1: return "PPCISD::MTFSB1";
441 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
442 case PPCISD::MTFSF: return "PPCISD::MTFSF";
443 case PPCISD::TAILCALL: return "PPCISD::TAILCALL";
444 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000445 }
446}
447
Duncan Sands5480c042009-01-01 15:52:00 +0000448MVT PPCTargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000449 return MVT::i32;
450}
451
Bill Wendlingb4202b82009-07-01 18:50:55 +0000452/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000453unsigned PPCTargetLowering::getFunctionAlignment(const Function *F) const {
454 if (getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin())
455 return F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4;
456 else
457 return 2;
458}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000459
Chris Lattner1a635d62006-04-14 06:01:58 +0000460//===----------------------------------------------------------------------===//
461// Node matching predicates, for use by the tblgen matching code.
462//===----------------------------------------------------------------------===//
463
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000464/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000465static bool isFloatingPointZero(SDValue Op) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000466 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000467 return CFP->getValueAPF().isZero();
Gabor Greifba36cb52008-08-28 21:40:38 +0000468 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000469 // Maybe this has already been legalized into the constant pool?
470 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Evan Chengc356a572006-09-12 21:04:05 +0000471 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000472 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000473 }
474 return false;
475}
476
Chris Lattnerddb739e2006-04-06 17:23:16 +0000477/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
478/// true if Op is undef or if it matches the specified value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000479static bool isConstantOrUndef(int Op, int Val) {
480 return Op < 0 || Op == Val;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000481}
482
483/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
484/// VPKUHUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000485bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000486 if (!isUnary) {
487 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000488 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000489 return false;
490 } else {
491 for (unsigned i = 0; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000492 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
493 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000494 return false;
495 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000496 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000497}
498
499/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
500/// VPKUWUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000501bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000502 if (!isUnary) {
503 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000504 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
505 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000506 return false;
507 } else {
508 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000509 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
510 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
511 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
512 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000513 return false;
514 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000515 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000516}
517
Chris Lattnercaad1632006-04-06 22:02:42 +0000518/// isVMerge - Common function, used to match vmrg* shuffles.
519///
Nate Begeman9008ca62009-04-27 18:41:29 +0000520static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnercaad1632006-04-06 22:02:42 +0000521 unsigned LHSStart, unsigned RHSStart) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000522 assert(N->getValueType(0) == MVT::v16i8 &&
523 "PPC only supports shuffles by bytes!");
Chris Lattner116cc482006-04-06 21:11:54 +0000524 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
525 "Unsupported merge size!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000526
Chris Lattner116cc482006-04-06 21:11:54 +0000527 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
528 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman9008ca62009-04-27 18:41:29 +0000529 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000530 LHSStart+j+i*UnitSize) ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000531 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000532 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000533 return false;
534 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000535 return true;
Chris Lattnercaad1632006-04-06 22:02:42 +0000536}
537
538/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
539/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Nate Begeman9008ca62009-04-27 18:41:29 +0000540bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
541 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000542 if (!isUnary)
543 return isVMerge(N, UnitSize, 8, 24);
544 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000545}
546
547/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
548/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Nate Begeman9008ca62009-04-27 18:41:29 +0000549bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
550 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000551 if (!isUnary)
552 return isVMerge(N, UnitSize, 0, 16);
553 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000554}
555
556
Chris Lattnerd0608e12006-04-06 18:26:28 +0000557/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
558/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000559int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000560 assert(N->getValueType(0) == MVT::v16i8 &&
561 "PPC only supports shuffles by bytes!");
562
563 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
564
Chris Lattnerd0608e12006-04-06 18:26:28 +0000565 // Find the first non-undef value in the shuffle mask.
566 unsigned i;
Nate Begeman9008ca62009-04-27 18:41:29 +0000567 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattnerd0608e12006-04-06 18:26:28 +0000568 /*search*/;
Scott Michelfdc40a02009-02-17 22:15:04 +0000569
Chris Lattnerd0608e12006-04-06 18:26:28 +0000570 if (i == 16) return -1; // all undef.
Scott Michelfdc40a02009-02-17 22:15:04 +0000571
Nate Begeman9008ca62009-04-27 18:41:29 +0000572 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattnerd0608e12006-04-06 18:26:28 +0000573 // numbered from this value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000574 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattnerd0608e12006-04-06 18:26:28 +0000575 if (ShiftAmt < i) return -1;
576 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000577
Chris Lattnerf24380e2006-04-06 22:28:36 +0000578 if (!isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000579 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000580 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000581 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000582 return -1;
583 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000584 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000585 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000586 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000587 return -1;
588 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000589 return ShiftAmt;
590}
Chris Lattneref819f82006-03-20 06:33:01 +0000591
592/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
593/// specifies a splat of a single element that is suitable for input to
594/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman9008ca62009-04-27 18:41:29 +0000595bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
596 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner7ff7e672006-04-04 17:25:31 +0000597 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelfdc40a02009-02-17 22:15:04 +0000598
Chris Lattner88a99ef2006-03-20 06:37:44 +0000599 // This is a splat operation if each element of the permute is the same, and
600 // if the value doesn't reference the second vector.
Nate Begeman9008ca62009-04-27 18:41:29 +0000601 unsigned ElementBase = N->getMaskElt(0);
602
603 // FIXME: Handle UNDEF elements too!
604 if (ElementBase >= 16)
Chris Lattner7ff7e672006-04-04 17:25:31 +0000605 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000606
Nate Begeman9008ca62009-04-27 18:41:29 +0000607 // Check that the indices are consecutive, in the case of a multi-byte element
608 // splatted with a v16i8 mask.
609 for (unsigned i = 1; i != EltSize; ++i)
610 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000611 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000612
Chris Lattner7ff7e672006-04-04 17:25:31 +0000613 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000614 if (N->getMaskElt(i) < 0) continue;
Chris Lattner7ff7e672006-04-04 17:25:31 +0000615 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman9008ca62009-04-27 18:41:29 +0000616 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000617 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000618 }
Chris Lattner7ff7e672006-04-04 17:25:31 +0000619 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000620}
621
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000622/// isAllNegativeZeroVector - Returns true if all elements of build_vector
623/// are -0.0.
624bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000625 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
626
627 APInt APVal, APUndef;
628 unsigned BitSize;
629 bool HasAnyUndefs;
630
631 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32))
632 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000633 return CFP->getValueAPF().isNegZero();
Nate Begeman9008ca62009-04-27 18:41:29 +0000634
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000635 return false;
636}
637
Chris Lattneref819f82006-03-20 06:33:01 +0000638/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
639/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000640unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000641 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
642 assert(isSplatShuffleMask(SVOp, EltSize));
643 return SVOp->getMaskElt(0) / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000644}
645
Chris Lattnere87192a2006-04-12 17:37:20 +0000646/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000647/// by using a vspltis[bhw] instruction of the specified element size, return
648/// the constant being splatted. The ByteSize field indicates the number of
649/// bytes of each element [124] -> [bhw].
Dan Gohman475871a2008-07-27 21:46:04 +0000650SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
651 SDValue OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000652
653 // If ByteSize of the splat is bigger than the element size of the
654 // build_vector, then we have a case where we are checking for a splat where
655 // multiple elements of the buildvector are folded together into a single
656 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
657 unsigned EltSize = 16/N->getNumOperands();
658 if (EltSize < ByteSize) {
659 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman475871a2008-07-27 21:46:04 +0000660 SDValue UniquedVals[4];
Chris Lattner79d9a882006-04-08 07:14:26 +0000661 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelfdc40a02009-02-17 22:15:04 +0000662
Chris Lattner79d9a882006-04-08 07:14:26 +0000663 // See if all of the elements in the buildvector agree across.
664 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
665 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
666 // If the element isn't a constant, bail fully out.
Dan Gohman475871a2008-07-27 21:46:04 +0000667 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000668
Scott Michelfdc40a02009-02-17 22:15:04 +0000669
Gabor Greifba36cb52008-08-28 21:40:38 +0000670 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000671 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
672 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000673 return SDValue(); // no match.
Chris Lattner79d9a882006-04-08 07:14:26 +0000674 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000675
Chris Lattner79d9a882006-04-08 07:14:26 +0000676 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
677 // either constant or undef values that are identical for each chunk. See
678 // if these chunks can form into a larger vspltis*.
Scott Michelfdc40a02009-02-17 22:15:04 +0000679
Chris Lattner79d9a882006-04-08 07:14:26 +0000680 // Check to see if all of the leading entries are either 0 or -1. If
681 // neither, then this won't fit into the immediate field.
682 bool LeadingZero = true;
683 bool LeadingOnes = true;
684 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000685 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Chris Lattner79d9a882006-04-08 07:14:26 +0000687 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
688 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
689 }
690 // Finally, check the least significant entry.
691 if (LeadingZero) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000692 if (UniquedVals[Multiple-1].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000693 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000694 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000695 if (Val < 16)
696 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
697 }
698 if (LeadingOnes) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000699 if (UniquedVals[Multiple-1].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000700 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman7810bfe2008-09-26 21:54:37 +0000701 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000702 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
703 return DAG.getTargetConstant(Val, MVT::i32);
704 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000705
Dan Gohman475871a2008-07-27 21:46:04 +0000706 return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000707 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000708
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000709 // Check to see if this buildvec has a single non-undef value in its elements.
710 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
711 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +0000712 if (OpVal.getNode() == 0)
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000713 OpVal = N->getOperand(i);
714 else if (OpVal != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000715 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000716 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000717
Gabor Greifba36cb52008-08-28 21:40:38 +0000718 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelfdc40a02009-02-17 22:15:04 +0000719
Eli Friedman1a8229b2009-05-24 02:03:36 +0000720 unsigned ValSizeInBytes = EltSize;
Nate Begeman98e70cc2006-03-28 04:15:58 +0000721 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000722 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000723 Value = CN->getZExtValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000724 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
725 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000726 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000727 }
728
729 // If the splat value is larger than the element value, then we can never do
730 // this splat. The only case that we could fit the replicated bits into our
731 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman475871a2008-07-27 21:46:04 +0000732 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000733
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000734 // If the element value is larger than the splat value, cut it in half and
735 // check to see if the two halves are equal. Continue doing this until we
736 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
737 while (ValSizeInBytes > ByteSize) {
738 ValSizeInBytes >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000739
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000740 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000741 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
742 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman475871a2008-07-27 21:46:04 +0000743 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000744 }
745
746 // Properly sign extend the value.
747 int ShAmt = (4-ByteSize)*8;
748 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
Scott Michelfdc40a02009-02-17 22:15:04 +0000749
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000750 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman475871a2008-07-27 21:46:04 +0000751 if (MaskVal == 0) return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000752
Chris Lattner140a58f2006-04-08 06:46:53 +0000753 // Finally, if this value fits in a 5 bit sext field, return it
754 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
755 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000756 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000757}
758
Chris Lattner1a635d62006-04-14 06:01:58 +0000759//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000760// Addressing Mode Selection
761//===----------------------------------------------------------------------===//
762
763/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
764/// or 64-bit immediate, and if the value can be accurately represented as a
765/// sign extension from a 16-bit value. If so, this returns true and the
766/// immediate.
767static bool isIntS16Immediate(SDNode *N, short &Imm) {
768 if (N->getOpcode() != ISD::Constant)
769 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000770
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000771 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000772 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000773 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000774 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000775 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000776}
Dan Gohman475871a2008-07-27 21:46:04 +0000777static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000778 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000779}
780
781
782/// SelectAddressRegReg - Given the specified addressed, check to see if it
783/// can be represented as an indexed [r+r] operation. Returns false if it
784/// can be more efficiently represented with [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +0000785bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
786 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000787 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000788 short imm = 0;
789 if (N.getOpcode() == ISD::ADD) {
790 if (isIntS16Immediate(N.getOperand(1), imm))
791 return false; // r+i
792 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
793 return false; // r+i
Scott Michelfdc40a02009-02-17 22:15:04 +0000794
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000795 Base = N.getOperand(0);
796 Index = N.getOperand(1);
797 return true;
798 } else if (N.getOpcode() == ISD::OR) {
799 if (isIntS16Immediate(N.getOperand(1), imm))
800 return false; // r+i can fold it if we can.
Scott Michelfdc40a02009-02-17 22:15:04 +0000801
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000802 // If this is an or of disjoint bitfields, we can codegen this as an add
803 // (for better address arithmetic) if the LHS and RHS of the OR are provably
804 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000805 APInt LHSKnownZero, LHSKnownOne;
806 APInt RHSKnownZero, RHSKnownOne;
807 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanec59b952008-02-27 21:12:32 +0000808 APInt::getAllOnesValue(N.getOperand(0)
809 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000810 LHSKnownZero, LHSKnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +0000811
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000812 if (LHSKnownZero.getBoolValue()) {
813 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanec59b952008-02-27 21:12:32 +0000814 APInt::getAllOnesValue(N.getOperand(1)
815 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000816 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000817 // If all of the bits are known zero on the LHS or RHS, the add won't
818 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +0000819 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000820 Base = N.getOperand(0);
821 Index = N.getOperand(1);
822 return true;
823 }
824 }
825 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000826
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000827 return false;
828}
829
830/// Returns true if the address N can be represented by a base register plus
831/// a signed 16-bit displacement [r+imm], and if it is not better
832/// represented as reg+reg.
Dan Gohman475871a2008-07-27 21:46:04 +0000833bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman73e09142009-01-15 16:29:45 +0000834 SDValue &Base,
835 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000836 // FIXME dl should come from parent load or store, not from address
837 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000838 // If this can be more profitably realized as r+r, fail.
839 if (SelectAddressRegReg(N, Disp, Base, DAG))
840 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000841
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000842 if (N.getOpcode() == ISD::ADD) {
843 short imm = 0;
844 if (isIntS16Immediate(N.getOperand(1), imm)) {
845 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
846 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
847 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
848 } else {
849 Base = N.getOperand(0);
850 }
851 return true; // [r+i]
852 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
853 // Match LOAD (ADD (X, Lo(G))).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000854 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000855 && "Cannot handle constant offsets yet!");
856 Disp = N.getOperand(1).getOperand(0); // The global address.
857 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
858 Disp.getOpcode() == ISD::TargetConstantPool ||
859 Disp.getOpcode() == ISD::TargetJumpTable);
860 Base = N.getOperand(0);
861 return true; // [&g+r]
862 }
863 } else if (N.getOpcode() == ISD::OR) {
864 short imm = 0;
865 if (isIntS16Immediate(N.getOperand(1), imm)) {
866 // If this is an or of disjoint bitfields, we can codegen this as an add
867 // (for better address arithmetic) if the LHS and RHS of the OR are
868 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000869 APInt LHSKnownZero, LHSKnownOne;
870 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000871 APInt::getAllOnesValue(N.getOperand(0)
872 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000873 LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +0000874
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000875 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000876 // If all of the bits are known zero on the LHS or RHS, the add won't
877 // carry.
878 Base = N.getOperand(0);
879 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
880 return true;
881 }
882 }
883 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
884 // Loading from a constant address.
Scott Michelfdc40a02009-02-17 22:15:04 +0000885
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000886 // If this address fits entirely in a 16-bit sext immediate field, codegen
887 // this as "d, 0"
888 short Imm;
889 if (isIntS16Immediate(CN, Imm)) {
890 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
891 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
892 return true;
893 }
Chris Lattnerbc681d62007-02-17 06:44:03 +0000894
895 // Handle 32-bit sext immediates with LIS + addr mode.
896 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000897 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
898 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000899
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000900 // Otherwise, break this down into an LIS + disp.
Chris Lattnerbc681d62007-02-17 06:44:03 +0000901 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000902
Chris Lattnerbc681d62007-02-17 06:44:03 +0000903 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
904 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000905 Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000906 return true;
907 }
908 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000909
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000910 Disp = DAG.getTargetConstant(0, getPointerTy());
911 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
912 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
913 else
914 Base = N;
915 return true; // [r+0]
916}
917
918/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
919/// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000920bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
921 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000922 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000923 // Check to see if we can easily represent this as an [r+r] address. This
924 // will fail if it thinks that the address is more profitably represented as
925 // reg+imm, e.g. where imm = 0.
926 if (SelectAddressRegReg(N, Base, Index, DAG))
927 return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000928
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000929 // If the operand is an addition, always emit this as [r+r], since this is
930 // better (for code size, and execution, as the memop does the add for free)
931 // than emitting an explicit add.
932 if (N.getOpcode() == ISD::ADD) {
933 Base = N.getOperand(0);
934 Index = N.getOperand(1);
935 return true;
936 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000937
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000938 // Otherwise, do it the hard way, using R0 as the base register.
939 Base = DAG.getRegister(PPC::R0, N.getValueType());
940 Index = N;
941 return true;
942}
943
944/// SelectAddressRegImmShift - Returns true if the address N can be
945/// represented by a base register plus a signed 14-bit displacement
946/// [r+imm*4]. Suitable for use by STD and friends.
Dan Gohman475871a2008-07-27 21:46:04 +0000947bool PPCTargetLowering::SelectAddressRegImmShift(SDValue N, SDValue &Disp,
948 SDValue &Base,
Dan Gohman73e09142009-01-15 16:29:45 +0000949 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000950 // FIXME dl should come from the parent load or store, not the address
951 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000952 // If this can be more profitably realized as r+r, fail.
953 if (SelectAddressRegReg(N, Disp, Base, DAG))
954 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000955
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000956 if (N.getOpcode() == ISD::ADD) {
957 short imm = 0;
958 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
959 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
960 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
961 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
962 } else {
963 Base = N.getOperand(0);
964 }
965 return true; // [r+i]
966 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
967 // Match LOAD (ADD (X, Lo(G))).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000968 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000969 && "Cannot handle constant offsets yet!");
970 Disp = N.getOperand(1).getOperand(0); // The global address.
971 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
972 Disp.getOpcode() == ISD::TargetConstantPool ||
973 Disp.getOpcode() == ISD::TargetJumpTable);
974 Base = N.getOperand(0);
975 return true; // [&g+r]
976 }
977 } else if (N.getOpcode() == ISD::OR) {
978 short imm = 0;
979 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
980 // If this is an or of disjoint bitfields, we can codegen this as an add
981 // (for better address arithmetic) if the LHS and RHS of the OR are
982 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000983 APInt LHSKnownZero, LHSKnownOne;
984 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000985 APInt::getAllOnesValue(N.getOperand(0)
986 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000987 LHSKnownZero, LHSKnownOne);
988 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000989 // If all of the bits are known zero on the LHS or RHS, the add won't
990 // carry.
991 Base = N.getOperand(0);
992 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
993 return true;
994 }
995 }
996 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000997 // Loading from a constant address. Verify low two bits are clear.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000998 if ((CN->getZExtValue() & 3) == 0) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +0000999 // If this address fits entirely in a 14-bit sext immediate field, codegen
1000 // this as "d, 0"
1001 short Imm;
1002 if (isIntS16Immediate(CN, Imm)) {
1003 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
1004 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
1005 return true;
1006 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001007
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001008 // Fold the low-part of 32-bit absolute addresses into addr mode.
1009 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001010 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
1011 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001012
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001013 // Otherwise, break this down into an LIS + disp.
1014 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001015 Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
1016 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dale Johannesenf5f5dce2009-02-06 19:16:40 +00001017 Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base),0);
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001018 return true;
1019 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001020 }
1021 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001022
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001023 Disp = DAG.getTargetConstant(0, getPointerTy());
1024 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1025 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1026 else
1027 Base = N;
1028 return true; // [r+0]
1029}
1030
1031
1032/// getPreIndexedAddressParts - returns true by value, base pointer and
1033/// offset pointer and addressing mode by reference if the node's address
1034/// can be legally represented as pre-indexed load / store address.
Dan Gohman475871a2008-07-27 21:46:04 +00001035bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1036 SDValue &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001037 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001038 SelectionDAG &DAG) const {
Chris Lattner4eab7142006-11-10 02:08:47 +00001039 // Disabled by default for now.
1040 if (!EnablePPCPreinc) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001041
Dan Gohman475871a2008-07-27 21:46:04 +00001042 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001043 MVT VT;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001044 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1045 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001046 VT = LD->getMemoryVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001047
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001048 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner4eab7142006-11-10 02:08:47 +00001049 ST = ST;
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001050 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001051 VT = ST->getMemoryVT();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001052 } else
1053 return false;
1054
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001055 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001056 if (VT.isVector())
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001057 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001058
Chris Lattner0851b4f2006-11-15 19:55:13 +00001059 // TODO: Check reg+reg first.
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Chris Lattner0851b4f2006-11-15 19:55:13 +00001061 // LDU/STU use reg+imm*4, others use reg+imm.
1062 if (VT != MVT::i64) {
1063 // reg + imm
1064 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
1065 return false;
1066 } else {
1067 // reg + imm * 4.
1068 if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
1069 return false;
1070 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001071
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001072 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001073 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1074 // sext i32 to i64 when addr mode is r+i.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001075 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001076 LD->getExtensionType() == ISD::SEXTLOAD &&
1077 isa<ConstantSDNode>(Offset))
1078 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001079 }
1080
Chris Lattner4eab7142006-11-10 02:08:47 +00001081 AM = ISD::PRE_INC;
1082 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001083}
1084
1085//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001086// LowerOperation implementation
1087//===----------------------------------------------------------------------===//
1088
Scott Michelfdc40a02009-02-17 22:15:04 +00001089SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001090 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001091 MVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001092 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +00001093 Constant *C = CP->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +00001094 SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
1095 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001096 // FIXME there isn't really any debug info here
1097 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00001098
1099 const TargetMachine &TM = DAG.getTarget();
Scott Michelfdc40a02009-02-17 22:15:04 +00001100
Dale Johannesende064702009-02-06 21:50:26 +00001101 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, CPI, Zero);
1102 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, CPI, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001103
Chris Lattner1a635d62006-04-14 06:01:58 +00001104 // If this is a non-darwin platform, we don't support non-static relo models
1105 // yet.
1106 if (TM.getRelocationModel() == Reloc::Static ||
1107 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1108 // Generate non-pic code that has direct accesses to the constant pool.
1109 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesende064702009-02-06 21:50:26 +00001110 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001111 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001112
Chris Lattner35d86fe2006-07-26 21:12:04 +00001113 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001114 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesende064702009-02-06 21:50:26 +00001115 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001116 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001117 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001119
Dale Johannesende064702009-02-06 21:50:26 +00001120 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001121 return Lo;
1122}
1123
Dan Gohman475871a2008-07-27 21:46:04 +00001124SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001125 MVT PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001126 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001127 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1128 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001129 // FIXME there isn't really any debug loc here
1130 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001131
Nate Begeman37efe672006-04-22 18:53:45 +00001132 const TargetMachine &TM = DAG.getTarget();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001133
Dale Johannesende064702009-02-06 21:50:26 +00001134 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, JTI, Zero);
1135 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, JTI, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001136
Nate Begeman37efe672006-04-22 18:53:45 +00001137 // If this is a non-darwin platform, we don't support non-static relo models
1138 // yet.
1139 if (TM.getRelocationModel() == Reloc::Static ||
1140 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1141 // Generate non-pic code that has direct accesses to the constant pool.
1142 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesende064702009-02-06 21:50:26 +00001143 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001145
Chris Lattner35d86fe2006-07-26 21:12:04 +00001146 if (TM.getRelocationModel() == Reloc::PIC_) {
Nate Begeman37efe672006-04-22 18:53:45 +00001147 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesende064702009-02-06 21:50:26 +00001148 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001149 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001150 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Nate Begeman37efe672006-04-22 18:53:45 +00001151 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001152
Dale Johannesende064702009-02-06 21:50:26 +00001153 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001154 return Lo;
1155}
1156
Scott Michelfdc40a02009-02-17 22:15:04 +00001157SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001158 SelectionDAG &DAG) {
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001159 assert(0 && "TLS not implemented for PPC.");
Dan Gohman475871a2008-07-27 21:46:04 +00001160 return SDValue(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001161}
1162
Scott Michelfdc40a02009-02-17 22:15:04 +00001163SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
Evan Chengee5c2b82009-01-16 22:57:32 +00001164 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001165 MVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001166 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
1167 GlobalValue *GV = GSDN->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001168 SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00001169 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001170 // FIXME there isn't really any debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001171 DebugLoc dl = GSDN->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001172
Chris Lattner1a635d62006-04-14 06:01:58 +00001173 const TargetMachine &TM = DAG.getTarget();
1174
Dale Johannesen33c960f2009-02-04 20:06:27 +00001175 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, GA, Zero);
1176 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, GA, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001177
Chris Lattner1a635d62006-04-14 06:01:58 +00001178 // If this is a non-darwin platform, we don't support non-static relo models
1179 // yet.
1180 if (TM.getRelocationModel() == Reloc::Static ||
1181 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1182 // Generate non-pic code that has direct accesses to globals.
1183 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesen33c960f2009-02-04 20:06:27 +00001184 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001185 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001186
Chris Lattner35d86fe2006-07-26 21:12:04 +00001187 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001188 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesen33c960f2009-02-04 20:06:27 +00001189 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001190 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001191 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001192 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001193
Dale Johannesen33c960f2009-02-04 20:06:27 +00001194 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00001195
Chris Lattner57fc62c2006-12-11 23:22:45 +00001196 if (!TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV))
Chris Lattner1a635d62006-04-14 06:01:58 +00001197 return Lo;
Scott Michelfdc40a02009-02-17 22:15:04 +00001198
Chris Lattner1a635d62006-04-14 06:01:58 +00001199 // If the global is weak or external, we have to go through the lazy
1200 // resolution stub.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001201 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Lo, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00001202}
1203
Dan Gohman475871a2008-07-27 21:46:04 +00001204SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001205 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001206 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001207
Chris Lattner1a635d62006-04-14 06:01:58 +00001208 // If we're comparing for equality to zero, expose the fact that this is
1209 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1210 // fold the new nodes.
1211 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1212 if (C->isNullValue() && CC == ISD::SETEQ) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001213 MVT VT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001214 SDValue Zext = Op.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00001215 if (VT.bitsLT(MVT::i32)) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001216 VT = MVT::i32;
Dale Johannesenf5d97892009-02-04 01:48:28 +00001217 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001218 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001219 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001220 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1221 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00001222 DAG.getConstant(Log2b, MVT::i32));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001223 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner1a635d62006-04-14 06:01:58 +00001224 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001225 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner1a635d62006-04-14 06:01:58 +00001226 // optimized. FIXME: revisit this when we can custom lower all setcc
1227 // optimizations.
1228 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman475871a2008-07-27 21:46:04 +00001229 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001230 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001231
Chris Lattner1a635d62006-04-14 06:01:58 +00001232 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001233 // by xor'ing the rhs with the lhs, which is faster than setting a
1234 // condition register, reading it back out, and masking the correct bit. The
1235 // normal approach here uses sub to do this instead of xor. Using xor exposes
1236 // the result to other bit-twiddling opportunities.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001237 MVT LHSVT = Op.getOperand(0).getValueType();
1238 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1239 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001240 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001241 Op.getOperand(1));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001242 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner1a635d62006-04-14 06:01:58 +00001243 }
Dan Gohman475871a2008-07-27 21:46:04 +00001244 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001245}
1246
Dan Gohman475871a2008-07-27 21:46:04 +00001247SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001248 int VarArgsFrameIndex,
1249 int VarArgsStackOffset,
1250 unsigned VarArgsNumGPR,
1251 unsigned VarArgsNumFPR,
1252 const PPCSubtarget &Subtarget) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001253
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001254 assert(0 && "VAARG not yet implemented for the SVR4 ABI!");
Dan Gohman475871a2008-07-27 21:46:04 +00001255 return SDValue(); // Not reached
Nicolas Geoffray01119992007-04-03 13:59:52 +00001256}
1257
Bill Wendling77959322008-09-17 00:30:57 +00001258SDValue PPCTargetLowering::LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
1259 SDValue Chain = Op.getOperand(0);
1260 SDValue Trmp = Op.getOperand(1); // trampoline
1261 SDValue FPtr = Op.getOperand(2); // nested function
1262 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001263 DebugLoc dl = Op.getDebugLoc();
Bill Wendling77959322008-09-17 00:30:57 +00001264
1265 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1266 bool isPPC64 = (PtrVT == MVT::i64);
1267 const Type *IntPtrTy =
1268 DAG.getTargetLoweringInfo().getTargetData()->getIntPtrType();
1269
Scott Michelfdc40a02009-02-17 22:15:04 +00001270 TargetLowering::ArgListTy Args;
Bill Wendling77959322008-09-17 00:30:57 +00001271 TargetLowering::ArgListEntry Entry;
1272
1273 Entry.Ty = IntPtrTy;
1274 Entry.Node = Trmp; Args.push_back(Entry);
1275
1276 // TrampSize == (isPPC64 ? 48 : 40);
1277 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
1278 isPPC64 ? MVT::i64 : MVT::i32);
1279 Args.push_back(Entry);
1280
1281 Entry.Node = FPtr; Args.push_back(Entry);
1282 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelfdc40a02009-02-17 22:15:04 +00001283
Bill Wendling77959322008-09-17 00:30:57 +00001284 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
1285 std::pair<SDValue, SDValue> CallResult =
1286 LowerCallTo(Chain, Op.getValueType().getTypeForMVT(), false, false,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00001287 false, false, 0, CallingConv::C, false,
Bill Wendling77959322008-09-17 00:30:57 +00001288 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001289 Args, DAG, dl);
Bill Wendling77959322008-09-17 00:30:57 +00001290
1291 SDValue Ops[] =
1292 { CallResult.first, CallResult.second };
1293
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001294 return DAG.getMergeValues(Ops, 2, dl);
Bill Wendling77959322008-09-17 00:30:57 +00001295}
1296
Dan Gohman475871a2008-07-27 21:46:04 +00001297SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bill Wendling77959322008-09-17 00:30:57 +00001298 int VarArgsFrameIndex,
1299 int VarArgsStackOffset,
1300 unsigned VarArgsNumGPR,
1301 unsigned VarArgsNumFPR,
1302 const PPCSubtarget &Subtarget) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001303 DebugLoc dl = Op.getDebugLoc();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001304
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001305 if (Subtarget.isDarwinABI()) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001306 // vastart just stores the address of the VarArgsFrameIndex slot into the
1307 // memory location argument.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001308 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001309 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001310 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001311 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001312 }
1313
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001314 // For the SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray01119992007-04-03 13:59:52 +00001315 // We suppose the given va_list is already allocated.
1316 //
1317 // typedef struct {
1318 // char gpr; /* index into the array of 8 GPRs
1319 // * stored in the register save area
1320 // * gpr=0 corresponds to r3,
1321 // * gpr=1 to r4, etc.
1322 // */
1323 // char fpr; /* index into the array of 8 FPRs
1324 // * stored in the register save area
1325 // * fpr=0 corresponds to f1,
1326 // * fpr=1 to f2, etc.
1327 // */
1328 // char *overflow_arg_area;
1329 // /* location on stack that holds
1330 // * the next overflow argument
1331 // */
1332 // char *reg_save_area;
1333 // /* where r3:r10 and f1:f8 (if saved)
1334 // * are stored
1335 // */
1336 // } va_list[1];
1337
1338
Tilmann Schellerffd02002009-07-03 06:45:56 +00001339 SDValue ArgGPR = DAG.getConstant(VarArgsNumGPR, MVT::i32);
1340 SDValue ArgFPR = DAG.getConstant(VarArgsNumFPR, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001341
Nicolas Geoffray01119992007-04-03 13:59:52 +00001342
Duncan Sands83ec4b62008-06-06 12:08:01 +00001343 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00001344
Dan Gohman475871a2008-07-27 21:46:04 +00001345 SDValue StackOffsetFI = DAG.getFrameIndex(VarArgsStackOffset, PtrVT);
1346 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001347
Duncan Sands83ec4b62008-06-06 12:08:01 +00001348 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman475871a2008-07-27 21:46:04 +00001349 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001350
Duncan Sands83ec4b62008-06-06 12:08:01 +00001351 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001352 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001353
1354 uint64_t FPROffset = 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001355 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001356
Dan Gohman69de1932008-02-06 22:27:42 +00001357 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001358
Nicolas Geoffray01119992007-04-03 13:59:52 +00001359 // Store first byte : number of int regs
Tilmann Schellerffd02002009-07-03 06:45:56 +00001360 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
1361 Op.getOperand(1), SV, 0, MVT::i8);
Dan Gohman69de1932008-02-06 22:27:42 +00001362 uint64_t nextOffset = FPROffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001363 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray01119992007-04-03 13:59:52 +00001364 ConstFPROffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001365
Nicolas Geoffray01119992007-04-03 13:59:52 +00001366 // Store second byte : number of float regs
Dan Gohman475871a2008-07-27 21:46:04 +00001367 SDValue secondStore =
Tilmann Schellerffd02002009-07-03 06:45:56 +00001368 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, SV, nextOffset, MVT::i8);
Dan Gohman69de1932008-02-06 22:27:42 +00001369 nextOffset += StackOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001370 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001371
Nicolas Geoffray01119992007-04-03 13:59:52 +00001372 // Store second word : arguments given on stack
Dan Gohman475871a2008-07-27 21:46:04 +00001373 SDValue thirdStore =
Dale Johannesen33c960f2009-02-04 20:06:27 +00001374 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, SV, nextOffset);
Dan Gohman69de1932008-02-06 22:27:42 +00001375 nextOffset += FrameOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001376 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001377
1378 // Store third word : arguments given in registers
Dale Johannesen33c960f2009-02-04 20:06:27 +00001379 return DAG.getStore(thirdStore, dl, FR, nextPtr, SV, nextOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001380
Chris Lattner1a635d62006-04-14 06:01:58 +00001381}
1382
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001383#include "PPCGenCallingConv.inc"
1384
Tilmann Schellerffd02002009-07-03 06:45:56 +00001385static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
1386 CCValAssign::LocInfo &LocInfo,
1387 ISD::ArgFlagsTy &ArgFlags,
1388 CCState &State) {
1389 return true;
1390}
1391
1392static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
1393 MVT &LocVT,
1394 CCValAssign::LocInfo &LocInfo,
1395 ISD::ArgFlagsTy &ArgFlags,
1396 CCState &State) {
1397 static const unsigned ArgRegs[] = {
1398 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1399 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1400 };
1401 const unsigned NumArgRegs = array_lengthof(ArgRegs);
1402
1403 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1404
1405 // Skip one register if the first unallocated register has an even register
1406 // number and there are still argument registers available which have not been
1407 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1408 // need to skip a register if RegNum is odd.
1409 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1410 State.AllocateReg(ArgRegs[RegNum]);
1411 }
1412
1413 // Always return false here, as this function only makes sure that the first
1414 // unallocated register has an odd register number and does not actually
1415 // allocate a register for the current argument.
1416 return false;
1417}
1418
1419static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
1420 MVT &LocVT,
1421 CCValAssign::LocInfo &LocInfo,
1422 ISD::ArgFlagsTy &ArgFlags,
1423 CCState &State) {
1424 static const unsigned ArgRegs[] = {
1425 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1426 PPC::F8
1427 };
1428
1429 const unsigned NumArgRegs = array_lengthof(ArgRegs);
1430
1431 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1432
1433 // If there is only one Floating-point register left we need to put both f64
1434 // values of a split ppc_fp128 value on the stack.
1435 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1436 State.AllocateReg(ArgRegs[RegNum]);
1437 }
1438
1439 // Always return false here, as this function only makes sure that the two f64
1440 // values a ppc_fp128 value is split into are both passed in registers or both
1441 // passed on the stack and does not actually allocate a register for the
1442 // current argument.
1443 return false;
1444}
1445
Chris Lattner9f0bc652007-02-25 05:34:32 +00001446/// GetFPR - Get the set of FP registers that should be allocated for arguments,
1447/// depending on which subtarget is selected.
1448static const unsigned *GetFPR(const PPCSubtarget &Subtarget) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001449 if (Subtarget.isDarwinABI()) {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001450 static const unsigned FPR[] = {
1451 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1452 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1453 };
1454 return FPR;
1455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
1457
Chris Lattner9f0bc652007-02-25 05:34:32 +00001458 static const unsigned FPR[] = {
1459 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Nicolas Geoffrayef3c0302007-04-03 10:27:07 +00001460 PPC::F8
Chris Lattner9f0bc652007-02-25 05:34:32 +00001461 };
1462 return FPR;
1463}
1464
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001465/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1466/// the stack.
Dan Gohman095cc292008-09-13 01:54:27 +00001467static unsigned CalculateStackSlotSize(SDValue Arg, ISD::ArgFlagsTy Flags,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001468 unsigned PtrByteSize) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001469 MVT ArgVT = Arg.getValueType();
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001470 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001471 if (Flags.isByVal())
1472 ArgSize = Flags.getByValSize();
1473 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1474
1475 return ArgSize;
1476}
1477
Dan Gohman475871a2008-07-27 21:46:04 +00001478SDValue
Tilmann Schellerffd02002009-07-03 06:45:56 +00001479PPCTargetLowering::LowerFORMAL_ARGUMENTS_SVR4(SDValue Op,
1480 SelectionDAG &DAG,
1481 int &VarArgsFrameIndex,
1482 int &VarArgsStackOffset,
1483 unsigned &VarArgsNumGPR,
1484 unsigned &VarArgsNumFPR,
1485 const PPCSubtarget &Subtarget) {
1486 // SVR4 ABI Stack Frame Layout:
1487 // +-----------------------------------+
1488 // +--> | Back chain |
1489 // | +-----------------------------------+
1490 // | | Floating-point register save area |
1491 // | +-----------------------------------+
1492 // | | General register save area |
1493 // | +-----------------------------------+
1494 // | | CR save word |
1495 // | +-----------------------------------+
1496 // | | VRSAVE save word |
1497 // | +-----------------------------------+
1498 // | | Alignment padding |
1499 // | +-----------------------------------+
1500 // | | Vector register save area |
1501 // | +-----------------------------------+
1502 // | | Local variable space |
1503 // | +-----------------------------------+
1504 // | | Parameter list area |
1505 // | +-----------------------------------+
1506 // | | LR save word |
1507 // | +-----------------------------------+
1508 // SP--> +--- | Back chain |
1509 // +-----------------------------------+
1510 //
1511 // Specifications:
1512 // System V Application Binary Interface PowerPC Processor Supplement
1513 // AltiVec Technology Programming Interface Manual
1514
1515 MachineFunction &MF = DAG.getMachineFunction();
1516 MachineFrameInfo *MFI = MF.getFrameInfo();
1517 SmallVector<SDValue, 8> ArgValues;
1518 SDValue Root = Op.getOperand(0);
1519 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
1520 DebugLoc dl = Op.getDebugLoc();
1521
1522 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1523 // Potential tail calls could cause overwriting of argument stack slots.
1524 unsigned CC = MF.getFunction()->getCallingConv();
1525 bool isImmutable = !(PerformTailCallOpt && (CC==CallingConv::Fast));
1526 unsigned PtrByteSize = 4;
1527
1528 // Assign locations to all of the incoming arguments.
1529 SmallVector<CCValAssign, 16> ArgLocs;
1530 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1531
1532 // Reserve space for the linkage area on the stack.
1533 CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
1534
1535 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_PPC_SVR4);
1536
1537 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1538 CCValAssign &VA = ArgLocs[i];
1539
1540 // Arguments stored in registers.
1541 if (VA.isRegLoc()) {
1542 TargetRegisterClass *RC;
1543 MVT ValVT = VA.getValVT();
1544
1545 switch (ValVT.getSimpleVT()) {
1546 default:
1547 assert(0 && "ValVT not supported by FORMAL_ARGUMENTS Lowering");
1548 case MVT::i32:
1549 RC = PPC::GPRCRegisterClass;
1550 break;
1551 case MVT::f32:
1552 RC = PPC::F4RCRegisterClass;
1553 break;
1554 case MVT::f64:
1555 RC = PPC::F8RCRegisterClass;
1556 break;
1557 case MVT::v16i8:
1558 case MVT::v8i16:
1559 case MVT::v4i32:
1560 case MVT::v4f32:
1561 RC = PPC::VRRCRegisterClass;
1562 break;
1563 }
1564
1565 // Transform the arguments stored in physical registers into virtual ones.
1566 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
1567 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, ValVT);
1568
1569 ArgValues.push_back(ArgValue);
1570 } else {
1571 // Argument stored in memory.
1572 assert(VA.isMemLoc());
1573
1574 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
1575 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1576 isImmutable);
1577
1578 // Create load nodes to retrieve arguments from the stack.
1579 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1580 ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0));
1581 }
1582 }
1583
1584 // Assign locations to all of the incoming aggregate by value arguments.
1585 // Aggregates passed by value are stored in the local variable space of the
1586 // caller's stack frame, right above the parameter list area.
1587 SmallVector<CCValAssign, 16> ByValArgLocs;
1588 CCState CCByValInfo(CC, isVarArg, getTargetMachine(), ByValArgLocs);
1589
1590 // Reserve stack space for the allocations in CCInfo.
1591 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
1592
1593 CCByValInfo.AnalyzeFormalArguments(Op.getNode(), CC_PPC_SVR4_ByVal);
1594
1595 // Area that is at least reserved in the caller of this function.
1596 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
1597
1598 // Set the size that is at least reserved in caller of this function. Tail
1599 // call optimized function's reserved stack space needs to be aligned so that
1600 // taking the difference between two stack areas will result in an aligned
1601 // stack.
1602 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1603
1604 MinReservedArea =
1605 std::max(MinReservedArea,
1606 PPCFrameInfo::getMinCallFrameSize(false, false));
1607
1608 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
1609 getStackAlignment();
1610 unsigned AlignMask = TargetAlign-1;
1611 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
1612
1613 FI->setMinReservedArea(MinReservedArea);
1614
1615 SmallVector<SDValue, 8> MemOps;
1616
1617 // If the function takes variable number of arguments, make a frame index for
1618 // the start of the first vararg value... for expansion of llvm.va_start.
1619 if (isVarArg) {
1620 static const unsigned GPArgRegs[] = {
1621 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1622 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1623 };
1624 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
1625
1626 static const unsigned FPArgRegs[] = {
1627 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1628 PPC::F8
1629 };
1630 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
1631
1632 VarArgsNumGPR = CCInfo.getFirstUnallocated(GPArgRegs, NumGPArgRegs);
1633 VarArgsNumFPR = CCInfo.getFirstUnallocated(FPArgRegs, NumFPArgRegs);
1634
1635 // Make room for NumGPArgRegs and NumFPArgRegs.
1636 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
1637 NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8;
1638
1639 VarArgsStackOffset = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
1640 CCInfo.getNextStackOffset());
1641
1642 VarArgsFrameIndex = MFI->CreateStackObject(Depth, 8);
1643 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1644
1645 // The fixed integer arguments of a variadic function are
1646 // stored to the VarArgsFrameIndex on the stack.
1647 unsigned GPRIndex = 0;
1648 for (; GPRIndex != VarArgsNumGPR; ++GPRIndex) {
1649 SDValue Val = DAG.getRegister(GPArgRegs[GPRIndex], PtrVT);
1650 SDValue Store = DAG.getStore(Root, dl, Val, FIN, NULL, 0);
1651 MemOps.push_back(Store);
1652 // Increment the address by four for the next argument to store
1653 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
1654 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1655 }
1656
1657 // If this function is vararg, store any remaining integer argument regs
1658 // to their spots on the stack so that they may be loaded by deferencing the
1659 // result of va_next.
1660 for (; GPRIndex != NumGPArgRegs; ++GPRIndex) {
1661 unsigned VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
1662
1663 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT);
1664 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1665 MemOps.push_back(Store);
1666 // Increment the address by four for the next argument to store
1667 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
1668 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1669 }
1670
1671 // FIXME SVR4: We only need to save FP argument registers if CR bit 6 is
1672 // set.
1673
1674 // The double arguments are stored to the VarArgsFrameIndex
1675 // on the stack.
1676 unsigned FPRIndex = 0;
1677 for (FPRIndex = 0; FPRIndex != VarArgsNumFPR; ++FPRIndex) {
1678 SDValue Val = DAG.getRegister(FPArgRegs[FPRIndex], MVT::f64);
1679 SDValue Store = DAG.getStore(Root, dl, Val, FIN, NULL, 0);
1680 MemOps.push_back(Store);
1681 // Increment the address by eight for the next argument to store
1682 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
1683 PtrVT);
1684 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1685 }
1686
1687 for (; FPRIndex != NumFPArgRegs; ++FPRIndex) {
1688 unsigned VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
1689
1690 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::f64);
1691 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1692 MemOps.push_back(Store);
1693 // Increment the address by eight for the next argument to store
1694 SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8,
1695 PtrVT);
1696 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1697 }
1698 }
1699
1700 if (!MemOps.empty())
1701 Root = DAG.getNode(ISD::TokenFactor, dl,
1702 MVT::Other, &MemOps[0], MemOps.size());
1703
1704
1705 ArgValues.push_back(Root);
1706
1707 // Return the new list of results.
1708 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
1709 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
1710}
1711
1712SDValue
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001713PPCTargetLowering::LowerFORMAL_ARGUMENTS_Darwin(SDValue Op,
1714 SelectionDAG &DAG,
1715 int &VarArgsFrameIndex,
1716 const PPCSubtarget &Subtarget) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001717 // TODO: add description of PPC stack frame format, or at least some docs.
1718 //
1719 MachineFunction &MF = DAG.getMachineFunction();
1720 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001721 SmallVector<SDValue, 8> ArgValues;
1722 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001723 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001724 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001725
Duncan Sands83ec4b62008-06-06 12:08:01 +00001726 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00001727 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001728 // Potential tail calls could cause overwriting of argument stack slots.
1729 unsigned CC = MF.getFunction()->getCallingConv();
1730 bool isImmutable = !(PerformTailCallOpt && (CC==CallingConv::Fast));
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001731 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00001732
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001733 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001734 // Area that is at least reserved in caller of this function.
1735 unsigned MinReservedArea = ArgOffset;
1736
Chris Lattnerc91a4752006-06-26 22:48:35 +00001737 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001738 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1739 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1740 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001741 static const unsigned GPR_64[] = { // 64-bit registers.
1742 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1743 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1744 };
Scott Michelfdc40a02009-02-17 22:15:04 +00001745
Chris Lattner9f0bc652007-02-25 05:34:32 +00001746 static const unsigned *FPR = GetFPR(Subtarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00001747
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001748 static const unsigned VR[] = {
1749 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1750 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1751 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001752
Owen Anderson718cb662007-09-07 04:06:50 +00001753 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001754 const unsigned Num_FPR_Regs = 13;
Owen Anderson718cb662007-09-07 04:06:50 +00001755 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00001756
1757 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00001758
Chris Lattnerc91a4752006-06-26 22:48:35 +00001759 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelfdc40a02009-02-17 22:15:04 +00001760
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001761 // In 32-bit non-varargs functions, the stack space for vectors is after the
1762 // stack space for non-vectors. We do not use this space unless we have
1763 // too many vectors to fit in registers, something that only occurs in
Scott Michelfdc40a02009-02-17 22:15:04 +00001764 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001765 // that out...for the pathological case, compute VecArgOffset as the
1766 // start of the vector parameter area. Computing VecArgOffset is the
1767 // entire point of the following loop.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001768 unsigned VecArgOffset = ArgOffset;
1769 if (!isVarArg && !isPPC64) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001770 for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues()-1; ArgNo != e;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001771 ++ArgNo) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001772 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
1773 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001774 ISD::ArgFlagsTy Flags =
1775 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001776
Duncan Sands276dcbd2008-03-21 09:14:45 +00001777 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001778 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001779 ObjSize = Flags.getByValSize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001780 unsigned ArgSize =
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001781 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1782 VecArgOffset += ArgSize;
1783 continue;
1784 }
1785
Duncan Sands83ec4b62008-06-06 12:08:01 +00001786 switch(ObjectVT.getSimpleVT()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001787 default: assert(0 && "Unhandled argument type!");
1788 case MVT::i32:
1789 case MVT::f32:
1790 VecArgOffset += isPPC64 ? 8 : 4;
1791 break;
1792 case MVT::i64: // PPC64
1793 case MVT::f64:
1794 VecArgOffset += 8;
1795 break;
1796 case MVT::v4f32:
1797 case MVT::v4i32:
1798 case MVT::v8i16:
1799 case MVT::v16i8:
1800 // Nothing to do, we're only looking at Nonvector args here.
1801 break;
1802 }
1803 }
1804 }
1805 // We've found where the vector parameter area in memory is. Skip the
1806 // first 12 parameters; these don't use that memory.
1807 VecArgOffset = ((VecArgOffset+15)/16)*16;
1808 VecArgOffset += 12*16;
1809
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001810 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00001811 // entry to a function on PPC, the arguments start after the linkage area,
1812 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001813
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001815 unsigned nAltivecParamsAtEnd = 0;
Gabor Greif93c53e52008-08-31 15:37:04 +00001816 for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues() - 1;
1817 ArgNo != e; ++ArgNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001818 SDValue ArgVal;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001819 bool needsLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001820 MVT ObjectVT = Op.getValue(ArgNo).getValueType();
1821 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey619965d2006-11-29 13:37:09 +00001822 unsigned ArgSize = ObjSize;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001823 ISD::ArgFlagsTy Flags =
1824 cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags();
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001825
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001826 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001827
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001828 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
1829 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
1830 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
1831 if (isVarArg || isPPC64) {
1832 MinReservedArea = ((MinReservedArea+15)/16)*16;
1833 MinReservedArea += CalculateStackSlotSize(Op.getValue(ArgNo),
Dan Gohman095cc292008-09-13 01:54:27 +00001834 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001835 PtrByteSize);
1836 } else nAltivecParamsAtEnd++;
1837 } else
1838 // Calculate min reserved area.
1839 MinReservedArea += CalculateStackSlotSize(Op.getValue(ArgNo),
Dan Gohman095cc292008-09-13 01:54:27 +00001840 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001841 PtrByteSize);
1842
Dale Johannesen8419dd62008-03-07 20:27:40 +00001843 // FIXME the codegen can be much improved in some cases.
1844 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001845 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00001846 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001847 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00001848 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001849 // Objects of size 1 and 2 are right justified, everything else is
1850 // left justified. This means the memory address is adjusted forwards.
1851 if (ObjSize==1 || ObjSize==2) {
1852 CurArgOffset = CurArgOffset + (4 - ObjSize);
1853 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001854 // The value of the object is its address.
1855 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +00001856 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001857 ArgValues.push_back(FIN);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001858 if (ObjSize==1 || ObjSize==2) {
1859 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001860 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dale Johannesen39355f92009-02-04 02:34:38 +00001861 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001862 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Dale Johannesen7f96f392008-03-08 01:41:42 +00001863 NULL, 0, ObjSize==1 ? MVT::i8 : MVT::i16 );
1864 MemOps.push_back(Store);
1865 ++GPR_idx;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001866 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001867
1868 ArgOffset += PtrByteSize;
1869
Dale Johannesen7f96f392008-03-08 01:41:42 +00001870 continue;
1871 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001872 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
1873 // Store whatever pieces of the object are in registers
1874 // to memory. ArgVal will be address of the beginning of
1875 // the object.
1876 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001877 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001878 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +00001879 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00001880 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT);
1881 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001882 MemOps.push_back(Store);
1883 ++GPR_idx;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001884 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001885 } else {
1886 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
1887 break;
1888 }
1889 }
1890 continue;
1891 }
1892
Duncan Sands83ec4b62008-06-06 12:08:01 +00001893 switch (ObjectVT.getSimpleVT()) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001894 default: assert(0 && "Unhandled argument type!");
1895 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001896 if (!isPPC64) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001897 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001898 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dale Johannesen39355f92009-02-04 02:34:38 +00001899 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001900 ++GPR_idx;
1901 } else {
1902 needsLoad = true;
1903 ArgSize = PtrByteSize;
1904 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001905 // All int arguments reserve stack space in the Darwin ABI.
1906 ArgOffset += PtrByteSize;
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001907 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001908 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001909 // FALLTHROUGH
Chris Lattner9f0bc652007-02-25 05:34:32 +00001910 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00001911 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001912 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Dale Johannesen39355f92009-02-04 02:34:38 +00001913 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001914
1915 if (ObjectVT == MVT::i32) {
1916 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
1917 // value to MVT::i64 and then truncate to the correct register size.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001918 if (Flags.isSExt())
Dale Johannesen39355f92009-02-04 02:34:38 +00001919 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001920 DAG.getValueType(ObjectVT));
Duncan Sands276dcbd2008-03-21 09:14:45 +00001921 else if (Flags.isZExt())
Dale Johannesen39355f92009-02-04 02:34:38 +00001922 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001923 DAG.getValueType(ObjectVT));
1924
Dale Johannesen39355f92009-02-04 02:34:38 +00001925 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001926 }
1927
Chris Lattnerc91a4752006-06-26 22:48:35 +00001928 ++GPR_idx;
1929 } else {
1930 needsLoad = true;
Evan Cheng982a0592008-07-24 08:17:07 +00001931 ArgSize = PtrByteSize;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001932 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001933 // All int arguments reserve stack space in the Darwin ABI.
1934 ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001935 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001936
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001937 case MVT::f32:
1938 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001939 // Every 4 bytes of argument space consumes one of the GPRs available for
1940 // argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001941 if (GPR_idx != Num_GPR_Regs) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001942 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001943 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001944 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001945 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001946 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001947 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001948
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001949 if (ObjectVT == MVT::f32)
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001950 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001951 else
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001952 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
1953
Dale Johannesen39355f92009-02-04 02:34:38 +00001954 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001955 ++FPR_idx;
1956 } else {
1957 needsLoad = true;
1958 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001959
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001960 // All FP arguments reserve stack space in the Darwin ABI.
1961 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001962 break;
1963 case MVT::v4f32:
1964 case MVT::v4i32:
1965 case MVT::v8i16:
1966 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00001967 // Note that vector arguments in registers don't reserve stack space,
1968 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001969 if (VR_idx != Num_VR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001970 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dale Johannesen39355f92009-02-04 02:34:38 +00001971 ArgVal = DAG.getCopyFromReg(Root, dl, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00001972 if (isVarArg) {
1973 while ((ArgOffset % 16) != 0) {
1974 ArgOffset += PtrByteSize;
1975 if (GPR_idx != Num_GPR_Regs)
1976 GPR_idx++;
1977 }
1978 ArgOffset += 16;
1979 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs);
1980 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001981 ++VR_idx;
1982 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001983 if (!isVarArg && !isPPC64) {
1984 // Vectors go after all the nonvectors.
1985 CurArgOffset = VecArgOffset;
1986 VecArgOffset += 16;
1987 } else {
1988 // Vectors are aligned.
1989 ArgOffset = ((ArgOffset+15)/16)*16;
1990 CurArgOffset = ArgOffset;
1991 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00001992 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001993 needsLoad = true;
1994 }
1995 break;
1996 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001997
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001998 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00001999 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002000 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002001 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002002 CurArgOffset + (ArgSize - ObjSize),
2003 isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00002004 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002005 ArgVal = DAG.getLoad(ObjectVT, dl, Root, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002006 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002007
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002008 ArgValues.push_back(ArgVal);
2009 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002010
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002011 // Set the size that is at least reserved in caller of this function. Tail
2012 // call optimized function's reserved stack space needs to be aligned so that
2013 // taking the difference between two stack areas will result in an aligned
2014 // stack.
2015 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2016 // Add the Altivec parameters at the end, if needed.
2017 if (nAltivecParamsAtEnd) {
2018 MinReservedArea = ((MinReservedArea+15)/16)*16;
2019 MinReservedArea += 16*nAltivecParamsAtEnd;
2020 }
2021 MinReservedArea =
2022 std::max(MinReservedArea,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002023 PPCFrameInfo::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002024 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
2025 getStackAlignment();
2026 unsigned AlignMask = TargetAlign-1;
2027 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2028 FI->setMinReservedArea(MinReservedArea);
2029
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002030 // If the function takes variable number of arguments, make a frame index for
2031 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002032 if (isVarArg) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002033 int Depth = ArgOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00002034
Duncan Sands83ec4b62008-06-06 12:08:01 +00002035 VarArgsFrameIndex = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002036 Depth);
Dan Gohman475871a2008-07-27 21:46:04 +00002037 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002038
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002039 // If this function is vararg, store any remaining integer argument regs
2040 // to their spots on the stack so that they may be loaded by deferencing the
2041 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002042 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002043 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002044
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002045 if (isPPC64)
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002046 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002047 else
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002048 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002049
Dale Johannesen39355f92009-02-04 02:34:38 +00002050 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT);
2051 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002052 MemOps.push_back(Store);
2053 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002054 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002055 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002056 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002057 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002058
Dale Johannesen8419dd62008-03-07 20:27:40 +00002059 if (!MemOps.empty())
Scott Michelfdc40a02009-02-17 22:15:04 +00002060 Root = DAG.getNode(ISD::TokenFactor, dl,
Dale Johannesen39355f92009-02-04 02:34:38 +00002061 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002062
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002063 ArgValues.push_back(Root);
Scott Michelfdc40a02009-02-17 22:15:04 +00002064
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002065 // Return the new list of results.
Dale Johannesen39355f92009-02-04 02:34:38 +00002066 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002067 &ArgValues[0], ArgValues.size());
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002068}
2069
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002070/// CalculateParameterAndLinkageAreaSize - Get the size of the paramter plus
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002071/// linkage area for the Darwin ABI.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002072static unsigned
2073CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2074 bool isPPC64,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002075 bool isVarArg,
2076 unsigned CC,
Dan Gohman095cc292008-09-13 01:54:27 +00002077 CallSDNode *TheCall,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002078 unsigned &nAltivecParamsAtEnd) {
2079 // Count how many bytes are to be pushed on the stack, including the linkage
2080 // area, and parameter passing area. We start with 24/48 bytes, which is
2081 // prereserved space for [SP][CR][LR][3 x unused].
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002082 unsigned NumBytes = PPCFrameInfo::getLinkageSize(isPPC64, true);
Dan Gohman095cc292008-09-13 01:54:27 +00002083 unsigned NumOps = TheCall->getNumArgs();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002084 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2085
2086 // Add up all the space actually used.
2087 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2088 // they all go in registers, but we must reserve stack space for them for
2089 // possible use by the caller. In varargs or 64-bit calls, parameters are
2090 // assigned stack space in order, with padding so Altivec parameters are
2091 // 16-byte aligned.
2092 nAltivecParamsAtEnd = 0;
2093 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman095cc292008-09-13 01:54:27 +00002094 SDValue Arg = TheCall->getArg(i);
2095 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002096 MVT ArgVT = Arg.getValueType();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002097 // Varargs Altivec parameters are padded to a 16 byte boundary.
2098 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2099 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
2100 if (!isVarArg && !isPPC64) {
2101 // Non-varargs Altivec parameters go after all the non-Altivec
2102 // parameters; handle those later so we know how much padding we need.
2103 nAltivecParamsAtEnd++;
2104 continue;
2105 }
2106 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2107 NumBytes = ((NumBytes+15)/16)*16;
2108 }
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002109 NumBytes += CalculateStackSlotSize(Arg, Flags, PtrByteSize);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002110 }
2111
2112 // Allow for Altivec parameters at the end, if needed.
2113 if (nAltivecParamsAtEnd) {
2114 NumBytes = ((NumBytes+15)/16)*16;
2115 NumBytes += 16*nAltivecParamsAtEnd;
2116 }
2117
2118 // The prolog code of the callee may store up to 8 GPR argument registers to
2119 // the stack, allowing va_start to index over them in memory if its varargs.
2120 // Because we cannot tell if this is needed on the caller side, we have to
2121 // conservatively assume that it is needed. As such, make sure we have at
2122 // least enough stack space for the caller to store the 8 GPRs.
2123 NumBytes = std::max(NumBytes,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002124 PPCFrameInfo::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002125
2126 // Tail call needs the stack to be aligned.
2127 if (CC==CallingConv::Fast && PerformTailCallOpt) {
2128 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
2129 getStackAlignment();
2130 unsigned AlignMask = TargetAlign-1;
2131 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2132 }
2133
2134 return NumBytes;
2135}
2136
2137/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
2138/// adjusted to accomodate the arguments for the tailcall.
2139static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool IsTailCall,
2140 unsigned ParamSize) {
2141
2142 if (!IsTailCall) return 0;
2143
2144 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2145 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2146 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2147 // Remember only if the new adjustement is bigger.
2148 if (SPDiff < FI->getTailCallSPDelta())
2149 FI->setTailCallSPDelta(SPDiff);
2150
2151 return SPDiff;
2152}
2153
2154/// IsEligibleForTailCallElimination - Check to see whether the next instruction
2155/// following the call is a return. A function is eligible if caller/callee
2156/// calling conventions match, currently only fastcc supports tail calls, and
2157/// the function CALL is immediatly followed by a RET.
2158bool
Dan Gohman095cc292008-09-13 01:54:27 +00002159PPCTargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00002160 SDValue Ret,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002161 SelectionDAG& DAG) const {
2162 // Variable argument functions are not supported.
Dan Gohman095cc292008-09-13 01:54:27 +00002163 if (!PerformTailCallOpt || TheCall->isVarArg())
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002164 return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002165
Dan Gohman095cc292008-09-13 01:54:27 +00002166 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002167 MachineFunction &MF = DAG.getMachineFunction();
2168 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00002169 unsigned CalleeCC = TheCall->getCallingConv();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002170 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2171 // Functions containing by val parameters are not supported.
Dan Gohman095cc292008-09-13 01:54:27 +00002172 for (unsigned i = 0; i != TheCall->getNumArgs(); i++) {
2173 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002174 if (Flags.isByVal()) return false;
2175 }
2176
Dan Gohman095cc292008-09-13 01:54:27 +00002177 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002178 // Non PIC/GOT tail calls are supported.
2179 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2180 return true;
2181
2182 // At the moment we can only do local tail calls (in same module, hidden
2183 // or protected) if we are generating PIC.
2184 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2185 return G->getGlobal()->hasHiddenVisibility()
2186 || G->getGlobal()->hasProtectedVisibility();
2187 }
2188 }
2189
2190 return false;
2191}
2192
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002193/// isCallCompatibleAddress - Return the immediate to use if the specified
2194/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00002195static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002196 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2197 if (!C) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002198
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002199 int Addr = C->getZExtValue();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002200 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
2201 (Addr << 6 >> 6) != Addr)
2202 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002203
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002204 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greifba36cb52008-08-28 21:40:38 +00002205 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002206}
2207
Dan Gohman844731a2008-05-13 00:00:25 +00002208namespace {
2209
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002210struct TailCallArgumentInfo {
Dan Gohman475871a2008-07-27 21:46:04 +00002211 SDValue Arg;
2212 SDValue FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002213 int FrameIdx;
2214
2215 TailCallArgumentInfo() : FrameIdx(0) {}
2216};
2217
Dan Gohman844731a2008-05-13 00:00:25 +00002218}
2219
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002220/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
2221static void
2222StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002223 SDValue Chain,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002224 const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002225 SmallVector<SDValue, 8> &MemOpChains,
2226 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002227 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002228 SDValue Arg = TailCallArgs[i].Arg;
2229 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002230 int FI = TailCallArgs[i].FrameIdx;
2231 // Store relative to framepointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002232 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00002233 PseudoSourceValue::getFixedStack(FI),
2234 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002235 }
2236}
2237
2238/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
2239/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman475871a2008-07-27 21:46:04 +00002240static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002241 MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002242 SDValue Chain,
2243 SDValue OldRetAddr,
2244 SDValue OldFP,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002245 int SPDiff,
2246 bool isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002247 bool isDarwinABI,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002248 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002249 if (SPDiff) {
2250 // Calculate the new stack slot for the return address.
2251 int SlotSize = isPPC64 ? 8 : 4;
2252 int NewRetAddrLoc = SPDiff + PPCFrameInfo::getReturnSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002253 isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002254 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
2255 NewRetAddrLoc);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002256 MVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002257 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002258 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00002259 PseudoSourceValue::getFixedStack(NewRetAddr), 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002260
2261 // When using the SVR4 ABI there is no need to move the FP stack slot
2262 // as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002263 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002264 int NewFPLoc =
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002265 SPDiff + PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002266 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc);
2267 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
2268 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
2269 PseudoSourceValue::getFixedStack(NewFPIdx), 0);
2270 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002271 }
2272 return Chain;
2273}
2274
2275/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
2276/// the position of the argument.
2277static void
2278CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman475871a2008-07-27 21:46:04 +00002279 SDValue Arg, int SPDiff, unsigned ArgOffset,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002280 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
2281 int Offset = ArgOffset + SPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002282 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002283 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002284 MVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002285 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002286 TailCallArgumentInfo Info;
2287 Info.Arg = Arg;
2288 Info.FrameIdxOp = FIN;
2289 Info.FrameIdx = FI;
2290 TailCallArguments.push_back(Info);
2291}
2292
2293/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
2294/// stack slot. Returns the chain as result and the loaded frame pointers in
2295/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman475871a2008-07-27 21:46:04 +00002296SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002297 int SPDiff,
2298 SDValue Chain,
2299 SDValue &LROpOut,
2300 SDValue &FPOpOut,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002301 bool isDarwinABI,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002302 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002303 if (SPDiff) {
2304 // Load the LR and FP stack slot for later adjusting.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002305 MVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002306 LROpOut = getReturnAddrFrameIndex(DAG);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002307 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002308 Chain = SDValue(LROpOut.getNode(), 1);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002309
2310 // When using the SVR4 ABI there is no need to load the FP stack slot
2311 // as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002312 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002313 FPOpOut = getFramePointerFrameIndex(DAG);
2314 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, NULL, 0);
2315 Chain = SDValue(FPOpOut.getNode(), 1);
2316 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002317 }
2318 return Chain;
2319}
2320
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002321/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelfdc40a02009-02-17 22:15:04 +00002322/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002323/// specified by the specific parameter attribute. The copy will be passed as
2324/// a byval function parameter.
2325/// Sometimes what we are copying is the end of a larger object, the part that
2326/// does not fit in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00002327static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00002328CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00002329 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002330 DebugLoc dl) {
2331 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen8ad9b432009-02-04 01:17:06 +00002332 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
2333 false, NULL, 0, NULL, 0);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002334}
Chris Lattner9f0bc652007-02-25 05:34:32 +00002335
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002336/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
2337/// tail calls.
2338static void
Dan Gohman475871a2008-07-27 21:46:04 +00002339LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
2340 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002341 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Dan Gohman475871a2008-07-27 21:46:04 +00002342 bool isVector, SmallVector<SDValue, 8> &MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002343 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments,
2344 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002345 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002346 if (!isTailCall) {
2347 if (isVector) {
Dan Gohman475871a2008-07-27 21:46:04 +00002348 SDValue StackPtr;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002349 if (isPPC64)
2350 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
2351 else
2352 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002353 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002354 DAG.getConstant(ArgOffset, PtrVT));
2355 }
Dale Johannesen33c960f2009-02-04 20:06:27 +00002356 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002357 // Calculate and remember argument location.
2358 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
2359 TailCallArguments);
2360}
2361
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002362static
2363void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
2364 DebugLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
2365 SDValue LROp, SDValue FPOp, bool isDarwinABI,
2366 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) {
2367 MachineFunction &MF = DAG.getMachineFunction();
2368
2369 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
2370 // might overwrite each other in case of tail call optimization.
2371 SmallVector<SDValue, 8> MemOpChains2;
2372 // Do not flag preceeding copytoreg stuff together with the following stuff.
2373 InFlag = SDValue();
2374 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
2375 MemOpChains2, dl);
2376 if (!MemOpChains2.empty())
2377 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2378 &MemOpChains2[0], MemOpChains2.size());
2379
2380 // Store the return address to the appropriate stack slot.
2381 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
2382 isPPC64, isDarwinABI, dl);
2383
2384 // Emit callseq_end just before tailcall node.
2385 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2386 DAG.getIntPtrConstant(0, true), InFlag);
2387 InFlag = Chain.getValue(1);
2388}
2389
2390static
2391unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
2392 SDValue &Chain, DebugLoc dl, int SPDiff, bool isTailCall,
2393 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
2394 SmallVector<SDValue, 8> &Ops, std::vector<MVT> &NodeTys,
2395 bool isSVR4ABI) {
2396 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2397 NodeTys.push_back(MVT::Other); // Returns a chain
2398 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
2399
2400 unsigned CallOpc = isSVR4ABI ? PPCISD::CALL_SVR4 : PPCISD::CALL_Darwin;
2401
2402 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2403 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2404 // node so that legalize doesn't hack it.
2405 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2406 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
2407 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
2408 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType());
2409 else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
2410 // If this is an absolute destination address, use the munged value.
2411 Callee = SDValue(Dest, 0);
2412 else {
2413 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
2414 // to do the call, we can't use PPCISD::CALL.
2415 SDValue MTCTROps[] = {Chain, Callee, InFlag};
2416 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
2417 2 + (InFlag.getNode() != 0));
2418 InFlag = Chain.getValue(1);
2419
2420 NodeTys.clear();
2421 NodeTys.push_back(MVT::Other);
2422 NodeTys.push_back(MVT::Flag);
2423 Ops.push_back(Chain);
2424 CallOpc = isSVR4ABI ? PPCISD::BCTRL_SVR4 : PPCISD::BCTRL_Darwin;
2425 Callee.setNode(0);
2426 // Add CTR register as callee so a bctr can be emitted later.
2427 if (isTailCall)
2428 Ops.push_back(DAG.getRegister(PPC::CTR, PtrVT));
2429 }
2430
2431 // If this is a direct call, pass the chain and the callee.
2432 if (Callee.getNode()) {
2433 Ops.push_back(Chain);
2434 Ops.push_back(Callee);
2435 }
2436 // If this is a tail call add stack pointer delta.
2437 if (isTailCall)
2438 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
2439
2440 // Add argument registers to the end of the list so that they are known live
2441 // into the call.
2442 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2443 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2444 RegsToPass[i].second.getValueType()));
2445
2446 return CallOpc;
2447}
2448
2449static SDValue LowerCallReturn(SDValue Op, SelectionDAG &DAG, TargetMachine &TM,
2450 CallSDNode *TheCall, SDValue Chain,
2451 SDValue InFlag) {
2452 bool isVarArg = TheCall->isVarArg();
2453 DebugLoc dl = TheCall->getDebugLoc();
2454 SmallVector<SDValue, 16> ResultVals;
2455 SmallVector<CCValAssign, 16> RVLocs;
2456 unsigned CallerCC = DAG.getMachineFunction().getFunction()->getCallingConv();
2457 CCState CCRetInfo(CallerCC, isVarArg, TM, RVLocs);
2458 CCRetInfo.AnalyzeCallResult(TheCall, RetCC_PPC);
2459
2460 // Copy all of the result registers out of their specified physreg.
2461 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
2462 CCValAssign &VA = RVLocs[i];
2463 MVT VT = VA.getValVT();
2464 assert(VA.isRegLoc() && "Can only return in registers!");
2465 Chain = DAG.getCopyFromReg(Chain, dl,
2466 VA.getLocReg(), VT, InFlag).getValue(1);
2467 ResultVals.push_back(Chain.getValue(0));
2468 InFlag = Chain.getValue(2);
2469 }
2470
2471 // If the function returns void, just return the chain.
2472 if (RVLocs.empty())
2473 return Chain;
2474
2475 // Otherwise, merge everything together with a MERGE_VALUES node.
2476 ResultVals.push_back(Chain);
2477 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
2478 &ResultVals[0], ResultVals.size());
2479 return Res.getValue(Op.getResNo());
2480}
2481
2482static
2483SDValue FinishCall(SelectionDAG &DAG, CallSDNode *TheCall, TargetMachine &TM,
2484 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
2485 SDValue Op, SDValue InFlag, SDValue Chain, SDValue &Callee,
2486 int SPDiff, unsigned NumBytes) {
2487 unsigned CC = TheCall->getCallingConv();
2488 DebugLoc dl = TheCall->getDebugLoc();
2489 bool isTailCall = TheCall->isTailCall()
2490 && CC == CallingConv::Fast && PerformTailCallOpt;
2491
2492 std::vector<MVT> NodeTys;
2493 SmallVector<SDValue, 8> Ops;
2494 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
2495 isTailCall, RegsToPass, Ops, NodeTys,
2496 TM.getSubtarget<PPCSubtarget>().isSVR4ABI());
2497
2498 // When performing tail call optimization the callee pops its arguments off
2499 // the stack. Account for this here so these bytes can be pushed back on in
2500 // PPCRegisterInfo::eliminateCallFramePseudoInstr.
2501 int BytesCalleePops =
2502 (CC==CallingConv::Fast && PerformTailCallOpt) ? NumBytes : 0;
2503
2504 if (InFlag.getNode())
2505 Ops.push_back(InFlag);
2506
2507 // Emit tail call.
2508 if (isTailCall) {
2509 assert(InFlag.getNode() &&
2510 "Flag must be set. Depend on flag being set in LowerRET");
2511 Chain = DAG.getNode(PPCISD::TAILCALL, dl,
2512 TheCall->getVTList(), &Ops[0], Ops.size());
2513 return SDValue(Chain.getNode(), Op.getResNo());
2514 }
2515
2516 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
2517 InFlag = Chain.getValue(1);
2518
2519 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2520 DAG.getIntPtrConstant(BytesCalleePops, true),
2521 InFlag);
2522 if (TheCall->getValueType(0) != MVT::Other)
2523 InFlag = Chain.getValue(1);
2524
2525 return LowerCallReturn(Op, DAG, TM, TheCall, Chain, InFlag);
2526}
2527
Tilmann Schellerffd02002009-07-03 06:45:56 +00002528SDValue PPCTargetLowering::LowerCALL_SVR4(SDValue Op, SelectionDAG &DAG,
2529 const PPCSubtarget &Subtarget,
2530 TargetMachine &TM) {
2531 // See PPCTargetLowering::LowerFORMAL_ARGUMENTS_SVR4() for a description
2532 // of the SVR4 ABI stack frame layout.
2533 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
2534 SDValue Chain = TheCall->getChain();
2535 bool isVarArg = TheCall->isVarArg();
2536 unsigned CC = TheCall->getCallingConv();
2537 assert((CC == CallingConv::C ||
2538 CC == CallingConv::Fast) && "Unknown calling convention!");
2539 bool isTailCall = TheCall->isTailCall()
2540 && CC == CallingConv::Fast && PerformTailCallOpt;
2541 SDValue Callee = TheCall->getCallee();
2542 DebugLoc dl = TheCall->getDebugLoc();
2543
2544 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
2545 unsigned PtrByteSize = 4;
2546
2547 MachineFunction &MF = DAG.getMachineFunction();
2548
2549 // Mark this function as potentially containing a function that contains a
2550 // tail call. As a consequence the frame pointer will be used for dynamicalloc
2551 // and restoring the callers stack pointer in this functions epilog. This is
2552 // done because by tail calling the called function might overwrite the value
2553 // in this function's (MF) stack pointer stack slot 0(SP).
2554 if (PerformTailCallOpt && CC==CallingConv::Fast)
2555 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
2556
2557 // Count how many bytes are to be pushed on the stack, including the linkage
2558 // area, parameter list area and the part of the local variable space which
2559 // contains copies of aggregates which are passed by value.
2560
2561 // Assign locations to all of the outgoing arguments.
2562 SmallVector<CCValAssign, 16> ArgLocs;
2563 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
2564
2565 // Reserve space for the linkage area on the stack.
2566 CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
2567
2568 if (isVarArg) {
2569 // Handle fixed and variable vector arguments differently.
2570 // Fixed vector arguments go into registers as long as registers are
2571 // available. Variable vector arguments always go into memory.
2572 unsigned NumArgs = TheCall->getNumArgs();
2573 unsigned NumFixedArgs = TheCall->getNumFixedArgs();
2574
2575 for (unsigned i = 0; i != NumArgs; ++i) {
2576 MVT ArgVT = TheCall->getArg(i).getValueType();
2577 ISD::ArgFlagsTy ArgFlags = TheCall->getArgFlags(i);
2578 bool Result;
2579
2580 if (i < NumFixedArgs) {
2581 Result = CC_PPC_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
2582 CCInfo);
2583 } else {
2584 Result = CC_PPC_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
2585 ArgFlags, CCInfo);
2586 }
2587
2588 if (Result) {
Torok Edwindac237e2009-07-08 20:53:28 +00002589#ifndef NDEBUG
Tilmann Schellerffd02002009-07-03 06:45:56 +00002590 cerr << "Call operand #" << i << " has unhandled type "
2591 << ArgVT.getMVTString() << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +00002592#endif
2593 llvm_unreachable();
Tilmann Schellerffd02002009-07-03 06:45:56 +00002594 }
2595 }
2596 } else {
2597 // All arguments are treated the same.
2598 CCInfo.AnalyzeCallOperands(TheCall, CC_PPC_SVR4);
2599 }
2600
2601 // Assign locations to all of the outgoing aggregate by value arguments.
2602 SmallVector<CCValAssign, 16> ByValArgLocs;
2603 CCState CCByValInfo(CC, isVarArg, getTargetMachine(), ByValArgLocs);
2604
2605 // Reserve stack space for the allocations in CCInfo.
2606 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2607
2608 CCByValInfo.AnalyzeCallOperands(TheCall, CC_PPC_SVR4_ByVal);
2609
2610 // Size of the linkage area, parameter list area and the part of the local
2611 // space variable where copies of aggregates which are passed by value are
2612 // stored.
2613 unsigned NumBytes = CCByValInfo.getNextStackOffset();
2614
2615 // Calculate by how many bytes the stack has to be adjusted in case of tail
2616 // call optimization.
2617 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
2618
2619 // Adjust the stack pointer for the new arguments...
2620 // These operations are automatically eliminated by the prolog/epilog pass
2621 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
2622 SDValue CallSeqStart = Chain;
2623
2624 // Load the return address and frame pointer so it can be moved somewhere else
2625 // later.
2626 SDValue LROp, FPOp;
2627 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
2628 dl);
2629
2630 // Set up a copy of the stack pointer for use loading and storing any
2631 // arguments that may not fit in the registers available for argument
2632 // passing.
2633 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
2634
2635 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2636 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
2637 SmallVector<SDValue, 8> MemOpChains;
2638
2639 // Walk the register/memloc assignments, inserting copies/loads.
2640 for (unsigned i = 0, j = 0, e = ArgLocs.size();
2641 i != e;
2642 ++i) {
2643 CCValAssign &VA = ArgLocs[i];
2644 SDValue Arg = TheCall->getArg(i);
2645 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
2646
2647 if (Flags.isByVal()) {
2648 // Argument is an aggregate which is passed by value, thus we need to
2649 // create a copy of it in the local variable space of the current stack
2650 // frame (which is the stack frame of the caller) and pass the address of
2651 // this copy to the callee.
2652 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
2653 CCValAssign &ByValVA = ByValArgLocs[j++];
2654 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
2655
2656 // Memory reserved in the local variable space of the callers stack frame.
2657 unsigned LocMemOffset = ByValVA.getLocMemOffset();
2658
2659 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
2660 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
2661
2662 // Create a copy of the argument in the local area of the current
2663 // stack frame.
2664 SDValue MemcpyCall =
2665 CreateCopyOfByValArgument(Arg, PtrOff,
2666 CallSeqStart.getNode()->getOperand(0),
2667 Flags, DAG, dl);
2668
2669 // This must go outside the CALLSEQ_START..END.
2670 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
2671 CallSeqStart.getNode()->getOperand(1));
2672 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
2673 NewCallSeqStart.getNode());
2674 Chain = CallSeqStart = NewCallSeqStart;
2675
2676 // Pass the address of the aggregate copy on the stack either in a
2677 // physical register or in the parameter list area of the current stack
2678 // frame to the callee.
2679 Arg = PtrOff;
2680 }
2681
2682 if (VA.isRegLoc()) {
2683 // Put argument in a physical register.
2684 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2685 } else {
2686 // Put argument in the parameter list area of the current stack frame.
2687 assert(VA.isMemLoc());
2688 unsigned LocMemOffset = VA.getLocMemOffset();
2689
2690 if (!isTailCall) {
2691 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
2692 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
2693
2694 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2695 PseudoSourceValue::getStack(), LocMemOffset));
2696 } else {
2697 // Calculate and remember argument location.
2698 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
2699 TailCallArguments);
2700 }
2701 }
2702 }
2703
2704 if (!MemOpChains.empty())
2705 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2706 &MemOpChains[0], MemOpChains.size());
2707
2708 // Build a sequence of copy-to-reg nodes chained together with token chain
2709 // and flag operands which copy the outgoing args into the appropriate regs.
2710 SDValue InFlag;
2711 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2712 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2713 RegsToPass[i].second, InFlag);
2714 InFlag = Chain.getValue(1);
2715 }
2716
2717 // Set CR6 to true if this is a vararg call.
2718 if (isVarArg) {
2719 SDValue SetCR(DAG.getTargetNode(PPC::CRSET, dl, MVT::i32), 0);
2720 Chain = DAG.getCopyToReg(Chain, dl, PPC::CR1EQ, SetCR, InFlag);
2721 InFlag = Chain.getValue(1);
2722 }
2723
Tilmann Schellerffd02002009-07-03 06:45:56 +00002724 if (isTailCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002725 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
2726 false, TailCallArguments);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002727 }
2728
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002729 return FinishCall(DAG, TheCall, TM, RegsToPass, Op, InFlag, Chain, Callee,
2730 SPDiff, NumBytes);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002731}
2732
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002733SDValue PPCTargetLowering::LowerCALL_Darwin(SDValue Op, SelectionDAG &DAG,
2734 const PPCSubtarget &Subtarget,
2735 TargetMachine &TM) {
Dan Gohman095cc292008-09-13 01:54:27 +00002736 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
2737 SDValue Chain = TheCall->getChain();
2738 bool isVarArg = TheCall->isVarArg();
2739 unsigned CC = TheCall->getCallingConv();
2740 bool isTailCall = TheCall->isTailCall()
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002741 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohman095cc292008-09-13 01:54:27 +00002742 SDValue Callee = TheCall->getCallee();
2743 unsigned NumOps = TheCall->getNumArgs();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00002744 DebugLoc dl = TheCall->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002745
Duncan Sands83ec4b62008-06-06 12:08:01 +00002746 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Chris Lattnerc91a4752006-06-26 22:48:35 +00002747 bool isPPC64 = PtrVT == MVT::i64;
2748 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002749
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002750 MachineFunction &MF = DAG.getMachineFunction();
2751
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002752 // Mark this function as potentially containing a function that contains a
2753 // tail call. As a consequence the frame pointer will be used for dynamicalloc
2754 // and restoring the callers stack pointer in this functions epilog. This is
2755 // done because by tail calling the called function might overwrite the value
2756 // in this function's (MF) stack pointer stack slot 0(SP).
2757 if (PerformTailCallOpt && CC==CallingConv::Fast)
2758 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
2759
2760 unsigned nAltivecParamsAtEnd = 0;
2761
Chris Lattnerabde4602006-05-16 22:56:08 +00002762 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00002763 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002764 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002765 unsigned NumBytes =
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002766 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CC, TheCall,
2767 nAltivecParamsAtEnd);
Dale Johannesen75092de2008-03-12 00:22:17 +00002768
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002769 // Calculate by how many bytes the stack has to be adjusted in case of tail
2770 // call optimization.
2771 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelfdc40a02009-02-17 22:15:04 +00002772
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002773 // Adjust the stack pointer for the new arguments...
2774 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +00002775 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohman475871a2008-07-27 21:46:04 +00002776 SDValue CallSeqStart = Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00002777
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002778 // Load the return address and frame pointer so it can be move somewhere else
2779 // later.
Dan Gohman475871a2008-07-27 21:46:04 +00002780 SDValue LROp, FPOp;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002781 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
2782 dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002783
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002784 // Set up a copy of the stack pointer for use loading and storing any
2785 // arguments that may not fit in the registers available for argument
2786 // passing.
Dan Gohman475871a2008-07-27 21:46:04 +00002787 SDValue StackPtr;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002788 if (isPPC64)
2789 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
2790 else
2791 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00002792
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002793 // Figure out which arguments are going to go in registers, and which in
2794 // memory. Also, if this is a vararg function, floating point operations
2795 // must be stored to our stack, and loaded into integer regs as well, if
2796 // any integer regs are available for argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002797 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, true);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002798 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002799
Chris Lattnerc91a4752006-06-26 22:48:35 +00002800 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00002801 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2802 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2803 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00002804 static const unsigned GPR_64[] = { // 64-bit registers.
2805 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2806 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2807 };
Chris Lattner9f0bc652007-02-25 05:34:32 +00002808 static const unsigned *FPR = GetFPR(Subtarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00002809
Chris Lattner9a2a4972006-05-17 06:01:33 +00002810 static const unsigned VR[] = {
2811 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2812 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2813 };
Owen Anderson718cb662007-09-07 04:06:50 +00002814 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002815 const unsigned NumFPRs = 13;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002816 const unsigned NumVRs = array_lengthof(VR);
Scott Michelfdc40a02009-02-17 22:15:04 +00002817
Chris Lattnerc91a4752006-06-26 22:48:35 +00002818 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
2819
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002820 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002821 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
2822
Dan Gohman475871a2008-07-27 21:46:04 +00002823 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00002824 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner9f0bc652007-02-25 05:34:32 +00002825 bool inMem = false;
Dan Gohman095cc292008-09-13 01:54:27 +00002826 SDValue Arg = TheCall->getArg(i);
2827 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002828
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002829 // PtrOff will be used to store the current argument to the stack if a
2830 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00002831 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00002832
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002833 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002834
Dale Johannesen39355f92009-02-04 02:34:38 +00002835 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002836
2837 // On PPC64, promote integers to 64-bit values.
2838 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00002839 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
2840 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Dale Johannesen39355f92009-02-04 02:34:38 +00002841 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002842 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002843
Dale Johannesen8419dd62008-03-07 20:27:40 +00002844 // FIXME memcpy is used way more than necessary. Correctness first.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002845 if (Flags.isByVal()) {
2846 unsigned Size = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00002847 if (Size==1 || Size==2) {
2848 // Very small objects are passed right-justified.
2849 // Everything else is passed left-justified.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002850 MVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002851 if (GPR_idx != NumGPRs) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002852 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Dale Johannesen8419dd62008-03-07 20:27:40 +00002853 NULL, 0, VT);
2854 MemOpChains.push_back(Load.getValue(1));
2855 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002856
2857 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002858 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00002859 SDValue Const = DAG.getConstant(4 - Size, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002860 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Dan Gohman475871a2008-07-27 21:46:04 +00002861 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr,
Scott Michelfdc40a02009-02-17 22:15:04 +00002862 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002863 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002864 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00002865 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00002866 CallSeqStart.getNode()->getOperand(1));
Gabor Greif93c53e52008-08-31 15:37:04 +00002867 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
2868 NewCallSeqStart.getNode());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002869 Chain = CallSeqStart = NewCallSeqStart;
2870 ArgOffset += PtrByteSize;
2871 }
2872 continue;
2873 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002874 // Copy entire object into memory. There are cases where gcc-generated
2875 // code assumes it is there, even if it could be put entirely into
2876 // registers. (This is not what the doc says.)
Dan Gohman475871a2008-07-27 21:46:04 +00002877 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00002878 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002879 Flags, DAG, dl);
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002880 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00002881 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00002882 CallSeqStart.getNode()->getOperand(1));
2883 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), NewCallSeqStart.getNode());
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002884 Chain = CallSeqStart = NewCallSeqStart;
2885 // And copy the pieces of it that fit into registers.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002886 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00002887 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002888 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002889 if (GPR_idx != NumGPRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002890 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, NULL, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00002891 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002892 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002893 ArgOffset += PtrByteSize;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002894 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002895 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002896 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002897 }
2898 }
2899 continue;
2900 }
2901
Duncan Sands83ec4b62008-06-06 12:08:01 +00002902 switch (Arg.getValueType().getSimpleVT()) {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002903 default: assert(0 && "Unexpected ValueType for argument!");
2904 case MVT::i32:
Chris Lattnerc91a4752006-06-26 22:48:35 +00002905 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00002906 if (GPR_idx != NumGPRs) {
2907 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002908 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002909 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
2910 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002911 TailCallArguments, dl);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002912 inMem = true;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002913 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002914 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002915 break;
2916 case MVT::f32:
2917 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00002918 if (FPR_idx != NumFPRs) {
2919 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
2920
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002921 if (isVarArg) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002922 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002923 MemOpChains.push_back(Store);
2924
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002925 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00002926 if (GPR_idx != NumGPRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002927 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002928 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002929 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002930 }
Jim Laskeyfbb74e62006-12-01 16:30:47 +00002931 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00002932 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002933 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
2934 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002935 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002936 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00002937 }
2938 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002939 // If we have any FPRs remaining, we may also have GPRs remaining.
2940 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
2941 // GPRs.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002942 if (GPR_idx != NumGPRs)
2943 ++GPR_idx;
2944 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
2945 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
2946 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00002947 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002948 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002949 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
2950 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002951 TailCallArguments, dl);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002952 inMem = true;
Chris Lattnerabde4602006-05-16 22:56:08 +00002953 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002954 if (isPPC64)
2955 ArgOffset += 8;
2956 else
2957 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002958 break;
2959 case MVT::v4f32:
2960 case MVT::v4i32:
2961 case MVT::v8i16:
2962 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00002963 if (isVarArg) {
2964 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00002965 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00002966 // V registers; in fact gcc does this only for arguments that are
2967 // prototyped, not for those that match the ... We do it for all
2968 // arguments, seems to work.
2969 while (ArgOffset % 16 !=0) {
2970 ArgOffset += PtrByteSize;
2971 if (GPR_idx != NumGPRs)
2972 GPR_idx++;
2973 }
2974 // We could elide this store in the case where the object fits
2975 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00002976 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00002977 DAG.getConstant(ArgOffset, PtrVT));
Dale Johannesen39355f92009-02-04 02:34:38 +00002978 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00002979 MemOpChains.push_back(Store);
2980 if (VR_idx != NumVRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002981 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00002982 MemOpChains.push_back(Load.getValue(1));
2983 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
2984 }
2985 ArgOffset += 16;
2986 for (unsigned i=0; i<16; i+=PtrByteSize) {
2987 if (GPR_idx == NumGPRs)
2988 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00002989 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00002990 DAG.getConstant(i, PtrVT));
Dale Johannesen39355f92009-02-04 02:34:38 +00002991 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00002992 MemOpChains.push_back(Load.getValue(1));
2993 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
2994 }
2995 break;
2996 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002997
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002998 // Non-varargs Altivec params generally go in registers, but have
2999 // stack space allocated at the end.
3000 if (VR_idx != NumVRs) {
3001 // Doesn't have GPR space allocated.
3002 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
3003 } else if (nAltivecParamsAtEnd==0) {
3004 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003005 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3006 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003007 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00003008 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00003009 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003010 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00003011 }
Chris Lattnerabde4602006-05-16 22:56:08 +00003012 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003013 // If all Altivec parameters fit in registers, as they usually do,
3014 // they get stack space following the non-Altivec parameters. We
3015 // don't track this here because nobody below needs it.
3016 // If there are more Altivec parameters than fit in registers emit
3017 // the stores here.
3018 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
3019 unsigned j = 0;
3020 // Offset is aligned; skip 1st 12 params which go in V registers.
3021 ArgOffset = ((ArgOffset+15)/16)*16;
3022 ArgOffset += 12*16;
3023 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman095cc292008-09-13 01:54:27 +00003024 SDValue Arg = TheCall->getArg(i);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003025 MVT ArgType = Arg.getValueType();
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003026 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
3027 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
3028 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00003029 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003030 // We are emitting Altivec params in order.
3031 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3032 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003033 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003034 ArgOffset += 16;
3035 }
3036 }
3037 }
3038 }
3039
Chris Lattner9a2a4972006-05-17 06:01:33 +00003040 if (!MemOpChains.empty())
Dale Johannesen39355f92009-02-04 02:34:38 +00003041 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00003042 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00003043
Chris Lattner9a2a4972006-05-17 06:01:33 +00003044 // Build a sequence of copy-to-reg nodes chained together with token chain
3045 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00003046 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00003047 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003048 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00003049 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003050 InFlag = Chain.getValue(1);
3051 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003052
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003053 if (isTailCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003054 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
3055 FPOp, true, TailCallArguments);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003056 }
3057
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003058 return FinishCall(DAG, TheCall, TM, RegsToPass, Op, InFlag, Chain, Callee,
3059 SPDiff, NumBytes);
Chris Lattnerabde4602006-05-16 22:56:08 +00003060}
3061
Scott Michelfdc40a02009-02-17 22:15:04 +00003062SDValue PPCTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003063 TargetMachine &TM) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003064 SmallVector<CCValAssign, 16> RVLocs;
3065 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +00003066 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Dale Johannesena05dca42009-02-04 23:02:30 +00003067 DebugLoc dl = Op.getDebugLoc();
Chris Lattner52387be2007-06-19 00:13:10 +00003068 CCState CCInfo(CC, isVarArg, TM, RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +00003069 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00003070
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003071 // If this is the first return lowered for this function, add the regs to the
3072 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00003073 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003074 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00003075 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003076 }
3077
Dan Gohman475871a2008-07-27 21:46:04 +00003078 SDValue Chain = Op.getOperand(0);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003079
3080 Chain = GetPossiblePreceedingTailCall(Chain, PPCISD::TAILCALL);
3081 if (Chain.getOpcode() == PPCISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003082 SDValue TailCall = Chain;
3083 SDValue TargetAddress = TailCall.getOperand(1);
3084 SDValue StackAdjustment = TailCall.getOperand(2);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003085
3086 assert(((TargetAddress.getOpcode() == ISD::Register &&
3087 cast<RegisterSDNode>(TargetAddress)->getReg() == PPC::CTR) ||
Bill Wendling056292f2008-09-16 21:48:12 +00003088 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003089 TargetAddress.getOpcode() == ISD::TargetGlobalAddress ||
3090 isa<ConstantSDNode>(TargetAddress)) &&
3091 "Expecting an global address, external symbol, absolute value or register");
3092
3093 assert(StackAdjustment.getOpcode() == ISD::Constant &&
3094 "Expecting a const value");
3095
Dan Gohman475871a2008-07-27 21:46:04 +00003096 SmallVector<SDValue,8> Operands;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003097 Operands.push_back(Chain.getOperand(0));
3098 Operands.push_back(TargetAddress);
3099 Operands.push_back(StackAdjustment);
3100 // Copy registers used by the call. Last operand is a flag so it is not
3101 // copied.
3102 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
3103 Operands.push_back(Chain.getOperand(i));
3104 }
Dale Johannesena05dca42009-02-04 23:02:30 +00003105 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003106 Operands.size());
3107 }
3108
Dan Gohman475871a2008-07-27 21:46:04 +00003109 SDValue Flag;
Scott Michelfdc40a02009-02-17 22:15:04 +00003110
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003111 // Copy the result values into the output registers.
3112 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3113 CCValAssign &VA = RVLocs[i];
3114 assert(VA.isRegLoc() && "Can only return in registers!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003115 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dale Johannesena05dca42009-02-04 23:02:30 +00003116 Op.getOperand(i*2+1), Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003117 Flag = Chain.getValue(1);
3118 }
3119
Gabor Greifba36cb52008-08-28 21:40:38 +00003120 if (Flag.getNode())
Dale Johannesena05dca42009-02-04 23:02:30 +00003121 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003122 else
Dale Johannesena05dca42009-02-04 23:02:30 +00003123 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner1a635d62006-04-14 06:01:58 +00003124}
3125
Dan Gohman475871a2008-07-27 21:46:04 +00003126SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Jim Laskeyefc7e522006-12-04 22:04:42 +00003127 const PPCSubtarget &Subtarget) {
3128 // When we pop the dynamic allocation we need to restore the SP link.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003129 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003130
Jim Laskeyefc7e522006-12-04 22:04:42 +00003131 // Get the corect type for pointers.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003132 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00003133
3134 // Construct the stack pointer operand.
3135 bool IsPPC64 = Subtarget.isPPC64();
3136 unsigned SP = IsPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00003137 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003138
3139 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00003140 SDValue Chain = Op.getOperand(0);
3141 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003142
Jim Laskeyefc7e522006-12-04 22:04:42 +00003143 // Load the old link SP.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003144 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr, NULL, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003145
Jim Laskeyefc7e522006-12-04 22:04:42 +00003146 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003147 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00003148
Jim Laskeyefc7e522006-12-04 22:04:42 +00003149 // Store the old link SP.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003150 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, NULL, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003151}
3152
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003153
3154
Dan Gohman475871a2008-07-27 21:46:04 +00003155SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003156PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003157 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003158 bool IsPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003159 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003160 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003161
3162 // Get current frame pointer save index. The users of this index will be
3163 // primarily DYNALLOC instructions.
3164 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3165 int RASI = FI->getReturnAddrSaveIndex();
3166
3167 // If the frame pointer save index hasn't been defined yet.
3168 if (!RASI) {
3169 // Find out what the fix offset of the frame pointer save area.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003170 int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64, isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003171 // Allocate the frame index for frame pointer save area.
3172 RASI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, LROffset);
3173 // Save the result.
3174 FI->setReturnAddrSaveIndex(RASI);
3175 }
3176 return DAG.getFrameIndex(RASI, PtrVT);
3177}
3178
Dan Gohman475871a2008-07-27 21:46:04 +00003179SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003180PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
3181 MachineFunction &MF = DAG.getMachineFunction();
3182 bool IsPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003183 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003184 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003185
3186 // Get current frame pointer save index. The users of this index will be
3187 // primarily DYNALLOC instructions.
3188 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3189 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003190
Jim Laskey2f616bf2006-11-16 22:43:37 +00003191 // If the frame pointer save index hasn't been defined yet.
3192 if (!FPSI) {
3193 // Find out what the fix offset of the frame pointer save area.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003194 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64,
3195 isDarwinABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00003196
Jim Laskey2f616bf2006-11-16 22:43:37 +00003197 // Allocate the frame index for frame pointer save area.
Scott Michelfdc40a02009-02-17 22:15:04 +00003198 FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003199 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00003200 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003201 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003202 return DAG.getFrameIndex(FPSI, PtrVT);
3203}
Jim Laskey2f616bf2006-11-16 22:43:37 +00003204
Dan Gohman475871a2008-07-27 21:46:04 +00003205SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003206 SelectionDAG &DAG,
3207 const PPCSubtarget &Subtarget) {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003208 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00003209 SDValue Chain = Op.getOperand(0);
3210 SDValue Size = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003211 DebugLoc dl = Op.getDebugLoc();
3212
Jim Laskey2f616bf2006-11-16 22:43:37 +00003213 // Get the corect type for pointers.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003214 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003215 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00003216 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00003217 DAG.getConstant(0, PtrVT), Size);
3218 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00003219 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003220 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00003221 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Jim Laskey2f616bf2006-11-16 22:43:37 +00003222 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00003223 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003224}
3225
Chris Lattner1a635d62006-04-14 06:01:58 +00003226/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
3227/// possible.
Dan Gohman475871a2008-07-27 21:46:04 +00003228SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00003229 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003230 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
3231 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00003232 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003233
Chris Lattner1a635d62006-04-14 06:01:58 +00003234 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003235
Chris Lattner1a635d62006-04-14 06:01:58 +00003236 // Cannot handle SETEQ/SETNE.
Eli Friedmanc06441e2009-05-28 04:31:08 +00003237 if (CC == ISD::SETEQ || CC == ISD::SETNE) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003238
Duncan Sands83ec4b62008-06-06 12:08:01 +00003239 MVT ResVT = Op.getValueType();
3240 MVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003241 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3242 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00003243 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003244
Chris Lattner1a635d62006-04-14 06:01:58 +00003245 // If the RHS of the comparison is a 0.0, we don't need to do the
3246 // subtraction at all.
3247 if (isFloatingPointZero(RHS))
3248 switch (CC) {
3249 default: break; // SETUO etc aren't handled by fsel.
3250 case ISD::SETULT:
3251 case ISD::SETLT:
3252 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003253 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003254 case ISD::SETGE:
3255 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003256 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
3257 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003258 case ISD::SETUGT:
3259 case ISD::SETGT:
3260 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003261 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003262 case ISD::SETLE:
3263 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003264 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
3265 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
3266 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003267 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003268
Dan Gohman475871a2008-07-27 21:46:04 +00003269 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00003270 switch (CC) {
3271 default: break; // SETUO etc aren't handled by fsel.
3272 case ISD::SETULT:
3273 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00003274 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Chris Lattner1a635d62006-04-14 06:01:58 +00003275 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003276 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
3277 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003278 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003279 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00003280 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Chris Lattner1a635d62006-04-14 06:01:58 +00003281 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003282 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
3283 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003284 case ISD::SETUGT:
3285 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00003286 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Chris Lattner1a635d62006-04-14 06:01:58 +00003287 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003288 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
3289 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003290 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003291 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00003292 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Chris Lattner1a635d62006-04-14 06:01:58 +00003293 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
Dale Johannesende064702009-02-06 21:50:26 +00003294 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
3295 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003296 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00003297 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00003298}
3299
Chris Lattner1f873002007-11-28 18:44:47 +00003300// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003301SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00003302 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003303 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00003304 SDValue Src = Op.getOperand(0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003305 if (Src.getValueType() == MVT::f32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003306 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003307
Dan Gohman475871a2008-07-27 21:46:04 +00003308 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003309 switch (Op.getValueType().getSimpleVT()) {
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003310 default: assert(0 && "Unhandled FP_TO_INT type in custom expander!");
Chris Lattner1a635d62006-04-14 06:01:58 +00003311 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003312 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
3313 PPCISD::FCTIDZ,
3314 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003315 break;
3316 case MVT::i64:
Dale Johannesen33c960f2009-02-04 20:06:27 +00003317 Tmp = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003318 break;
3319 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00003320
Chris Lattner1a635d62006-04-14 06:01:58 +00003321 // Convert the FP value to an int value through memory.
Dan Gohman475871a2008-07-27 21:46:04 +00003322 SDValue FIPtr = DAG.CreateStackTemporary(MVT::f64);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003323
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003324 // Emit a store to the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003325 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, NULL, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003326
3327 // Result is a load from the stack slot. If loading 4 bytes, make sure to
3328 // add in a bias.
Chris Lattner1a635d62006-04-14 06:01:58 +00003329 if (Op.getValueType() == MVT::i32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003330 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003331 DAG.getConstant(4, FIPtr.getValueType()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00003332 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003333}
3334
Dan Gohman475871a2008-07-27 21:46:04 +00003335SDValue PPCTargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003336 DebugLoc dl = Op.getDebugLoc();
Dan Gohman034f60e2008-03-11 01:59:03 +00003337 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
3338 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00003339 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00003340
Chris Lattner1a635d62006-04-14 06:01:58 +00003341 if (Op.getOperand(0).getValueType() == MVT::i64) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003342 SDValue Bits = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003343 MVT::f64, Op.getOperand(0));
3344 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Bits);
Chris Lattner1a635d62006-04-14 06:01:58 +00003345 if (Op.getValueType() == MVT::f32)
Scott Michelfdc40a02009-02-17 22:15:04 +00003346 FP = DAG.getNode(ISD::FP_ROUND, dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003347 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003348 return FP;
3349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003350
Chris Lattner1a635d62006-04-14 06:01:58 +00003351 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
3352 "Unhandled SINT_TO_FP type in custom expander!");
3353 // Since we only generate this in 64-bit mode, we can take advantage of
3354 // 64-bit registers. In particular, sign extend the input value into the
3355 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
3356 // then lfd it and fcfid it.
3357 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3358 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003359 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003360 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003361
Dale Johannesen33c960f2009-02-04 20:06:27 +00003362 SDValue Ext64 = DAG.getNode(PPCISD::EXTSW_32, dl, MVT::i32,
Chris Lattner1a635d62006-04-14 06:01:58 +00003363 Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003364
Chris Lattner1a635d62006-04-14 06:01:58 +00003365 // STD the extended value into the stack slot.
Dan Gohmana54cf172008-07-11 22:44:52 +00003366 MachineMemOperand MO(PseudoSourceValue::getFixedStack(FrameIdx),
3367 MachineMemOperand::MOStore, 0, 8, 8);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003368 SDValue Store = DAG.getNode(PPCISD::STD_32, dl, MVT::Other,
Chris Lattner1a635d62006-04-14 06:01:58 +00003369 DAG.getEntryNode(), Ext64, FIdx,
Dan Gohman69de1932008-02-06 22:27:42 +00003370 DAG.getMemOperand(MO));
Chris Lattner1a635d62006-04-14 06:01:58 +00003371 // Load the value as a double.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003372 SDValue Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, NULL, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003373
Chris Lattner1a635d62006-04-14 06:01:58 +00003374 // FCFID it and return it.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003375 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Ld);
Chris Lattner1a635d62006-04-14 06:01:58 +00003376 if (Op.getValueType() == MVT::f32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003377 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003378 return FP;
3379}
3380
Dan Gohman475871a2008-07-27 21:46:04 +00003381SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003382 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003383 /*
3384 The rounding mode is in bits 30:31 of FPSR, and has the following
3385 settings:
3386 00 Round to nearest
3387 01 Round to 0
3388 10 Round to +inf
3389 11 Round to -inf
3390
3391 FLT_ROUNDS, on the other hand, expects the following:
3392 -1 Undefined
3393 0 Round to 0
3394 1 Round to nearest
3395 2 Round to +inf
3396 3 Round to -inf
3397
3398 To perform the conversion, we do:
3399 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
3400 */
3401
3402 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003403 MVT VT = Op.getValueType();
3404 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3405 std::vector<MVT> NodeTys;
Dan Gohman475871a2008-07-27 21:46:04 +00003406 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003407
3408 // Save FP Control Word to register
3409 NodeTys.push_back(MVT::f64); // return register
3410 NodeTys.push_back(MVT::Flag); // unused in this context
Dale Johannesen33c960f2009-02-04 20:06:27 +00003411 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003412
3413 // Save FP register to stack slot
3414 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00003415 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003416 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003417 StackSlot, NULL, 0);
3418
3419 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00003420 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003421 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
3422 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, NULL, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003423
3424 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00003425 SDValue CWD1 =
Dale Johannesen33c960f2009-02-04 20:06:27 +00003426 DAG.getNode(ISD::AND, dl, MVT::i32,
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003427 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00003428 SDValue CWD2 =
Dale Johannesen33c960f2009-02-04 20:06:27 +00003429 DAG.getNode(ISD::SRL, dl, MVT::i32,
3430 DAG.getNode(ISD::AND, dl, MVT::i32,
3431 DAG.getNode(ISD::XOR, dl, MVT::i32,
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003432 CWD, DAG.getConstant(3, MVT::i32)),
3433 DAG.getConstant(3, MVT::i32)),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003434 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003435
Dan Gohman475871a2008-07-27 21:46:04 +00003436 SDValue RetVal =
Dale Johannesen33c960f2009-02-04 20:06:27 +00003437 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003438
Duncan Sands83ec4b62008-06-06 12:08:01 +00003439 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00003440 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003441}
3442
Dan Gohman475871a2008-07-27 21:46:04 +00003443SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003444 MVT VT = Op.getValueType();
3445 unsigned BitWidth = VT.getSizeInBits();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003446 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003447 assert(Op.getNumOperands() == 3 &&
3448 VT == Op.getOperand(1).getValueType() &&
3449 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003450
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00003451 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003452 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003453 SDValue Lo = Op.getOperand(0);
3454 SDValue Hi = Op.getOperand(1);
3455 SDValue Amt = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003456 MVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003457
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003458 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003459 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003460 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
3461 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
3462 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
3463 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003464 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003465 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
3466 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3467 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003468 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003469 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003470}
3471
Dan Gohman475871a2008-07-27 21:46:04 +00003472SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003473 MVT VT = Op.getValueType();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003474 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003475 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003476 assert(Op.getNumOperands() == 3 &&
3477 VT == Op.getOperand(1).getValueType() &&
3478 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003479
Dan Gohman9ed06db2008-03-07 20:36:53 +00003480 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003481 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003482 SDValue Lo = Op.getOperand(0);
3483 SDValue Hi = Op.getOperand(1);
3484 SDValue Amt = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003485 MVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003486
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003487 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003488 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003489 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3490 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3491 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3492 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003493 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003494 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
3495 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3496 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003497 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003498 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003499}
3500
Dan Gohman475871a2008-07-27 21:46:04 +00003501SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003502 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003503 MVT VT = Op.getValueType();
3504 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003505 assert(Op.getNumOperands() == 3 &&
3506 VT == Op.getOperand(1).getValueType() &&
3507 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003508
Dan Gohman9ed06db2008-03-07 20:36:53 +00003509 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00003510 SDValue Lo = Op.getOperand(0);
3511 SDValue Hi = Op.getOperand(1);
3512 SDValue Amt = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003513 MVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003514
Dale Johannesenf5d97892009-02-04 01:48:28 +00003515 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003516 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00003517 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3518 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3519 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3520 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003521 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00003522 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
3523 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
3524 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003525 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00003526 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003527 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003528}
3529
3530//===----------------------------------------------------------------------===//
3531// Vector related lowering.
3532//
3533
Chris Lattner4a998b92006-04-17 06:00:21 +00003534/// BuildSplatI - Build a canonical splati of Val with an element size of
3535/// SplatSize. Cast the result to VT.
Dan Gohman475871a2008-07-27 21:46:04 +00003536static SDValue BuildSplatI(int Val, unsigned SplatSize, MVT VT,
Dale Johannesened2eee62009-02-06 01:31:28 +00003537 SelectionDAG &DAG, DebugLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00003538 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00003539
Duncan Sands83ec4b62008-06-06 12:08:01 +00003540 static const MVT VTys[] = { // canonical VT to use for each size.
Chris Lattner4a998b92006-04-17 06:00:21 +00003541 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
3542 };
Chris Lattner70fa4932006-12-01 01:45:39 +00003543
Duncan Sands83ec4b62008-06-06 12:08:01 +00003544 MVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003545
Chris Lattner70fa4932006-12-01 01:45:39 +00003546 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
3547 if (Val == -1)
3548 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00003549
Duncan Sands83ec4b62008-06-06 12:08:01 +00003550 MVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003551
Chris Lattner4a998b92006-04-17 06:00:21 +00003552 // Build a canonical splat for this value.
Eli Friedman1a8229b2009-05-24 02:03:36 +00003553 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003554 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003555 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00003556 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
3557 &Ops[0], Ops.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00003558 return DAG.getNode(ISD::BIT_CONVERT, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003559}
3560
Chris Lattnere7c768e2006-04-18 03:24:30 +00003561/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00003562/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003563static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Dale Johannesened2eee62009-02-06 01:31:28 +00003564 SelectionDAG &DAG, DebugLoc dl,
3565 MVT DestVT = MVT::Other) {
Chris Lattnere7c768e2006-04-18 03:24:30 +00003566 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003567 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Chris Lattner6876e662006-04-17 06:58:41 +00003568 DAG.getConstant(IID, MVT::i32), LHS, RHS);
3569}
3570
Chris Lattnere7c768e2006-04-18 03:24:30 +00003571/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
3572/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003573static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00003574 SDValue Op2, SelectionDAG &DAG,
3575 DebugLoc dl, MVT DestVT = MVT::Other) {
Chris Lattnere7c768e2006-04-18 03:24:30 +00003576 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003577 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Chris Lattnere7c768e2006-04-18 03:24:30 +00003578 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
3579}
3580
3581
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003582/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
3583/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003584static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Dale Johannesened2eee62009-02-06 01:31:28 +00003585 MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003586 // Force LHS/RHS to be the right type.
Dale Johannesened2eee62009-02-06 01:31:28 +00003587 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, LHS);
3588 RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00003589
Nate Begeman9008ca62009-04-27 18:41:29 +00003590 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003591 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003592 Ops[i] = i + Amt;
3593 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Dale Johannesened2eee62009-02-06 01:31:28 +00003594 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003595}
3596
Chris Lattnerf1b47082006-04-14 05:19:18 +00003597// If this is a case we can't handle, return null and let the default
3598// expansion code take care of it. If we CAN select this case, and if it
3599// selects to a single instruction, return Op. Otherwise, if we can codegen
3600// this case more efficiently than a constant pool load, lower it to the
3601// sequence of ops that should be used.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003602SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00003603 DebugLoc dl = Op.getDebugLoc();
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003604 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3605 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00003606
Bob Wilson24e338e2009-03-02 23:24:16 +00003607 // Check if this is a splat of a constant value.
3608 APInt APSplatBits, APSplatUndef;
3609 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003610 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00003611 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
3612 HasAnyUndefs) || SplatBitSize > 32)
3613 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00003614
Bob Wilsonf2950b02009-03-03 19:26:27 +00003615 unsigned SplatBits = APSplatBits.getZExtValue();
3616 unsigned SplatUndef = APSplatUndef.getZExtValue();
3617 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00003618
Bob Wilsonf2950b02009-03-03 19:26:27 +00003619 // First, handle single instruction cases.
3620
3621 // All zeros?
3622 if (SplatBits == 0) {
3623 // Canonicalize all zero vectors to be v4i32.
3624 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
3625 SDValue Z = DAG.getConstant(0, MVT::i32);
3626 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
3627 Op = DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003628 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003629 return Op;
3630 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00003631
Bob Wilsonf2950b02009-03-03 19:26:27 +00003632 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
3633 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
3634 (32-SplatBitSize));
3635 if (SextVal >= -16 && SextVal <= 15)
3636 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003637
3638
Bob Wilsonf2950b02009-03-03 19:26:27 +00003639 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00003640
Bob Wilsonf2950b02009-03-03 19:26:27 +00003641 // If this value is in the range [-32,30] and is even, use:
3642 // tmp = VSPLTI[bhw], result = add tmp, tmp
3643 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
3644 SDValue Res = BuildSplatI(SextVal >> 1, SplatSize, MVT::Other, DAG, dl);
3645 Res = DAG.getNode(ISD::ADD, dl, Res.getValueType(), Res, Res);
3646 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3647 }
3648
3649 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
3650 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
3651 // for fneg/fabs.
3652 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
3653 // Make -1 and vspltisw -1:
3654 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
3655
3656 // Make the VSLW intrinsic, computing 0x8000_0000.
3657 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
3658 OnesV, DAG, dl);
3659
3660 // xor by OnesV to invert it.
3661 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
3662 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3663 }
3664
3665 // Check to see if this is a wide variety of vsplti*, binop self cases.
3666 static const signed char SplatCsts[] = {
3667 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
3668 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
3669 };
3670
3671 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
3672 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
3673 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
3674 int i = SplatCsts[idx];
3675
3676 // Figure out what shift amount will be used by altivec if shifted by i in
3677 // this splat size.
3678 unsigned TypeShiftAmt = i & (SplatBitSize-1);
3679
3680 // vsplti + shl self.
3681 if (SextVal == (i << (int)TypeShiftAmt)) {
3682 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
3683 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3684 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
3685 Intrinsic::ppc_altivec_vslw
3686 };
3687 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Dale Johannesened2eee62009-02-06 01:31:28 +00003688 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003689 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003690
Bob Wilsonf2950b02009-03-03 19:26:27 +00003691 // vsplti + srl self.
3692 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
3693 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
3694 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3695 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
3696 Intrinsic::ppc_altivec_vsrw
3697 };
3698 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Dale Johannesened2eee62009-02-06 01:31:28 +00003699 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003700 }
3701
Bob Wilsonf2950b02009-03-03 19:26:27 +00003702 // vsplti + sra self.
3703 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
3704 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
3705 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3706 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
3707 Intrinsic::ppc_altivec_vsraw
3708 };
3709 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
3710 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003711 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003712
Bob Wilsonf2950b02009-03-03 19:26:27 +00003713 // vsplti + rol self.
3714 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
3715 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
3716 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
3717 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3718 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
3719 Intrinsic::ppc_altivec_vrlw
3720 };
3721 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
3722 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3723 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003724
Bob Wilsonf2950b02009-03-03 19:26:27 +00003725 // t = vsplti c, result = vsldoi t, t, 1
3726 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
3727 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
3728 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00003729 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003730 // t = vsplti c, result = vsldoi t, t, 2
3731 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
3732 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
3733 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003734 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003735 // t = vsplti c, result = vsldoi t, t, 3
3736 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
3737 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
3738 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
3739 }
3740 }
3741
3742 // Three instruction sequences.
3743
3744 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
3745 if (SextVal >= 0 && SextVal <= 31) {
3746 SDValue LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG, dl);
3747 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
3748 LHS = DAG.getNode(ISD::SUB, dl, LHS.getValueType(), LHS, RHS);
3749 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
3750 }
3751 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
3752 if (SextVal >= -31 && SextVal <= 0) {
3753 SDValue LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG, dl);
3754 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
3755 LHS = DAG.getNode(ISD::ADD, dl, LHS.getValueType(), LHS, RHS);
3756 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003757 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003758
Dan Gohman475871a2008-07-27 21:46:04 +00003759 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00003760}
3761
Chris Lattner59138102006-04-17 05:28:54 +00003762/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3763/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00003764static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00003765 SDValue RHS, SelectionDAG &DAG,
Dale Johannesened2eee62009-02-06 01:31:28 +00003766 DebugLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00003767 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00003768 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00003769 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003770
Chris Lattner59138102006-04-17 05:28:54 +00003771 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00003772 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 +00003773 OP_VMRGHW,
3774 OP_VMRGLW,
3775 OP_VSPLTISW0,
3776 OP_VSPLTISW1,
3777 OP_VSPLTISW2,
3778 OP_VSPLTISW3,
3779 OP_VSLDOI4,
3780 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00003781 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00003782 };
Scott Michelfdc40a02009-02-17 22:15:04 +00003783
Chris Lattner59138102006-04-17 05:28:54 +00003784 if (OpNum == OP_COPY) {
3785 if (LHSID == (1*9+2)*9+3) return LHS;
3786 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3787 return RHS;
3788 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003789
Dan Gohman475871a2008-07-27 21:46:04 +00003790 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00003791 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3792 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003793
Nate Begeman9008ca62009-04-27 18:41:29 +00003794 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00003795 switch (OpNum) {
3796 default: assert(0 && "Unknown i32 permute!");
3797 case OP_VMRGHW:
3798 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
3799 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
3800 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
3801 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
3802 break;
3803 case OP_VMRGLW:
3804 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
3805 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
3806 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
3807 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
3808 break;
3809 case OP_VSPLTISW0:
3810 for (unsigned i = 0; i != 16; ++i)
3811 ShufIdxs[i] = (i&3)+0;
3812 break;
3813 case OP_VSPLTISW1:
3814 for (unsigned i = 0; i != 16; ++i)
3815 ShufIdxs[i] = (i&3)+4;
3816 break;
3817 case OP_VSPLTISW2:
3818 for (unsigned i = 0; i != 16; ++i)
3819 ShufIdxs[i] = (i&3)+8;
3820 break;
3821 case OP_VSPLTISW3:
3822 for (unsigned i = 0; i != 16; ++i)
3823 ShufIdxs[i] = (i&3)+12;
3824 break;
3825 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00003826 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003827 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00003828 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003829 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00003830 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003831 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003832 MVT VT = OpLHS.getValueType();
3833 OpLHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpLHS);
3834 OpRHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpRHS);
3835 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
3836 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00003837}
3838
Chris Lattnerf1b47082006-04-14 05:19:18 +00003839/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
3840/// is a shuffle we can handle in a single instruction, return it. Otherwise,
3841/// return the code it can be lowered into. Worst case, it can always be
3842/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00003843SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Nate Begeman9008ca62009-04-27 18:41:29 +00003844 SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00003845 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003846 SDValue V1 = Op.getOperand(0);
3847 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
3849 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003850
Chris Lattnerf1b47082006-04-14 05:19:18 +00003851 // Cases that are handled by instructions that take permute immediates
3852 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
3853 // selected by the instruction selector.
3854 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003855 if (PPC::isSplatShuffleMask(SVOp, 1) ||
3856 PPC::isSplatShuffleMask(SVOp, 2) ||
3857 PPC::isSplatShuffleMask(SVOp, 4) ||
3858 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
3859 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
3860 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
3861 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
3862 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
3863 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
3864 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
3865 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
3866 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00003867 return Op;
3868 }
3869 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003870
Chris Lattnerf1b47082006-04-14 05:19:18 +00003871 // Altivec has a variety of "shuffle immediates" that take two vector inputs
3872 // and produce a fixed permutation. If any of these match, do not lower to
3873 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
3875 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
3876 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
3877 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
3878 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
3879 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
3880 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
3881 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
3882 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00003883 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003884
Chris Lattner59138102006-04-17 05:28:54 +00003885 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
3886 // perfect shuffle table to emit an optimal matching sequence.
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 SmallVector<int, 16> PermMask;
3888 SVOp->getMask(PermMask);
3889
Chris Lattner59138102006-04-17 05:28:54 +00003890 unsigned PFIndexes[4];
3891 bool isFourElementShuffle = true;
3892 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
3893 unsigned EltNo = 8; // Start out undef.
3894 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00003895 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00003896 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00003897
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00003899 if ((ByteSource & 3) != j) {
3900 isFourElementShuffle = false;
3901 break;
3902 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003903
Chris Lattner59138102006-04-17 05:28:54 +00003904 if (EltNo == 8) {
3905 EltNo = ByteSource/4;
3906 } else if (EltNo != ByteSource/4) {
3907 isFourElementShuffle = false;
3908 break;
3909 }
3910 }
3911 PFIndexes[i] = EltNo;
3912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003913
3914 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00003915 // perfect shuffle vector to determine if it is cost effective to do this as
3916 // discrete instructions, or whether we should use a vperm.
3917 if (isFourElementShuffle) {
3918 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00003919 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00003920 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00003921
Chris Lattner59138102006-04-17 05:28:54 +00003922 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3923 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00003924
Chris Lattner59138102006-04-17 05:28:54 +00003925 // Determining when to avoid vperm is tricky. Many things affect the cost
3926 // of vperm, particularly how many times the perm mask needs to be computed.
3927 // For example, if the perm mask can be hoisted out of a loop or is already
3928 // used (perhaps because there are multiple permutes with the same shuffle
3929 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
3930 // the loop requires an extra register.
3931 //
3932 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00003933 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00003934 // available, if this block is within a loop, we should avoid using vperm
3935 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00003936 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00003937 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003939
Chris Lattnerf1b47082006-04-14 05:19:18 +00003940 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
3941 // vector that will get spilled to the constant pool.
3942 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00003943
Chris Lattnerf1b47082006-04-14 05:19:18 +00003944 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
3945 // that it is in input element units, not in bytes. Convert now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003946 MVT EltVT = V1.getValueType().getVectorElementType();
3947 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00003948
Dan Gohman475871a2008-07-27 21:46:04 +00003949 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
3951 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00003952
Chris Lattnerf1b47082006-04-14 05:19:18 +00003953 for (unsigned j = 0; j != BytesPerElement; ++j)
3954 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Eli Friedman1a8229b2009-05-24 02:03:36 +00003955 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00003956 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003957
Evan Chenga87008d2009-02-25 22:49:59 +00003958 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
3959 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00003960 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003961}
3962
Chris Lattner90564f22006-04-18 17:59:36 +00003963/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
3964/// altivec comparison. If it is, return true and fill in Opc/isDot with
3965/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00003966static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00003967 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003968 unsigned IntrinsicID =
3969 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00003970 CompareOpc = -1;
3971 isDot = false;
3972 switch (IntrinsicID) {
3973 default: return false;
3974 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00003975 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
3976 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
3977 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
3978 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
3979 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
3980 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
3981 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
3982 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
3983 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
3984 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
3985 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
3986 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
3987 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00003988
Chris Lattner1a635d62006-04-14 06:01:58 +00003989 // Normal Comparisons.
3990 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
3991 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
3992 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
3993 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
3994 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
3995 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
3996 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
3997 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
3998 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
3999 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
4000 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
4001 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
4002 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
4003 }
Chris Lattner90564f22006-04-18 17:59:36 +00004004 return true;
4005}
4006
4007/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
4008/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00004009SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004010 SelectionDAG &DAG) {
Chris Lattner90564f22006-04-18 17:59:36 +00004011 // If this is a lowered altivec predicate compare, CompareOpc is set to the
4012 // opcode number of the comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00004013 DebugLoc dl = Op.getDebugLoc();
Chris Lattner90564f22006-04-18 17:59:36 +00004014 int CompareOpc;
4015 bool isDot;
4016 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00004017 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00004018
Chris Lattner90564f22006-04-18 17:59:36 +00004019 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00004020 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004021 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner1a635d62006-04-14 06:01:58 +00004022 Op.getOperand(1), Op.getOperand(2),
4023 DAG.getConstant(CompareOpc, MVT::i32));
Dale Johannesen3484c092009-02-05 22:07:54 +00004024 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00004025 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004026
Chris Lattner1a635d62006-04-14 06:01:58 +00004027 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00004028 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00004029 Op.getOperand(2), // LHS
4030 Op.getOperand(3), // RHS
4031 DAG.getConstant(CompareOpc, MVT::i32)
4032 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00004033 std::vector<MVT> VTs;
Chris Lattner1a635d62006-04-14 06:01:58 +00004034 VTs.push_back(Op.getOperand(2).getValueType());
4035 VTs.push_back(MVT::Flag);
Dale Johannesen3484c092009-02-05 22:07:54 +00004036 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00004037
Chris Lattner1a635d62006-04-14 06:01:58 +00004038 // Now that we have the comparison, emit a copy from the CR to a GPR.
4039 // This is flagged to the above dot comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00004040 SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32,
Chris Lattner1a635d62006-04-14 06:01:58 +00004041 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00004042 CompNode.getValue(1));
4043
Chris Lattner1a635d62006-04-14 06:01:58 +00004044 // Unpack the result based on how the target uses it.
4045 unsigned BitNo; // Bit # of CR6.
4046 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004047 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00004048 default: // Can't happen, don't crash on invalid number though.
4049 case 0: // Return the value of the EQ bit of CR6.
4050 BitNo = 0; InvertBit = false;
4051 break;
4052 case 1: // Return the inverted value of the EQ bit of CR6.
4053 BitNo = 0; InvertBit = true;
4054 break;
4055 case 2: // Return the value of the LT bit of CR6.
4056 BitNo = 2; InvertBit = false;
4057 break;
4058 case 3: // Return the inverted value of the LT bit of CR6.
4059 BitNo = 2; InvertBit = true;
4060 break;
4061 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004062
Chris Lattner1a635d62006-04-14 06:01:58 +00004063 // Shift the bit into the low position.
Dale Johannesen3484c092009-02-05 22:07:54 +00004064 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
Chris Lattner1a635d62006-04-14 06:01:58 +00004065 DAG.getConstant(8-(3-BitNo), MVT::i32));
4066 // Isolate the bit.
Dale Johannesen3484c092009-02-05 22:07:54 +00004067 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
Chris Lattner1a635d62006-04-14 06:01:58 +00004068 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00004069
Chris Lattner1a635d62006-04-14 06:01:58 +00004070 // If we are supposed to, toggle the bit.
4071 if (InvertBit)
Dale Johannesen3484c092009-02-05 22:07:54 +00004072 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
Chris Lattner1a635d62006-04-14 06:01:58 +00004073 DAG.getConstant(1, MVT::i32));
4074 return Flags;
4075}
4076
Scott Michelfdc40a02009-02-17 22:15:04 +00004077SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004078 SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004079 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00004080 // Create a stack slot that is 16-byte aligned.
4081 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4082 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004083 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004084 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004085
Chris Lattner1a635d62006-04-14 06:01:58 +00004086 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004087 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004088 Op.getOperand(0), FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004089 // Load it out.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004090 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004091}
4092
Dan Gohman475871a2008-07-27 21:46:04 +00004093SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00004094 DebugLoc dl = Op.getDebugLoc();
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004095 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00004096 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004097
Dale Johannesened2eee62009-02-06 01:31:28 +00004098 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
4099 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00004100
Dan Gohman475871a2008-07-27 21:46:04 +00004101 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00004102 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00004103
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004104 // Shrinkify inputs to v8i16.
Dale Johannesened2eee62009-02-06 01:31:28 +00004105 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, LHS);
4106 RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHS);
4107 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00004108
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004109 // Low parts multiplied together, generating 32-bit results (we ignore the
4110 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00004111 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Dale Johannesened2eee62009-02-06 01:31:28 +00004112 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004113
Dan Gohman475871a2008-07-27 21:46:04 +00004114 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00004115 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004116 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00004117 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00004118 Neg16, DAG, dl);
4119 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004120 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004121 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004122
Dale Johannesened2eee62009-02-06 01:31:28 +00004123 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004124
Chris Lattnercea2aa72006-04-18 04:28:57 +00004125 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00004126 LHS, RHS, Zero, DAG, dl);
Chris Lattner19a81522006-04-18 03:57:35 +00004127 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004128 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004129
Chris Lattner19a81522006-04-18 03:57:35 +00004130 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004131 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Dale Johannesened2eee62009-02-06 01:31:28 +00004132 LHS, RHS, DAG, dl, MVT::v8i16);
4133 EvenParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004134
Chris Lattner19a81522006-04-18 03:57:35 +00004135 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004136 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Dale Johannesened2eee62009-02-06 01:31:28 +00004137 LHS, RHS, DAG, dl, MVT::v8i16);
4138 OddParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004139
Chris Lattner19a81522006-04-18 03:57:35 +00004140 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00004141 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00004142 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004143 Ops[i*2 ] = 2*i+1;
4144 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00004145 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004146 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004147 } else {
Torok Edwindac237e2009-07-08 20:53:28 +00004148 LLVM_UNREACHABLE("Unknown mul to lower!");
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004149 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00004150}
4151
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004152/// LowerOperation - Provide custom lowering hooks for some operations.
4153///
Dan Gohman475871a2008-07-27 21:46:04 +00004154SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004155 switch (Op.getOpcode()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004156 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00004157 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4158 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00004159 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00004160 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00004161 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bill Wendling77959322008-09-17 00:30:57 +00004162 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004163 case ISD::VASTART:
Nicolas Geoffray01119992007-04-03 13:59:52 +00004164 return LowerVASTART(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
4165 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00004166
4167 case ISD::VAARG:
Nicolas Geoffray01119992007-04-03 13:59:52 +00004168 return LowerVAARG(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
4169 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
4170
Chris Lattneref957102006-06-21 00:34:03 +00004171 case ISD::FORMAL_ARGUMENTS:
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004172 if (PPCSubTarget.isSVR4ABI()) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00004173 return LowerFORMAL_ARGUMENTS_SVR4(Op, DAG, VarArgsFrameIndex,
4174 VarArgsStackOffset, VarArgsNumGPR,
4175 VarArgsNumFPR, PPCSubTarget);
4176 } else {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004177 return LowerFORMAL_ARGUMENTS_Darwin(Op, DAG, VarArgsFrameIndex,
4178 PPCSubTarget);
Tilmann Schellerffd02002009-07-03 06:45:56 +00004179 }
Nicolas Geoffray01119992007-04-03 13:59:52 +00004180
Tilmann Schellerffd02002009-07-03 06:45:56 +00004181 case ISD::CALL:
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004182 if (PPCSubTarget.isSVR4ABI()) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00004183 return LowerCALL_SVR4(Op, DAG, PPCSubTarget, getTargetMachine());
4184 } else {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00004185 return LowerCALL_Darwin(Op, DAG, PPCSubTarget, getTargetMachine());
Tilmann Schellerffd02002009-07-03 06:45:56 +00004186 }
4187
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00004188 case ISD::RET: return LowerRET(Op, DAG, getTargetMachine());
Jim Laskeyefc7e522006-12-04 22:04:42 +00004189 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00004190 case ISD::DYNAMIC_STACKALLOC:
4191 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00004192
Chris Lattner1a635d62006-04-14 06:01:58 +00004193 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004194 case ISD::FP_TO_UINT:
4195 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00004196 Op.getDebugLoc());
Chris Lattner1a635d62006-04-14 06:01:58 +00004197 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00004198 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004199
Chris Lattner1a635d62006-04-14 06:01:58 +00004200 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00004201 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
4202 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
4203 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004204
Chris Lattner1a635d62006-04-14 06:01:58 +00004205 // Vector-related lowering.
4206 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4207 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4208 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4209 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00004210 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004211
Chris Lattner3fc027d2007-12-08 06:59:59 +00004212 // Frame & Return address.
4213 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004214 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00004215 }
Dan Gohman475871a2008-07-27 21:46:04 +00004216 return SDValue();
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004217}
4218
Duncan Sands1607f052008-12-01 11:39:25 +00004219void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
4220 SmallVectorImpl<SDValue>&Results,
4221 SelectionDAG &DAG) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004222 DebugLoc dl = N->getDebugLoc();
Chris Lattner1f873002007-11-28 18:44:47 +00004223 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00004224 default:
Duncan Sands1607f052008-12-01 11:39:25 +00004225 assert(false && "Do not know how to custom type legalize this operation!");
4226 return;
4227 case ISD::FP_ROUND_INREG: {
4228 assert(N->getValueType(0) == MVT::ppcf128);
4229 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00004230 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesen3484c092009-02-05 22:07:54 +00004231 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004232 DAG.getIntPtrConstant(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00004233 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
4234 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004235 DAG.getIntPtrConstant(1));
4236
4237 // This sequence changes FPSCR to do round-to-zero, adds the two halves
4238 // of the long double, and puts FPSCR back the way it was. We do not
4239 // actually model FPSCR.
4240 std::vector<MVT> NodeTys;
4241 SDValue Ops[4], Result, MFFSreg, InFlag, FPreg;
4242
4243 NodeTys.push_back(MVT::f64); // Return register
4244 NodeTys.push_back(MVT::Flag); // Returns a flag for later insns
Dale Johannesen3484c092009-02-05 22:07:54 +00004245 Result = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Duncan Sands1607f052008-12-01 11:39:25 +00004246 MFFSreg = Result.getValue(0);
4247 InFlag = Result.getValue(1);
4248
4249 NodeTys.clear();
4250 NodeTys.push_back(MVT::Flag); // Returns a flag
4251 Ops[0] = DAG.getConstant(31, MVT::i32);
4252 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004253 Result = DAG.getNode(PPCISD::MTFSB1, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004254 InFlag = Result.getValue(0);
4255
4256 NodeTys.clear();
4257 NodeTys.push_back(MVT::Flag); // Returns a flag
4258 Ops[0] = DAG.getConstant(30, MVT::i32);
4259 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004260 Result = DAG.getNode(PPCISD::MTFSB0, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004261 InFlag = Result.getValue(0);
4262
4263 NodeTys.clear();
4264 NodeTys.push_back(MVT::f64); // result of add
4265 NodeTys.push_back(MVT::Flag); // Returns a flag
4266 Ops[0] = Lo;
4267 Ops[1] = Hi;
4268 Ops[2] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004269 Result = DAG.getNode(PPCISD::FADDRTZ, dl, NodeTys, Ops, 3);
Duncan Sands1607f052008-12-01 11:39:25 +00004270 FPreg = Result.getValue(0);
4271 InFlag = Result.getValue(1);
4272
4273 NodeTys.clear();
4274 NodeTys.push_back(MVT::f64);
4275 Ops[0] = DAG.getConstant(1, MVT::i32);
4276 Ops[1] = MFFSreg;
4277 Ops[2] = FPreg;
4278 Ops[3] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004279 Result = DAG.getNode(PPCISD::MTFSF, dl, NodeTys, Ops, 4);
Duncan Sands1607f052008-12-01 11:39:25 +00004280 FPreg = Result.getValue(0);
4281
4282 // We know the low half is about to be thrown away, so just use something
4283 // convenient.
Scott Michelfdc40a02009-02-17 22:15:04 +00004284 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00004285 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00004286 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00004287 }
Duncan Sands1607f052008-12-01 11:39:25 +00004288 case ISD::FP_TO_SINT:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004289 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00004290 return;
Chris Lattner1f873002007-11-28 18:44:47 +00004291 }
4292}
4293
4294
Chris Lattner1a635d62006-04-14 06:01:58 +00004295//===----------------------------------------------------------------------===//
4296// Other Lowering Code
4297//===----------------------------------------------------------------------===//
4298
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004299MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004300PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004301 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004302 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004303 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4304
4305 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4306 MachineFunction *F = BB->getParent();
4307 MachineFunction::iterator It = BB;
4308 ++It;
4309
4310 unsigned dest = MI->getOperand(0).getReg();
4311 unsigned ptrA = MI->getOperand(1).getReg();
4312 unsigned ptrB = MI->getOperand(2).getReg();
4313 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004314 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004315
4316 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4317 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4318 F->insert(It, loopMBB);
4319 F->insert(It, exitMBB);
4320 exitMBB->transferSuccessors(BB);
4321
4322 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00004323 unsigned TmpReg = (!BinOpcode) ? incr :
4324 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00004325 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4326 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004327
4328 // thisMBB:
4329 // ...
4330 // fallthrough --> loopMBB
4331 BB->addSuccessor(loopMBB);
4332
4333 // loopMBB:
4334 // l[wd]arx dest, ptr
4335 // add r0, dest, incr
4336 // st[wd]cx. r0, ptr
4337 // bne- loopMBB
4338 // fallthrough --> exitMBB
4339 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004340 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004341 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004342 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004343 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
4344 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004345 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004346 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004347 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004348 BB->addSuccessor(loopMBB);
4349 BB->addSuccessor(exitMBB);
4350
4351 // exitMBB:
4352 // ...
4353 BB = exitMBB;
4354 return BB;
4355}
4356
4357MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00004358PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00004359 MachineBasicBlock *BB,
4360 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004361 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004362 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00004363 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4364 // In 64 bit mode we have to use 64 bits for addresses, even though the
4365 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
4366 // registers without caring whether they're 32 or 64, but here we're
4367 // doing actual arithmetic on the addresses.
4368 bool is64bit = PPCSubTarget.isPPC64();
4369
4370 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4371 MachineFunction *F = BB->getParent();
4372 MachineFunction::iterator It = BB;
4373 ++It;
4374
4375 unsigned dest = MI->getOperand(0).getReg();
4376 unsigned ptrA = MI->getOperand(1).getReg();
4377 unsigned ptrB = MI->getOperand(2).getReg();
4378 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004379 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00004380
4381 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4382 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4383 F->insert(It, loopMBB);
4384 F->insert(It, exitMBB);
4385 exitMBB->transferSuccessors(BB);
4386
4387 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004388 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004389 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4390 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00004391 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4392 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4393 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4394 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
4395 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4396 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4397 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4398 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4399 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
4400 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004401 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004402 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00004403 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004404
4405 // thisMBB:
4406 // ...
4407 // fallthrough --> loopMBB
4408 BB->addSuccessor(loopMBB);
4409
4410 // The 4-byte load must be aligned, while a char or short may be
4411 // anywhere in the word. Hence all this nasty bookkeeping code.
4412 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4413 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004414 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00004415 // rlwinm ptr, ptr1, 0, 0, 29
4416 // slw incr2, incr, shift
4417 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4418 // slw mask, mask2, shift
4419 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004420 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00004421 // add tmp, tmpDest, incr2
4422 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00004423 // and tmp3, tmp, mask
4424 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004425 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00004426 // bne- loopMBB
4427 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00004428 // srw dest, tmpDest, shift
Dale Johannesen97efa362008-08-28 17:53:09 +00004429
4430 if (ptrA!=PPC::R0) {
4431 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004432 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004433 .addReg(ptrA).addReg(ptrB);
4434 } else {
4435 Ptr1Reg = ptrB;
4436 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004437 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004438 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004439 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004440 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4441 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004442 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004443 .addReg(Ptr1Reg).addImm(0).addImm(61);
4444 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004445 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004446 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004447 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004448 .addReg(incr).addReg(ShiftReg);
4449 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004450 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00004451 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004452 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4453 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00004454 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004455 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004456 .addReg(Mask2Reg).addReg(ShiftReg);
4457
4458 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004459 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004460 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004461 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004462 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004463 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004464 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004465 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004466 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004467 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004468 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004469 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004470 BuildMI(BB, dl, TII->get(PPC::STWCX))
Dale Johannesen97efa362008-08-28 17:53:09 +00004471 .addReg(Tmp4Reg).addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004472 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004473 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00004474 BB->addSuccessor(loopMBB);
4475 BB->addSuccessor(exitMBB);
4476
4477 // exitMBB:
4478 // ...
4479 BB = exitMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004480 BuildMI(BB, dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg).addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00004481 return BB;
4482}
4483
4484MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004485PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004486 MachineBasicBlock *BB) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004487 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00004488
4489 // To "insert" these instructions we actually have to insert their
4490 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004491 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004492 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004493 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00004494
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004495 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00004496
4497 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
4498 MI->getOpcode() == PPC::SELECT_CC_I8 ||
4499 MI->getOpcode() == PPC::SELECT_CC_F4 ||
4500 MI->getOpcode() == PPC::SELECT_CC_F8 ||
4501 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
4502
4503 // The incoming instruction knows the destination vreg to set, the
4504 // condition code register to branch on, the true/false values to
4505 // select between, and a branch opcode to use.
4506
4507 // thisMBB:
4508 // ...
4509 // TrueVal = ...
4510 // cmpTY ccX, r1, r2
4511 // bCC copy1MBB
4512 // fallthrough --> copy0MBB
4513 MachineBasicBlock *thisMBB = BB;
4514 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4515 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4516 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004517 DebugLoc dl = MI->getDebugLoc();
4518 BuildMI(BB, dl, TII->get(PPC::BCC))
Evan Cheng53301922008-07-12 02:23:19 +00004519 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
4520 F->insert(It, copy0MBB);
4521 F->insert(It, sinkMBB);
4522 // Update machine-CFG edges by transferring all successors of the current
4523 // block to the new block which will contain the Phi node for the select.
4524 sinkMBB->transferSuccessors(BB);
4525 // Next, add the true and fallthrough blocks as its successors.
4526 BB->addSuccessor(copy0MBB);
4527 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004528
Evan Cheng53301922008-07-12 02:23:19 +00004529 // copy0MBB:
4530 // %FalseValue = ...
4531 // # fallthrough to sinkMBB
4532 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00004533
Evan Cheng53301922008-07-12 02:23:19 +00004534 // Update machine-CFG edges
4535 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004536
Evan Cheng53301922008-07-12 02:23:19 +00004537 // sinkMBB:
4538 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4539 // ...
4540 BB = sinkMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004541 BuildMI(BB, dl, TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00004542 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
4543 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4544 }
Dale Johannesen97efa362008-08-28 17:53:09 +00004545 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
4546 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
4547 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
4548 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004549 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
4550 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
4551 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
4552 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004553
4554 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
4555 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
4556 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
4557 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004558 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
4559 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
4560 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
4561 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004562
4563 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
4564 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
4565 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
4566 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004567 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
4568 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
4569 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
4570 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004571
4572 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
4573 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
4574 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
4575 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004576 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
4577 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
4578 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
4579 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004580
4581 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00004582 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004583 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00004584 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004585 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00004586 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004587 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00004588 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004589
4590 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
4591 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
4592 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
4593 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004594 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
4595 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
4596 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
4597 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004598
Dale Johannesen0e55f062008-08-29 18:29:46 +00004599 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
4600 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
4601 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
4602 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
4603 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
4604 BB = EmitAtomicBinary(MI, BB, false, 0);
4605 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
4606 BB = EmitAtomicBinary(MI, BB, true, 0);
4607
Evan Cheng53301922008-07-12 02:23:19 +00004608 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
4609 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
4610 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
4611
4612 unsigned dest = MI->getOperand(0).getReg();
4613 unsigned ptrA = MI->getOperand(1).getReg();
4614 unsigned ptrB = MI->getOperand(2).getReg();
4615 unsigned oldval = MI->getOperand(3).getReg();
4616 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004617 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00004618
Dale Johannesen65e39732008-08-25 18:53:26 +00004619 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4620 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4621 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00004622 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004623 F->insert(It, loop1MBB);
4624 F->insert(It, loop2MBB);
4625 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00004626 F->insert(It, exitMBB);
4627 exitMBB->transferSuccessors(BB);
4628
4629 // thisMBB:
4630 // ...
4631 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00004632 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00004633
Dale Johannesen65e39732008-08-25 18:53:26 +00004634 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00004635 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00004636 // cmp[wd] dest, oldval
4637 // bne- midMBB
4638 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00004639 // st[wd]cx. newval, ptr
4640 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00004641 // b exitBB
4642 // midMBB:
4643 // st[wd]cx. dest, ptr
4644 // exitBB:
4645 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004646 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00004647 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004648 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00004649 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004650 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00004651 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
4652 BB->addSuccessor(loop2MBB);
4653 BB->addSuccessor(midMBB);
4654
4655 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004656 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00004657 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004658 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00004659 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004660 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004661 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00004662 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004663
Dale Johannesen65e39732008-08-25 18:53:26 +00004664 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004665 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00004666 .addReg(dest).addReg(ptrA).addReg(ptrB);
4667 BB->addSuccessor(exitMBB);
4668
Evan Cheng53301922008-07-12 02:23:19 +00004669 // exitMBB:
4670 // ...
4671 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004672 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
4673 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
4674 // We must use 64-bit registers for addresses when targeting 64-bit,
4675 // since we're actually doing arithmetic on them. Other registers
4676 // can be 32-bit.
4677 bool is64bit = PPCSubTarget.isPPC64();
4678 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
4679
4680 unsigned dest = MI->getOperand(0).getReg();
4681 unsigned ptrA = MI->getOperand(1).getReg();
4682 unsigned ptrB = MI->getOperand(2).getReg();
4683 unsigned oldval = MI->getOperand(3).getReg();
4684 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004685 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004686
4687 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4688 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4689 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
4690 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4691 F->insert(It, loop1MBB);
4692 F->insert(It, loop2MBB);
4693 F->insert(It, midMBB);
4694 F->insert(It, exitMBB);
4695 exitMBB->transferSuccessors(BB);
4696
4697 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004698 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004699 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4700 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004701 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4702 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4703 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4704 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
4705 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
4706 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
4707 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
4708 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4709 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4710 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4711 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4712 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
4713 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
4714 unsigned Ptr1Reg;
4715 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
4716 // thisMBB:
4717 // ...
4718 // fallthrough --> loopMBB
4719 BB->addSuccessor(loop1MBB);
4720
4721 // The 4-byte load must be aligned, while a char or short may be
4722 // anywhere in the word. Hence all this nasty bookkeeping code.
4723 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4724 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004725 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004726 // rlwinm ptr, ptr1, 0, 0, 29
4727 // slw newval2, newval, shift
4728 // slw oldval2, oldval,shift
4729 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4730 // slw mask, mask2, shift
4731 // and newval3, newval2, mask
4732 // and oldval3, oldval2, mask
4733 // loop1MBB:
4734 // lwarx tmpDest, ptr
4735 // and tmp, tmpDest, mask
4736 // cmpw tmp, oldval3
4737 // bne- midMBB
4738 // loop2MBB:
4739 // andc tmp2, tmpDest, mask
4740 // or tmp4, tmp2, newval3
4741 // stwcx. tmp4, ptr
4742 // bne- loop1MBB
4743 // b exitBB
4744 // midMBB:
4745 // stwcx. tmpDest, ptr
4746 // exitBB:
4747 // srw dest, tmpDest, shift
4748 if (ptrA!=PPC::R0) {
4749 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004750 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004751 .addReg(ptrA).addReg(ptrB);
4752 } else {
4753 Ptr1Reg = ptrB;
4754 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004755 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004756 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004757 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004758 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4759 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004760 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004761 .addReg(Ptr1Reg).addImm(0).addImm(61);
4762 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004763 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004764 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004765 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004766 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004767 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004768 .addReg(oldval).addReg(ShiftReg);
4769 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004770 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004771 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004772 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4773 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
4774 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004775 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004776 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004777 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004778 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004779 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004780 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004781 .addReg(OldVal2Reg).addReg(MaskReg);
4782
4783 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004784 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004785 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004786 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
4787 .addReg(TmpDestReg).addReg(MaskReg);
4788 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004789 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004790 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004791 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
4792 BB->addSuccessor(loop2MBB);
4793 BB->addSuccessor(midMBB);
4794
4795 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004796 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
4797 .addReg(TmpDestReg).addReg(MaskReg);
4798 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
4799 .addReg(Tmp2Reg).addReg(NewVal3Reg);
4800 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004801 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004802 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004803 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004804 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004805 BB->addSuccessor(loop1MBB);
4806 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004807
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004808 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004809 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004810 .addReg(PPC::R0).addReg(PtrReg);
4811 BB->addSuccessor(exitMBB);
4812
4813 // exitMBB:
4814 // ...
4815 BB = exitMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004816 BuildMI(BB, dl, TII->get(PPC::SRW),dest).addReg(TmpReg).addReg(ShiftReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004817 } else {
Evan Cheng53301922008-07-12 02:23:19 +00004818 assert(0 && "Unexpected instr type to insert");
4819 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004820
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004821 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004822 return BB;
4823}
4824
Chris Lattner1a635d62006-04-14 06:01:58 +00004825//===----------------------------------------------------------------------===//
4826// Target Optimization Hooks
4827//===----------------------------------------------------------------------===//
4828
Duncan Sands25cf2272008-11-24 14:53:14 +00004829SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
4830 DAGCombinerInfo &DCI) const {
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004831 TargetMachine &TM = getTargetMachine();
4832 SelectionDAG &DAG = DCI.DAG;
Dale Johannesen3484c092009-02-05 22:07:54 +00004833 DebugLoc dl = N->getDebugLoc();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004834 switch (N->getOpcode()) {
4835 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004836 case PPCISD::SHL:
4837 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004838 if (C->getZExtValue() == 0) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004839 return N->getOperand(0);
4840 }
4841 break;
4842 case PPCISD::SRL:
4843 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004844 if (C->getZExtValue() == 0) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004845 return N->getOperand(0);
4846 }
4847 break;
4848 case PPCISD::SRA:
4849 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004850 if (C->getZExtValue() == 0 || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004851 C->isAllOnesValue()) // -1 >>s V -> -1.
4852 return N->getOperand(0);
4853 }
4854 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004855
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004856 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00004857 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004858 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
4859 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
4860 // We allow the src/dst to be either f32/f64, but the intermediate
4861 // type must be i64.
Dale Johannesen79217062007-10-23 23:20:14 +00004862 if (N->getOperand(0).getValueType() == MVT::i64 &&
4863 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004864 SDValue Val = N->getOperand(0).getOperand(0);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004865 if (Val.getValueType() == MVT::f32) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004866 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004867 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004868 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004869
Dale Johannesen3484c092009-02-05 22:07:54 +00004870 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004871 DCI.AddToWorklist(Val.getNode());
Dale Johannesen3484c092009-02-05 22:07:54 +00004872 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004873 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004874 if (N->getValueType(0) == MVT::f32) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004875 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00004876 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004877 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004878 }
4879 return Val;
4880 } else if (N->getOperand(0).getValueType() == MVT::i32) {
4881 // If the intermediate type is i32, we can avoid the load/store here
4882 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004883 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004884 }
4885 }
4886 break;
Chris Lattner51269842006-03-01 05:50:56 +00004887 case ISD::STORE:
4888 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
4889 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00004890 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00004891 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Dale Johannesen79217062007-10-23 23:20:14 +00004892 N->getOperand(1).getValueType() == MVT::i32 &&
4893 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004894 SDValue Val = N->getOperand(1).getOperand(0);
Chris Lattner51269842006-03-01 05:50:56 +00004895 if (Val.getValueType() == MVT::f32) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004896 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004897 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004898 }
Dale Johannesen3484c092009-02-05 22:07:54 +00004899 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004900 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004901
Dale Johannesen3484c092009-02-05 22:07:54 +00004902 Val = DAG.getNode(PPCISD::STFIWX, dl, MVT::Other, N->getOperand(0), Val,
Chris Lattner51269842006-03-01 05:50:56 +00004903 N->getOperand(2), N->getOperand(3));
Gabor Greifba36cb52008-08-28 21:40:38 +00004904 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004905 return Val;
4906 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004907
Chris Lattnerd9989382006-07-10 20:56:58 +00004908 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
4909 if (N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004910 N->getOperand(1).getNode()->hasOneUse() &&
Chris Lattnerd9989382006-07-10 20:56:58 +00004911 (N->getOperand(1).getValueType() == MVT::i32 ||
4912 N->getOperand(1).getValueType() == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004913 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00004914 // Do an any-extend to 32-bits if this is a half-word input.
4915 if (BSwapOp.getValueType() == MVT::i16)
Dale Johannesen3484c092009-02-05 22:07:54 +00004916 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00004917
Dale Johannesen3484c092009-02-05 22:07:54 +00004918 return DAG.getNode(PPCISD::STBRX, dl, MVT::Other, N->getOperand(0),
4919 BSwapOp, N->getOperand(2), N->getOperand(3),
Chris Lattnerd9989382006-07-10 20:56:58 +00004920 DAG.getValueType(N->getOperand(1).getValueType()));
4921 }
4922 break;
4923 case ISD::BSWAP:
4924 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00004925 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00004926 N->getOperand(0).hasOneUse() &&
4927 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004928 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00004929 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00004930 // Create the byte-swapping load.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004931 std::vector<MVT> VTs;
Chris Lattnerd9989382006-07-10 20:56:58 +00004932 VTs.push_back(MVT::i32);
4933 VTs.push_back(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004934 SDValue MO = DAG.getMemOperand(LD->getMemOperand());
4935 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00004936 LD->getChain(), // Chain
4937 LD->getBasePtr(), // Ptr
Dan Gohman69de1932008-02-06 22:27:42 +00004938 MO, // MemOperand
Chris Lattner79e490a2006-08-11 17:18:05 +00004939 DAG.getValueType(N->getValueType(0)) // VT
4940 };
Dale Johannesen3484c092009-02-05 22:07:54 +00004941 SDValue BSLoad = DAG.getNode(PPCISD::LBRX, dl, VTs, Ops, 4);
Chris Lattnerd9989382006-07-10 20:56:58 +00004942
Scott Michelfdc40a02009-02-17 22:15:04 +00004943 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00004944 SDValue ResVal = BSLoad;
Chris Lattnerd9989382006-07-10 20:56:58 +00004945 if (N->getValueType(0) == MVT::i16)
Dale Johannesen3484c092009-02-05 22:07:54 +00004946 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00004947
Chris Lattnerd9989382006-07-10 20:56:58 +00004948 // First, combine the bswap away. This makes the value produced by the
4949 // load dead.
4950 DCI.CombineTo(N, ResVal);
4951
4952 // Next, combine the load away, we give it a bogus result value but a real
4953 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00004954 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004955
Chris Lattnerd9989382006-07-10 20:56:58 +00004956 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00004957 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00004958 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004959
Chris Lattner51269842006-03-01 05:50:56 +00004960 break;
Chris Lattner4468c222006-03-31 06:02:07 +00004961 case PPCISD::VCMP: {
4962 // If a VCMPo node already exists with exactly the same operands as this
4963 // node, use its result instead of this node (VCMPo computes both a CR6 and
4964 // a normal output).
4965 //
4966 if (!N->getOperand(0).hasOneUse() &&
4967 !N->getOperand(1).hasOneUse() &&
4968 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004969
Chris Lattner4468c222006-03-31 06:02:07 +00004970 // Scan all of the users of the LHS, looking for VCMPo's that match.
4971 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004972
Gabor Greifba36cb52008-08-28 21:40:38 +00004973 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00004974 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
4975 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004976 if (UI->getOpcode() == PPCISD::VCMPo &&
4977 UI->getOperand(1) == N->getOperand(1) &&
4978 UI->getOperand(2) == N->getOperand(2) &&
4979 UI->getOperand(0) == N->getOperand(0)) {
4980 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00004981 break;
4982 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004983
Chris Lattner00901202006-04-18 18:28:22 +00004984 // If there is no VCMPo node, or if the flag value has a single use, don't
4985 // transform this.
4986 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
4987 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004988
4989 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00004990 // chain, this transformation is more complex. Note that multiple things
4991 // could use the value result, which we should ignore.
4992 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004993 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00004994 FlagUser == 0; ++UI) {
4995 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00004996 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00004997 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004998 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00004999 FlagUser = User;
5000 break;
5001 }
5002 }
5003 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005004
Chris Lattner00901202006-04-18 18:28:22 +00005005 // If the user is a MFCR instruction, we know this is safe. Otherwise we
5006 // give up for right now.
5007 if (FlagUser->getOpcode() == PPCISD::MFCR)
Dan Gohman475871a2008-07-27 21:46:04 +00005008 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00005009 }
5010 break;
5011 }
Chris Lattner90564f22006-04-18 17:59:36 +00005012 case ISD::BR_CC: {
5013 // If this is a branch on an altivec predicate comparison, lower this so
5014 // that we don't have to do a MFCR: instead, branch directly on CR6. This
5015 // lowering is done pre-legalize, because the legalizer lowers the predicate
5016 // compare down to code that is difficult to reassemble.
5017 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00005018 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Chris Lattner90564f22006-04-18 17:59:36 +00005019 int CompareOpc;
5020 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00005021
Chris Lattner90564f22006-04-18 17:59:36 +00005022 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
5023 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
5024 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
5025 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005026
Chris Lattner90564f22006-04-18 17:59:36 +00005027 // If this is a comparison against something other than 0/1, then we know
5028 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005029 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005030 if (Val != 0 && Val != 1) {
5031 if (CC == ISD::SETEQ) // Cond never true, remove branch.
5032 return N->getOperand(0);
5033 // Always !=, turn it into an unconditional branch.
Dale Johannesen3484c092009-02-05 22:07:54 +00005034 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00005035 N->getOperand(0), N->getOperand(4));
5036 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005037
Chris Lattner90564f22006-04-18 17:59:36 +00005038 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005039
Chris Lattner90564f22006-04-18 17:59:36 +00005040 // Create the PPCISD altivec 'dot' comparison node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005041 std::vector<MVT> VTs;
Dan Gohman475871a2008-07-27 21:46:04 +00005042 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005043 LHS.getOperand(2), // LHS of compare
5044 LHS.getOperand(3), // RHS of compare
5045 DAG.getConstant(CompareOpc, MVT::i32)
5046 };
Chris Lattner90564f22006-04-18 17:59:36 +00005047 VTs.push_back(LHS.getOperand(2).getValueType());
5048 VTs.push_back(MVT::Flag);
Dale Johannesen3484c092009-02-05 22:07:54 +00005049 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005050
Chris Lattner90564f22006-04-18 17:59:36 +00005051 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005052 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005053 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00005054 default: // Can't happen, don't crash on invalid number though.
5055 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005056 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00005057 break;
5058 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005059 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00005060 break;
5061 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005062 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00005063 break;
5064 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005065 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00005066 break;
5067 }
5068
Dale Johannesen3484c092009-02-05 22:07:54 +00005069 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
Chris Lattner90564f22006-04-18 17:59:36 +00005070 DAG.getConstant(CompOpc, MVT::i32),
Chris Lattner18258c62006-11-17 22:37:34 +00005071 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00005072 N->getOperand(4), CompNode.getValue(1));
5073 }
5074 break;
5075 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005076 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005077
Dan Gohman475871a2008-07-27 21:46:04 +00005078 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005079}
5080
Chris Lattner1a635d62006-04-14 06:01:58 +00005081//===----------------------------------------------------------------------===//
5082// Inline Assembly Support
5083//===----------------------------------------------------------------------===//
5084
Dan Gohman475871a2008-07-27 21:46:04 +00005085void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005086 const APInt &Mask,
Scott Michelfdc40a02009-02-17 22:15:04 +00005087 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005088 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005089 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005090 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005091 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005092 switch (Op.getOpcode()) {
5093 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00005094 case PPCISD::LBRX: {
5095 // lhbrx is known to have the top bits cleared out.
5096 if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16)
5097 KnownZero = 0xFFFF0000;
5098 break;
5099 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005100 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005101 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005102 default: break;
5103 case Intrinsic::ppc_altivec_vcmpbfp_p:
5104 case Intrinsic::ppc_altivec_vcmpeqfp_p:
5105 case Intrinsic::ppc_altivec_vcmpequb_p:
5106 case Intrinsic::ppc_altivec_vcmpequh_p:
5107 case Intrinsic::ppc_altivec_vcmpequw_p:
5108 case Intrinsic::ppc_altivec_vcmpgefp_p:
5109 case Intrinsic::ppc_altivec_vcmpgtfp_p:
5110 case Intrinsic::ppc_altivec_vcmpgtsb_p:
5111 case Intrinsic::ppc_altivec_vcmpgtsh_p:
5112 case Intrinsic::ppc_altivec_vcmpgtsw_p:
5113 case Intrinsic::ppc_altivec_vcmpgtub_p:
5114 case Intrinsic::ppc_altivec_vcmpgtuh_p:
5115 case Intrinsic::ppc_altivec_vcmpgtuw_p:
5116 KnownZero = ~1U; // All bits but the low one are known to be zero.
5117 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005118 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005119 }
5120 }
5121}
5122
5123
Chris Lattner4234f572007-03-25 02:14:49 +00005124/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005125/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00005126PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005127PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
5128 if (Constraint.size() == 1) {
5129 switch (Constraint[0]) {
5130 default: break;
5131 case 'b':
5132 case 'r':
5133 case 'f':
5134 case 'v':
5135 case 'y':
5136 return C_RegisterClass;
5137 }
5138 }
5139 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005140}
5141
Scott Michelfdc40a02009-02-17 22:15:04 +00005142std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00005143PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005144 MVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00005145 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00005146 // GCC RS6000 Constraint Letters
5147 switch (Constraint[0]) {
5148 case 'b': // R1-R31
5149 case 'r': // R0-R31
5150 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
5151 return std::make_pair(0U, PPC::G8RCRegisterClass);
5152 return std::make_pair(0U, PPC::GPRCRegisterClass);
5153 case 'f':
5154 if (VT == MVT::f32)
5155 return std::make_pair(0U, PPC::F4RCRegisterClass);
5156 else if (VT == MVT::f64)
5157 return std::make_pair(0U, PPC::F8RCRegisterClass);
5158 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005159 case 'v':
Chris Lattner331d1bc2006-11-02 01:44:04 +00005160 return std::make_pair(0U, PPC::VRRCRegisterClass);
5161 case 'y': // crrc
5162 return std::make_pair(0U, PPC::CRRCRegisterClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005163 }
5164 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005165
Chris Lattner331d1bc2006-11-02 01:44:04 +00005166 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005167}
Chris Lattner763317d2006-02-07 00:47:13 +00005168
Chris Lattner331d1bc2006-11-02 01:44:04 +00005169
Chris Lattner48884cd2007-08-25 00:47:38 +00005170/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Evan Chengda43bcf2008-09-24 00:05:32 +00005171/// vector. If it is invalid, don't add anything to Ops. If hasMemory is true
5172/// it means one of the asm constraint of the inline asm instruction being
5173/// processed is 'm'.
Dan Gohman475871a2008-07-27 21:46:04 +00005174void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, char Letter,
Evan Chengda43bcf2008-09-24 00:05:32 +00005175 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00005176 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00005177 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00005178 SDValue Result(0,0);
Chris Lattner763317d2006-02-07 00:47:13 +00005179 switch (Letter) {
5180 default: break;
5181 case 'I':
5182 case 'J':
5183 case 'K':
5184 case 'L':
5185 case 'M':
5186 case 'N':
5187 case 'O':
5188 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00005189 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00005190 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005191 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00005192 switch (Letter) {
5193 default: assert(0 && "Unknown constraint letter!");
5194 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005195 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005196 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005197 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005198 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
5199 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005200 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005201 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005202 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005203 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005204 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005205 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005206 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005207 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005208 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00005209 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005210 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005211 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005212 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00005213 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005214 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005215 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005216 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005217 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005218 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005219 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005220 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005221 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005222 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005223 }
5224 break;
5225 }
5226 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005227
Gabor Greifba36cb52008-08-28 21:40:38 +00005228 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005229 Ops.push_back(Result);
5230 return;
5231 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005232
Chris Lattner763317d2006-02-07 00:47:13 +00005233 // Handle standard constraint letters.
Evan Chengda43bcf2008-09-24 00:05:32 +00005234 TargetLowering::LowerAsmOperandForConstraint(Op, Letter, hasMemory, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00005235}
Evan Chengc4c62572006-03-13 23:20:37 +00005236
Chris Lattnerc9addb72007-03-30 23:15:24 +00005237// isLegalAddressingMode - Return true if the addressing mode represented
5238// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00005239bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005240 const Type *Ty) const {
5241 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00005242
Chris Lattnerc9addb72007-03-30 23:15:24 +00005243 // PPC allows a sign-extended 16-bit immediate field.
5244 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
5245 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005246
Chris Lattnerc9addb72007-03-30 23:15:24 +00005247 // No global is ever allowed as a base.
5248 if (AM.BaseGV)
5249 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005250
5251 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005252 switch (AM.Scale) {
5253 case 0: // "r+i" or just "i", depending on HasBaseReg.
5254 break;
5255 case 1:
5256 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
5257 return false;
5258 // Otherwise we have r+r or r+i.
5259 break;
5260 case 2:
5261 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
5262 return false;
5263 // Allow 2*r as r+r.
5264 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00005265 default:
5266 // No other scales are supported.
5267 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00005268 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005269
Chris Lattnerc9addb72007-03-30 23:15:24 +00005270 return true;
5271}
5272
Evan Chengc4c62572006-03-13 23:20:37 +00005273/// isLegalAddressImmediate - Return true if the integer value can be used
Evan Cheng86193912007-03-12 23:29:01 +00005274/// as the offset of the target addressing mode for load / store of the
5275/// given type.
5276bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
Evan Chengc4c62572006-03-13 23:20:37 +00005277 // PPC allows a sign-extended 16-bit immediate field.
5278 return (V > -(1 << 16) && V < (1 << 16)-1);
5279}
Reid Spencer3a9ec242006-08-28 01:02:49 +00005280
5281bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michelfdc40a02009-02-17 22:15:04 +00005282 return false;
Reid Spencer3a9ec242006-08-28 01:02:49 +00005283}
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005284
Dan Gohman475871a2008-07-27 21:46:04 +00005285SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005286 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005287 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005288 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005289 return SDValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005290
5291 MachineFunction &MF = DAG.getMachineFunction();
5292 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005293
Chris Lattner3fc027d2007-12-08 06:59:59 +00005294 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00005295 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00005296
5297 // Make sure the function really does not optimize away the store of the RA
5298 // to the stack.
5299 FuncInfo->setLRStoreRequired();
Scott Michelfdc40a02009-02-17 22:15:04 +00005300 return DAG.getLoad(getPointerTy(), dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00005301 DAG.getEntryNode(), RetAddrFI, NULL, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00005302}
5303
Dan Gohman475871a2008-07-27 21:46:04 +00005304SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesena05dca42009-02-04 23:02:30 +00005305 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005306 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005307 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005308 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005309
Duncan Sands83ec4b62008-06-06 12:08:01 +00005310 MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005311 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00005312
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005313 MachineFunction &MF = DAG.getMachineFunction();
5314 MachineFrameInfo *MFI = MF.getFrameInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005315 bool is31 = (NoFramePointerElim || MFI->hasVarSizedObjects())
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005316 && MFI->getStackSize();
5317
5318 if (isPPC64)
Dale Johannesena05dca42009-02-04 23:02:30 +00005319 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::X31 : PPC::X1,
Bill Wendlingb8a80f02007-08-30 00:59:19 +00005320 MVT::i64);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005321 else
Dale Johannesena05dca42009-02-04 23:02:30 +00005322 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::R31 : PPC::R1,
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005323 MVT::i32);
5324}
Dan Gohman54aeea32008-10-21 03:41:46 +00005325
5326bool
5327PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5328 // The PowerPC target isn't yet aware of offsets.
5329 return false;
5330}
Tilmann Schellerffd02002009-07-03 06:45:56 +00005331
5332MVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
5333 bool isSrcConst, bool isSrcStr,
5334 SelectionDAG &DAG) const {
5335 if (this->PPCSubTarget.isPPC64()) {
5336 return MVT::i64;
5337 } else {
5338 return MVT::i32;
5339 }
5340}