blob: ecdf3f5fbfb41bbcebd756e8e8dbd5bd730d905b [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 Lattnerf0144122009-07-28 03:13:23 +000034#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner4eab7142006-11-10 02:08:47 +000035#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Jay Foad8d730fb2009-05-11 19:38:09 +000038#include "llvm/DerivedTypes.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000039using namespace llvm;
40
Owen Andersone50ed302009-08-10 22:56:29 +000041static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +000042 CCValAssign::LocInfo &LocInfo,
43 ISD::ArgFlagsTy &ArgFlags,
44 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000045static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, EVT &ValVT,
46 EVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +000047 CCValAssign::LocInfo &LocInfo,
48 ISD::ArgFlagsTy &ArgFlags,
49 CCState &State);
Owen Andersone50ed302009-08-10 22:56:29 +000050static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, EVT &ValVT,
51 EVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +000052 CCValAssign::LocInfo &LocInfo,
53 ISD::ArgFlagsTy &ArgFlags,
54 CCState &State);
55
Scott Michelfdc40a02009-02-17 22:15:04 +000056static cl::opt<bool> EnablePPCPreinc("enable-ppc-preinc",
Chris Lattner3ee77402007-06-19 05:46:06 +000057cl::desc("enable preincrement load/store generation on PPC (experimental)"),
58 cl::Hidden);
Chris Lattner4eab7142006-11-10 02:08:47 +000059
Chris Lattnerf0144122009-07-28 03:13:23 +000060static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
61 if (TM.getSubtargetImpl()->isDarwin())
Chris Lattnerf26e03b2009-07-31 17:42:42 +000062 return new TargetLoweringObjectFileMachO();
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000063 return new TargetLoweringObjectFileELF();
Chris Lattnerf0144122009-07-28 03:13:23 +000064}
65
66
Chris Lattner331d1bc2006-11-02 01:44:04 +000067PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000068 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Scott Michelfdc40a02009-02-17 22:15:04 +000069
Nate Begeman405e3ec2005-10-21 00:02:42 +000070 setPow2DivIsCheap();
Dale Johannesen72324642008-07-31 18:13:12 +000071
Chris Lattnerd145a612005-09-27 22:18:25 +000072 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000073 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
Scott Michelfdc40a02009-02-17 22:15:04 +000075
Chris Lattner7c5a3d32005-08-16 17:14:42 +000076 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000077 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
78 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
79 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +000080
Evan Chengc5484282006-10-04 00:56:09 +000081 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson825b72b2009-08-11 20:47:22 +000082 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
83 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000084
Owen Anderson825b72b2009-08-11 20:47:22 +000085 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +000086
Chris Lattner94e509c2006-11-10 23:58:45 +000087 // PowerPC has pre-inc load and store's.
Owen Anderson825b72b2009-08-11 20:47:22 +000088 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
89 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
90 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
91 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
92 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
93 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
94 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
95 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
97 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Chengcd633192006-11-09 19:11:50 +000098
Dale Johannesen6eaeff22007-10-10 01:01:31 +000099 // This is used in the ppcf128->int sequence. Note it has different semantics
100 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000101 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +0000102
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000103 // PowerPC has no SREM/UREM instructions
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 setOperationAction(ISD::SREM, MVT::i32, Expand);
105 setOperationAction(ISD::UREM, MVT::i32, Expand);
106 setOperationAction(ISD::SREM, MVT::i64, Expand);
107 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +0000108
109 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson825b72b2009-08-11 20:47:22 +0000110 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
111 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
112 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
113 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
114 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
115 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
116 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
117 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000119 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 setOperationAction(ISD::FSIN , MVT::f64, Expand);
121 setOperationAction(ISD::FCOS , MVT::f64, Expand);
122 setOperationAction(ISD::FREM , MVT::f64, Expand);
123 setOperationAction(ISD::FPOW , MVT::f64, Expand);
124 setOperationAction(ISD::FSIN , MVT::f32, Expand);
125 setOperationAction(ISD::FCOS , MVT::f32, Expand);
126 setOperationAction(ISD::FREM , MVT::f32, Expand);
127 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000128
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000130
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000131 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000132 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000133 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
134 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000135 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000136
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
138 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000139
Nate Begemand88fc032006-01-14 03:14:10 +0000140 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
142 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
143 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
144 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
145 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
146 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000147
Nate Begeman35ef9132006-01-11 21:21:00 +0000148 // PowerPC does not have ROTR
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
150 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000151
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000152 // PowerPC does not have Select
Owen Anderson825b72b2009-08-11 20:47:22 +0000153 setOperationAction(ISD::SELECT, MVT::i32, Expand);
154 setOperationAction(ISD::SELECT, MVT::i64, Expand);
155 setOperationAction(ISD::SELECT, MVT::f32, Expand);
156 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000157
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000158 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
160 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000161
Nate Begeman750ac1b2006-02-01 07:19:44 +0000162 // PowerPC wants to optimize integer setcc a bit
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000164
Nate Begeman81e80972006-03-17 01:40:33 +0000165 // PowerPC does not have BRCOND which requires SetCC
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000167
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000169
Chris Lattnerf7605322005-08-31 21:09:52 +0000170 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000172
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000173 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
175 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000176
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
178 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
179 setOperationAction(ISD::BIT_CONVERT, MVT::i64, Expand);
180 setOperationAction(ISD::BIT_CONVERT, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000181
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000182 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000184
Jim Laskeyabf6d172006-01-05 01:25:28 +0000185 // Support label based line numbers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
187 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000188
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
190 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
191 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
192 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000193
194
195 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman28a6b022005-12-10 02:36:00 +0000196 // appropriate instructions to materialize the address.
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
198 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
199 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
200 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
201 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
202 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
203 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
204 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000205
Nate Begeman1db3c922008-08-11 17:36:31 +0000206 // TRAP is legal.
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling77959322008-09-17 00:30:57 +0000208
209 // TRAMPOLINE is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Bill Wendling77959322008-09-17 00:30:57 +0000211
Nate Begemanacc398c2006-01-25 18:21:52 +0000212 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000214
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000215 // VAARG is custom lowered with the 32-bit SVR4 ABI.
216 if ( TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
217 && !TM.getSubtarget<PPCSubtarget>().isPPC64())
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Nicolas Geoffray01119992007-04-03 13:59:52 +0000219 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000221
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000222 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
224 setOperationAction(ISD::VAEND , MVT::Other, Expand);
225 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
226 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
227 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
228 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000229
Chris Lattner6d92cad2006-03-26 10:06:40 +0000230 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000231 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000232
Dale Johannesen53e4e442008-11-07 22:54:33 +0000233 // Comparisons that require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
235 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
236 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
237 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
238 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
239 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
240 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
241 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
242 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
243 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
244 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
245 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000246
Chris Lattnera7a58542006-06-16 17:34:12 +0000247 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000248 // They also have instructions for converting between i64 and fp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
250 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
251 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
252 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen4c9369d2009-06-04 20:53:52 +0000253 // This is just the low 32 bits of a (signed) fp->i64 conversion.
254 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000256
Chris Lattner7fbcef72006-03-24 07:53:47 +0000257 // FIXME: disable this lowered code. This generates 64-bit register values,
258 // and we don't model the fact that the top part is clobbered by calls. We
259 // need to flag these together so that the value isn't live across a call.
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000261 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000262 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000264 }
265
Chris Lattnera7a58542006-06-16 17:34:12 +0000266 if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000267 // 64-bit PowerPC implementations can support i64 types directly
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000269 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000271 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
273 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
274 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000275 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000276 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
278 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
279 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000280 }
Evan Chengd30bf012006-03-01 01:11:20 +0000281
Nate Begeman425a9692005-11-29 08:17:20 +0000282 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000283 // First set operation action for all vector types to expand. Then we
284 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
286 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
287 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000288
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000289 // add/sub are legal for all supported vector VT's.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000290 setOperationAction(ISD::ADD , VT, Legal);
291 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000292
Chris Lattner7ff7e672006-04-04 17:25:31 +0000293 // We promote all shuffles to v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000294 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000296
297 // We promote all non-typed operations to v4i32.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000298 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000300 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000301 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000302 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000304 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000306 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000308 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000310
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000311 // No other operations are legal.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000312 setOperationAction(ISD::MUL , VT, Expand);
313 setOperationAction(ISD::SDIV, VT, Expand);
314 setOperationAction(ISD::SREM, VT, Expand);
315 setOperationAction(ISD::UDIV, VT, Expand);
316 setOperationAction(ISD::UREM, VT, Expand);
317 setOperationAction(ISD::FDIV, VT, Expand);
318 setOperationAction(ISD::FNEG, VT, Expand);
319 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
320 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
321 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
322 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
323 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
324 setOperationAction(ISD::UDIVREM, VT, Expand);
325 setOperationAction(ISD::SDIVREM, VT, Expand);
326 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
327 setOperationAction(ISD::FPOW, VT, Expand);
328 setOperationAction(ISD::CTPOP, VT, Expand);
329 setOperationAction(ISD::CTLZ, VT, Expand);
330 setOperationAction(ISD::CTTZ, VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000331 }
332
Chris Lattner7ff7e672006-04-04 17:25:31 +0000333 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
334 // with merges, splats, etc.
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000336
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::AND , MVT::v4i32, Legal);
338 setOperationAction(ISD::OR , MVT::v4i32, Legal);
339 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
340 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
341 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
342 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000343
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
345 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
346 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
347 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +0000348
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
350 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
351 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
352 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000353
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
355 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000356
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
358 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
359 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
360 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000361 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000362
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setShiftAmountType(MVT::i32);
Duncan Sands03228082008-11-23 15:47:28 +0000364 setBooleanContents(ZeroOrOneBooleanContent);
Scott Michelfdc40a02009-02-17 22:15:04 +0000365
Jim Laskey2ad9f172007-02-22 14:56:36 +0000366 if (TM.getSubtarget<PPCSubtarget>().isPPC64()) {
Chris Lattner10da9572006-10-18 01:20:43 +0000367 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000368 setExceptionPointerRegister(PPC::X3);
369 setExceptionSelectorRegister(PPC::X4);
370 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000371 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000372 setExceptionPointerRegister(PPC::R3);
373 setExceptionSelectorRegister(PPC::R4);
374 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000375
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000376 // We have target-specific dag combine patterns for the following nodes:
377 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000378 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000379 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000380 setTargetDAGCombine(ISD::BSWAP);
Scott Michelfdc40a02009-02-17 22:15:04 +0000381
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000382 // Darwin long double math library functions have $LDBL128 appended.
383 if (TM.getSubtarget<PPCSubtarget>().isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000384 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000385 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
386 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000387 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
388 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000389 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
390 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
391 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
392 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
393 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000394 }
395
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000396 computeRegisterProperties();
397}
398
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000399/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
400/// function arguments in the caller parameter area.
401unsigned PPCTargetLowering::getByValTypeAlignment(const Type *Ty) const {
402 TargetMachine &TM = getTargetMachine();
403 // Darwin passes everything on 4 byte boundary.
404 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
405 return 4;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000406 // FIXME SVR4 TBD
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000407 return 4;
408}
409
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000410const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
411 switch (Opcode) {
412 default: return 0;
Evan Cheng53301922008-07-12 02:23:19 +0000413 case PPCISD::FSEL: return "PPCISD::FSEL";
414 case PPCISD::FCFID: return "PPCISD::FCFID";
415 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
416 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
417 case PPCISD::STFIWX: return "PPCISD::STFIWX";
418 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
419 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
420 case PPCISD::VPERM: return "PPCISD::VPERM";
421 case PPCISD::Hi: return "PPCISD::Hi";
422 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000423 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Evan Cheng53301922008-07-12 02:23:19 +0000424 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
425 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
426 case PPCISD::SRL: return "PPCISD::SRL";
427 case PPCISD::SRA: return "PPCISD::SRA";
428 case PPCISD::SHL: return "PPCISD::SHL";
429 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
430 case PPCISD::STD_32: return "PPCISD::STD_32";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000431 case PPCISD::CALL_SVR4: return "PPCISD::CALL_SVR4";
432 case PPCISD::CALL_Darwin: return "PPCISD::CALL_Darwin";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000433 case PPCISD::NOP: return "PPCISD::NOP";
Evan Cheng53301922008-07-12 02:23:19 +0000434 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000435 case PPCISD::BCTRL_Darwin: return "PPCISD::BCTRL_Darwin";
436 case PPCISD::BCTRL_SVR4: return "PPCISD::BCTRL_SVR4";
Evan Cheng53301922008-07-12 02:23:19 +0000437 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
438 case PPCISD::MFCR: return "PPCISD::MFCR";
439 case PPCISD::VCMP: return "PPCISD::VCMP";
440 case PPCISD::VCMPo: return "PPCISD::VCMPo";
441 case PPCISD::LBRX: return "PPCISD::LBRX";
442 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng53301922008-07-12 02:23:19 +0000443 case PPCISD::LARX: return "PPCISD::LARX";
444 case PPCISD::STCX: return "PPCISD::STCX";
445 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
446 case PPCISD::MFFS: return "PPCISD::MFFS";
447 case PPCISD::MTFSB0: return "PPCISD::MTFSB0";
448 case PPCISD::MTFSB1: return "PPCISD::MTFSB1";
449 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
450 case PPCISD::MTFSF: return "PPCISD::MTFSF";
Evan Cheng53301922008-07-12 02:23:19 +0000451 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000452 }
453}
454
Owen Anderson825b72b2009-08-11 20:47:22 +0000455MVT::SimpleValueType PPCTargetLowering::getSetCCResultType(EVT VT) const {
456 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000457}
458
Bill Wendlingb4202b82009-07-01 18:50:55 +0000459/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000460unsigned PPCTargetLowering::getFunctionAlignment(const Function *F) const {
461 if (getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin())
462 return F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4;
463 else
464 return 2;
465}
Scott Michel5b8f82e2008-03-10 15:42:14 +0000466
Chris Lattner1a635d62006-04-14 06:01:58 +0000467//===----------------------------------------------------------------------===//
468// Node matching predicates, for use by the tblgen matching code.
469//===----------------------------------------------------------------------===//
470
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000471/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000472static bool isFloatingPointZero(SDValue Op) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000473 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000474 return CFP->getValueAPF().isZero();
Gabor Greifba36cb52008-08-28 21:40:38 +0000475 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000476 // Maybe this has already been legalized into the constant pool?
477 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Evan Chengc356a572006-09-12 21:04:05 +0000478 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000479 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000480 }
481 return false;
482}
483
Chris Lattnerddb739e2006-04-06 17:23:16 +0000484/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
485/// true if Op is undef or if it matches the specified value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000486static bool isConstantOrUndef(int Op, int Val) {
487 return Op < 0 || Op == Val;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000488}
489
490/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
491/// VPKUHUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000492bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000493 if (!isUnary) {
494 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000495 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000496 return false;
497 } else {
498 for (unsigned i = 0; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000499 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
500 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000501 return false;
502 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000503 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000504}
505
506/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
507/// VPKUWUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000508bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000509 if (!isUnary) {
510 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000511 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
512 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000513 return false;
514 } else {
515 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000516 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
517 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
518 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
519 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000520 return false;
521 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000522 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000523}
524
Chris Lattnercaad1632006-04-06 22:02:42 +0000525/// isVMerge - Common function, used to match vmrg* shuffles.
526///
Nate Begeman9008ca62009-04-27 18:41:29 +0000527static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnercaad1632006-04-06 22:02:42 +0000528 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000530 "PPC only supports shuffles by bytes!");
Chris Lattner116cc482006-04-06 21:11:54 +0000531 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
532 "Unsupported merge size!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000533
Chris Lattner116cc482006-04-06 21:11:54 +0000534 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
535 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman9008ca62009-04-27 18:41:29 +0000536 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000537 LHSStart+j+i*UnitSize) ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000538 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000539 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000540 return false;
541 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000542 return true;
Chris Lattnercaad1632006-04-06 22:02:42 +0000543}
544
545/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
546/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Nate Begeman9008ca62009-04-27 18:41:29 +0000547bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
548 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000549 if (!isUnary)
550 return isVMerge(N, UnitSize, 8, 24);
551 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000552}
553
554/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
555/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Nate Begeman9008ca62009-04-27 18:41:29 +0000556bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
557 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000558 if (!isUnary)
559 return isVMerge(N, UnitSize, 0, 16);
560 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000561}
562
563
Chris Lattnerd0608e12006-04-06 18:26:28 +0000564/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
565/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000566int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000567 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000568 "PPC only supports shuffles by bytes!");
569
570 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
571
Chris Lattnerd0608e12006-04-06 18:26:28 +0000572 // Find the first non-undef value in the shuffle mask.
573 unsigned i;
Nate Begeman9008ca62009-04-27 18:41:29 +0000574 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattnerd0608e12006-04-06 18:26:28 +0000575 /*search*/;
Scott Michelfdc40a02009-02-17 22:15:04 +0000576
Chris Lattnerd0608e12006-04-06 18:26:28 +0000577 if (i == 16) return -1; // all undef.
Scott Michelfdc40a02009-02-17 22:15:04 +0000578
Nate Begeman9008ca62009-04-27 18:41:29 +0000579 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattnerd0608e12006-04-06 18:26:28 +0000580 // numbered from this value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000581 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattnerd0608e12006-04-06 18:26:28 +0000582 if (ShiftAmt < i) return -1;
583 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000584
Chris Lattnerf24380e2006-04-06 22:28:36 +0000585 if (!isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000586 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000587 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000588 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000589 return -1;
590 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000591 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000592 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000593 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000594 return -1;
595 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000596 return ShiftAmt;
597}
Chris Lattneref819f82006-03-20 06:33:01 +0000598
599/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
600/// specifies a splat of a single element that is suitable for input to
601/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman9008ca62009-04-27 18:41:29 +0000602bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner7ff7e672006-04-04 17:25:31 +0000604 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelfdc40a02009-02-17 22:15:04 +0000605
Chris Lattner88a99ef2006-03-20 06:37:44 +0000606 // This is a splat operation if each element of the permute is the same, and
607 // if the value doesn't reference the second vector.
Nate Begeman9008ca62009-04-27 18:41:29 +0000608 unsigned ElementBase = N->getMaskElt(0);
609
610 // FIXME: Handle UNDEF elements too!
611 if (ElementBase >= 16)
Chris Lattner7ff7e672006-04-04 17:25:31 +0000612 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000613
Nate Begeman9008ca62009-04-27 18:41:29 +0000614 // Check that the indices are consecutive, in the case of a multi-byte element
615 // splatted with a v16i8 mask.
616 for (unsigned i = 1; i != EltSize; ++i)
617 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000618 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000619
Chris Lattner7ff7e672006-04-04 17:25:31 +0000620 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000621 if (N->getMaskElt(i) < 0) continue;
Chris Lattner7ff7e672006-04-04 17:25:31 +0000622 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman9008ca62009-04-27 18:41:29 +0000623 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000624 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000625 }
Chris Lattner7ff7e672006-04-04 17:25:31 +0000626 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000627}
628
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000629/// isAllNegativeZeroVector - Returns true if all elements of build_vector
630/// are -0.0.
631bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000632 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
633
634 APInt APVal, APUndef;
635 unsigned BitSize;
636 bool HasAnyUndefs;
637
638 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32))
639 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000640 return CFP->getValueAPF().isNegZero();
Nate Begeman9008ca62009-04-27 18:41:29 +0000641
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000642 return false;
643}
644
Chris Lattneref819f82006-03-20 06:33:01 +0000645/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
646/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000647unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000648 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
649 assert(isSplatShuffleMask(SVOp, EltSize));
650 return SVOp->getMaskElt(0) / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000651}
652
Chris Lattnere87192a2006-04-12 17:37:20 +0000653/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000654/// by using a vspltis[bhw] instruction of the specified element size, return
655/// the constant being splatted. The ByteSize field indicates the number of
656/// bytes of each element [124] -> [bhw].
Dan Gohman475871a2008-07-27 21:46:04 +0000657SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
658 SDValue OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000659
660 // If ByteSize of the splat is bigger than the element size of the
661 // build_vector, then we have a case where we are checking for a splat where
662 // multiple elements of the buildvector are folded together into a single
663 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
664 unsigned EltSize = 16/N->getNumOperands();
665 if (EltSize < ByteSize) {
666 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman475871a2008-07-27 21:46:04 +0000667 SDValue UniquedVals[4];
Chris Lattner79d9a882006-04-08 07:14:26 +0000668 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelfdc40a02009-02-17 22:15:04 +0000669
Chris Lattner79d9a882006-04-08 07:14:26 +0000670 // See if all of the elements in the buildvector agree across.
671 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
672 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
673 // If the element isn't a constant, bail fully out.
Dan Gohman475871a2008-07-27 21:46:04 +0000674 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000675
Scott Michelfdc40a02009-02-17 22:15:04 +0000676
Gabor Greifba36cb52008-08-28 21:40:38 +0000677 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000678 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
679 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000680 return SDValue(); // no match.
Chris Lattner79d9a882006-04-08 07:14:26 +0000681 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000682
Chris Lattner79d9a882006-04-08 07:14:26 +0000683 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
684 // either constant or undef values that are identical for each chunk. See
685 // if these chunks can form into a larger vspltis*.
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Chris Lattner79d9a882006-04-08 07:14:26 +0000687 // Check to see if all of the leading entries are either 0 or -1. If
688 // neither, then this won't fit into the immediate field.
689 bool LeadingZero = true;
690 bool LeadingOnes = true;
691 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000692 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelfdc40a02009-02-17 22:15:04 +0000693
Chris Lattner79d9a882006-04-08 07:14:26 +0000694 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
695 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
696 }
697 // Finally, check the least significant entry.
698 if (LeadingZero) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000699 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000701 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000702 if (Val < 16)
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattner79d9a882006-04-08 07:14:26 +0000704 }
705 if (LeadingOnes) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000706 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman7810bfe2008-09-26 21:54:37 +0000708 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000709 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattner79d9a882006-04-08 07:14:26 +0000711 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000712
Dan Gohman475871a2008-07-27 21:46:04 +0000713 return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000714 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000715
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000716 // Check to see if this buildvec has a single non-undef value in its elements.
717 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
718 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +0000719 if (OpVal.getNode() == 0)
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000720 OpVal = N->getOperand(i);
721 else if (OpVal != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000722 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000723 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000724
Gabor Greifba36cb52008-08-28 21:40:38 +0000725 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelfdc40a02009-02-17 22:15:04 +0000726
Eli Friedman1a8229b2009-05-24 02:03:36 +0000727 unsigned ValSizeInBytes = EltSize;
Nate Begeman98e70cc2006-03-28 04:15:58 +0000728 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000729 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000730 Value = CN->getZExtValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000731 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000732 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000733 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000734 }
735
736 // If the splat value is larger than the element value, then we can never do
737 // this splat. The only case that we could fit the replicated bits into our
738 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman475871a2008-07-27 21:46:04 +0000739 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000740
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000741 // If the element value is larger than the splat value, cut it in half and
742 // check to see if the two halves are equal. Continue doing this until we
743 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
744 while (ValSizeInBytes > ByteSize) {
745 ValSizeInBytes >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000746
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000747 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000748 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
749 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman475871a2008-07-27 21:46:04 +0000750 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000751 }
752
753 // Properly sign extend the value.
754 int ShAmt = (4-ByteSize)*8;
755 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
Scott Michelfdc40a02009-02-17 22:15:04 +0000756
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000757 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman475871a2008-07-27 21:46:04 +0000758 if (MaskVal == 0) return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000759
Chris Lattner140a58f2006-04-08 06:46:53 +0000760 // Finally, if this value fits in a 5 bit sext field, return it
761 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000763 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000764}
765
Chris Lattner1a635d62006-04-14 06:01:58 +0000766//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000767// Addressing Mode Selection
768//===----------------------------------------------------------------------===//
769
770/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
771/// or 64-bit immediate, and if the value can be accurately represented as a
772/// sign extension from a 16-bit value. If so, this returns true and the
773/// immediate.
774static bool isIntS16Immediate(SDNode *N, short &Imm) {
775 if (N->getOpcode() != ISD::Constant)
776 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000777
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000778 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000780 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000781 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000782 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000783}
Dan Gohman475871a2008-07-27 21:46:04 +0000784static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000785 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000786}
787
788
789/// SelectAddressRegReg - Given the specified addressed, check to see if it
790/// can be represented as an indexed [r+r] operation. Returns false if it
791/// can be more efficiently represented with [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +0000792bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
793 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000794 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000795 short imm = 0;
796 if (N.getOpcode() == ISD::ADD) {
797 if (isIntS16Immediate(N.getOperand(1), imm))
798 return false; // r+i
799 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
800 return false; // r+i
Scott Michelfdc40a02009-02-17 22:15:04 +0000801
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000802 Base = N.getOperand(0);
803 Index = N.getOperand(1);
804 return true;
805 } else if (N.getOpcode() == ISD::OR) {
806 if (isIntS16Immediate(N.getOperand(1), imm))
807 return false; // r+i can fold it if we can.
Scott Michelfdc40a02009-02-17 22:15:04 +0000808
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000809 // If this is an or of disjoint bitfields, we can codegen this as an add
810 // (for better address arithmetic) if the LHS and RHS of the OR are provably
811 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000812 APInt LHSKnownZero, LHSKnownOne;
813 APInt RHSKnownZero, RHSKnownOne;
814 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanec59b952008-02-27 21:12:32 +0000815 APInt::getAllOnesValue(N.getOperand(0)
816 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000817 LHSKnownZero, LHSKnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +0000818
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000819 if (LHSKnownZero.getBoolValue()) {
820 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanec59b952008-02-27 21:12:32 +0000821 APInt::getAllOnesValue(N.getOperand(1)
822 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000823 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000824 // If all of the bits are known zero on the LHS or RHS, the add won't
825 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +0000826 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000827 Base = N.getOperand(0);
828 Index = N.getOperand(1);
829 return true;
830 }
831 }
832 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000833
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000834 return false;
835}
836
837/// Returns true if the address N can be represented by a base register plus
838/// a signed 16-bit displacement [r+imm], and if it is not better
839/// represented as reg+reg.
Dan Gohman475871a2008-07-27 21:46:04 +0000840bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman73e09142009-01-15 16:29:45 +0000841 SDValue &Base,
842 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000843 // FIXME dl should come from parent load or store, not from address
844 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000845 // If this can be more profitably realized as r+r, fail.
846 if (SelectAddressRegReg(N, Disp, Base, DAG))
847 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000848
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000849 if (N.getOpcode() == ISD::ADD) {
850 short imm = 0;
851 if (isIntS16Immediate(N.getOperand(1), imm)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000853 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
854 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
855 } else {
856 Base = N.getOperand(0);
857 }
858 return true; // [r+i]
859 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
860 // Match LOAD (ADD (X, Lo(G))).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000861 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000862 && "Cannot handle constant offsets yet!");
863 Disp = N.getOperand(1).getOperand(0); // The global address.
864 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
865 Disp.getOpcode() == ISD::TargetConstantPool ||
866 Disp.getOpcode() == ISD::TargetJumpTable);
867 Base = N.getOperand(0);
868 return true; // [&g+r]
869 }
870 } else if (N.getOpcode() == ISD::OR) {
871 short imm = 0;
872 if (isIntS16Immediate(N.getOperand(1), imm)) {
873 // If this is an or of disjoint bitfields, we can codegen this as an add
874 // (for better address arithmetic) if the LHS and RHS of the OR are
875 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000876 APInt LHSKnownZero, LHSKnownOne;
877 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000878 APInt::getAllOnesValue(N.getOperand(0)
879 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000880 LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +0000881
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000882 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000883 // If all of the bits are known zero on the LHS or RHS, the add won't
884 // carry.
885 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000887 return true;
888 }
889 }
890 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
891 // Loading from a constant address.
Scott Michelfdc40a02009-02-17 22:15:04 +0000892
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000893 // If this address fits entirely in a 16-bit sext immediate field, codegen
894 // this as "d, 0"
895 short Imm;
896 if (isIntS16Immediate(CN, Imm)) {
897 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
898 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
899 return true;
900 }
Chris Lattnerbc681d62007-02-17 06:44:03 +0000901
902 // Handle 32-bit sext immediates with LIS + addr mode.
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000904 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
905 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000906
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000907 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000909
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
911 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000912 Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000913 return true;
914 }
915 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000916
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000917 Disp = DAG.getTargetConstant(0, getPointerTy());
918 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
919 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
920 else
921 Base = N;
922 return true; // [r+0]
923}
924
925/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
926/// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000927bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
928 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000929 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000930 // Check to see if we can easily represent this as an [r+r] address. This
931 // will fail if it thinks that the address is more profitably represented as
932 // reg+imm, e.g. where imm = 0.
933 if (SelectAddressRegReg(N, Base, Index, DAG))
934 return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000935
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000936 // If the operand is an addition, always emit this as [r+r], since this is
937 // better (for code size, and execution, as the memop does the add for free)
938 // than emitting an explicit add.
939 if (N.getOpcode() == ISD::ADD) {
940 Base = N.getOperand(0);
941 Index = N.getOperand(1);
942 return true;
943 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000944
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000945 // Otherwise, do it the hard way, using R0 as the base register.
946 Base = DAG.getRegister(PPC::R0, N.getValueType());
947 Index = N;
948 return true;
949}
950
951/// SelectAddressRegImmShift - Returns true if the address N can be
952/// represented by a base register plus a signed 14-bit displacement
953/// [r+imm*4]. Suitable for use by STD and friends.
Dan Gohman475871a2008-07-27 21:46:04 +0000954bool PPCTargetLowering::SelectAddressRegImmShift(SDValue N, SDValue &Disp,
955 SDValue &Base,
Dan Gohman73e09142009-01-15 16:29:45 +0000956 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000957 // FIXME dl should come from the parent load or store, not the address
958 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000959 // If this can be more profitably realized as r+r, fail.
960 if (SelectAddressRegReg(N, Disp, Base, DAG))
961 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000962
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000963 if (N.getOpcode() == ISD::ADD) {
964 short imm = 0;
965 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000967 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
968 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
969 } else {
970 Base = N.getOperand(0);
971 }
972 return true; // [r+i]
973 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
974 // Match LOAD (ADD (X, Lo(G))).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000975 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000976 && "Cannot handle constant offsets yet!");
977 Disp = N.getOperand(1).getOperand(0); // The global address.
978 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
979 Disp.getOpcode() == ISD::TargetConstantPool ||
980 Disp.getOpcode() == ISD::TargetJumpTable);
981 Base = N.getOperand(0);
982 return true; // [&g+r]
983 }
984 } else if (N.getOpcode() == ISD::OR) {
985 short imm = 0;
986 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
987 // If this is an or of disjoint bitfields, we can codegen this as an add
988 // (for better address arithmetic) if the LHS and RHS of the OR are
989 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000990 APInt LHSKnownZero, LHSKnownOne;
991 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000992 APInt::getAllOnesValue(N.getOperand(0)
993 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000994 LHSKnownZero, LHSKnownOne);
995 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000996 // If all of the bits are known zero on the LHS or RHS, the add won't
997 // carry.
998 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000999 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001000 return true;
1001 }
1002 }
1003 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001004 // Loading from a constant address. Verify low two bits are clear.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001005 if ((CN->getZExtValue() & 3) == 0) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001006 // If this address fits entirely in a 14-bit sext immediate field, codegen
1007 // this as "d, 0"
1008 short Imm;
1009 if (isIntS16Immediate(CN, Imm)) {
1010 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
1011 Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
1012 return true;
1013 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001014
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001015 // Fold the low-part of 32-bit absolute addresses into addr mode.
Owen Anderson825b72b2009-08-11 20:47:22 +00001016 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001017 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
1018 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001019
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001020 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +00001021 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
1022 Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
1023 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dale Johannesenf5f5dce2009-02-06 19:16:40 +00001024 Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base),0);
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001025 return true;
1026 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001027 }
1028 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001029
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001030 Disp = DAG.getTargetConstant(0, getPointerTy());
1031 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1032 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1033 else
1034 Base = N;
1035 return true; // [r+0]
1036}
1037
1038
1039/// getPreIndexedAddressParts - returns true by value, base pointer and
1040/// offset pointer and addressing mode by reference if the node's address
1041/// can be legally represented as pre-indexed load / store address.
Dan Gohman475871a2008-07-27 21:46:04 +00001042bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1043 SDValue &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001044 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001045 SelectionDAG &DAG) const {
Chris Lattner4eab7142006-11-10 02:08:47 +00001046 // Disabled by default for now.
1047 if (!EnablePPCPreinc) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001048
Dan Gohman475871a2008-07-27 21:46:04 +00001049 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00001050 EVT VT;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001051 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1052 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001053 VT = LD->getMemoryVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001054
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001055 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner4eab7142006-11-10 02:08:47 +00001056 ST = ST;
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001057 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001058 VT = ST->getMemoryVT();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001059 } else
1060 return false;
1061
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001062 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001063 if (VT.isVector())
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001064 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001065
Chris Lattner0851b4f2006-11-15 19:55:13 +00001066 // TODO: Check reg+reg first.
Scott Michelfdc40a02009-02-17 22:15:04 +00001067
Chris Lattner0851b4f2006-11-15 19:55:13 +00001068 // LDU/STU use reg+imm*4, others use reg+imm.
Owen Anderson825b72b2009-08-11 20:47:22 +00001069 if (VT != MVT::i64) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001070 // reg + imm
1071 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
1072 return false;
1073 } else {
1074 // reg + imm * 4.
1075 if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
1076 return false;
1077 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001078
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001079 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001080 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1081 // sext i32 to i64 when addr mode is r+i.
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001083 LD->getExtensionType() == ISD::SEXTLOAD &&
1084 isa<ConstantSDNode>(Offset))
1085 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001086 }
1087
Chris Lattner4eab7142006-11-10 02:08:47 +00001088 AM = ISD::PRE_INC;
1089 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001090}
1091
1092//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001093// LowerOperation implementation
1094//===----------------------------------------------------------------------===//
1095
Scott Michelfdc40a02009-02-17 22:15:04 +00001096SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001097 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001098 EVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001099 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengc356a572006-09-12 21:04:05 +00001100 Constant *C = CP->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +00001101 SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
1102 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001103 // FIXME there isn't really any debug info here
1104 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00001105
1106 const TargetMachine &TM = DAG.getTarget();
Scott Michelfdc40a02009-02-17 22:15:04 +00001107
Dale Johannesende064702009-02-06 21:50:26 +00001108 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, CPI, Zero);
1109 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, CPI, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001110
Chris Lattner1a635d62006-04-14 06:01:58 +00001111 // If this is a non-darwin platform, we don't support non-static relo models
1112 // yet.
1113 if (TM.getRelocationModel() == Reloc::Static ||
1114 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1115 // Generate non-pic code that has direct accesses to the constant pool.
1116 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesende064702009-02-06 21:50:26 +00001117 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001119
Chris Lattner35d86fe2006-07-26 21:12:04 +00001120 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001121 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesende064702009-02-06 21:50:26 +00001122 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001123 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001124 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001125 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001126
Dale Johannesende064702009-02-06 21:50:26 +00001127 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001128 return Lo;
1129}
1130
Dan Gohman475871a2008-07-27 21:46:04 +00001131SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001132 EVT PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001133 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00001134 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1135 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001136 // FIXME there isn't really any debug loc here
1137 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001138
Nate Begeman37efe672006-04-22 18:53:45 +00001139 const TargetMachine &TM = DAG.getTarget();
Chris Lattner059ca0f2006-06-16 21:01:35 +00001140
Dale Johannesende064702009-02-06 21:50:26 +00001141 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, JTI, Zero);
1142 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, JTI, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001143
Nate Begeman37efe672006-04-22 18:53:45 +00001144 // If this is a non-darwin platform, we don't support non-static relo models
1145 // yet.
1146 if (TM.getRelocationModel() == Reloc::Static ||
1147 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1148 // Generate non-pic code that has direct accesses to the constant pool.
1149 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesende064702009-02-06 21:50:26 +00001150 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001151 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001152
Chris Lattner35d86fe2006-07-26 21:12:04 +00001153 if (TM.getRelocationModel() == Reloc::PIC_) {
Nate Begeman37efe672006-04-22 18:53:45 +00001154 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesende064702009-02-06 21:50:26 +00001155 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001156 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001157 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Nate Begeman37efe672006-04-22 18:53:45 +00001158 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001159
Dale Johannesende064702009-02-06 21:50:26 +00001160 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Nate Begeman37efe672006-04-22 18:53:45 +00001161 return Lo;
1162}
1163
Scott Michelfdc40a02009-02-17 22:15:04 +00001164SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00001165 SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001166 llvm_unreachable("TLS not implemented for PPC.");
Dan Gohman475871a2008-07-27 21:46:04 +00001167 return SDValue(); // Not reached
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001168}
1169
Scott Michelfdc40a02009-02-17 22:15:04 +00001170SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
Evan Chengee5c2b82009-01-16 22:57:32 +00001171 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001172 EVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001173 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
1174 GlobalValue *GV = GSDN->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00001175 SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00001176 SDValue Zero = DAG.getConstant(0, PtrVT);
Dale Johannesende064702009-02-06 21:50:26 +00001177 // FIXME there isn't really any debug info here
Dale Johannesen33c960f2009-02-04 20:06:27 +00001178 DebugLoc dl = GSDN->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001179
Chris Lattner1a635d62006-04-14 06:01:58 +00001180 const TargetMachine &TM = DAG.getTarget();
1181
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001182 // 64-bit SVR4 ABI code is always position-independent.
1183 // The actual address of the GlobalValue is stored in the TOC.
1184 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1185 return DAG.getNode(PPCISD::TOC_ENTRY, dl, MVT::i64, GA,
1186 DAG.getRegister(PPC::X2, MVT::i64));
1187 }
1188
Dale Johannesen33c960f2009-02-04 20:06:27 +00001189 SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, GA, Zero);
1190 SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, GA, Zero);
Chris Lattner059ca0f2006-06-16 21:01:35 +00001191
Chris Lattner1a635d62006-04-14 06:01:58 +00001192 // If this is a non-darwin platform, we don't support non-static relo models
1193 // yet.
1194 if (TM.getRelocationModel() == Reloc::Static ||
1195 !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
1196 // Generate non-pic code that has direct accesses to globals.
1197 // The address of the global is just (hi(&g)+lo(&g)).
Dale Johannesen33c960f2009-02-04 20:06:27 +00001198 return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Chris Lattner1a635d62006-04-14 06:01:58 +00001199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001200
Chris Lattner35d86fe2006-07-26 21:12:04 +00001201 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001202 // With PIC, the first instruction is actually "GR+hi(&G)".
Dale Johannesen33c960f2009-02-04 20:06:27 +00001203 Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00001204 DAG.getNode(PPCISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001205 DebugLoc::getUnknownLoc(), PtrVT), Hi);
Chris Lattner1a635d62006-04-14 06:01:58 +00001206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001207
Dale Johannesen33c960f2009-02-04 20:06:27 +00001208 Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
Scott Michelfdc40a02009-02-17 22:15:04 +00001209
Daniel Dunbar3be03402009-08-02 22:11:08 +00001210 if (!TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM))
Chris Lattner1a635d62006-04-14 06:01:58 +00001211 return Lo;
Scott Michelfdc40a02009-02-17 22:15:04 +00001212
Chris Lattner1a635d62006-04-14 06:01:58 +00001213 // If the global is weak or external, we have to go through the lazy
1214 // resolution stub.
Dale Johannesen33c960f2009-02-04 20:06:27 +00001215 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Lo, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00001216}
1217
Dan Gohman475871a2008-07-27 21:46:04 +00001218SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00001219 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001220 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001221
Chris Lattner1a635d62006-04-14 06:01:58 +00001222 // If we're comparing for equality to zero, expose the fact that this is
1223 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1224 // fold the new nodes.
1225 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1226 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Andersone50ed302009-08-10 22:56:29 +00001227 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001228 SDValue Zext = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 if (VT.bitsLT(MVT::i32)) {
1230 VT = MVT::i32;
Dale Johannesenf5d97892009-02-04 01:48:28 +00001231 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001232 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001233 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001234 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1235 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson825b72b2009-08-11 20:47:22 +00001236 DAG.getConstant(Log2b, MVT::i32));
1237 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner1a635d62006-04-14 06:01:58 +00001238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001239 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner1a635d62006-04-14 06:01:58 +00001240 // optimized. FIXME: revisit this when we can custom lower all setcc
1241 // optimizations.
1242 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman475871a2008-07-27 21:46:04 +00001243 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001244 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001245
Chris Lattner1a635d62006-04-14 06:01:58 +00001246 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001247 // by xor'ing the rhs with the lhs, which is faster than setting a
1248 // condition register, reading it back out, and masking the correct bit. The
1249 // normal approach here uses sub to do this instead of xor. Using xor exposes
1250 // the result to other bit-twiddling opportunities.
Owen Andersone50ed302009-08-10 22:56:29 +00001251 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001252 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001253 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001254 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001255 Op.getOperand(1));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001256 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner1a635d62006-04-14 06:01:58 +00001257 }
Dan Gohman475871a2008-07-27 21:46:04 +00001258 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001259}
1260
Dan Gohman475871a2008-07-27 21:46:04 +00001261SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Nicolas Geoffray01119992007-04-03 13:59:52 +00001262 int VarArgsFrameIndex,
1263 int VarArgsStackOffset,
1264 unsigned VarArgsNumGPR,
1265 unsigned VarArgsNumFPR,
1266 const PPCSubtarget &Subtarget) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001267
Torok Edwinc23197a2009-07-14 16:55:14 +00001268 llvm_unreachable("VAARG not yet implemented for the SVR4 ABI!");
Dan Gohman475871a2008-07-27 21:46:04 +00001269 return SDValue(); // Not reached
Nicolas Geoffray01119992007-04-03 13:59:52 +00001270}
1271
Bill Wendling77959322008-09-17 00:30:57 +00001272SDValue PPCTargetLowering::LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
1273 SDValue Chain = Op.getOperand(0);
1274 SDValue Trmp = Op.getOperand(1); // trampoline
1275 SDValue FPtr = Op.getOperand(2); // nested function
1276 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001277 DebugLoc dl = Op.getDebugLoc();
Bill Wendling77959322008-09-17 00:30:57 +00001278
Owen Andersone50ed302009-08-10 22:56:29 +00001279 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00001280 bool isPPC64 = (PtrVT == MVT::i64);
Bill Wendling77959322008-09-17 00:30:57 +00001281 const Type *IntPtrTy =
Owen Anderson1d0be152009-08-13 21:58:54 +00001282 DAG.getTargetLoweringInfo().getTargetData()->getIntPtrType(
1283 *DAG.getContext());
Bill Wendling77959322008-09-17 00:30:57 +00001284
Scott Michelfdc40a02009-02-17 22:15:04 +00001285 TargetLowering::ArgListTy Args;
Bill Wendling77959322008-09-17 00:30:57 +00001286 TargetLowering::ArgListEntry Entry;
1287
1288 Entry.Ty = IntPtrTy;
1289 Entry.Node = Trmp; Args.push_back(Entry);
1290
1291 // TrampSize == (isPPC64 ? 48 : 40);
1292 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling77959322008-09-17 00:30:57 +00001294 Args.push_back(Entry);
1295
1296 Entry.Node = FPtr; Args.push_back(Entry);
1297 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelfdc40a02009-02-17 22:15:04 +00001298
Bill Wendling77959322008-09-17 00:30:57 +00001299 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
1300 std::pair<SDValue, SDValue> CallResult =
Owen Anderson23b9b192009-08-12 00:36:31 +00001301 LowerCallTo(Chain, Op.getValueType().getTypeForEVT(*DAG.getContext()),
Owen Andersond1474d02009-07-09 17:57:24 +00001302 false, false, false, false, 0, CallingConv::C, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001303 /*isReturnValueUsed=*/true,
Bill Wendling77959322008-09-17 00:30:57 +00001304 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Dale Johannesen7d2ad622009-01-30 23:10:59 +00001305 Args, DAG, dl);
Bill Wendling77959322008-09-17 00:30:57 +00001306
1307 SDValue Ops[] =
1308 { CallResult.first, CallResult.second };
1309
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00001310 return DAG.getMergeValues(Ops, 2, dl);
Bill Wendling77959322008-09-17 00:30:57 +00001311}
1312
Dan Gohman475871a2008-07-27 21:46:04 +00001313SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Bill Wendling77959322008-09-17 00:30:57 +00001314 int VarArgsFrameIndex,
1315 int VarArgsStackOffset,
1316 unsigned VarArgsNumGPR,
1317 unsigned VarArgsNumFPR,
1318 const PPCSubtarget &Subtarget) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001319 DebugLoc dl = Op.getDebugLoc();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001320
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001321 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001322 // vastart just stores the address of the VarArgsFrameIndex slot into the
1323 // memory location argument.
Owen Andersone50ed302009-08-10 22:56:29 +00001324 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001325 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001326 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001327 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001328 }
1329
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001330 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray01119992007-04-03 13:59:52 +00001331 // We suppose the given va_list is already allocated.
1332 //
1333 // typedef struct {
1334 // char gpr; /* index into the array of 8 GPRs
1335 // * stored in the register save area
1336 // * gpr=0 corresponds to r3,
1337 // * gpr=1 to r4, etc.
1338 // */
1339 // char fpr; /* index into the array of 8 FPRs
1340 // * stored in the register save area
1341 // * fpr=0 corresponds to f1,
1342 // * fpr=1 to f2, etc.
1343 // */
1344 // char *overflow_arg_area;
1345 // /* location on stack that holds
1346 // * the next overflow argument
1347 // */
1348 // char *reg_save_area;
1349 // /* where r3:r10 and f1:f8 (if saved)
1350 // * are stored
1351 // */
1352 // } va_list[1];
1353
1354
Owen Anderson825b72b2009-08-11 20:47:22 +00001355 SDValue ArgGPR = DAG.getConstant(VarArgsNumGPR, MVT::i32);
1356 SDValue ArgFPR = DAG.getConstant(VarArgsNumFPR, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001357
Nicolas Geoffray01119992007-04-03 13:59:52 +00001358
Owen Andersone50ed302009-08-10 22:56:29 +00001359 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00001360
Dan Gohman475871a2008-07-27 21:46:04 +00001361 SDValue StackOffsetFI = DAG.getFrameIndex(VarArgsStackOffset, PtrVT);
1362 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001363
Duncan Sands83ec4b62008-06-06 12:08:01 +00001364 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman475871a2008-07-27 21:46:04 +00001365 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001366
Duncan Sands83ec4b62008-06-06 12:08:01 +00001367 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001368 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001369
1370 uint64_t FPROffset = 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001371 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001372
Dan Gohman69de1932008-02-06 22:27:42 +00001373 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001374
Nicolas Geoffray01119992007-04-03 13:59:52 +00001375 // Store first byte : number of int regs
Tilmann Schellerffd02002009-07-03 06:45:56 +00001376 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Owen Anderson825b72b2009-08-11 20:47:22 +00001377 Op.getOperand(1), SV, 0, MVT::i8);
Dan Gohman69de1932008-02-06 22:27:42 +00001378 uint64_t nextOffset = FPROffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001379 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray01119992007-04-03 13:59:52 +00001380 ConstFPROffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001381
Nicolas Geoffray01119992007-04-03 13:59:52 +00001382 // Store second byte : number of float regs
Dan Gohman475871a2008-07-27 21:46:04 +00001383 SDValue secondStore =
Owen Anderson825b72b2009-08-11 20:47:22 +00001384 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, SV, nextOffset, MVT::i8);
Dan Gohman69de1932008-02-06 22:27:42 +00001385 nextOffset += StackOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001386 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001387
Nicolas Geoffray01119992007-04-03 13:59:52 +00001388 // Store second word : arguments given on stack
Dan Gohman475871a2008-07-27 21:46:04 +00001389 SDValue thirdStore =
Dale Johannesen33c960f2009-02-04 20:06:27 +00001390 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, SV, nextOffset);
Dan Gohman69de1932008-02-06 22:27:42 +00001391 nextOffset += FrameOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001392 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001393
1394 // Store third word : arguments given in registers
Dale Johannesen33c960f2009-02-04 20:06:27 +00001395 return DAG.getStore(thirdStore, dl, FR, nextPtr, SV, nextOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001396
Chris Lattner1a635d62006-04-14 06:01:58 +00001397}
1398
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001399#include "PPCGenCallingConv.inc"
1400
Owen Andersone50ed302009-08-10 22:56:29 +00001401static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001402 CCValAssign::LocInfo &LocInfo,
1403 ISD::ArgFlagsTy &ArgFlags,
1404 CCState &State) {
1405 return true;
1406}
1407
Owen Andersone50ed302009-08-10 22:56:29 +00001408static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, EVT &ValVT,
1409 EVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001410 CCValAssign::LocInfo &LocInfo,
1411 ISD::ArgFlagsTy &ArgFlags,
1412 CCState &State) {
1413 static const unsigned ArgRegs[] = {
1414 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1415 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1416 };
1417 const unsigned NumArgRegs = array_lengthof(ArgRegs);
1418
1419 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1420
1421 // Skip one register if the first unallocated register has an even register
1422 // number and there are still argument registers available which have not been
1423 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1424 // need to skip a register if RegNum is odd.
1425 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1426 State.AllocateReg(ArgRegs[RegNum]);
1427 }
1428
1429 // Always return false here, as this function only makes sure that the first
1430 // unallocated register has an odd register number and does not actually
1431 // allocate a register for the current argument.
1432 return false;
1433}
1434
Owen Andersone50ed302009-08-10 22:56:29 +00001435static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, EVT &ValVT,
1436 EVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001437 CCValAssign::LocInfo &LocInfo,
1438 ISD::ArgFlagsTy &ArgFlags,
1439 CCState &State) {
1440 static const unsigned ArgRegs[] = {
1441 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1442 PPC::F8
1443 };
1444
1445 const unsigned NumArgRegs = array_lengthof(ArgRegs);
1446
1447 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1448
1449 // If there is only one Floating-point register left we need to put both f64
1450 // values of a split ppc_fp128 value on the stack.
1451 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1452 State.AllocateReg(ArgRegs[RegNum]);
1453 }
1454
1455 // Always return false here, as this function only makes sure that the two f64
1456 // values a ppc_fp128 value is split into are both passed in registers or both
1457 // passed on the stack and does not actually allocate a register for the
1458 // current argument.
1459 return false;
1460}
1461
Chris Lattner9f0bc652007-02-25 05:34:32 +00001462/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001463/// on Darwin.
1464static const unsigned *GetFPR() {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001465 static const unsigned FPR[] = {
1466 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001467 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner9f0bc652007-02-25 05:34:32 +00001468 };
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001469
Chris Lattner9f0bc652007-02-25 05:34:32 +00001470 return FPR;
1471}
1472
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001473/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1474/// the stack.
Owen Andersone50ed302009-08-10 22:56:29 +00001475static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001476 unsigned PtrByteSize) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001477 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001478 if (Flags.isByVal())
1479 ArgSize = Flags.getByValSize();
1480 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1481
1482 return ArgSize;
1483}
1484
Dan Gohman475871a2008-07-27 21:46:04 +00001485SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001486PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001487 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488 const SmallVectorImpl<ISD::InputArg>
1489 &Ins,
1490 DebugLoc dl, SelectionDAG &DAG,
1491 SmallVectorImpl<SDValue> &InVals) {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001492 if (PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493 return LowerFormalArguments_SVR4(Chain, CallConv, isVarArg, Ins,
1494 dl, DAG, InVals);
1495 } else {
1496 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1497 dl, DAG, InVals);
1498 }
1499}
1500
1501SDValue
1502PPCTargetLowering::LowerFormalArguments_SVR4(
1503 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001504 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001505 const SmallVectorImpl<ISD::InputArg>
1506 &Ins,
1507 DebugLoc dl, SelectionDAG &DAG,
1508 SmallVectorImpl<SDValue> &InVals) {
1509
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001510 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001511 // +-----------------------------------+
1512 // +--> | Back chain |
1513 // | +-----------------------------------+
1514 // | | Floating-point register save area |
1515 // | +-----------------------------------+
1516 // | | General register save area |
1517 // | +-----------------------------------+
1518 // | | CR save word |
1519 // | +-----------------------------------+
1520 // | | VRSAVE save word |
1521 // | +-----------------------------------+
1522 // | | Alignment padding |
1523 // | +-----------------------------------+
1524 // | | Vector register save area |
1525 // | +-----------------------------------+
1526 // | | Local variable space |
1527 // | +-----------------------------------+
1528 // | | Parameter list area |
1529 // | +-----------------------------------+
1530 // | | LR save word |
1531 // | +-----------------------------------+
1532 // SP--> +--- | Back chain |
1533 // +-----------------------------------+
1534 //
1535 // Specifications:
1536 // System V Application Binary Interface PowerPC Processor Supplement
1537 // AltiVec Technology Programming Interface Manual
1538
1539 MachineFunction &MF = DAG.getMachineFunction();
1540 MachineFrameInfo *MFI = MF.getFrameInfo();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001541
Owen Andersone50ed302009-08-10 22:56:29 +00001542 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001543 // Potential tail calls could cause overwriting of argument stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001544 bool isImmutable = !(PerformTailCallOpt && (CallConv==CallingConv::Fast));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001545 unsigned PtrByteSize = 4;
1546
1547 // Assign locations to all of the incoming arguments.
1548 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001549 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1550 *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001551
1552 // Reserve space for the linkage area on the stack.
1553 CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
1554
Dan Gohman98ca4f22009-08-05 01:29:28 +00001555 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC_SVR4);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001556
1557 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1558 CCValAssign &VA = ArgLocs[i];
1559
1560 // Arguments stored in registers.
1561 if (VA.isRegLoc()) {
1562 TargetRegisterClass *RC;
Owen Andersone50ed302009-08-10 22:56:29 +00001563 EVT ValVT = VA.getValVT();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001564
Owen Anderson825b72b2009-08-11 20:47:22 +00001565 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001566 default:
Dan Gohman98ca4f22009-08-05 01:29:28 +00001567 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 case MVT::i32:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001569 RC = PPC::GPRCRegisterClass;
1570 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001571 case MVT::f32:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001572 RC = PPC::F4RCRegisterClass;
1573 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001574 case MVT::f64:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001575 RC = PPC::F8RCRegisterClass;
1576 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001577 case MVT::v16i8:
1578 case MVT::v8i16:
1579 case MVT::v4i32:
1580 case MVT::v4f32:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001581 RC = PPC::VRRCRegisterClass;
1582 break;
1583 }
1584
1585 // Transform the arguments stored in physical registers into virtual ones.
1586 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001587 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001588
Dan Gohman98ca4f22009-08-05 01:29:28 +00001589 InVals.push_back(ArgValue);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001590 } else {
1591 // Argument stored in memory.
1592 assert(VA.isMemLoc());
1593
1594 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
1595 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
1596 isImmutable);
1597
1598 // Create load nodes to retrieve arguments from the stack.
1599 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001600 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001601 }
1602 }
1603
1604 // Assign locations to all of the incoming aggregate by value arguments.
1605 // Aggregates passed by value are stored in the local variable space of the
1606 // caller's stack frame, right above the parameter list area.
1607 SmallVector<CCValAssign, 16> ByValArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608 CCState CCByValInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001609 ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001610
1611 // Reserve stack space for the allocations in CCInfo.
1612 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
1613
Dan Gohman98ca4f22009-08-05 01:29:28 +00001614 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001615
1616 // Area that is at least reserved in the caller of this function.
1617 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
1618
1619 // Set the size that is at least reserved in caller of this function. Tail
1620 // call optimized function's reserved stack space needs to be aligned so that
1621 // taking the difference between two stack areas will result in an aligned
1622 // stack.
1623 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1624
1625 MinReservedArea =
1626 std::max(MinReservedArea,
1627 PPCFrameInfo::getMinCallFrameSize(false, false));
1628
1629 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
1630 getStackAlignment();
1631 unsigned AlignMask = TargetAlign-1;
1632 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
1633
1634 FI->setMinReservedArea(MinReservedArea);
1635
1636 SmallVector<SDValue, 8> MemOps;
1637
1638 // If the function takes variable number of arguments, make a frame index for
1639 // the start of the first vararg value... for expansion of llvm.va_start.
1640 if (isVarArg) {
1641 static const unsigned GPArgRegs[] = {
1642 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1643 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1644 };
1645 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
1646
1647 static const unsigned FPArgRegs[] = {
1648 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1649 PPC::F8
1650 };
1651 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
1652
1653 VarArgsNumGPR = CCInfo.getFirstUnallocated(GPArgRegs, NumGPArgRegs);
1654 VarArgsNumFPR = CCInfo.getFirstUnallocated(FPArgRegs, NumFPArgRegs);
1655
1656 // Make room for NumGPArgRegs and NumFPArgRegs.
1657 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson825b72b2009-08-11 20:47:22 +00001658 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001659
1660 VarArgsStackOffset = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
1661 CCInfo.getNextStackOffset());
1662
1663 VarArgsFrameIndex = MFI->CreateStackObject(Depth, 8);
1664 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
1665
1666 // The fixed integer arguments of a variadic function are
1667 // stored to the VarArgsFrameIndex on the stack.
1668 unsigned GPRIndex = 0;
1669 for (; GPRIndex != VarArgsNumGPR; ++GPRIndex) {
1670 SDValue Val = DAG.getRegister(GPArgRegs[GPRIndex], PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001671 SDValue Store = DAG.getStore(Chain, dl, Val, FIN, NULL, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001672 MemOps.push_back(Store);
1673 // Increment the address by four for the next argument to store
1674 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
1675 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1676 }
1677
1678 // If this function is vararg, store any remaining integer argument regs
1679 // to their spots on the stack so that they may be loaded by deferencing the
1680 // result of va_next.
1681 for (; GPRIndex != NumGPArgRegs; ++GPRIndex) {
1682 unsigned VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
1683
Dan Gohman98ca4f22009-08-05 01:29:28 +00001684 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001685 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1686 MemOps.push_back(Store);
1687 // Increment the address by four for the next argument to store
1688 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
1689 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1690 }
1691
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001692 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
1693 // is set.
Tilmann Schellerffd02002009-07-03 06:45:56 +00001694
1695 // The double arguments are stored to the VarArgsFrameIndex
1696 // on the stack.
1697 unsigned FPRIndex = 0;
1698 for (FPRIndex = 0; FPRIndex != VarArgsNumFPR; ++FPRIndex) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 SDValue Val = DAG.getRegister(FPArgRegs[FPRIndex], MVT::f64);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001700 SDValue Store = DAG.getStore(Chain, dl, Val, FIN, NULL, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001701 MemOps.push_back(Store);
1702 // Increment the address by eight for the next argument to store
Owen Anderson825b72b2009-08-11 20:47:22 +00001703 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001704 PtrVT);
1705 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1706 }
1707
1708 for (; FPRIndex != NumFPArgRegs; ++FPRIndex) {
1709 unsigned VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
1710
Owen Anderson825b72b2009-08-11 20:47:22 +00001711 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001712 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
1713 MemOps.push_back(Store);
1714 // Increment the address by eight for the next argument to store
Owen Anderson825b72b2009-08-11 20:47:22 +00001715 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001716 PtrVT);
1717 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1718 }
1719 }
1720
1721 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00001722 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001723 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001724
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 return Chain;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001726}
1727
1728SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001729PPCTargetLowering::LowerFormalArguments_Darwin(
1730 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001731 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732 const SmallVectorImpl<ISD::InputArg>
1733 &Ins,
1734 DebugLoc dl, SelectionDAG &DAG,
1735 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001736 // TODO: add description of PPC stack frame format, or at least some docs.
1737 //
1738 MachineFunction &MF = DAG.getMachineFunction();
1739 MachineFrameInfo *MFI = MF.getFrameInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
Owen Andersone50ed302009-08-10 22:56:29 +00001741 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00001742 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001743 // Potential tail calls could cause overwriting of argument stack slots.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001744 bool isImmutable = !(PerformTailCallOpt && (CallConv==CallingConv::Fast));
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001745 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00001746
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001747 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001748 // Area that is at least reserved in caller of this function.
1749 unsigned MinReservedArea = ArgOffset;
1750
Chris Lattnerc91a4752006-06-26 22:48:35 +00001751 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001752 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1753 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1754 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001755 static const unsigned GPR_64[] = { // 64-bit registers.
1756 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1757 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1758 };
Scott Michelfdc40a02009-02-17 22:15:04 +00001759
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001760 static const unsigned *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00001761
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001762 static const unsigned VR[] = {
1763 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1764 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1765 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001766
Owen Anderson718cb662007-09-07 04:06:50 +00001767 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001768 const unsigned Num_FPR_Regs = 13;
Owen Anderson718cb662007-09-07 04:06:50 +00001769 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00001770
1771 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00001772
Chris Lattnerc91a4752006-06-26 22:48:35 +00001773 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelfdc40a02009-02-17 22:15:04 +00001774
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001775 // In 32-bit non-varargs functions, the stack space for vectors is after the
1776 // stack space for non-vectors. We do not use this space unless we have
1777 // too many vectors to fit in registers, something that only occurs in
Scott Michelfdc40a02009-02-17 22:15:04 +00001778 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001779 // that out...for the pathological case, compute VecArgOffset as the
1780 // start of the vector parameter area. Computing VecArgOffset is the
1781 // entire point of the following loop.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001782 unsigned VecArgOffset = ArgOffset;
1783 if (!isVarArg && !isPPC64) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001785 ++ArgNo) {
Owen Andersone50ed302009-08-10 22:56:29 +00001786 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001787 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001788 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001789
Duncan Sands276dcbd2008-03-21 09:14:45 +00001790 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001791 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001792 ObjSize = Flags.getByValSize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001793 unsigned ArgSize =
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001794 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1795 VecArgOffset += ArgSize;
1796 continue;
1797 }
1798
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001800 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001801 case MVT::i32:
1802 case MVT::f32:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001803 VecArgOffset += isPPC64 ? 8 : 4;
1804 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 case MVT::i64: // PPC64
1806 case MVT::f64:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001807 VecArgOffset += 8;
1808 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001809 case MVT::v4f32:
1810 case MVT::v4i32:
1811 case MVT::v8i16:
1812 case MVT::v16i8:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001813 // Nothing to do, we're only looking at Nonvector args here.
1814 break;
1815 }
1816 }
1817 }
1818 // We've found where the vector parameter area in memory is. Skip the
1819 // first 12 parameters; these don't use that memory.
1820 VecArgOffset = ((VecArgOffset+15)/16)*16;
1821 VecArgOffset += 12*16;
1822
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001823 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00001824 // entry to a function on PPC, the arguments start after the linkage area,
1825 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001826
Dan Gohman475871a2008-07-27 21:46:04 +00001827 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001828 unsigned nAltivecParamsAtEnd = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001829 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001830 SDValue ArgVal;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001831 bool needsLoad = false;
Owen Andersone50ed302009-08-10 22:56:29 +00001832 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001833 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey619965d2006-11-29 13:37:09 +00001834 unsigned ArgSize = ObjSize;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001835 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001836
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001837 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001838
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001839 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
1841 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001842 if (isVarArg || isPPC64) {
1843 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001844 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohman095cc292008-09-13 01:54:27 +00001845 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001846 PtrByteSize);
1847 } else nAltivecParamsAtEnd++;
1848 } else
1849 // Calculate min reserved area.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001850 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohman095cc292008-09-13 01:54:27 +00001851 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001852 PtrByteSize);
1853
Dale Johannesen8419dd62008-03-07 20:27:40 +00001854 // FIXME the codegen can be much improved in some cases.
1855 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001856 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00001857 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001858 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00001859 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001860 // Objects of size 1 and 2 are right justified, everything else is
1861 // left justified. This means the memory address is adjusted forwards.
1862 if (ObjSize==1 || ObjSize==2) {
1863 CurArgOffset = CurArgOffset + (4 - ObjSize);
1864 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001865 // The value of the object is its address.
1866 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +00001867 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001868 InVals.push_back(FIN);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001869 if (ObjSize==1 || ObjSize==2) {
1870 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001871 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001873 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Owen Anderson825b72b2009-08-11 20:47:22 +00001874 NULL, 0, ObjSize==1 ? MVT::i8 : MVT::i16 );
Dale Johannesen7f96f392008-03-08 01:41:42 +00001875 MemOps.push_back(Store);
1876 ++GPR_idx;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001877 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001878
1879 ArgOffset += PtrByteSize;
1880
Dale Johannesen7f96f392008-03-08 01:41:42 +00001881 continue;
1882 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001883 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
1884 // Store whatever pieces of the object are in registers
1885 // to memory. ArgVal will be address of the beginning of
1886 // the object.
1887 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001888 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001889 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +00001890 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001891 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00001892 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00001893 MemOps.push_back(Store);
1894 ++GPR_idx;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001895 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001896 } else {
1897 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
1898 break;
1899 }
1900 }
1901 continue;
1902 }
1903
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001905 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001906 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001907 if (!isPPC64) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001908 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001909 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001911 ++GPR_idx;
1912 } else {
1913 needsLoad = true;
1914 ArgSize = PtrByteSize;
1915 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001916 // All int arguments reserve stack space in the Darwin ABI.
1917 ArgOffset += PtrByteSize;
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001918 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001919 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001920 // FALLTHROUGH
Owen Anderson825b72b2009-08-11 20:47:22 +00001921 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00001922 if (GPR_idx != Num_GPR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001923 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001924 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001925
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 if (ObjectVT == MVT::i32) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001927 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson825b72b2009-08-11 20:47:22 +00001928 // value to MVT::i64 and then truncate to the correct register size.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001929 if (Flags.isSExt())
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001931 DAG.getValueType(ObjectVT));
Duncan Sands276dcbd2008-03-21 09:14:45 +00001932 else if (Flags.isZExt())
Owen Anderson825b72b2009-08-11 20:47:22 +00001933 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001934 DAG.getValueType(ObjectVT));
1935
Owen Anderson825b72b2009-08-11 20:47:22 +00001936 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00001937 }
1938
Chris Lattnerc91a4752006-06-26 22:48:35 +00001939 ++GPR_idx;
1940 } else {
1941 needsLoad = true;
Evan Cheng982a0592008-07-24 08:17:07 +00001942 ArgSize = PtrByteSize;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001943 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001944 // All int arguments reserve stack space in the Darwin ABI.
1945 ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00001946 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001947
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 case MVT::f32:
1949 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001950 // Every 4 bytes of argument space consumes one of the GPRs available for
1951 // argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001952 if (GPR_idx != Num_GPR_Regs) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001953 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00001954 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001955 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001956 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001957 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001958 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001959
Owen Anderson825b72b2009-08-11 20:47:22 +00001960 if (ObjectVT == MVT::f32)
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001961 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001962 else
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001963 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
1964
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001966 ++FPR_idx;
1967 } else {
1968 needsLoad = true;
1969 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001970
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001971 // All FP arguments reserve stack space in the Darwin ABI.
1972 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001973 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001974 case MVT::v4f32:
1975 case MVT::v4i32:
1976 case MVT::v8i16:
1977 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00001978 // Note that vector arguments in registers don't reserve stack space,
1979 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00001980 if (VR_idx != Num_VR_Regs) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001981 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00001983 if (isVarArg) {
1984 while ((ArgOffset % 16) != 0) {
1985 ArgOffset += PtrByteSize;
1986 if (GPR_idx != Num_GPR_Regs)
1987 GPR_idx++;
1988 }
1989 ArgOffset += 16;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001990 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesen75092de2008-03-12 00:22:17 +00001991 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001992 ++VR_idx;
1993 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001994 if (!isVarArg && !isPPC64) {
1995 // Vectors go after all the nonvectors.
1996 CurArgOffset = VecArgOffset;
1997 VecArgOffset += 16;
1998 } else {
1999 // Vectors are aligned.
2000 ArgOffset = ((ArgOffset+15)/16)*16;
2001 CurArgOffset = ArgOffset;
2002 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00002003 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002004 needsLoad = true;
2005 }
2006 break;
2007 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002008
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002009 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002010 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002011 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002012 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002013 CurArgOffset + (ArgSize - ObjSize),
2014 isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00002015 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002016 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002017 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002018
Dan Gohman98ca4f22009-08-05 01:29:28 +00002019 InVals.push_back(ArgVal);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002020 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002021
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002022 // Set the size that is at least reserved in caller of this function. Tail
2023 // call optimized function's reserved stack space needs to be aligned so that
2024 // taking the difference between two stack areas will result in an aligned
2025 // stack.
2026 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2027 // Add the Altivec parameters at the end, if needed.
2028 if (nAltivecParamsAtEnd) {
2029 MinReservedArea = ((MinReservedArea+15)/16)*16;
2030 MinReservedArea += 16*nAltivecParamsAtEnd;
2031 }
2032 MinReservedArea =
2033 std::max(MinReservedArea,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002034 PPCFrameInfo::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002035 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
2036 getStackAlignment();
2037 unsigned AlignMask = TargetAlign-1;
2038 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2039 FI->setMinReservedArea(MinReservedArea);
2040
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002041 // If the function takes variable number of arguments, make a frame index for
2042 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002043 if (isVarArg) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002044 int Depth = ArgOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00002045
Duncan Sands83ec4b62008-06-06 12:08:01 +00002046 VarArgsFrameIndex = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002047 Depth);
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002049
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002050 // If this function is vararg, store any remaining integer argument regs
2051 // to their spots on the stack so that they may be loaded by deferencing the
2052 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002053 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002054 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002055
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002056 if (isPPC64)
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002057 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002058 else
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002059 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002060
Dan Gohman98ca4f22009-08-05 01:29:28 +00002061 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002062 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002063 MemOps.push_back(Store);
2064 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002065 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002066 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002067 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002068 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002069
Dale Johannesen8419dd62008-03-07 20:27:40 +00002070 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002071 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002072 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002073
Dan Gohman98ca4f22009-08-05 01:29:28 +00002074 return Chain;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002075}
2076
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002077/// CalculateParameterAndLinkageAreaSize - Get the size of the paramter plus
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002078/// linkage area for the Darwin ABI.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002079static unsigned
2080CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2081 bool isPPC64,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002082 bool isVarArg,
2083 unsigned CC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002084 const SmallVectorImpl<ISD::OutputArg>
2085 &Outs,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002086 unsigned &nAltivecParamsAtEnd) {
2087 // Count how many bytes are to be pushed on the stack, including the linkage
2088 // area, and parameter passing area. We start with 24/48 bytes, which is
2089 // prereserved space for [SP][CR][LR][3 x unused].
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002090 unsigned NumBytes = PPCFrameInfo::getLinkageSize(isPPC64, true);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002091 unsigned NumOps = Outs.size();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002092 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2093
2094 // Add up all the space actually used.
2095 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2096 // they all go in registers, but we must reserve stack space for them for
2097 // possible use by the caller. In varargs or 64-bit calls, parameters are
2098 // assigned stack space in order, with padding so Altivec parameters are
2099 // 16-byte aligned.
2100 nAltivecParamsAtEnd = 0;
2101 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002102 SDValue Arg = Outs[i].Val;
2103 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Owen Andersone50ed302009-08-10 22:56:29 +00002104 EVT ArgVT = Arg.getValueType();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002105 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002106 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2107 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002108 if (!isVarArg && !isPPC64) {
2109 // Non-varargs Altivec parameters go after all the non-Altivec
2110 // parameters; handle those later so we know how much padding we need.
2111 nAltivecParamsAtEnd++;
2112 continue;
2113 }
2114 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2115 NumBytes = ((NumBytes+15)/16)*16;
2116 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002117 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002118 }
2119
2120 // Allow for Altivec parameters at the end, if needed.
2121 if (nAltivecParamsAtEnd) {
2122 NumBytes = ((NumBytes+15)/16)*16;
2123 NumBytes += 16*nAltivecParamsAtEnd;
2124 }
2125
2126 // The prolog code of the callee may store up to 8 GPR argument registers to
2127 // the stack, allowing va_start to index over them in memory if its varargs.
2128 // Because we cannot tell if this is needed on the caller side, we have to
2129 // conservatively assume that it is needed. As such, make sure we have at
2130 // least enough stack space for the caller to store the 8 GPRs.
2131 NumBytes = std::max(NumBytes,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002132 PPCFrameInfo::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002133
2134 // Tail call needs the stack to be aligned.
2135 if (CC==CallingConv::Fast && PerformTailCallOpt) {
2136 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
2137 getStackAlignment();
2138 unsigned AlignMask = TargetAlign-1;
2139 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2140 }
2141
2142 return NumBytes;
2143}
2144
2145/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
2146/// adjusted to accomodate the arguments for the tailcall.
2147static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool IsTailCall,
2148 unsigned ParamSize) {
2149
2150 if (!IsTailCall) return 0;
2151
2152 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2153 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2154 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2155 // Remember only if the new adjustement is bigger.
2156 if (SPDiff < FI->getTailCallSPDelta())
2157 FI->setTailCallSPDelta(SPDiff);
2158
2159 return SPDiff;
2160}
2161
Dan Gohman98ca4f22009-08-05 01:29:28 +00002162/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2163/// for tail call optimization. Targets which want to do tail call
2164/// optimization should implement this function.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002165bool
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002167 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002168 bool isVarArg,
2169 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002170 SelectionDAG& DAG) const {
2171 // Variable argument functions are not supported.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002172 if (isVarArg)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002173 return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002174
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002176 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2178 // Functions containing by val parameters are not supported.
2179 for (unsigned i = 0; i != Ins.size(); i++) {
2180 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2181 if (Flags.isByVal()) return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002182 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183
2184 // Non PIC/GOT tail calls are supported.
2185 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2186 return true;
2187
2188 // At the moment we can only do local tail calls (in same module, hidden
2189 // or protected) if we are generating PIC.
2190 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2191 return G->getGlobal()->hasHiddenVisibility()
2192 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002193 }
2194
2195 return false;
2196}
2197
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002198/// isCallCompatibleAddress - Return the immediate to use if the specified
2199/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00002200static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002201 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2202 if (!C) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002203
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002204 int Addr = C->getZExtValue();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002205 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
2206 (Addr << 6 >> 6) != Addr)
2207 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002208
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002209 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greifba36cb52008-08-28 21:40:38 +00002210 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002211}
2212
Dan Gohman844731a2008-05-13 00:00:25 +00002213namespace {
2214
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002215struct TailCallArgumentInfo {
Dan Gohman475871a2008-07-27 21:46:04 +00002216 SDValue Arg;
2217 SDValue FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002218 int FrameIdx;
2219
2220 TailCallArgumentInfo() : FrameIdx(0) {}
2221};
2222
Dan Gohman844731a2008-05-13 00:00:25 +00002223}
2224
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002225/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
2226static void
2227StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002228 SDValue Chain,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002229 const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002230 SmallVector<SDValue, 8> &MemOpChains,
2231 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002232 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002233 SDValue Arg = TailCallArgs[i].Arg;
2234 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002235 int FI = TailCallArgs[i].FrameIdx;
2236 // Store relative to framepointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002237 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00002238 PseudoSourceValue::getFixedStack(FI),
2239 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002240 }
2241}
2242
2243/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
2244/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman475871a2008-07-27 21:46:04 +00002245static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002246 MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002247 SDValue Chain,
2248 SDValue OldRetAddr,
2249 SDValue OldFP,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002250 int SPDiff,
2251 bool isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002252 bool isDarwinABI,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002253 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002254 if (SPDiff) {
2255 // Calculate the new stack slot for the return address.
2256 int SlotSize = isPPC64 ? 8 : 4;
2257 int NewRetAddrLoc = SPDiff + PPCFrameInfo::getReturnSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002258 isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002259 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
2260 NewRetAddrLoc);
Owen Anderson825b72b2009-08-11 20:47:22 +00002261 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002262 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002263 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00002264 PseudoSourceValue::getFixedStack(NewRetAddr), 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002265
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002266 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
2267 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002268 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002269 int NewFPLoc =
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002270 SPDiff + PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002271 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc);
2272 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
2273 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
2274 PseudoSourceValue::getFixedStack(NewFPIdx), 0);
2275 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002276 }
2277 return Chain;
2278}
2279
2280/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
2281/// the position of the argument.
2282static void
2283CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman475871a2008-07-27 21:46:04 +00002284 SDValue Arg, int SPDiff, unsigned ArgOffset,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002285 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
2286 int Offset = ArgOffset + SPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002287 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002288 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Owen Anderson825b72b2009-08-11 20:47:22 +00002289 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002290 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002291 TailCallArgumentInfo Info;
2292 Info.Arg = Arg;
2293 Info.FrameIdxOp = FIN;
2294 Info.FrameIdx = FI;
2295 TailCallArguments.push_back(Info);
2296}
2297
2298/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
2299/// stack slot. Returns the chain as result and the loaded frame pointers in
2300/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman475871a2008-07-27 21:46:04 +00002301SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002302 int SPDiff,
2303 SDValue Chain,
2304 SDValue &LROpOut,
2305 SDValue &FPOpOut,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002306 bool isDarwinABI,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002307 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002308 if (SPDiff) {
2309 // Load the LR and FP stack slot for later adjusting.
Owen Anderson825b72b2009-08-11 20:47:22 +00002310 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002311 LROpOut = getReturnAddrFrameIndex(DAG);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002312 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002313 Chain = SDValue(LROpOut.getNode(), 1);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002314
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002315 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
2316 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002317 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002318 FPOpOut = getFramePointerFrameIndex(DAG);
2319 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, NULL, 0);
2320 Chain = SDValue(FPOpOut.getNode(), 1);
2321 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002322 }
2323 return Chain;
2324}
2325
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002326/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelfdc40a02009-02-17 22:15:04 +00002327/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002328/// specified by the specific parameter attribute. The copy will be passed as
2329/// a byval function parameter.
2330/// Sometimes what we are copying is the end of a larger object, the part that
2331/// does not fit in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00002332static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00002333CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00002334 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002335 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002336 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen8ad9b432009-02-04 01:17:06 +00002337 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
2338 false, NULL, 0, NULL, 0);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002339}
Chris Lattner9f0bc652007-02-25 05:34:32 +00002340
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002341/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
2342/// tail calls.
2343static void
Dan Gohman475871a2008-07-27 21:46:04 +00002344LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
2345 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002346 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Dan Gohman475871a2008-07-27 21:46:04 +00002347 bool isVector, SmallVector<SDValue, 8> &MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002348 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments,
2349 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002350 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002351 if (!isTailCall) {
2352 if (isVector) {
Dan Gohman475871a2008-07-27 21:46:04 +00002353 SDValue StackPtr;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002354 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002355 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002356 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002357 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002358 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002359 DAG.getConstant(ArgOffset, PtrVT));
2360 }
Dale Johannesen33c960f2009-02-04 20:06:27 +00002361 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002362 // Calculate and remember argument location.
2363 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
2364 TailCallArguments);
2365}
2366
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002367static
2368void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
2369 DebugLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
2370 SDValue LROp, SDValue FPOp, bool isDarwinABI,
2371 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) {
2372 MachineFunction &MF = DAG.getMachineFunction();
2373
2374 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
2375 // might overwrite each other in case of tail call optimization.
2376 SmallVector<SDValue, 8> MemOpChains2;
2377 // Do not flag preceeding copytoreg stuff together with the following stuff.
2378 InFlag = SDValue();
2379 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
2380 MemOpChains2, dl);
2381 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002382 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002383 &MemOpChains2[0], MemOpChains2.size());
2384
2385 // Store the return address to the appropriate stack slot.
2386 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
2387 isPPC64, isDarwinABI, dl);
2388
2389 // Emit callseq_end just before tailcall node.
2390 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2391 DAG.getIntPtrConstant(0, true), InFlag);
2392 InFlag = Chain.getValue(1);
2393}
2394
2395static
2396unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
2397 SDValue &Chain, DebugLoc dl, int SPDiff, bool isTailCall,
2398 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
Owen Andersone50ed302009-08-10 22:56:29 +00002399 SmallVector<SDValue, 8> &Ops, std::vector<EVT> &NodeTys,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002400 bool isSVR4ABI) {
Owen Andersone50ed302009-08-10 22:56:29 +00002401 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00002402 NodeTys.push_back(MVT::Other); // Returns a chain
2403 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002404
2405 unsigned CallOpc = isSVR4ABI ? PPCISD::CALL_SVR4 : PPCISD::CALL_Darwin;
2406
2407 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2408 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2409 // node so that legalize doesn't hack it.
2410 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2411 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
2412 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
2413 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType());
2414 else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
2415 // If this is an absolute destination address, use the munged value.
2416 Callee = SDValue(Dest, 0);
2417 else {
2418 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
2419 // to do the call, we can't use PPCISD::CALL.
2420 SDValue MTCTROps[] = {Chain, Callee, InFlag};
2421 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
2422 2 + (InFlag.getNode() != 0));
2423 InFlag = Chain.getValue(1);
2424
2425 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00002426 NodeTys.push_back(MVT::Other);
2427 NodeTys.push_back(MVT::Flag);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002428 Ops.push_back(Chain);
2429 CallOpc = isSVR4ABI ? PPCISD::BCTRL_SVR4 : PPCISD::BCTRL_Darwin;
2430 Callee.setNode(0);
2431 // Add CTR register as callee so a bctr can be emitted later.
2432 if (isTailCall)
2433 Ops.push_back(DAG.getRegister(PPC::CTR, PtrVT));
2434 }
2435
2436 // If this is a direct call, pass the chain and the callee.
2437 if (Callee.getNode()) {
2438 Ops.push_back(Chain);
2439 Ops.push_back(Callee);
2440 }
2441 // If this is a tail call add stack pointer delta.
2442 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002443 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002444
2445 // Add argument registers to the end of the list so that they are known live
2446 // into the call.
2447 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2448 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2449 RegsToPass[i].second.getValueType()));
2450
2451 return CallOpc;
2452}
2453
Dan Gohman98ca4f22009-08-05 01:29:28 +00002454SDValue
2455PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002456 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002457 const SmallVectorImpl<ISD::InputArg> &Ins,
2458 DebugLoc dl, SelectionDAG &DAG,
2459 SmallVectorImpl<SDValue> &InVals) {
2460
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002461 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002462 CCState CCRetInfo(CallConv, isVarArg, getTargetMachine(),
2463 RVLocs, *DAG.getContext());
2464 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002465
2466 // Copy all of the result registers out of their specified physreg.
2467 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
2468 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002469 EVT VT = VA.getValVT();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002470 assert(VA.isRegLoc() && "Can only return in registers!");
2471 Chain = DAG.getCopyFromReg(Chain, dl,
2472 VA.getLocReg(), VT, InFlag).getValue(1);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002473 InVals.push_back(Chain.getValue(0));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002474 InFlag = Chain.getValue(2);
2475 }
2476
Dan Gohman98ca4f22009-08-05 01:29:28 +00002477 return Chain;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002478}
2479
Dan Gohman98ca4f22009-08-05 01:29:28 +00002480SDValue
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002481PPCTargetLowering::FinishCall(CallingConv::ID CallConv, DebugLoc dl,
2482 bool isTailCall, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002483 SelectionDAG &DAG,
2484 SmallVector<std::pair<unsigned, SDValue>, 8>
2485 &RegsToPass,
2486 SDValue InFlag, SDValue Chain,
2487 SDValue &Callee,
2488 int SPDiff, unsigned NumBytes,
2489 const SmallVectorImpl<ISD::InputArg> &Ins,
2490 SmallVectorImpl<SDValue> &InVals) {
Owen Andersone50ed302009-08-10 22:56:29 +00002491 std::vector<EVT> NodeTys;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002492 SmallVector<SDValue, 8> Ops;
2493 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
2494 isTailCall, RegsToPass, Ops, NodeTys,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002495 PPCSubTarget.isSVR4ABI());
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002496
2497 // When performing tail call optimization the callee pops its arguments off
2498 // the stack. Account for this here so these bytes can be pushed back on in
2499 // PPCRegisterInfo::eliminateCallFramePseudoInstr.
2500 int BytesCalleePops =
Dan Gohman98ca4f22009-08-05 01:29:28 +00002501 (CallConv==CallingConv::Fast && PerformTailCallOpt) ? NumBytes : 0;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002502
2503 if (InFlag.getNode())
2504 Ops.push_back(InFlag);
2505
2506 // Emit tail call.
2507 if (isTailCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002508 // If this is the first return lowered for this function, add the regs
2509 // to the liveout set for the function.
2510 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
2511 SmallVector<CCValAssign, 16> RVLocs;
2512 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2513 *DAG.getContext());
2514 CCInfo.AnalyzeCallResult(Ins, RetCC_PPC);
2515 for (unsigned i = 0; i != RVLocs.size(); ++i)
2516 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2517 }
2518
2519 assert(((Callee.getOpcode() == ISD::Register &&
2520 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
2521 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2522 Callee.getOpcode() == ISD::TargetGlobalAddress ||
2523 isa<ConstantSDNode>(Callee)) &&
2524 "Expecting an global address, external symbol, absolute value or register");
2525
Owen Anderson825b72b2009-08-11 20:47:22 +00002526 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002527 }
2528
2529 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
2530 InFlag = Chain.getValue(1);
2531
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002532 // Add a NOP immediately after the branch instruction when using the 64-bit
2533 // SVR4 ABI. At link time, if caller and callee are in a different module and
2534 // thus have a different TOC, the call will be replaced with a call to a stub
2535 // function which saves the current TOC, loads the TOC of the callee and
2536 // branches to the callee. The NOP will be replaced with a load instruction
2537 // which restores the TOC of the caller from the TOC save slot of the current
2538 // stack frame. If caller and callee belong to the same module (and have the
2539 // same TOC), the NOP will remain unchanged.
2540 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
2541 // Insert NOP.
2542 InFlag = DAG.getNode(PPCISD::NOP, dl, MVT::Flag, InFlag);
2543 }
2544
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002545 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2546 DAG.getIntPtrConstant(BytesCalleePops, true),
2547 InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002548 if (!Ins.empty())
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002549 InFlag = Chain.getValue(1);
2550
Dan Gohman98ca4f22009-08-05 01:29:28 +00002551 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2552 Ins, dl, DAG, InVals);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002553}
2554
Dan Gohman98ca4f22009-08-05 01:29:28 +00002555SDValue
2556PPCTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002557 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002558 bool isTailCall,
2559 const SmallVectorImpl<ISD::OutputArg> &Outs,
2560 const SmallVectorImpl<ISD::InputArg> &Ins,
2561 DebugLoc dl, SelectionDAG &DAG,
2562 SmallVectorImpl<SDValue> &InVals) {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002563 if (PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002564 return LowerCall_SVR4(Chain, Callee, CallConv, isVarArg,
2565 isTailCall, Outs, Ins,
2566 dl, DAG, InVals);
2567 } else {
2568 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
2569 isTailCall, Outs, Ins,
2570 dl, DAG, InVals);
2571 }
2572}
2573
2574SDValue
2575PPCTargetLowering::LowerCall_SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002576 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002577 bool isTailCall,
2578 const SmallVectorImpl<ISD::OutputArg> &Outs,
2579 const SmallVectorImpl<ISD::InputArg> &Ins,
2580 DebugLoc dl, SelectionDAG &DAG,
2581 SmallVectorImpl<SDValue> &InVals) {
2582 // See PPCTargetLowering::LowerFormalArguments_SVR4() for a description
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002583 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002584
2585 assert((!isTailCall ||
2586 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
2587 "IsEligibleForTailCallOptimization missed a case!");
2588
2589 assert((CallConv == CallingConv::C ||
2590 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerffd02002009-07-03 06:45:56 +00002591
Owen Andersone50ed302009-08-10 22:56:29 +00002592 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerffd02002009-07-03 06:45:56 +00002593 unsigned PtrByteSize = 4;
2594
2595 MachineFunction &MF = DAG.getMachineFunction();
2596
2597 // Mark this function as potentially containing a function that contains a
2598 // tail call. As a consequence the frame pointer will be used for dynamicalloc
2599 // and restoring the callers stack pointer in this functions epilog. This is
2600 // done because by tail calling the called function might overwrite the value
2601 // in this function's (MF) stack pointer stack slot 0(SP).
Dan Gohman98ca4f22009-08-05 01:29:28 +00002602 if (PerformTailCallOpt && CallConv==CallingConv::Fast)
Tilmann Schellerffd02002009-07-03 06:45:56 +00002603 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
2604
2605 // Count how many bytes are to be pushed on the stack, including the linkage
2606 // area, parameter list area and the part of the local variable space which
2607 // contains copies of aggregates which are passed by value.
2608
2609 // Assign locations to all of the outgoing arguments.
2610 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002611 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2612 ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002613
2614 // Reserve space for the linkage area on the stack.
2615 CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
2616
2617 if (isVarArg) {
2618 // Handle fixed and variable vector arguments differently.
2619 // Fixed vector arguments go into registers as long as registers are
2620 // available. Variable vector arguments always go into memory.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002621 unsigned NumArgs = Outs.size();
Tilmann Schellerffd02002009-07-03 06:45:56 +00002622
2623 for (unsigned i = 0; i != NumArgs; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00002624 EVT ArgVT = Outs[i].Val.getValueType();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002625 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002626 bool Result;
2627
Dan Gohman98ca4f22009-08-05 01:29:28 +00002628 if (Outs[i].IsFixed) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002629 Result = CC_PPC_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
2630 CCInfo);
2631 } else {
2632 Result = CC_PPC_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
2633 ArgFlags, CCInfo);
2634 }
2635
2636 if (Result) {
Torok Edwindac237e2009-07-08 20:53:28 +00002637#ifndef NDEBUG
Chris Lattner45cfe542009-08-23 06:03:38 +00002638 errs() << "Call operand #" << i << " has unhandled type "
Owen Andersone50ed302009-08-10 22:56:29 +00002639 << ArgVT.getEVTString() << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +00002640#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00002641 llvm_unreachable(0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002642 }
2643 }
2644 } else {
2645 // All arguments are treated the same.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002646 CCInfo.AnalyzeCallOperands(Outs, CC_PPC_SVR4);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002647 }
2648
2649 // Assign locations to all of the outgoing aggregate by value arguments.
2650 SmallVector<CCValAssign, 16> ByValArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002651 CCState CCByValInfo(CallConv, isVarArg, getTargetMachine(), ByValArgLocs,
Owen Andersone922c022009-07-22 00:24:57 +00002652 *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002653
2654 // Reserve stack space for the allocations in CCInfo.
2655 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2656
Dan Gohman98ca4f22009-08-05 01:29:28 +00002657 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002658
2659 // Size of the linkage area, parameter list area and the part of the local
2660 // space variable where copies of aggregates which are passed by value are
2661 // stored.
2662 unsigned NumBytes = CCByValInfo.getNextStackOffset();
2663
2664 // Calculate by how many bytes the stack has to be adjusted in case of tail
2665 // call optimization.
2666 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
2667
2668 // Adjust the stack pointer for the new arguments...
2669 // These operations are automatically eliminated by the prolog/epilog pass
2670 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
2671 SDValue CallSeqStart = Chain;
2672
2673 // Load the return address and frame pointer so it can be moved somewhere else
2674 // later.
2675 SDValue LROp, FPOp;
2676 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
2677 dl);
2678
2679 // Set up a copy of the stack pointer for use loading and storing any
2680 // arguments that may not fit in the registers available for argument
2681 // passing.
Owen Anderson825b72b2009-08-11 20:47:22 +00002682 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002683
2684 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2685 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
2686 SmallVector<SDValue, 8> MemOpChains;
2687
2688 // Walk the register/memloc assignments, inserting copies/loads.
2689 for (unsigned i = 0, j = 0, e = ArgLocs.size();
2690 i != e;
2691 ++i) {
2692 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002693 SDValue Arg = Outs[i].Val;
2694 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002695
2696 if (Flags.isByVal()) {
2697 // Argument is an aggregate which is passed by value, thus we need to
2698 // create a copy of it in the local variable space of the current stack
2699 // frame (which is the stack frame of the caller) and pass the address of
2700 // this copy to the callee.
2701 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
2702 CCValAssign &ByValVA = ByValArgLocs[j++];
2703 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
2704
2705 // Memory reserved in the local variable space of the callers stack frame.
2706 unsigned LocMemOffset = ByValVA.getLocMemOffset();
2707
2708 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
2709 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
2710
2711 // Create a copy of the argument in the local area of the current
2712 // stack frame.
2713 SDValue MemcpyCall =
2714 CreateCopyOfByValArgument(Arg, PtrOff,
2715 CallSeqStart.getNode()->getOperand(0),
2716 Flags, DAG, dl);
2717
2718 // This must go outside the CALLSEQ_START..END.
2719 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
2720 CallSeqStart.getNode()->getOperand(1));
2721 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
2722 NewCallSeqStart.getNode());
2723 Chain = CallSeqStart = NewCallSeqStart;
2724
2725 // Pass the address of the aggregate copy on the stack either in a
2726 // physical register or in the parameter list area of the current stack
2727 // frame to the callee.
2728 Arg = PtrOff;
2729 }
2730
2731 if (VA.isRegLoc()) {
2732 // Put argument in a physical register.
2733 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2734 } else {
2735 // Put argument in the parameter list area of the current stack frame.
2736 assert(VA.isMemLoc());
2737 unsigned LocMemOffset = VA.getLocMemOffset();
2738
2739 if (!isTailCall) {
2740 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
2741 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
2742
2743 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2744 PseudoSourceValue::getStack(), LocMemOffset));
2745 } else {
2746 // Calculate and remember argument location.
2747 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
2748 TailCallArguments);
2749 }
2750 }
2751 }
2752
2753 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002754 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerffd02002009-07-03 06:45:56 +00002755 &MemOpChains[0], MemOpChains.size());
2756
2757 // Build a sequence of copy-to-reg nodes chained together with token chain
2758 // and flag operands which copy the outgoing args into the appropriate regs.
2759 SDValue InFlag;
2760 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2761 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
2762 RegsToPass[i].second, InFlag);
2763 InFlag = Chain.getValue(1);
2764 }
2765
2766 // Set CR6 to true if this is a vararg call.
2767 if (isVarArg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002768 SDValue SetCR(DAG.getTargetNode(PPC::CRSET, dl, MVT::i32), 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002769 Chain = DAG.getCopyToReg(Chain, dl, PPC::CR1EQ, SetCR, InFlag);
2770 InFlag = Chain.getValue(1);
2771 }
2772
Tilmann Schellerffd02002009-07-03 06:45:56 +00002773 if (isTailCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002774 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
2775 false, TailCallArguments);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002776 }
2777
Dan Gohman98ca4f22009-08-05 01:29:28 +00002778 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
2779 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
2780 Ins, InVals);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002781}
2782
Dan Gohman98ca4f22009-08-05 01:29:28 +00002783SDValue
2784PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002785 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002786 bool isTailCall,
2787 const SmallVectorImpl<ISD::OutputArg> &Outs,
2788 const SmallVectorImpl<ISD::InputArg> &Ins,
2789 DebugLoc dl, SelectionDAG &DAG,
2790 SmallVectorImpl<SDValue> &InVals) {
2791
2792 unsigned NumOps = Outs.size();
Scott Michelfdc40a02009-02-17 22:15:04 +00002793
Owen Andersone50ed302009-08-10 22:56:29 +00002794 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00002795 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002796 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00002797
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002798 MachineFunction &MF = DAG.getMachineFunction();
2799
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002800 // Mark this function as potentially containing a function that contains a
2801 // tail call. As a consequence the frame pointer will be used for dynamicalloc
2802 // and restoring the callers stack pointer in this functions epilog. This is
2803 // done because by tail calling the called function might overwrite the value
2804 // in this function's (MF) stack pointer stack slot 0(SP).
Dan Gohman98ca4f22009-08-05 01:29:28 +00002805 if (PerformTailCallOpt && CallConv==CallingConv::Fast)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002806 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
2807
2808 unsigned nAltivecParamsAtEnd = 0;
2809
Chris Lattnerabde4602006-05-16 22:56:08 +00002810 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00002811 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002812 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002813 unsigned NumBytes =
Dan Gohman98ca4f22009-08-05 01:29:28 +00002814 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
2815 Outs,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002816 nAltivecParamsAtEnd);
Dale Johannesen75092de2008-03-12 00:22:17 +00002817
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002818 // Calculate by how many bytes the stack has to be adjusted in case of tail
2819 // call optimization.
2820 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelfdc40a02009-02-17 22:15:04 +00002821
Dan Gohman98ca4f22009-08-05 01:29:28 +00002822 // To protect arguments on the stack from being clobbered in a tail call,
2823 // force all the loads to happen before doing any other lowering.
2824 if (isTailCall)
2825 Chain = DAG.getStackArgumentTokenFactor(Chain);
2826
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002827 // Adjust the stack pointer for the new arguments...
2828 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +00002829 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohman475871a2008-07-27 21:46:04 +00002830 SDValue CallSeqStart = Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00002831
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002832 // Load the return address and frame pointer so it can be move somewhere else
2833 // later.
Dan Gohman475871a2008-07-27 21:46:04 +00002834 SDValue LROp, FPOp;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002835 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
2836 dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002837
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002838 // Set up a copy of the stack pointer for use loading and storing any
2839 // arguments that may not fit in the registers available for argument
2840 // passing.
Dan Gohman475871a2008-07-27 21:46:04 +00002841 SDValue StackPtr;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002842 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002843 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002844 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002845 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00002846
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002847 // Figure out which arguments are going to go in registers, and which in
2848 // memory. Also, if this is a vararg function, floating point operations
2849 // must be stored to our stack, and loaded into integer regs as well, if
2850 // any integer regs are available for argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002851 unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, true);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002852 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002853
Chris Lattnerc91a4752006-06-26 22:48:35 +00002854 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00002855 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
2856 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
2857 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00002858 static const unsigned GPR_64[] = { // 64-bit registers.
2859 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
2860 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
2861 };
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002862 static const unsigned *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00002863
Chris Lattner9a2a4972006-05-17 06:01:33 +00002864 static const unsigned VR[] = {
2865 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
2866 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
2867 };
Owen Anderson718cb662007-09-07 04:06:50 +00002868 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002869 const unsigned NumFPRs = 13;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002870 const unsigned NumVRs = array_lengthof(VR);
Scott Michelfdc40a02009-02-17 22:15:04 +00002871
Chris Lattnerc91a4752006-06-26 22:48:35 +00002872 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
2873
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002874 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002875 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
2876
Dan Gohman475871a2008-07-27 21:46:04 +00002877 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00002878 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002879 SDValue Arg = Outs[i].Val;
2880 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002881
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002882 // PtrOff will be used to store the current argument to the stack if a
2883 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00002884 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00002885
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002886 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002887
Dale Johannesen39355f92009-02-04 02:34:38 +00002888 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002889
2890 // On PPC64, promote integers to 64-bit values.
Owen Anderson825b72b2009-08-11 20:47:22 +00002891 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00002892 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
2893 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson825b72b2009-08-11 20:47:22 +00002894 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002895 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002896
Dale Johannesen8419dd62008-03-07 20:27:40 +00002897 // FIXME memcpy is used way more than necessary. Correctness first.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002898 if (Flags.isByVal()) {
2899 unsigned Size = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00002900 if (Size==1 || Size==2) {
2901 // Very small objects are passed right-justified.
2902 // Everything else is passed left-justified.
Owen Anderson825b72b2009-08-11 20:47:22 +00002903 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002904 if (GPR_idx != NumGPRs) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002905 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Dale Johannesen8419dd62008-03-07 20:27:40 +00002906 NULL, 0, VT);
2907 MemOpChains.push_back(Load.getValue(1));
2908 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002909
2910 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002911 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00002912 SDValue Const = DAG.getConstant(4 - Size, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002913 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Dan Gohman475871a2008-07-27 21:46:04 +00002914 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr,
Scott Michelfdc40a02009-02-17 22:15:04 +00002915 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002916 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002917 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00002918 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00002919 CallSeqStart.getNode()->getOperand(1));
Gabor Greif93c53e52008-08-31 15:37:04 +00002920 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
2921 NewCallSeqStart.getNode());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002922 Chain = CallSeqStart = NewCallSeqStart;
2923 ArgOffset += PtrByteSize;
2924 }
2925 continue;
2926 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002927 // Copy entire object into memory. There are cases where gcc-generated
2928 // code assumes it is there, even if it could be put entirely into
2929 // registers. (This is not what the doc says.)
Dan Gohman475871a2008-07-27 21:46:04 +00002930 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00002931 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002932 Flags, DAG, dl);
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002933 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00002934 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00002935 CallSeqStart.getNode()->getOperand(1));
2936 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), NewCallSeqStart.getNode());
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002937 Chain = CallSeqStart = NewCallSeqStart;
2938 // And copy the pieces of it that fit into registers.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002939 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00002940 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002941 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002942 if (GPR_idx != NumGPRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002943 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, NULL, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00002944 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002945 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002946 ArgOffset += PtrByteSize;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002947 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002948 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002949 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002950 }
2951 }
2952 continue;
2953 }
2954
Owen Anderson825b72b2009-08-11 20:47:22 +00002955 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002956 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 case MVT::i32:
2958 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00002959 if (GPR_idx != NumGPRs) {
2960 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002961 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002962 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
2963 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002964 TailCallArguments, dl);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002965 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002966 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002967 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002968 case MVT::f32:
2969 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00002970 if (FPR_idx != NumFPRs) {
2971 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
2972
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002973 if (isVarArg) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002974 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002975 MemOpChains.push_back(Store);
2976
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002977 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00002978 if (GPR_idx != NumGPRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002979 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002980 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002981 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002982 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002983 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00002984 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002985 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
2986 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002987 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002988 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00002989 }
2990 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002991 // If we have any FPRs remaining, we may also have GPRs remaining.
2992 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
2993 // GPRs.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002994 if (GPR_idx != NumGPRs)
2995 ++GPR_idx;
Owen Anderson825b72b2009-08-11 20:47:22 +00002996 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002997 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
2998 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00002999 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003000 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003001 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3002 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003003 TailCallArguments, dl);
Chris Lattnerabde4602006-05-16 22:56:08 +00003004 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003005 if (isPPC64)
3006 ArgOffset += 8;
3007 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003008 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003009 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00003010 case MVT::v4f32:
3011 case MVT::v4i32:
3012 case MVT::v8i16:
3013 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00003014 if (isVarArg) {
3015 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00003016 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00003017 // V registers; in fact gcc does this only for arguments that are
3018 // prototyped, not for those that match the ... We do it for all
3019 // arguments, seems to work.
3020 while (ArgOffset % 16 !=0) {
3021 ArgOffset += PtrByteSize;
3022 if (GPR_idx != NumGPRs)
3023 GPR_idx++;
3024 }
3025 // We could elide this store in the case where the object fits
3026 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00003027 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00003028 DAG.getConstant(ArgOffset, PtrVT));
Dale Johannesen39355f92009-02-04 02:34:38 +00003029 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003030 MemOpChains.push_back(Store);
3031 if (VR_idx != NumVRs) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003032 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003033 MemOpChains.push_back(Load.getValue(1));
3034 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
3035 }
3036 ArgOffset += 16;
3037 for (unsigned i=0; i<16; i+=PtrByteSize) {
3038 if (GPR_idx == NumGPRs)
3039 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00003040 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00003041 DAG.getConstant(i, PtrVT));
Dale Johannesen39355f92009-02-04 02:34:38 +00003042 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003043 MemOpChains.push_back(Load.getValue(1));
3044 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3045 }
3046 break;
3047 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003048
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003049 // Non-varargs Altivec params generally go in registers, but have
3050 // stack space allocated at the end.
3051 if (VR_idx != NumVRs) {
3052 // Doesn't have GPR space allocated.
3053 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
3054 } else if (nAltivecParamsAtEnd==0) {
3055 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003056 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3057 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003058 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00003059 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00003060 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003061 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00003062 }
Chris Lattnerabde4602006-05-16 22:56:08 +00003063 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003064 // If all Altivec parameters fit in registers, as they usually do,
3065 // they get stack space following the non-Altivec parameters. We
3066 // don't track this here because nobody below needs it.
3067 // If there are more Altivec parameters than fit in registers emit
3068 // the stores here.
3069 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
3070 unsigned j = 0;
3071 // Offset is aligned; skip 1st 12 params which go in V registers.
3072 ArgOffset = ((ArgOffset+15)/16)*16;
3073 ArgOffset += 12*16;
3074 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003075 SDValue Arg = Outs[i].Val;
Owen Andersone50ed302009-08-10 22:56:29 +00003076 EVT ArgType = Arg.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00003077 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
3078 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003079 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00003080 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003081 // We are emitting Altivec params in order.
3082 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3083 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003084 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003085 ArgOffset += 16;
3086 }
3087 }
3088 }
3089 }
3090
Chris Lattner9a2a4972006-05-17 06:01:33 +00003091 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003092 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00003093 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00003094
Chris Lattner9a2a4972006-05-17 06:01:33 +00003095 // Build a sequence of copy-to-reg nodes chained together with token chain
3096 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00003097 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00003098 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003099 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00003100 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003101 InFlag = Chain.getValue(1);
3102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003103
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003104 if (isTailCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003105 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
3106 FPOp, true, TailCallArguments);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003107 }
3108
Dan Gohman98ca4f22009-08-05 01:29:28 +00003109 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3110 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3111 Ins, InVals);
Chris Lattnerabde4602006-05-16 22:56:08 +00003112}
3113
Dan Gohman98ca4f22009-08-05 01:29:28 +00003114SDValue
3115PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003116 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003117 const SmallVectorImpl<ISD::OutputArg> &Outs,
3118 DebugLoc dl, SelectionDAG &DAG) {
3119
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003120 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00003121 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
3122 RVLocs, *DAG.getContext());
3123 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00003124
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003125 // If this is the first return lowered for this function, add the regs to the
3126 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00003127 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003128 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00003129 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003130 }
3131
Dan Gohman475871a2008-07-27 21:46:04 +00003132 SDValue Flag;
Scott Michelfdc40a02009-02-17 22:15:04 +00003133
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003134 // Copy the result values into the output registers.
3135 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3136 CCValAssign &VA = RVLocs[i];
3137 assert(VA.isRegLoc() && "Can only return in registers!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003138 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00003139 Outs[i].Val, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003140 Flag = Chain.getValue(1);
3141 }
3142
Gabor Greifba36cb52008-08-28 21:40:38 +00003143 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003145 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003146 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner1a635d62006-04-14 06:01:58 +00003147}
3148
Dan Gohman475871a2008-07-27 21:46:04 +00003149SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Jim Laskeyefc7e522006-12-04 22:04:42 +00003150 const PPCSubtarget &Subtarget) {
3151 // When we pop the dynamic allocation we need to restore the SP link.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003152 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003153
Jim Laskeyefc7e522006-12-04 22:04:42 +00003154 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00003155 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00003156
3157 // Construct the stack pointer operand.
3158 bool IsPPC64 = Subtarget.isPPC64();
3159 unsigned SP = IsPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00003160 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003161
3162 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00003163 SDValue Chain = Op.getOperand(0);
3164 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003165
Jim Laskeyefc7e522006-12-04 22:04:42 +00003166 // Load the old link SP.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003167 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr, NULL, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003168
Jim Laskeyefc7e522006-12-04 22:04:42 +00003169 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003170 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00003171
Jim Laskeyefc7e522006-12-04 22:04:42 +00003172 // Store the old link SP.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003173 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, NULL, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003174}
3175
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003176
3177
Dan Gohman475871a2008-07-27 21:46:04 +00003178SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003179PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003180 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003181 bool IsPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003182 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00003183 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003184
3185 // Get current frame pointer save index. The users of this index will be
3186 // primarily DYNALLOC instructions.
3187 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3188 int RASI = FI->getReturnAddrSaveIndex();
3189
3190 // If the frame pointer save index hasn't been defined yet.
3191 if (!RASI) {
3192 // Find out what the fix offset of the frame pointer save area.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003193 int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64, isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003194 // Allocate the frame index for frame pointer save area.
3195 RASI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, LROffset);
3196 // Save the result.
3197 FI->setReturnAddrSaveIndex(RASI);
3198 }
3199 return DAG.getFrameIndex(RASI, PtrVT);
3200}
3201
Dan Gohman475871a2008-07-27 21:46:04 +00003202SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003203PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
3204 MachineFunction &MF = DAG.getMachineFunction();
3205 bool IsPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003206 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00003207 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003208
3209 // Get current frame pointer save index. The users of this index will be
3210 // primarily DYNALLOC instructions.
3211 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3212 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003213
Jim Laskey2f616bf2006-11-16 22:43:37 +00003214 // If the frame pointer save index hasn't been defined yet.
3215 if (!FPSI) {
3216 // Find out what the fix offset of the frame pointer save area.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003217 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64,
3218 isDarwinABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00003219
Jim Laskey2f616bf2006-11-16 22:43:37 +00003220 // Allocate the frame index for frame pointer save area.
Scott Michelfdc40a02009-02-17 22:15:04 +00003221 FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003222 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00003223 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003224 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003225 return DAG.getFrameIndex(FPSI, PtrVT);
3226}
Jim Laskey2f616bf2006-11-16 22:43:37 +00003227
Dan Gohman475871a2008-07-27 21:46:04 +00003228SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003229 SelectionDAG &DAG,
3230 const PPCSubtarget &Subtarget) {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003231 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00003232 SDValue Chain = Op.getOperand(0);
3233 SDValue Size = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003234 DebugLoc dl = Op.getDebugLoc();
3235
Jim Laskey2f616bf2006-11-16 22:43:37 +00003236 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00003237 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003238 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00003239 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00003240 DAG.getConstant(0, PtrVT), Size);
3241 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00003242 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003243 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00003244 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00003246 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003247}
3248
Chris Lattner1a635d62006-04-14 06:01:58 +00003249/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
3250/// possible.
Dan Gohman475871a2008-07-27 21:46:04 +00003251SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00003252 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003253 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
3254 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00003255 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003256
Chris Lattner1a635d62006-04-14 06:01:58 +00003257 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003258
Chris Lattner1a635d62006-04-14 06:01:58 +00003259 // Cannot handle SETEQ/SETNE.
Eli Friedmanc06441e2009-05-28 04:31:08 +00003260 if (CC == ISD::SETEQ || CC == ISD::SETNE) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003261
Owen Andersone50ed302009-08-10 22:56:29 +00003262 EVT ResVT = Op.getValueType();
3263 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003264 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3265 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00003266 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003267
Chris Lattner1a635d62006-04-14 06:01:58 +00003268 // If the RHS of the comparison is a 0.0, we don't need to do the
3269 // subtraction at all.
3270 if (isFloatingPointZero(RHS))
3271 switch (CC) {
3272 default: break; // SETUO etc aren't handled by fsel.
3273 case ISD::SETULT:
3274 case ISD::SETLT:
3275 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003276 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003277 case ISD::SETGE:
Owen Anderson825b72b2009-08-11 20:47:22 +00003278 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
3279 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00003280 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003281 case ISD::SETUGT:
3282 case ISD::SETGT:
3283 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003284 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003285 case ISD::SETLE:
Owen Anderson825b72b2009-08-11 20:47:22 +00003286 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
3287 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00003288 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003291
Dan Gohman475871a2008-07-27 21:46:04 +00003292 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00003293 switch (CC) {
3294 default: break; // SETUO etc aren't handled by fsel.
3295 case ISD::SETULT:
3296 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00003297 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3299 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003300 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003301 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003302 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00003303 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003304 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3305 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003306 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003307 case ISD::SETUGT:
3308 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00003309 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003310 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3311 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003312 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003313 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003314 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00003315 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3317 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003318 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003319 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00003320 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00003321}
3322
Chris Lattner1f873002007-11-28 18:44:47 +00003323// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003324SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00003325 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003326 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00003327 SDValue Src = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 if (Src.getValueType() == MVT::f32)
3329 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003330
Dan Gohman475871a2008-07-27 21:46:04 +00003331 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003333 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003335 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
3336 PPCISD::FCTIDZ,
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003338 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 case MVT::i64:
3340 Tmp = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003341 break;
3342 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00003343
Chris Lattner1a635d62006-04-14 06:01:58 +00003344 // Convert the FP value to an int value through memory.
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDValue FIPtr = DAG.CreateStackTemporary(MVT::f64);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003346
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003347 // Emit a store to the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003348 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, NULL, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003349
3350 // Result is a load from the stack slot. If loading 4 bytes, make sure to
3351 // add in a bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 if (Op.getValueType() == MVT::i32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003353 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003354 DAG.getConstant(4, FIPtr.getValueType()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00003355 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003356}
3357
Dan Gohman475871a2008-07-27 21:46:04 +00003358SDValue PPCTargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003359 DebugLoc dl = Op.getDebugLoc();
Dan Gohman034f60e2008-03-11 01:59:03 +00003360 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00003362 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00003363
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 if (Op.getOperand(0).getValueType() == MVT::i64) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003365 SDValue Bits = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003366 MVT::f64, Op.getOperand(0));
3367 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Bits);
3368 if (Op.getValueType() == MVT::f32)
Scott Michelfdc40a02009-02-17 22:15:04 +00003369 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003371 return FP;
3372 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003373
Owen Anderson825b72b2009-08-11 20:47:22 +00003374 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Chris Lattner1a635d62006-04-14 06:01:58 +00003375 "Unhandled SINT_TO_FP type in custom expander!");
3376 // Since we only generate this in 64-bit mode, we can take advantage of
3377 // 64-bit registers. In particular, sign extend the input value into the
3378 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
3379 // then lfd it and fcfid it.
3380 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3381 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
Owen Andersone50ed302009-08-10 22:56:29 +00003382 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003383 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003384
Owen Anderson825b72b2009-08-11 20:47:22 +00003385 SDValue Ext64 = DAG.getNode(PPCISD::EXTSW_32, dl, MVT::i32,
Chris Lattner1a635d62006-04-14 06:01:58 +00003386 Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003387
Chris Lattner1a635d62006-04-14 06:01:58 +00003388 // STD the extended value into the stack slot.
Dan Gohmana54cf172008-07-11 22:44:52 +00003389 MachineMemOperand MO(PseudoSourceValue::getFixedStack(FrameIdx),
3390 MachineMemOperand::MOStore, 0, 8, 8);
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 SDValue Store = DAG.getNode(PPCISD::STD_32, dl, MVT::Other,
Chris Lattner1a635d62006-04-14 06:01:58 +00003392 DAG.getEntryNode(), Ext64, FIdx,
Dan Gohman69de1932008-02-06 22:27:42 +00003393 DAG.getMemOperand(MO));
Chris Lattner1a635d62006-04-14 06:01:58 +00003394 // Load the value as a double.
Owen Anderson825b72b2009-08-11 20:47:22 +00003395 SDValue Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, NULL, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003396
Chris Lattner1a635d62006-04-14 06:01:58 +00003397 // FCFID it and return it.
Owen Anderson825b72b2009-08-11 20:47:22 +00003398 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Ld);
3399 if (Op.getValueType() == MVT::f32)
3400 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003401 return FP;
3402}
3403
Dan Gohman475871a2008-07-27 21:46:04 +00003404SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003405 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003406 /*
3407 The rounding mode is in bits 30:31 of FPSR, and has the following
3408 settings:
3409 00 Round to nearest
3410 01 Round to 0
3411 10 Round to +inf
3412 11 Round to -inf
3413
3414 FLT_ROUNDS, on the other hand, expects the following:
3415 -1 Undefined
3416 0 Round to 0
3417 1 Round to nearest
3418 2 Round to +inf
3419 3 Round to -inf
3420
3421 To perform the conversion, we do:
3422 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
3423 */
3424
3425 MachineFunction &MF = DAG.getMachineFunction();
Owen Andersone50ed302009-08-10 22:56:29 +00003426 EVT VT = Op.getValueType();
3427 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3428 std::vector<EVT> NodeTys;
Dan Gohman475871a2008-07-27 21:46:04 +00003429 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003430
3431 // Save FP Control Word to register
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 NodeTys.push_back(MVT::f64); // return register
3433 NodeTys.push_back(MVT::Flag); // unused in this context
Dale Johannesen33c960f2009-02-04 20:06:27 +00003434 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003435
3436 // Save FP register to stack slot
3437 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00003438 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003439 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003440 StackSlot, NULL, 0);
3441
3442 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00003443 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003444 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, NULL, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003446
3447 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00003448 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00003449 DAG.getNode(ISD::AND, dl, MVT::i32,
3450 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00003451 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00003452 DAG.getNode(ISD::SRL, dl, MVT::i32,
3453 DAG.getNode(ISD::AND, dl, MVT::i32,
3454 DAG.getNode(ISD::XOR, dl, MVT::i32,
3455 CWD, DAG.getConstant(3, MVT::i32)),
3456 DAG.getConstant(3, MVT::i32)),
3457 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003458
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00003460 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003461
Duncan Sands83ec4b62008-06-06 12:08:01 +00003462 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00003463 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003464}
3465
Dan Gohman475871a2008-07-27 21:46:04 +00003466SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003467 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003468 unsigned BitWidth = VT.getSizeInBits();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003469 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003470 assert(Op.getNumOperands() == 3 &&
3471 VT == Op.getOperand(1).getValueType() &&
3472 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003473
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00003474 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003475 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003476 SDValue Lo = Op.getOperand(0);
3477 SDValue Hi = Op.getOperand(1);
3478 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003479 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003480
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003481 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003482 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003483 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
3484 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
3485 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
3486 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003487 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003488 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
3489 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3490 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003491 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003492 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003493}
3494
Dan Gohman475871a2008-07-27 21:46:04 +00003495SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003496 EVT VT = Op.getValueType();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003497 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003498 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003499 assert(Op.getNumOperands() == 3 &&
3500 VT == Op.getOperand(1).getValueType() &&
3501 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003502
Dan Gohman9ed06db2008-03-07 20:36:53 +00003503 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003504 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003505 SDValue Lo = Op.getOperand(0);
3506 SDValue Hi = Op.getOperand(1);
3507 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003508 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003509
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003510 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003511 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003512 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3513 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3514 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3515 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003516 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003517 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
3518 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3519 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003520 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003521 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003522}
3523
Dan Gohman475871a2008-07-27 21:46:04 +00003524SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003525 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003526 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003527 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003528 assert(Op.getNumOperands() == 3 &&
3529 VT == Op.getOperand(1).getValueType() &&
3530 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003531
Dan Gohman9ed06db2008-03-07 20:36:53 +00003532 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00003533 SDValue Lo = Op.getOperand(0);
3534 SDValue Hi = Op.getOperand(1);
3535 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003536 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003537
Dale Johannesenf5d97892009-02-04 01:48:28 +00003538 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003539 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00003540 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3541 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3542 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3543 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003544 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00003545 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
3546 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
3547 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003548 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00003549 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003550 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003551}
3552
3553//===----------------------------------------------------------------------===//
3554// Vector related lowering.
3555//
3556
Chris Lattner4a998b92006-04-17 06:00:21 +00003557/// BuildSplatI - Build a canonical splati of Val with an element size of
3558/// SplatSize. Cast the result to VT.
Owen Andersone50ed302009-08-10 22:56:29 +00003559static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Dale Johannesened2eee62009-02-06 01:31:28 +00003560 SelectionDAG &DAG, DebugLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00003561 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00003562
Owen Andersone50ed302009-08-10 22:56:29 +00003563 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner4a998b92006-04-17 06:00:21 +00003565 };
Chris Lattner70fa4932006-12-01 01:45:39 +00003566
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003568
Chris Lattner70fa4932006-12-01 01:45:39 +00003569 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
3570 if (Val == -1)
3571 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00003572
Owen Andersone50ed302009-08-10 22:56:29 +00003573 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003574
Chris Lattner4a998b92006-04-17 06:00:21 +00003575 // Build a canonical splat for this value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003576 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003577 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003578 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00003579 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
3580 &Ops[0], Ops.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00003581 return DAG.getNode(ISD::BIT_CONVERT, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003582}
3583
Chris Lattnere7c768e2006-04-18 03:24:30 +00003584/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00003585/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003586static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Dale Johannesened2eee62009-02-06 01:31:28 +00003587 SelectionDAG &DAG, DebugLoc dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 EVT DestVT = MVT::Other) {
3589 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003590 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003591 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner6876e662006-04-17 06:58:41 +00003592}
3593
Chris Lattnere7c768e2006-04-18 03:24:30 +00003594/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
3595/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003596static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00003597 SDValue Op2, SelectionDAG &DAG,
Owen Anderson825b72b2009-08-11 20:47:22 +00003598 DebugLoc dl, EVT DestVT = MVT::Other) {
3599 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003600 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnere7c768e2006-04-18 03:24:30 +00003602}
3603
3604
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003605/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
3606/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003607static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Owen Andersone50ed302009-08-10 22:56:29 +00003608 EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003609 // Force LHS/RHS to be the right type.
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, LHS);
3611 RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00003612
Nate Begeman9008ca62009-04-27 18:41:29 +00003613 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003614 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003615 Ops[i] = i + Amt;
Owen Anderson825b72b2009-08-11 20:47:22 +00003616 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Dale Johannesened2eee62009-02-06 01:31:28 +00003617 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003618}
3619
Chris Lattnerf1b47082006-04-14 05:19:18 +00003620// If this is a case we can't handle, return null and let the default
3621// expansion code take care of it. If we CAN select this case, and if it
3622// selects to a single instruction, return Op. Otherwise, if we can codegen
3623// this case more efficiently than a constant pool load, lower it to the
3624// sequence of ops that should be used.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003625SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00003626 DebugLoc dl = Op.getDebugLoc();
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003627 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3628 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00003629
Bob Wilson24e338e2009-03-02 23:24:16 +00003630 // Check if this is a splat of a constant value.
3631 APInt APSplatBits, APSplatUndef;
3632 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003633 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00003634 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
3635 HasAnyUndefs) || SplatBitSize > 32)
3636 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00003637
Bob Wilsonf2950b02009-03-03 19:26:27 +00003638 unsigned SplatBits = APSplatBits.getZExtValue();
3639 unsigned SplatUndef = APSplatUndef.getZExtValue();
3640 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00003641
Bob Wilsonf2950b02009-03-03 19:26:27 +00003642 // First, handle single instruction cases.
3643
3644 // All zeros?
3645 if (SplatBits == 0) {
3646 // Canonicalize all zero vectors to be v4i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
3648 SDValue Z = DAG.getConstant(0, MVT::i32);
3649 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003650 Op = DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003651 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003652 return Op;
3653 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00003654
Bob Wilsonf2950b02009-03-03 19:26:27 +00003655 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
3656 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
3657 (32-SplatBitSize));
3658 if (SextVal >= -16 && SextVal <= 15)
3659 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003660
3661
Bob Wilsonf2950b02009-03-03 19:26:27 +00003662 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00003663
Bob Wilsonf2950b02009-03-03 19:26:27 +00003664 // If this value is in the range [-32,30] and is even, use:
3665 // tmp = VSPLTI[bhw], result = add tmp, tmp
3666 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003667 SDValue Res = BuildSplatI(SextVal >> 1, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003668 Res = DAG.getNode(ISD::ADD, dl, Res.getValueType(), Res, Res);
3669 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3670 }
3671
3672 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
3673 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
3674 // for fneg/fabs.
3675 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
3676 // Make -1 and vspltisw -1:
Owen Anderson825b72b2009-08-11 20:47:22 +00003677 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003678
3679 // Make the VSLW intrinsic, computing 0x8000_0000.
3680 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
3681 OnesV, DAG, dl);
3682
3683 // xor by OnesV to invert it.
Owen Anderson825b72b2009-08-11 20:47:22 +00003684 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003685 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3686 }
3687
3688 // Check to see if this is a wide variety of vsplti*, binop self cases.
3689 static const signed char SplatCsts[] = {
3690 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
3691 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
3692 };
3693
3694 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
3695 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
3696 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
3697 int i = SplatCsts[idx];
3698
3699 // Figure out what shift amount will be used by altivec if shifted by i in
3700 // this splat size.
3701 unsigned TypeShiftAmt = i & (SplatBitSize-1);
3702
3703 // vsplti + shl self.
3704 if (SextVal == (i << (int)TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003705 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003706 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3707 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
3708 Intrinsic::ppc_altivec_vslw
3709 };
3710 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Dale Johannesened2eee62009-02-06 01:31:28 +00003711 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003712 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003713
Bob Wilsonf2950b02009-03-03 19:26:27 +00003714 // vsplti + srl self.
3715 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003717 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3718 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
3719 Intrinsic::ppc_altivec_vsrw
3720 };
3721 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Dale Johannesened2eee62009-02-06 01:31:28 +00003722 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003723 }
3724
Bob Wilsonf2950b02009-03-03 19:26:27 +00003725 // vsplti + sra self.
3726 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003728 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3729 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
3730 Intrinsic::ppc_altivec_vsraw
3731 };
3732 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
3733 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003734 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003735
Bob Wilsonf2950b02009-03-03 19:26:27 +00003736 // vsplti + rol self.
3737 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
3738 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003739 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003740 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3741 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
3742 Intrinsic::ppc_altivec_vrlw
3743 };
3744 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
3745 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3746 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003747
Bob Wilsonf2950b02009-03-03 19:26:27 +00003748 // t = vsplti c, result = vsldoi t, t, 1
3749 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003750 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003751 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00003752 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003753 // t = vsplti c, result = vsldoi t, t, 2
3754 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003755 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003756 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003757 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003758 // t = vsplti c, result = vsldoi t, t, 3
3759 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003760 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003761 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
3762 }
3763 }
3764
3765 // Three instruction sequences.
3766
3767 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
3768 if (SextVal >= 0 && SextVal <= 31) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003769 SDValue LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG, dl);
3770 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003771 LHS = DAG.getNode(ISD::SUB, dl, LHS.getValueType(), LHS, RHS);
3772 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
3773 }
3774 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
3775 if (SextVal >= -31 && SextVal <= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003776 SDValue LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG, dl);
3777 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003778 LHS = DAG.getNode(ISD::ADD, dl, LHS.getValueType(), LHS, RHS);
3779 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003780 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003781
Dan Gohman475871a2008-07-27 21:46:04 +00003782 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00003783}
3784
Chris Lattner59138102006-04-17 05:28:54 +00003785/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3786/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00003787static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00003788 SDValue RHS, SelectionDAG &DAG,
Dale Johannesened2eee62009-02-06 01:31:28 +00003789 DebugLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00003790 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00003791 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00003792 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003793
Chris Lattner59138102006-04-17 05:28:54 +00003794 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00003795 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 +00003796 OP_VMRGHW,
3797 OP_VMRGLW,
3798 OP_VSPLTISW0,
3799 OP_VSPLTISW1,
3800 OP_VSPLTISW2,
3801 OP_VSPLTISW3,
3802 OP_VSLDOI4,
3803 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00003804 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00003805 };
Scott Michelfdc40a02009-02-17 22:15:04 +00003806
Chris Lattner59138102006-04-17 05:28:54 +00003807 if (OpNum == OP_COPY) {
3808 if (LHSID == (1*9+2)*9+3) return LHS;
3809 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3810 return RHS;
3811 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003812
Dan Gohman475871a2008-07-27 21:46:04 +00003813 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00003814 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3815 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003816
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00003818 switch (OpNum) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003819 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner59138102006-04-17 05:28:54 +00003820 case OP_VMRGHW:
3821 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
3822 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
3823 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
3824 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
3825 break;
3826 case OP_VMRGLW:
3827 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
3828 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
3829 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
3830 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
3831 break;
3832 case OP_VSPLTISW0:
3833 for (unsigned i = 0; i != 16; ++i)
3834 ShufIdxs[i] = (i&3)+0;
3835 break;
3836 case OP_VSPLTISW1:
3837 for (unsigned i = 0; i != 16; ++i)
3838 ShufIdxs[i] = (i&3)+4;
3839 break;
3840 case OP_VSPLTISW2:
3841 for (unsigned i = 0; i != 16; ++i)
3842 ShufIdxs[i] = (i&3)+8;
3843 break;
3844 case OP_VSPLTISW3:
3845 for (unsigned i = 0; i != 16; ++i)
3846 ShufIdxs[i] = (i&3)+12;
3847 break;
3848 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00003849 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003850 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00003851 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003852 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00003853 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003854 }
Owen Andersone50ed302009-08-10 22:56:29 +00003855 EVT VT = OpLHS.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 OpLHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpLHS);
3857 OpRHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpRHS);
3858 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Nate Begeman9008ca62009-04-27 18:41:29 +00003859 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00003860}
3861
Chris Lattnerf1b47082006-04-14 05:19:18 +00003862/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
3863/// is a shuffle we can handle in a single instruction, return it. Otherwise,
3864/// return the code it can be lowered into. Worst case, it can always be
3865/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00003866SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00003868 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003869 SDValue V1 = Op.getOperand(0);
3870 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00003872 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003873
Chris Lattnerf1b47082006-04-14 05:19:18 +00003874 // Cases that are handled by instructions that take permute immediates
3875 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
3876 // selected by the instruction selector.
3877 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003878 if (PPC::isSplatShuffleMask(SVOp, 1) ||
3879 PPC::isSplatShuffleMask(SVOp, 2) ||
3880 PPC::isSplatShuffleMask(SVOp, 4) ||
3881 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
3882 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
3883 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
3884 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
3885 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
3886 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
3887 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
3888 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
3889 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00003890 return Op;
3891 }
3892 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003893
Chris Lattnerf1b47082006-04-14 05:19:18 +00003894 // Altivec has a variety of "shuffle immediates" that take two vector inputs
3895 // and produce a fixed permutation. If any of these match, do not lower to
3896 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
3898 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
3899 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
3900 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
3901 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
3902 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
3903 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
3904 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
3905 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00003906 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003907
Chris Lattner59138102006-04-17 05:28:54 +00003908 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
3909 // perfect shuffle table to emit an optimal matching sequence.
Nate Begeman9008ca62009-04-27 18:41:29 +00003910 SmallVector<int, 16> PermMask;
3911 SVOp->getMask(PermMask);
3912
Chris Lattner59138102006-04-17 05:28:54 +00003913 unsigned PFIndexes[4];
3914 bool isFourElementShuffle = true;
3915 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
3916 unsigned EltNo = 8; // Start out undef.
3917 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00003918 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00003919 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00003920
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00003922 if ((ByteSource & 3) != j) {
3923 isFourElementShuffle = false;
3924 break;
3925 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003926
Chris Lattner59138102006-04-17 05:28:54 +00003927 if (EltNo == 8) {
3928 EltNo = ByteSource/4;
3929 } else if (EltNo != ByteSource/4) {
3930 isFourElementShuffle = false;
3931 break;
3932 }
3933 }
3934 PFIndexes[i] = EltNo;
3935 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003936
3937 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00003938 // perfect shuffle vector to determine if it is cost effective to do this as
3939 // discrete instructions, or whether we should use a vperm.
3940 if (isFourElementShuffle) {
3941 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00003942 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00003943 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00003944
Chris Lattner59138102006-04-17 05:28:54 +00003945 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3946 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00003947
Chris Lattner59138102006-04-17 05:28:54 +00003948 // Determining when to avoid vperm is tricky. Many things affect the cost
3949 // of vperm, particularly how many times the perm mask needs to be computed.
3950 // For example, if the perm mask can be hoisted out of a loop or is already
3951 // used (perhaps because there are multiple permutes with the same shuffle
3952 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
3953 // the loop requires an extra register.
3954 //
3955 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00003956 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00003957 // available, if this block is within a loop, we should avoid using vperm
3958 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00003959 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00003960 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003961 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003962
Chris Lattnerf1b47082006-04-14 05:19:18 +00003963 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
3964 // vector that will get spilled to the constant pool.
3965 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00003966
Chris Lattnerf1b47082006-04-14 05:19:18 +00003967 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
3968 // that it is in input element units, not in bytes. Convert now.
Owen Andersone50ed302009-08-10 22:56:29 +00003969 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003970 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00003971
Dan Gohman475871a2008-07-27 21:46:04 +00003972 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00003973 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
3974 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00003975
Chris Lattnerf1b47082006-04-14 05:19:18 +00003976 for (unsigned j = 0; j != BytesPerElement; ++j)
3977 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson825b72b2009-08-11 20:47:22 +00003978 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00003979 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003980
Owen Anderson825b72b2009-08-11 20:47:22 +00003981 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga87008d2009-02-25 22:49:59 +00003982 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00003983 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003984}
3985
Chris Lattner90564f22006-04-18 17:59:36 +00003986/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
3987/// altivec comparison. If it is, return true and fill in Opc/isDot with
3988/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00003989static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00003990 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003991 unsigned IntrinsicID =
3992 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00003993 CompareOpc = -1;
3994 isDot = false;
3995 switch (IntrinsicID) {
3996 default: return false;
3997 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00003998 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
3999 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
4000 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
4001 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
4002 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
4003 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
4004 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
4005 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
4006 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
4007 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
4008 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
4009 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
4010 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004011
Chris Lattner1a635d62006-04-14 06:01:58 +00004012 // Normal Comparisons.
4013 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
4014 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
4015 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
4016 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
4017 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
4018 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
4019 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
4020 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
4021 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
4022 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
4023 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
4024 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
4025 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
4026 }
Chris Lattner90564f22006-04-18 17:59:36 +00004027 return true;
4028}
4029
4030/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
4031/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00004032SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004033 SelectionDAG &DAG) {
Chris Lattner90564f22006-04-18 17:59:36 +00004034 // If this is a lowered altivec predicate compare, CompareOpc is set to the
4035 // opcode number of the comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00004036 DebugLoc dl = Op.getDebugLoc();
Chris Lattner90564f22006-04-18 17:59:36 +00004037 int CompareOpc;
4038 bool isDot;
4039 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00004040 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00004041
Chris Lattner90564f22006-04-18 17:59:36 +00004042 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00004043 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004044 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner1a635d62006-04-14 06:01:58 +00004045 Op.getOperand(1), Op.getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004046 DAG.getConstant(CompareOpc, MVT::i32));
Dale Johannesen3484c092009-02-05 22:07:54 +00004047 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00004048 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004049
Chris Lattner1a635d62006-04-14 06:01:58 +00004050 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00004051 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00004052 Op.getOperand(2), // LHS
4053 Op.getOperand(3), // RHS
Owen Anderson825b72b2009-08-11 20:47:22 +00004054 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00004055 };
Owen Andersone50ed302009-08-10 22:56:29 +00004056 std::vector<EVT> VTs;
Chris Lattner1a635d62006-04-14 06:01:58 +00004057 VTs.push_back(Op.getOperand(2).getValueType());
Owen Anderson825b72b2009-08-11 20:47:22 +00004058 VTs.push_back(MVT::Flag);
Dale Johannesen3484c092009-02-05 22:07:54 +00004059 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00004060
Chris Lattner1a635d62006-04-14 06:01:58 +00004061 // Now that we have the comparison, emit a copy from the CR to a GPR.
4062 // This is flagged to the above dot comparison.
Owen Anderson825b72b2009-08-11 20:47:22 +00004063 SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32,
4064 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00004065 CompNode.getValue(1));
4066
Chris Lattner1a635d62006-04-14 06:01:58 +00004067 // Unpack the result based on how the target uses it.
4068 unsigned BitNo; // Bit # of CR6.
4069 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004070 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00004071 default: // Can't happen, don't crash on invalid number though.
4072 case 0: // Return the value of the EQ bit of CR6.
4073 BitNo = 0; InvertBit = false;
4074 break;
4075 case 1: // Return the inverted value of the EQ bit of CR6.
4076 BitNo = 0; InvertBit = true;
4077 break;
4078 case 2: // Return the value of the LT bit of CR6.
4079 BitNo = 2; InvertBit = false;
4080 break;
4081 case 3: // Return the inverted value of the LT bit of CR6.
4082 BitNo = 2; InvertBit = true;
4083 break;
4084 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004085
Chris Lattner1a635d62006-04-14 06:01:58 +00004086 // Shift the bit into the low position.
Owen Anderson825b72b2009-08-11 20:47:22 +00004087 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
4088 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00004089 // Isolate the bit.
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
4091 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00004092
Chris Lattner1a635d62006-04-14 06:01:58 +00004093 // If we are supposed to, toggle the bit.
4094 if (InvertBit)
Owen Anderson825b72b2009-08-11 20:47:22 +00004095 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
4096 DAG.getConstant(1, MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00004097 return Flags;
4098}
4099
Scott Michelfdc40a02009-02-17 22:15:04 +00004100SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004101 SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004102 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00004103 // Create a stack slot that is 16-byte aligned.
4104 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4105 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
Owen Andersone50ed302009-08-10 22:56:29 +00004106 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004107 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004108
Chris Lattner1a635d62006-04-14 06:01:58 +00004109 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004110 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004111 Op.getOperand(0), FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004112 // Load it out.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004113 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004114}
4115
Dan Gohman475871a2008-07-27 21:46:04 +00004116SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00004117 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004118 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00004119 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004120
Owen Anderson825b72b2009-08-11 20:47:22 +00004121 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
4122 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00004123
Dan Gohman475871a2008-07-27 21:46:04 +00004124 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00004125 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00004126
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004127 // Shrinkify inputs to v8i16.
Owen Anderson825b72b2009-08-11 20:47:22 +00004128 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, LHS);
4129 RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHS);
4130 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00004131
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004132 // Low parts multiplied together, generating 32-bit results (we ignore the
4133 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00004134 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004136
Dan Gohman475871a2008-07-27 21:46:04 +00004137 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004139 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00004140 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00004141 Neg16, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
4143 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004144 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004145
Owen Anderson825b72b2009-08-11 20:47:22 +00004146 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004147
Chris Lattnercea2aa72006-04-18 04:28:57 +00004148 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00004149 LHS, RHS, Zero, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004151 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004152
Chris Lattner19a81522006-04-18 03:57:35 +00004153 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004154 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson825b72b2009-08-11 20:47:22 +00004155 LHS, RHS, DAG, dl, MVT::v8i16);
4156 EvenParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004157
Chris Lattner19a81522006-04-18 03:57:35 +00004158 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004159 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 LHS, RHS, DAG, dl, MVT::v8i16);
4161 OddParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004162
Chris Lattner19a81522006-04-18 03:57:35 +00004163 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00004164 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00004165 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004166 Ops[i*2 ] = 2*i+1;
4167 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00004168 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004170 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004171 llvm_unreachable("Unknown mul to lower!");
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004172 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00004173}
4174
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004175/// LowerOperation - Provide custom lowering hooks for some operations.
4176///
Dan Gohman475871a2008-07-27 21:46:04 +00004177SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004178 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004179 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00004180 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4181 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00004182 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00004183 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00004184 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bill Wendling77959322008-09-17 00:30:57 +00004185 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004186 case ISD::VASTART:
Nicolas Geoffray01119992007-04-03 13:59:52 +00004187 return LowerVASTART(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
4188 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00004189
4190 case ISD::VAARG:
Nicolas Geoffray01119992007-04-03 13:59:52 +00004191 return LowerVAARG(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
4192 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
4193
Jim Laskeyefc7e522006-12-04 22:04:42 +00004194 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00004195 case ISD::DYNAMIC_STACKALLOC:
4196 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00004197
Chris Lattner1a635d62006-04-14 06:01:58 +00004198 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004199 case ISD::FP_TO_UINT:
4200 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00004201 Op.getDebugLoc());
Chris Lattner1a635d62006-04-14 06:01:58 +00004202 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00004203 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004204
Chris Lattner1a635d62006-04-14 06:01:58 +00004205 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00004206 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
4207 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
4208 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004209
Chris Lattner1a635d62006-04-14 06:01:58 +00004210 // Vector-related lowering.
4211 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4212 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4213 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4214 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00004215 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004216
Chris Lattner3fc027d2007-12-08 06:59:59 +00004217 // Frame & Return address.
4218 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004219 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00004220 }
Dan Gohman475871a2008-07-27 21:46:04 +00004221 return SDValue();
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004222}
4223
Duncan Sands1607f052008-12-01 11:39:25 +00004224void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
4225 SmallVectorImpl<SDValue>&Results,
4226 SelectionDAG &DAG) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004227 DebugLoc dl = N->getDebugLoc();
Chris Lattner1f873002007-11-28 18:44:47 +00004228 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00004229 default:
Duncan Sands1607f052008-12-01 11:39:25 +00004230 assert(false && "Do not know how to custom type legalize this operation!");
4231 return;
4232 case ISD::FP_ROUND_INREG: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 assert(N->getValueType(0) == MVT::ppcf128);
4234 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00004235 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004237 DAG.getIntPtrConstant(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00004238 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004240 DAG.getIntPtrConstant(1));
4241
4242 // This sequence changes FPSCR to do round-to-zero, adds the two halves
4243 // of the long double, and puts FPSCR back the way it was. We do not
4244 // actually model FPSCR.
Owen Andersone50ed302009-08-10 22:56:29 +00004245 std::vector<EVT> NodeTys;
Duncan Sands1607f052008-12-01 11:39:25 +00004246 SDValue Ops[4], Result, MFFSreg, InFlag, FPreg;
4247
Owen Anderson825b72b2009-08-11 20:47:22 +00004248 NodeTys.push_back(MVT::f64); // Return register
4249 NodeTys.push_back(MVT::Flag); // Returns a flag for later insns
Dale Johannesen3484c092009-02-05 22:07:54 +00004250 Result = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Duncan Sands1607f052008-12-01 11:39:25 +00004251 MFFSreg = Result.getValue(0);
4252 InFlag = Result.getValue(1);
4253
4254 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 NodeTys.push_back(MVT::Flag); // Returns a flag
4256 Ops[0] = DAG.getConstant(31, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004257 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004258 Result = DAG.getNode(PPCISD::MTFSB1, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004259 InFlag = Result.getValue(0);
4260
4261 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004262 NodeTys.push_back(MVT::Flag); // Returns a flag
4263 Ops[0] = DAG.getConstant(30, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004264 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004265 Result = DAG.getNode(PPCISD::MTFSB0, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004266 InFlag = Result.getValue(0);
4267
4268 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004269 NodeTys.push_back(MVT::f64); // result of add
4270 NodeTys.push_back(MVT::Flag); // Returns a flag
Duncan Sands1607f052008-12-01 11:39:25 +00004271 Ops[0] = Lo;
4272 Ops[1] = Hi;
4273 Ops[2] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004274 Result = DAG.getNode(PPCISD::FADDRTZ, dl, NodeTys, Ops, 3);
Duncan Sands1607f052008-12-01 11:39:25 +00004275 FPreg = Result.getValue(0);
4276 InFlag = Result.getValue(1);
4277
4278 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 NodeTys.push_back(MVT::f64);
4280 Ops[0] = DAG.getConstant(1, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004281 Ops[1] = MFFSreg;
4282 Ops[2] = FPreg;
4283 Ops[3] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004284 Result = DAG.getNode(PPCISD::MTFSF, dl, NodeTys, Ops, 4);
Duncan Sands1607f052008-12-01 11:39:25 +00004285 FPreg = Result.getValue(0);
4286
4287 // We know the low half is about to be thrown away, so just use something
4288 // convenient.
Owen Anderson825b72b2009-08-11 20:47:22 +00004289 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00004290 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00004291 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00004292 }
Duncan Sands1607f052008-12-01 11:39:25 +00004293 case ISD::FP_TO_SINT:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004294 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00004295 return;
Chris Lattner1f873002007-11-28 18:44:47 +00004296 }
4297}
4298
4299
Chris Lattner1a635d62006-04-14 06:01:58 +00004300//===----------------------------------------------------------------------===//
4301// Other Lowering Code
4302//===----------------------------------------------------------------------===//
4303
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004304MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004305PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004306 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004307 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004308 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4309
4310 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4311 MachineFunction *F = BB->getParent();
4312 MachineFunction::iterator It = BB;
4313 ++It;
4314
4315 unsigned dest = MI->getOperand(0).getReg();
4316 unsigned ptrA = MI->getOperand(1).getReg();
4317 unsigned ptrB = MI->getOperand(2).getReg();
4318 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004319 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004320
4321 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4322 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4323 F->insert(It, loopMBB);
4324 F->insert(It, exitMBB);
4325 exitMBB->transferSuccessors(BB);
4326
4327 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00004328 unsigned TmpReg = (!BinOpcode) ? incr :
4329 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00004330 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4331 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004332
4333 // thisMBB:
4334 // ...
4335 // fallthrough --> loopMBB
4336 BB->addSuccessor(loopMBB);
4337
4338 // loopMBB:
4339 // l[wd]arx dest, ptr
4340 // add r0, dest, incr
4341 // st[wd]cx. r0, ptr
4342 // bne- loopMBB
4343 // fallthrough --> exitMBB
4344 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004345 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004346 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004347 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004348 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
4349 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004350 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004351 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004352 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004353 BB->addSuccessor(loopMBB);
4354 BB->addSuccessor(exitMBB);
4355
4356 // exitMBB:
4357 // ...
4358 BB = exitMBB;
4359 return BB;
4360}
4361
4362MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00004363PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00004364 MachineBasicBlock *BB,
4365 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004366 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004367 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00004368 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4369 // In 64 bit mode we have to use 64 bits for addresses, even though the
4370 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
4371 // registers without caring whether they're 32 or 64, but here we're
4372 // doing actual arithmetic on the addresses.
4373 bool is64bit = PPCSubTarget.isPPC64();
4374
4375 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4376 MachineFunction *F = BB->getParent();
4377 MachineFunction::iterator It = BB;
4378 ++It;
4379
4380 unsigned dest = MI->getOperand(0).getReg();
4381 unsigned ptrA = MI->getOperand(1).getReg();
4382 unsigned ptrB = MI->getOperand(2).getReg();
4383 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004384 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00004385
4386 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4387 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4388 F->insert(It, loopMBB);
4389 F->insert(It, exitMBB);
4390 exitMBB->transferSuccessors(BB);
4391
4392 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004393 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004394 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4395 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00004396 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4397 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4398 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4399 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
4400 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4401 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4402 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4403 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4404 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
4405 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004406 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004407 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00004408 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004409
4410 // thisMBB:
4411 // ...
4412 // fallthrough --> loopMBB
4413 BB->addSuccessor(loopMBB);
4414
4415 // The 4-byte load must be aligned, while a char or short may be
4416 // anywhere in the word. Hence all this nasty bookkeeping code.
4417 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4418 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004419 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00004420 // rlwinm ptr, ptr1, 0, 0, 29
4421 // slw incr2, incr, shift
4422 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4423 // slw mask, mask2, shift
4424 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004425 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00004426 // add tmp, tmpDest, incr2
4427 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00004428 // and tmp3, tmp, mask
4429 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004430 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00004431 // bne- loopMBB
4432 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00004433 // srw dest, tmpDest, shift
Dale Johannesen97efa362008-08-28 17:53:09 +00004434
4435 if (ptrA!=PPC::R0) {
4436 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004437 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004438 .addReg(ptrA).addReg(ptrB);
4439 } else {
4440 Ptr1Reg = ptrB;
4441 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004442 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004443 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004444 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004445 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4446 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004447 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004448 .addReg(Ptr1Reg).addImm(0).addImm(61);
4449 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004450 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004451 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004452 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004453 .addReg(incr).addReg(ShiftReg);
4454 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004455 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00004456 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004457 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4458 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00004459 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004460 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004461 .addReg(Mask2Reg).addReg(ShiftReg);
4462
4463 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004464 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004465 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004466 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004467 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004468 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004469 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004470 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004471 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004472 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004473 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004474 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004475 BuildMI(BB, dl, TII->get(PPC::STWCX))
Dale Johannesen97efa362008-08-28 17:53:09 +00004476 .addReg(Tmp4Reg).addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004477 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004478 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00004479 BB->addSuccessor(loopMBB);
4480 BB->addSuccessor(exitMBB);
4481
4482 // exitMBB:
4483 // ...
4484 BB = exitMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004485 BuildMI(BB, dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg).addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00004486 return BB;
4487}
4488
4489MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004490PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00004491 MachineBasicBlock *BB,
4492 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004493 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00004494
4495 // To "insert" these instructions we actually have to insert their
4496 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004497 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004498 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004499 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00004500
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004501 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00004502
4503 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
4504 MI->getOpcode() == PPC::SELECT_CC_I8 ||
4505 MI->getOpcode() == PPC::SELECT_CC_F4 ||
4506 MI->getOpcode() == PPC::SELECT_CC_F8 ||
4507 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
4508
4509 // The incoming instruction knows the destination vreg to set, the
4510 // condition code register to branch on, the true/false values to
4511 // select between, and a branch opcode to use.
4512
4513 // thisMBB:
4514 // ...
4515 // TrueVal = ...
4516 // cmpTY ccX, r1, r2
4517 // bCC copy1MBB
4518 // fallthrough --> copy0MBB
4519 MachineBasicBlock *thisMBB = BB;
4520 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4521 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4522 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004523 DebugLoc dl = MI->getDebugLoc();
4524 BuildMI(BB, dl, TII->get(PPC::BCC))
Evan Cheng53301922008-07-12 02:23:19 +00004525 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
4526 F->insert(It, copy0MBB);
4527 F->insert(It, sinkMBB);
4528 // Update machine-CFG edges by transferring all successors of the current
4529 // block to the new block which will contain the Phi node for the select.
4530 sinkMBB->transferSuccessors(BB);
4531 // Next, add the true and fallthrough blocks as its successors.
4532 BB->addSuccessor(copy0MBB);
4533 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004534
Evan Cheng53301922008-07-12 02:23:19 +00004535 // copy0MBB:
4536 // %FalseValue = ...
4537 // # fallthrough to sinkMBB
4538 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00004539
Evan Cheng53301922008-07-12 02:23:19 +00004540 // Update machine-CFG edges
4541 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004542
Evan Cheng53301922008-07-12 02:23:19 +00004543 // sinkMBB:
4544 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4545 // ...
4546 BB = sinkMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004547 BuildMI(BB, dl, TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00004548 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
4549 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4550 }
Dale Johannesen97efa362008-08-28 17:53:09 +00004551 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
4552 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
4553 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
4554 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004555 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
4556 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
4557 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
4558 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004559
4560 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
4561 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
4562 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
4563 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004564 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
4565 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
4566 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
4567 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004568
4569 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
4570 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
4571 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
4572 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004573 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
4574 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
4575 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
4576 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004577
4578 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
4579 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
4580 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
4581 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004582 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
4583 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
4584 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
4585 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004586
4587 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00004588 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004589 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00004590 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004591 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00004592 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004593 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00004594 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004595
4596 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
4597 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
4598 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
4599 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004600 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
4601 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
4602 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
4603 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004604
Dale Johannesen0e55f062008-08-29 18:29:46 +00004605 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
4606 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
4607 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
4608 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
4609 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
4610 BB = EmitAtomicBinary(MI, BB, false, 0);
4611 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
4612 BB = EmitAtomicBinary(MI, BB, true, 0);
4613
Evan Cheng53301922008-07-12 02:23:19 +00004614 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
4615 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
4616 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
4617
4618 unsigned dest = MI->getOperand(0).getReg();
4619 unsigned ptrA = MI->getOperand(1).getReg();
4620 unsigned ptrB = MI->getOperand(2).getReg();
4621 unsigned oldval = MI->getOperand(3).getReg();
4622 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004623 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00004624
Dale Johannesen65e39732008-08-25 18:53:26 +00004625 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4626 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4627 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00004628 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004629 F->insert(It, loop1MBB);
4630 F->insert(It, loop2MBB);
4631 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00004632 F->insert(It, exitMBB);
4633 exitMBB->transferSuccessors(BB);
4634
4635 // thisMBB:
4636 // ...
4637 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00004638 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00004639
Dale Johannesen65e39732008-08-25 18:53:26 +00004640 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00004641 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00004642 // cmp[wd] dest, oldval
4643 // bne- midMBB
4644 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00004645 // st[wd]cx. newval, ptr
4646 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00004647 // b exitBB
4648 // midMBB:
4649 // st[wd]cx. dest, ptr
4650 // exitBB:
4651 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004652 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00004653 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004654 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00004655 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004656 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00004657 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
4658 BB->addSuccessor(loop2MBB);
4659 BB->addSuccessor(midMBB);
4660
4661 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004662 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00004663 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004664 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00004665 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004666 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004667 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00004668 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004669
Dale Johannesen65e39732008-08-25 18:53:26 +00004670 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004671 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00004672 .addReg(dest).addReg(ptrA).addReg(ptrB);
4673 BB->addSuccessor(exitMBB);
4674
Evan Cheng53301922008-07-12 02:23:19 +00004675 // exitMBB:
4676 // ...
4677 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004678 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
4679 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
4680 // We must use 64-bit registers for addresses when targeting 64-bit,
4681 // since we're actually doing arithmetic on them. Other registers
4682 // can be 32-bit.
4683 bool is64bit = PPCSubTarget.isPPC64();
4684 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
4685
4686 unsigned dest = MI->getOperand(0).getReg();
4687 unsigned ptrA = MI->getOperand(1).getReg();
4688 unsigned ptrB = MI->getOperand(2).getReg();
4689 unsigned oldval = MI->getOperand(3).getReg();
4690 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004691 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004692
4693 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4694 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4695 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
4696 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4697 F->insert(It, loop1MBB);
4698 F->insert(It, loop2MBB);
4699 F->insert(It, midMBB);
4700 F->insert(It, exitMBB);
4701 exitMBB->transferSuccessors(BB);
4702
4703 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004704 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004705 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4706 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004707 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4708 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4709 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4710 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
4711 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
4712 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
4713 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
4714 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4715 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4716 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4717 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4718 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
4719 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
4720 unsigned Ptr1Reg;
4721 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
4722 // thisMBB:
4723 // ...
4724 // fallthrough --> loopMBB
4725 BB->addSuccessor(loop1MBB);
4726
4727 // The 4-byte load must be aligned, while a char or short may be
4728 // anywhere in the word. Hence all this nasty bookkeeping code.
4729 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4730 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004731 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004732 // rlwinm ptr, ptr1, 0, 0, 29
4733 // slw newval2, newval, shift
4734 // slw oldval2, oldval,shift
4735 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4736 // slw mask, mask2, shift
4737 // and newval3, newval2, mask
4738 // and oldval3, oldval2, mask
4739 // loop1MBB:
4740 // lwarx tmpDest, ptr
4741 // and tmp, tmpDest, mask
4742 // cmpw tmp, oldval3
4743 // bne- midMBB
4744 // loop2MBB:
4745 // andc tmp2, tmpDest, mask
4746 // or tmp4, tmp2, newval3
4747 // stwcx. tmp4, ptr
4748 // bne- loop1MBB
4749 // b exitBB
4750 // midMBB:
4751 // stwcx. tmpDest, ptr
4752 // exitBB:
4753 // srw dest, tmpDest, shift
4754 if (ptrA!=PPC::R0) {
4755 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004756 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004757 .addReg(ptrA).addReg(ptrB);
4758 } else {
4759 Ptr1Reg = ptrB;
4760 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004761 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004762 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004763 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004764 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4765 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004766 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004767 .addReg(Ptr1Reg).addImm(0).addImm(61);
4768 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004769 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004770 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004771 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004772 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004773 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004774 .addReg(oldval).addReg(ShiftReg);
4775 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004776 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004777 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004778 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4779 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
4780 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004781 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004782 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004783 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004784 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004785 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004786 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004787 .addReg(OldVal2Reg).addReg(MaskReg);
4788
4789 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004790 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004791 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004792 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
4793 .addReg(TmpDestReg).addReg(MaskReg);
4794 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004795 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004796 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004797 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
4798 BB->addSuccessor(loop2MBB);
4799 BB->addSuccessor(midMBB);
4800
4801 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004802 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
4803 .addReg(TmpDestReg).addReg(MaskReg);
4804 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
4805 .addReg(Tmp2Reg).addReg(NewVal3Reg);
4806 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004807 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004808 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004809 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004810 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004811 BB->addSuccessor(loop1MBB);
4812 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004813
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004814 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004815 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004816 .addReg(PPC::R0).addReg(PtrReg);
4817 BB->addSuccessor(exitMBB);
4818
4819 // exitMBB:
4820 // ...
4821 BB = exitMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004822 BuildMI(BB, dl, TII->get(PPC::SRW),dest).addReg(TmpReg).addReg(ShiftReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004823 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004824 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng53301922008-07-12 02:23:19 +00004825 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004826
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004827 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004828 return BB;
4829}
4830
Chris Lattner1a635d62006-04-14 06:01:58 +00004831//===----------------------------------------------------------------------===//
4832// Target Optimization Hooks
4833//===----------------------------------------------------------------------===//
4834
Duncan Sands25cf2272008-11-24 14:53:14 +00004835SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
4836 DAGCombinerInfo &DCI) const {
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004837 TargetMachine &TM = getTargetMachine();
4838 SelectionDAG &DAG = DCI.DAG;
Dale Johannesen3484c092009-02-05 22:07:54 +00004839 DebugLoc dl = N->getDebugLoc();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004840 switch (N->getOpcode()) {
4841 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004842 case PPCISD::SHL:
4843 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004844 if (C->getZExtValue() == 0) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004845 return N->getOperand(0);
4846 }
4847 break;
4848 case PPCISD::SRL:
4849 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004850 if (C->getZExtValue() == 0) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004851 return N->getOperand(0);
4852 }
4853 break;
4854 case PPCISD::SRA:
4855 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004856 if (C->getZExtValue() == 0 || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004857 C->isAllOnesValue()) // -1 >>s V -> -1.
4858 return N->getOperand(0);
4859 }
4860 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004861
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004862 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00004863 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004864 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
4865 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
4866 // We allow the src/dst to be either f32/f64, but the intermediate
4867 // type must be i64.
Owen Anderson825b72b2009-08-11 20:47:22 +00004868 if (N->getOperand(0).getValueType() == MVT::i64 &&
4869 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004870 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 if (Val.getValueType() == MVT::f32) {
4872 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004873 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004874 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004875
Owen Anderson825b72b2009-08-11 20:47:22 +00004876 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004877 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00004878 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004879 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00004880 if (N->getValueType(0) == MVT::f32) {
4881 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00004882 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004883 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004884 }
4885 return Val;
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004887 // If the intermediate type is i32, we can avoid the load/store here
4888 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004889 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004890 }
4891 }
4892 break;
Chris Lattner51269842006-03-01 05:50:56 +00004893 case ISD::STORE:
4894 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
4895 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00004896 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00004897 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004898 N->getOperand(1).getValueType() == MVT::i32 &&
4899 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004900 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 if (Val.getValueType() == MVT::f32) {
4902 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004903 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004904 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004905 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004906 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004907
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 Val = DAG.getNode(PPCISD::STFIWX, dl, MVT::Other, N->getOperand(0), Val,
Chris Lattner51269842006-03-01 05:50:56 +00004909 N->getOperand(2), N->getOperand(3));
Gabor Greifba36cb52008-08-28 21:40:38 +00004910 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004911 return Val;
4912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004913
Chris Lattnerd9989382006-07-10 20:56:58 +00004914 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
4915 if (N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004916 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004917 (N->getOperand(1).getValueType() == MVT::i32 ||
4918 N->getOperand(1).getValueType() == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004919 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00004920 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson825b72b2009-08-11 20:47:22 +00004921 if (BSwapOp.getValueType() == MVT::i16)
4922 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00004923
Owen Anderson825b72b2009-08-11 20:47:22 +00004924 return DAG.getNode(PPCISD::STBRX, dl, MVT::Other, N->getOperand(0),
Dale Johannesen3484c092009-02-05 22:07:54 +00004925 BSwapOp, N->getOperand(2), N->getOperand(3),
Chris Lattnerd9989382006-07-10 20:56:58 +00004926 DAG.getValueType(N->getOperand(1).getValueType()));
4927 }
4928 break;
4929 case ISD::BSWAP:
4930 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00004931 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00004932 N->getOperand(0).hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004933 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004934 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00004935 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00004936 // Create the byte-swapping load.
Owen Andersone50ed302009-08-10 22:56:29 +00004937 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +00004938 VTs.push_back(MVT::i32);
4939 VTs.push_back(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004940 SDValue MO = DAG.getMemOperand(LD->getMemOperand());
4941 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00004942 LD->getChain(), // Chain
4943 LD->getBasePtr(), // Ptr
Dan Gohman69de1932008-02-06 22:27:42 +00004944 MO, // MemOperand
Chris Lattner79e490a2006-08-11 17:18:05 +00004945 DAG.getValueType(N->getValueType(0)) // VT
4946 };
Dale Johannesen3484c092009-02-05 22:07:54 +00004947 SDValue BSLoad = DAG.getNode(PPCISD::LBRX, dl, VTs, Ops, 4);
Chris Lattnerd9989382006-07-10 20:56:58 +00004948
Scott Michelfdc40a02009-02-17 22:15:04 +00004949 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00004950 SDValue ResVal = BSLoad;
Owen Anderson825b72b2009-08-11 20:47:22 +00004951 if (N->getValueType(0) == MVT::i16)
4952 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00004953
Chris Lattnerd9989382006-07-10 20:56:58 +00004954 // First, combine the bswap away. This makes the value produced by the
4955 // load dead.
4956 DCI.CombineTo(N, ResVal);
4957
4958 // Next, combine the load away, we give it a bogus result value but a real
4959 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00004960 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004961
Chris Lattnerd9989382006-07-10 20:56:58 +00004962 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00004963 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00004964 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004965
Chris Lattner51269842006-03-01 05:50:56 +00004966 break;
Chris Lattner4468c222006-03-31 06:02:07 +00004967 case PPCISD::VCMP: {
4968 // If a VCMPo node already exists with exactly the same operands as this
4969 // node, use its result instead of this node (VCMPo computes both a CR6 and
4970 // a normal output).
4971 //
4972 if (!N->getOperand(0).hasOneUse() &&
4973 !N->getOperand(1).hasOneUse() &&
4974 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004975
Chris Lattner4468c222006-03-31 06:02:07 +00004976 // Scan all of the users of the LHS, looking for VCMPo's that match.
4977 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004978
Gabor Greifba36cb52008-08-28 21:40:38 +00004979 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00004980 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
4981 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004982 if (UI->getOpcode() == PPCISD::VCMPo &&
4983 UI->getOperand(1) == N->getOperand(1) &&
4984 UI->getOperand(2) == N->getOperand(2) &&
4985 UI->getOperand(0) == N->getOperand(0)) {
4986 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00004987 break;
4988 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004989
Chris Lattner00901202006-04-18 18:28:22 +00004990 // If there is no VCMPo node, or if the flag value has a single use, don't
4991 // transform this.
4992 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
4993 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004994
4995 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00004996 // chain, this transformation is more complex. Note that multiple things
4997 // could use the value result, which we should ignore.
4998 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004999 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00005000 FlagUser == 0; ++UI) {
5001 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00005002 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00005003 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005004 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00005005 FlagUser = User;
5006 break;
5007 }
5008 }
5009 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005010
Chris Lattner00901202006-04-18 18:28:22 +00005011 // If the user is a MFCR instruction, we know this is safe. Otherwise we
5012 // give up for right now.
5013 if (FlagUser->getOpcode() == PPCISD::MFCR)
Dan Gohman475871a2008-07-27 21:46:04 +00005014 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00005015 }
5016 break;
5017 }
Chris Lattner90564f22006-04-18 17:59:36 +00005018 case ISD::BR_CC: {
5019 // If this is a branch on an altivec predicate comparison, lower this so
5020 // that we don't have to do a MFCR: instead, branch directly on CR6. This
5021 // lowering is done pre-legalize, because the legalizer lowers the predicate
5022 // compare down to code that is difficult to reassemble.
5023 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00005024 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Chris Lattner90564f22006-04-18 17:59:36 +00005025 int CompareOpc;
5026 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00005027
Chris Lattner90564f22006-04-18 17:59:36 +00005028 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
5029 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
5030 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
5031 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005032
Chris Lattner90564f22006-04-18 17:59:36 +00005033 // If this is a comparison against something other than 0/1, then we know
5034 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005035 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005036 if (Val != 0 && Val != 1) {
5037 if (CC == ISD::SETEQ) // Cond never true, remove branch.
5038 return N->getOperand(0);
5039 // Always !=, turn it into an unconditional branch.
Owen Anderson825b72b2009-08-11 20:47:22 +00005040 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00005041 N->getOperand(0), N->getOperand(4));
5042 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005043
Chris Lattner90564f22006-04-18 17:59:36 +00005044 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005045
Chris Lattner90564f22006-04-18 17:59:36 +00005046 // Create the PPCISD altivec 'dot' comparison node.
Owen Andersone50ed302009-08-10 22:56:29 +00005047 std::vector<EVT> VTs;
Dan Gohman475871a2008-07-27 21:46:04 +00005048 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005049 LHS.getOperand(2), // LHS of compare
5050 LHS.getOperand(3), // RHS of compare
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00005052 };
Chris Lattner90564f22006-04-18 17:59:36 +00005053 VTs.push_back(LHS.getOperand(2).getValueType());
Owen Anderson825b72b2009-08-11 20:47:22 +00005054 VTs.push_back(MVT::Flag);
Dale Johannesen3484c092009-02-05 22:07:54 +00005055 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005056
Chris Lattner90564f22006-04-18 17:59:36 +00005057 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005058 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005059 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00005060 default: // Can't happen, don't crash on invalid number though.
5061 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005062 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00005063 break;
5064 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005065 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00005066 break;
5067 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005068 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00005069 break;
5070 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005071 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00005072 break;
5073 }
5074
Owen Anderson825b72b2009-08-11 20:47:22 +00005075 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
5076 DAG.getConstant(CompOpc, MVT::i32),
5077 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00005078 N->getOperand(4), CompNode.getValue(1));
5079 }
5080 break;
5081 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005083
Dan Gohman475871a2008-07-27 21:46:04 +00005084 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005085}
5086
Chris Lattner1a635d62006-04-14 06:01:58 +00005087//===----------------------------------------------------------------------===//
5088// Inline Assembly Support
5089//===----------------------------------------------------------------------===//
5090
Dan Gohman475871a2008-07-27 21:46:04 +00005091void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005092 const APInt &Mask,
Scott Michelfdc40a02009-02-17 22:15:04 +00005093 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005094 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005095 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005096 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005097 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005098 switch (Op.getOpcode()) {
5099 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00005100 case PPCISD::LBRX: {
5101 // lhbrx is known to have the top bits cleared out.
Owen Anderson825b72b2009-08-11 20:47:22 +00005102 if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16)
Chris Lattnerd9989382006-07-10 20:56:58 +00005103 KnownZero = 0xFFFF0000;
5104 break;
5105 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005106 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005107 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005108 default: break;
5109 case Intrinsic::ppc_altivec_vcmpbfp_p:
5110 case Intrinsic::ppc_altivec_vcmpeqfp_p:
5111 case Intrinsic::ppc_altivec_vcmpequb_p:
5112 case Intrinsic::ppc_altivec_vcmpequh_p:
5113 case Intrinsic::ppc_altivec_vcmpequw_p:
5114 case Intrinsic::ppc_altivec_vcmpgefp_p:
5115 case Intrinsic::ppc_altivec_vcmpgtfp_p:
5116 case Intrinsic::ppc_altivec_vcmpgtsb_p:
5117 case Intrinsic::ppc_altivec_vcmpgtsh_p:
5118 case Intrinsic::ppc_altivec_vcmpgtsw_p:
5119 case Intrinsic::ppc_altivec_vcmpgtub_p:
5120 case Intrinsic::ppc_altivec_vcmpgtuh_p:
5121 case Intrinsic::ppc_altivec_vcmpgtuw_p:
5122 KnownZero = ~1U; // All bits but the low one are known to be zero.
5123 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005124 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005125 }
5126 }
5127}
5128
5129
Chris Lattner4234f572007-03-25 02:14:49 +00005130/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005131/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00005132PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005133PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
5134 if (Constraint.size() == 1) {
5135 switch (Constraint[0]) {
5136 default: break;
5137 case 'b':
5138 case 'r':
5139 case 'f':
5140 case 'v':
5141 case 'y':
5142 return C_RegisterClass;
5143 }
5144 }
5145 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005146}
5147
Scott Michelfdc40a02009-02-17 22:15:04 +00005148std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00005149PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005150 EVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00005151 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00005152 // GCC RS6000 Constraint Letters
5153 switch (Constraint[0]) {
5154 case 'b': // R1-R31
5155 case 'r': // R0-R31
Owen Anderson825b72b2009-08-11 20:47:22 +00005156 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Chris Lattner331d1bc2006-11-02 01:44:04 +00005157 return std::make_pair(0U, PPC::G8RCRegisterClass);
5158 return std::make_pair(0U, PPC::GPRCRegisterClass);
5159 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00005160 if (VT == MVT::f32)
Chris Lattner331d1bc2006-11-02 01:44:04 +00005161 return std::make_pair(0U, PPC::F4RCRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00005162 else if (VT == MVT::f64)
Chris Lattner331d1bc2006-11-02 01:44:04 +00005163 return std::make_pair(0U, PPC::F8RCRegisterClass);
5164 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005165 case 'v':
Chris Lattner331d1bc2006-11-02 01:44:04 +00005166 return std::make_pair(0U, PPC::VRRCRegisterClass);
5167 case 'y': // crrc
5168 return std::make_pair(0U, PPC::CRRCRegisterClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005169 }
5170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005171
Chris Lattner331d1bc2006-11-02 01:44:04 +00005172 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005173}
Chris Lattner763317d2006-02-07 00:47:13 +00005174
Chris Lattner331d1bc2006-11-02 01:44:04 +00005175
Chris Lattner48884cd2007-08-25 00:47:38 +00005176/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Evan Chengda43bcf2008-09-24 00:05:32 +00005177/// vector. If it is invalid, don't add anything to Ops. If hasMemory is true
5178/// it means one of the asm constraint of the inline asm instruction being
5179/// processed is 'm'.
Dan Gohman475871a2008-07-27 21:46:04 +00005180void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, char Letter,
Evan Chengda43bcf2008-09-24 00:05:32 +00005181 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00005182 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00005183 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00005184 SDValue Result(0,0);
Chris Lattner763317d2006-02-07 00:47:13 +00005185 switch (Letter) {
5186 default: break;
5187 case 'I':
5188 case 'J':
5189 case 'K':
5190 case 'L':
5191 case 'M':
5192 case 'N':
5193 case 'O':
5194 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00005195 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00005196 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005197 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00005198 switch (Letter) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005199 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner763317d2006-02-07 00:47:13 +00005200 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005201 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005202 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005203 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005204 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
5205 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005206 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005207 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005208 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005209 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005210 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005211 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005212 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005213 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005214 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00005215 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005216 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005217 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005218 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00005219 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005220 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005221 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005222 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005223 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005224 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005225 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005226 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005227 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005228 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005229 }
5230 break;
5231 }
5232 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005233
Gabor Greifba36cb52008-08-28 21:40:38 +00005234 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005235 Ops.push_back(Result);
5236 return;
5237 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005238
Chris Lattner763317d2006-02-07 00:47:13 +00005239 // Handle standard constraint letters.
Evan Chengda43bcf2008-09-24 00:05:32 +00005240 TargetLowering::LowerAsmOperandForConstraint(Op, Letter, hasMemory, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00005241}
Evan Chengc4c62572006-03-13 23:20:37 +00005242
Chris Lattnerc9addb72007-03-30 23:15:24 +00005243// isLegalAddressingMode - Return true if the addressing mode represented
5244// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00005245bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005246 const Type *Ty) const {
5247 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00005248
Chris Lattnerc9addb72007-03-30 23:15:24 +00005249 // PPC allows a sign-extended 16-bit immediate field.
5250 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
5251 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005252
Chris Lattnerc9addb72007-03-30 23:15:24 +00005253 // No global is ever allowed as a base.
5254 if (AM.BaseGV)
5255 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005256
5257 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005258 switch (AM.Scale) {
5259 case 0: // "r+i" or just "i", depending on HasBaseReg.
5260 break;
5261 case 1:
5262 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
5263 return false;
5264 // Otherwise we have r+r or r+i.
5265 break;
5266 case 2:
5267 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
5268 return false;
5269 // Allow 2*r as r+r.
5270 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00005271 default:
5272 // No other scales are supported.
5273 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00005274 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005275
Chris Lattnerc9addb72007-03-30 23:15:24 +00005276 return true;
5277}
5278
Evan Chengc4c62572006-03-13 23:20:37 +00005279/// isLegalAddressImmediate - Return true if the integer value can be used
Evan Cheng86193912007-03-12 23:29:01 +00005280/// as the offset of the target addressing mode for load / store of the
5281/// given type.
5282bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
Evan Chengc4c62572006-03-13 23:20:37 +00005283 // PPC allows a sign-extended 16-bit immediate field.
5284 return (V > -(1 << 16) && V < (1 << 16)-1);
5285}
Reid Spencer3a9ec242006-08-28 01:02:49 +00005286
5287bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michelfdc40a02009-02-17 22:15:04 +00005288 return false;
Reid Spencer3a9ec242006-08-28 01:02:49 +00005289}
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005290
Dan Gohman475871a2008-07-27 21:46:04 +00005291SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005292 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005293 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005294 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005295 return SDValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005296
5297 MachineFunction &MF = DAG.getMachineFunction();
5298 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005299
Chris Lattner3fc027d2007-12-08 06:59:59 +00005300 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00005301 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00005302
5303 // Make sure the function really does not optimize away the store of the RA
5304 // to the stack.
5305 FuncInfo->setLRStoreRequired();
Scott Michelfdc40a02009-02-17 22:15:04 +00005306 return DAG.getLoad(getPointerTy(), dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00005307 DAG.getEntryNode(), RetAddrFI, NULL, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00005308}
5309
Dan Gohman475871a2008-07-27 21:46:04 +00005310SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesena05dca42009-02-04 23:02:30 +00005311 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005312 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005313 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005314 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005315
Owen Andersone50ed302009-08-10 22:56:29 +00005316 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00005317 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00005318
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005319 MachineFunction &MF = DAG.getMachineFunction();
5320 MachineFrameInfo *MFI = MF.getFrameInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005321 bool is31 = (NoFramePointerElim || MFI->hasVarSizedObjects())
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005322 && MFI->getStackSize();
5323
5324 if (isPPC64)
Dale Johannesena05dca42009-02-04 23:02:30 +00005325 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::X31 : PPC::X1,
Owen Anderson825b72b2009-08-11 20:47:22 +00005326 MVT::i64);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005327 else
Dale Johannesena05dca42009-02-04 23:02:30 +00005328 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::R31 : PPC::R1,
Owen Anderson825b72b2009-08-11 20:47:22 +00005329 MVT::i32);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005330}
Dan Gohman54aeea32008-10-21 03:41:46 +00005331
5332bool
5333PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5334 // The PowerPC target isn't yet aware of offsets.
5335 return false;
5336}
Tilmann Schellerffd02002009-07-03 06:45:56 +00005337
Owen Andersone50ed302009-08-10 22:56:29 +00005338EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Tilmann Schellerffd02002009-07-03 06:45:56 +00005339 bool isSrcConst, bool isSrcStr,
5340 SelectionDAG &DAG) const {
5341 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005342 return MVT::i64;
Tilmann Schellerffd02002009-07-03 06:45:56 +00005343 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00005344 return MVT::i32;
Tilmann Schellerffd02002009-07-03 06:45:56 +00005345 }
5346}