blob: 69c02ac533e20a28d9b5144eba5b14fd572f86cf [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) {
Chris Lattner9f0bc652007-02-25 05:34:32 +00002879 bool inMem = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002880 SDValue Arg = Outs[i].Val;
2881 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002882
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002883 // PtrOff will be used to store the current argument to the stack if a
2884 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00002885 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00002886
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002887 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00002888
Dale Johannesen39355f92009-02-04 02:34:38 +00002889 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002890
2891 // On PPC64, promote integers to 64-bit values.
Owen Anderson825b72b2009-08-11 20:47:22 +00002892 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00002893 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
2894 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson825b72b2009-08-11 20:47:22 +00002895 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00002896 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002897
Dale Johannesen8419dd62008-03-07 20:27:40 +00002898 // FIXME memcpy is used way more than necessary. Correctness first.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002899 if (Flags.isByVal()) {
2900 unsigned Size = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00002901 if (Size==1 || Size==2) {
2902 // Very small objects are passed right-justified.
2903 // Everything else is passed left-justified.
Owen Anderson825b72b2009-08-11 20:47:22 +00002904 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002905 if (GPR_idx != NumGPRs) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002906 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Dale Johannesen8419dd62008-03-07 20:27:40 +00002907 NULL, 0, VT);
2908 MemOpChains.push_back(Load.getValue(1));
2909 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002910
2911 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002912 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00002913 SDValue Const = DAG.getConstant(4 - Size, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002914 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Dan Gohman475871a2008-07-27 21:46:04 +00002915 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr,
Scott Michelfdc40a02009-02-17 22:15:04 +00002916 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002917 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002918 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00002919 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00002920 CallSeqStart.getNode()->getOperand(1));
Gabor Greif93c53e52008-08-31 15:37:04 +00002921 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
2922 NewCallSeqStart.getNode());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002923 Chain = CallSeqStart = NewCallSeqStart;
2924 ArgOffset += PtrByteSize;
2925 }
2926 continue;
2927 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002928 // Copy entire object into memory. There are cases where gcc-generated
2929 // code assumes it is there, even if it could be put entirely into
2930 // registers. (This is not what the doc says.)
Dan Gohman475871a2008-07-27 21:46:04 +00002931 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00002932 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002933 Flags, DAG, dl);
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002934 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00002935 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00002936 CallSeqStart.getNode()->getOperand(1));
2937 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), NewCallSeqStart.getNode());
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002938 Chain = CallSeqStart = NewCallSeqStart;
2939 // And copy the pieces of it that fit into registers.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002940 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00002941 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002942 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002943 if (GPR_idx != NumGPRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002944 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, NULL, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00002945 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002946 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002947 ArgOffset += PtrByteSize;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002948 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00002949 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002950 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002951 }
2952 }
2953 continue;
2954 }
2955
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002957 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 case MVT::i32:
2959 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00002960 if (GPR_idx != NumGPRs) {
2961 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002962 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002963 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
2964 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002965 TailCallArguments, dl);
Chris Lattner9f0bc652007-02-25 05:34:32 +00002966 inMem = true;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002967 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002968 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002969 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002970 case MVT::f32:
2971 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00002972 if (FPR_idx != NumFPRs) {
2973 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
2974
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002975 if (isVarArg) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002976 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002977 MemOpChains.push_back(Store);
2978
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002979 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00002980 if (GPR_idx != NumGPRs) {
Dale Johannesen39355f92009-02-04 02:34:38 +00002981 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002982 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002983 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002984 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002985 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00002986 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00002987 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
2988 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00002989 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002990 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00002991 }
2992 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00002993 // If we have any FPRs remaining, we may also have GPRs remaining.
2994 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
2995 // GPRs.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002996 if (GPR_idx != NumGPRs)
2997 ++GPR_idx;
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002999 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
3000 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00003001 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003002 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003003 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3004 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003005 TailCallArguments, dl);
Chris Lattner9f0bc652007-02-25 05:34:32 +00003006 inMem = true;
Chris Lattnerabde4602006-05-16 22:56:08 +00003007 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003008 if (isPPC64)
3009 ArgOffset += 8;
3010 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003011 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003012 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00003013 case MVT::v4f32:
3014 case MVT::v4i32:
3015 case MVT::v8i16:
3016 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00003017 if (isVarArg) {
3018 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00003019 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00003020 // V registers; in fact gcc does this only for arguments that are
3021 // prototyped, not for those that match the ... We do it for all
3022 // arguments, seems to work.
3023 while (ArgOffset % 16 !=0) {
3024 ArgOffset += PtrByteSize;
3025 if (GPR_idx != NumGPRs)
3026 GPR_idx++;
3027 }
3028 // We could elide this store in the case where the object fits
3029 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00003030 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00003031 DAG.getConstant(ArgOffset, PtrVT));
Dale Johannesen39355f92009-02-04 02:34:38 +00003032 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003033 MemOpChains.push_back(Store);
3034 if (VR_idx != NumVRs) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003036 MemOpChains.push_back(Load.getValue(1));
3037 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
3038 }
3039 ArgOffset += 16;
3040 for (unsigned i=0; i<16; i+=PtrByteSize) {
3041 if (GPR_idx == NumGPRs)
3042 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00003043 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00003044 DAG.getConstant(i, PtrVT));
Dale Johannesen39355f92009-02-04 02:34:38 +00003045 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, NULL, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003046 MemOpChains.push_back(Load.getValue(1));
3047 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3048 }
3049 break;
3050 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003051
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003052 // Non-varargs Altivec params generally go in registers, but have
3053 // stack space allocated at the end.
3054 if (VR_idx != NumVRs) {
3055 // Doesn't have GPR space allocated.
3056 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
3057 } else if (nAltivecParamsAtEnd==0) {
3058 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003059 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3060 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003061 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00003062 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00003063 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003064 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00003065 }
Chris Lattnerabde4602006-05-16 22:56:08 +00003066 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003067 // If all Altivec parameters fit in registers, as they usually do,
3068 // they get stack space following the non-Altivec parameters. We
3069 // don't track this here because nobody below needs it.
3070 // If there are more Altivec parameters than fit in registers emit
3071 // the stores here.
3072 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
3073 unsigned j = 0;
3074 // Offset is aligned; skip 1st 12 params which go in V registers.
3075 ArgOffset = ((ArgOffset+15)/16)*16;
3076 ArgOffset += 12*16;
3077 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003078 SDValue Arg = Outs[i].Val;
Owen Andersone50ed302009-08-10 22:56:29 +00003079 EVT ArgType = Arg.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00003080 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
3081 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003082 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00003083 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003084 // We are emitting Altivec params in order.
3085 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3086 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003087 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003088 ArgOffset += 16;
3089 }
3090 }
3091 }
3092 }
3093
Chris Lattner9a2a4972006-05-17 06:01:33 +00003094 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00003096 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00003097
Chris Lattner9a2a4972006-05-17 06:01:33 +00003098 // Build a sequence of copy-to-reg nodes chained together with token chain
3099 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00003100 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00003101 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003102 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00003103 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003104 InFlag = Chain.getValue(1);
3105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003106
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003107 if (isTailCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003108 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
3109 FPOp, true, TailCallArguments);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003110 }
3111
Dan Gohman98ca4f22009-08-05 01:29:28 +00003112 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3113 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3114 Ins, InVals);
Chris Lattnerabde4602006-05-16 22:56:08 +00003115}
3116
Dan Gohman98ca4f22009-08-05 01:29:28 +00003117SDValue
3118PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003119 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003120 const SmallVectorImpl<ISD::OutputArg> &Outs,
3121 DebugLoc dl, SelectionDAG &DAG) {
3122
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003123 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00003124 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
3125 RVLocs, *DAG.getContext());
3126 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00003127
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003128 // If this is the first return lowered for this function, add the regs to the
3129 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00003130 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003131 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00003132 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003133 }
3134
Dan Gohman475871a2008-07-27 21:46:04 +00003135 SDValue Flag;
Scott Michelfdc40a02009-02-17 22:15:04 +00003136
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003137 // Copy the result values into the output registers.
3138 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3139 CCValAssign &VA = RVLocs[i];
3140 assert(VA.isRegLoc() && "Can only return in registers!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003141 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00003142 Outs[i].Val, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003143 Flag = Chain.getValue(1);
3144 }
3145
Gabor Greifba36cb52008-08-28 21:40:38 +00003146 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003148 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner1a635d62006-04-14 06:01:58 +00003150}
3151
Dan Gohman475871a2008-07-27 21:46:04 +00003152SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Jim Laskeyefc7e522006-12-04 22:04:42 +00003153 const PPCSubtarget &Subtarget) {
3154 // When we pop the dynamic allocation we need to restore the SP link.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003155 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003156
Jim Laskeyefc7e522006-12-04 22:04:42 +00003157 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00003158 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00003159
3160 // Construct the stack pointer operand.
3161 bool IsPPC64 = Subtarget.isPPC64();
3162 unsigned SP = IsPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00003163 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003164
3165 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00003166 SDValue Chain = Op.getOperand(0);
3167 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003168
Jim Laskeyefc7e522006-12-04 22:04:42 +00003169 // Load the old link SP.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003170 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr, NULL, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003171
Jim Laskeyefc7e522006-12-04 22:04:42 +00003172 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003173 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00003174
Jim Laskeyefc7e522006-12-04 22:04:42 +00003175 // Store the old link SP.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003176 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, NULL, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003177}
3178
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003179
3180
Dan Gohman475871a2008-07-27 21:46:04 +00003181SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003182PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003183 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003184 bool IsPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003185 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00003186 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003187
3188 // Get current frame pointer save index. The users of this index will be
3189 // primarily DYNALLOC instructions.
3190 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3191 int RASI = FI->getReturnAddrSaveIndex();
3192
3193 // If the frame pointer save index hasn't been defined yet.
3194 if (!RASI) {
3195 // Find out what the fix offset of the frame pointer save area.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003196 int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64, isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003197 // Allocate the frame index for frame pointer save area.
3198 RASI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, LROffset);
3199 // Save the result.
3200 FI->setReturnAddrSaveIndex(RASI);
3201 }
3202 return DAG.getFrameIndex(RASI, PtrVT);
3203}
3204
Dan Gohman475871a2008-07-27 21:46:04 +00003205SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003206PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
3207 MachineFunction &MF = DAG.getMachineFunction();
3208 bool IsPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003209 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00003210 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003211
3212 // Get current frame pointer save index. The users of this index will be
3213 // primarily DYNALLOC instructions.
3214 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3215 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003216
Jim Laskey2f616bf2006-11-16 22:43:37 +00003217 // If the frame pointer save index hasn't been defined yet.
3218 if (!FPSI) {
3219 // Find out what the fix offset of the frame pointer save area.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003220 int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64,
3221 isDarwinABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00003222
Jim Laskey2f616bf2006-11-16 22:43:37 +00003223 // Allocate the frame index for frame pointer save area.
Scott Michelfdc40a02009-02-17 22:15:04 +00003224 FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003225 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00003226 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003227 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003228 return DAG.getFrameIndex(FPSI, PtrVT);
3229}
Jim Laskey2f616bf2006-11-16 22:43:37 +00003230
Dan Gohman475871a2008-07-27 21:46:04 +00003231SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003232 SelectionDAG &DAG,
3233 const PPCSubtarget &Subtarget) {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003234 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00003235 SDValue Chain = Op.getOperand(0);
3236 SDValue Size = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003237 DebugLoc dl = Op.getDebugLoc();
3238
Jim Laskey2f616bf2006-11-16 22:43:37 +00003239 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00003240 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003241 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00003242 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00003243 DAG.getConstant(0, PtrVT), Size);
3244 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00003245 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003246 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00003247 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00003249 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003250}
3251
Chris Lattner1a635d62006-04-14 06:01:58 +00003252/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
3253/// possible.
Dan Gohman475871a2008-07-27 21:46:04 +00003254SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
Chris Lattner1a635d62006-04-14 06:01:58 +00003255 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003256 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
3257 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00003258 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003259
Chris Lattner1a635d62006-04-14 06:01:58 +00003260 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003261
Chris Lattner1a635d62006-04-14 06:01:58 +00003262 // Cannot handle SETEQ/SETNE.
Eli Friedmanc06441e2009-05-28 04:31:08 +00003263 if (CC == ISD::SETEQ || CC == ISD::SETNE) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003264
Owen Andersone50ed302009-08-10 22:56:29 +00003265 EVT ResVT = Op.getValueType();
3266 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003267 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3268 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00003269 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003270
Chris Lattner1a635d62006-04-14 06:01:58 +00003271 // If the RHS of the comparison is a 0.0, we don't need to do the
3272 // subtraction at all.
3273 if (isFloatingPointZero(RHS))
3274 switch (CC) {
3275 default: break; // SETUO etc aren't handled by fsel.
3276 case ISD::SETULT:
3277 case ISD::SETLT:
3278 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003279 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003280 case ISD::SETGE:
Owen Anderson825b72b2009-08-11 20:47:22 +00003281 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
3282 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00003283 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003284 case ISD::SETUGT:
3285 case ISD::SETGT:
3286 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003287 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003288 case ISD::SETLE:
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
3290 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00003291 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003293 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003294
Dan Gohman475871a2008-07-27 21:46:04 +00003295 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00003296 switch (CC) {
3297 default: break; // SETUO etc aren't handled by fsel.
3298 case ISD::SETULT:
3299 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00003300 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003301 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3302 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003303 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003304 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003305 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00003306 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003307 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3308 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003309 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003310 case ISD::SETUGT:
3311 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00003312 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3314 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003315 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003316 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003317 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00003318 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3320 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003321 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003322 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00003323 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00003324}
3325
Chris Lattner1f873002007-11-28 18:44:47 +00003326// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003327SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00003328 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00003330 SDValue Src = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 if (Src.getValueType() == MVT::f32)
3332 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003333
Dan Gohman475871a2008-07-27 21:46:04 +00003334 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003336 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003338 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
3339 PPCISD::FCTIDZ,
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003341 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 case MVT::i64:
3343 Tmp = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003344 break;
3345 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00003346
Chris Lattner1a635d62006-04-14 06:01:58 +00003347 // Convert the FP value to an int value through memory.
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 SDValue FIPtr = DAG.CreateStackTemporary(MVT::f64);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003349
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003350 // Emit a store to the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003351 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, NULL, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003352
3353 // Result is a load from the stack slot. If loading 4 bytes, make sure to
3354 // add in a bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 if (Op.getValueType() == MVT::i32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003356 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003357 DAG.getConstant(4, FIPtr.getValueType()));
Dale Johannesen33c960f2009-02-04 20:06:27 +00003358 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003359}
3360
Dan Gohman475871a2008-07-27 21:46:04 +00003361SDValue PPCTargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003362 DebugLoc dl = Op.getDebugLoc();
Dan Gohman034f60e2008-03-11 01:59:03 +00003363 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00003365 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00003366
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 if (Op.getOperand(0).getValueType() == MVT::i64) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003368 SDValue Bits = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 MVT::f64, Op.getOperand(0));
3370 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Bits);
3371 if (Op.getValueType() == MVT::f32)
Scott Michelfdc40a02009-02-17 22:15:04 +00003372 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003374 return FP;
3375 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003376
Owen Anderson825b72b2009-08-11 20:47:22 +00003377 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Chris Lattner1a635d62006-04-14 06:01:58 +00003378 "Unhandled SINT_TO_FP type in custom expander!");
3379 // Since we only generate this in 64-bit mode, we can take advantage of
3380 // 64-bit registers. In particular, sign extend the input value into the
3381 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
3382 // then lfd it and fcfid it.
3383 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
3384 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
Owen Andersone50ed302009-08-10 22:56:29 +00003385 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003386 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003387
Owen Anderson825b72b2009-08-11 20:47:22 +00003388 SDValue Ext64 = DAG.getNode(PPCISD::EXTSW_32, dl, MVT::i32,
Chris Lattner1a635d62006-04-14 06:01:58 +00003389 Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003390
Chris Lattner1a635d62006-04-14 06:01:58 +00003391 // STD the extended value into the stack slot.
Dan Gohmana54cf172008-07-11 22:44:52 +00003392 MachineMemOperand MO(PseudoSourceValue::getFixedStack(FrameIdx),
3393 MachineMemOperand::MOStore, 0, 8, 8);
Owen Anderson825b72b2009-08-11 20:47:22 +00003394 SDValue Store = DAG.getNode(PPCISD::STD_32, dl, MVT::Other,
Chris Lattner1a635d62006-04-14 06:01:58 +00003395 DAG.getEntryNode(), Ext64, FIdx,
Dan Gohman69de1932008-02-06 22:27:42 +00003396 DAG.getMemOperand(MO));
Chris Lattner1a635d62006-04-14 06:01:58 +00003397 // Load the value as a double.
Owen Anderson825b72b2009-08-11 20:47:22 +00003398 SDValue Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, NULL, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003399
Chris Lattner1a635d62006-04-14 06:01:58 +00003400 // FCFID it and return it.
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Ld);
3402 if (Op.getValueType() == MVT::f32)
3403 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003404 return FP;
3405}
3406
Dan Gohman475871a2008-07-27 21:46:04 +00003407SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003408 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003409 /*
3410 The rounding mode is in bits 30:31 of FPSR, and has the following
3411 settings:
3412 00 Round to nearest
3413 01 Round to 0
3414 10 Round to +inf
3415 11 Round to -inf
3416
3417 FLT_ROUNDS, on the other hand, expects the following:
3418 -1 Undefined
3419 0 Round to 0
3420 1 Round to nearest
3421 2 Round to +inf
3422 3 Round to -inf
3423
3424 To perform the conversion, we do:
3425 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
3426 */
3427
3428 MachineFunction &MF = DAG.getMachineFunction();
Owen Andersone50ed302009-08-10 22:56:29 +00003429 EVT VT = Op.getValueType();
3430 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3431 std::vector<EVT> NodeTys;
Dan Gohman475871a2008-07-27 21:46:04 +00003432 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003433
3434 // Save FP Control Word to register
Owen Anderson825b72b2009-08-11 20:47:22 +00003435 NodeTys.push_back(MVT::f64); // return register
3436 NodeTys.push_back(MVT::Flag); // unused in this context
Dale Johannesen33c960f2009-02-04 20:06:27 +00003437 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003438
3439 // Save FP register to stack slot
3440 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00003441 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003442 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003443 StackSlot, NULL, 0);
3444
3445 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00003446 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003447 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, NULL, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003449
3450 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00003451 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00003452 DAG.getNode(ISD::AND, dl, MVT::i32,
3453 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00003454 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 DAG.getNode(ISD::SRL, dl, MVT::i32,
3456 DAG.getNode(ISD::AND, dl, MVT::i32,
3457 DAG.getNode(ISD::XOR, dl, MVT::i32,
3458 CWD, DAG.getConstant(3, MVT::i32)),
3459 DAG.getConstant(3, MVT::i32)),
3460 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003461
Dan Gohman475871a2008-07-27 21:46:04 +00003462 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003464
Duncan Sands83ec4b62008-06-06 12:08:01 +00003465 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00003466 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003467}
3468
Dan Gohman475871a2008-07-27 21:46:04 +00003469SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003470 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003471 unsigned BitWidth = VT.getSizeInBits();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003472 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003473 assert(Op.getNumOperands() == 3 &&
3474 VT == Op.getOperand(1).getValueType() &&
3475 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003476
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00003477 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003478 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003479 SDValue Lo = Op.getOperand(0);
3480 SDValue Hi = Op.getOperand(1);
3481 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003482 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003483
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003484 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003485 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003486 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
3487 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
3488 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
3489 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003490 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003491 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
3492 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3493 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003494 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003495 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003496}
3497
Dan Gohman475871a2008-07-27 21:46:04 +00003498SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003499 EVT VT = Op.getValueType();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003500 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003501 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003502 assert(Op.getNumOperands() == 3 &&
3503 VT == Op.getOperand(1).getValueType() &&
3504 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003505
Dan Gohman9ed06db2008-03-07 20:36:53 +00003506 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003507 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003508 SDValue Lo = Op.getOperand(0);
3509 SDValue Hi = Op.getOperand(1);
3510 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003511 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003512
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003513 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003514 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003515 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3516 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3517 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3518 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003519 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003520 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
3521 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3522 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003523 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003524 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003525}
3526
Dan Gohman475871a2008-07-27 21:46:04 +00003527SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003528 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003529 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003530 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003531 assert(Op.getNumOperands() == 3 &&
3532 VT == Op.getOperand(1).getValueType() &&
3533 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003534
Dan Gohman9ed06db2008-03-07 20:36:53 +00003535 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00003536 SDValue Lo = Op.getOperand(0);
3537 SDValue Hi = Op.getOperand(1);
3538 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003539 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003540
Dale Johannesenf5d97892009-02-04 01:48:28 +00003541 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003542 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00003543 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3544 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3545 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3546 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003547 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00003548 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
3549 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
3550 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003551 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00003552 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003553 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003554}
3555
3556//===----------------------------------------------------------------------===//
3557// Vector related lowering.
3558//
3559
Chris Lattner4a998b92006-04-17 06:00:21 +00003560/// BuildSplatI - Build a canonical splati of Val with an element size of
3561/// SplatSize. Cast the result to VT.
Owen Andersone50ed302009-08-10 22:56:29 +00003562static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Dale Johannesened2eee62009-02-06 01:31:28 +00003563 SelectionDAG &DAG, DebugLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00003564 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00003565
Owen Andersone50ed302009-08-10 22:56:29 +00003566 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner4a998b92006-04-17 06:00:21 +00003568 };
Chris Lattner70fa4932006-12-01 01:45:39 +00003569
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003571
Chris Lattner70fa4932006-12-01 01:45:39 +00003572 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
3573 if (Val == -1)
3574 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00003575
Owen Andersone50ed302009-08-10 22:56:29 +00003576 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003577
Chris Lattner4a998b92006-04-17 06:00:21 +00003578 // Build a canonical splat for this value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003580 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003581 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00003582 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
3583 &Ops[0], Ops.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00003584 return DAG.getNode(ISD::BIT_CONVERT, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003585}
3586
Chris Lattnere7c768e2006-04-18 03:24:30 +00003587/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00003588/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003589static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Dale Johannesened2eee62009-02-06 01:31:28 +00003590 SelectionDAG &DAG, DebugLoc dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003591 EVT DestVT = MVT::Other) {
3592 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003593 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner6876e662006-04-17 06:58:41 +00003595}
3596
Chris Lattnere7c768e2006-04-18 03:24:30 +00003597/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
3598/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003599static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00003600 SDValue Op2, SelectionDAG &DAG,
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 DebugLoc dl, EVT DestVT = MVT::Other) {
3602 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003603 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnere7c768e2006-04-18 03:24:30 +00003605}
3606
3607
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003608/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
3609/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003610static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Owen Andersone50ed302009-08-10 22:56:29 +00003611 EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003612 // Force LHS/RHS to be the right type.
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, LHS);
3614 RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00003615
Nate Begeman9008ca62009-04-27 18:41:29 +00003616 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003617 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003618 Ops[i] = i + Amt;
Owen Anderson825b72b2009-08-11 20:47:22 +00003619 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Dale Johannesened2eee62009-02-06 01:31:28 +00003620 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003621}
3622
Chris Lattnerf1b47082006-04-14 05:19:18 +00003623// If this is a case we can't handle, return null and let the default
3624// expansion code take care of it. If we CAN select this case, and if it
3625// selects to a single instruction, return Op. Otherwise, if we can codegen
3626// this case more efficiently than a constant pool load, lower it to the
3627// sequence of ops that should be used.
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003628SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00003629 DebugLoc dl = Op.getDebugLoc();
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003630 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3631 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00003632
Bob Wilson24e338e2009-03-02 23:24:16 +00003633 // Check if this is a splat of a constant value.
3634 APInt APSplatBits, APSplatUndef;
3635 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003636 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00003637 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
3638 HasAnyUndefs) || SplatBitSize > 32)
3639 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00003640
Bob Wilsonf2950b02009-03-03 19:26:27 +00003641 unsigned SplatBits = APSplatBits.getZExtValue();
3642 unsigned SplatUndef = APSplatUndef.getZExtValue();
3643 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00003644
Bob Wilsonf2950b02009-03-03 19:26:27 +00003645 // First, handle single instruction cases.
3646
3647 // All zeros?
3648 if (SplatBits == 0) {
3649 // Canonicalize all zero vectors to be v4i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00003650 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
3651 SDValue Z = DAG.getConstant(0, MVT::i32);
3652 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003653 Op = DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003654 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003655 return Op;
3656 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00003657
Bob Wilsonf2950b02009-03-03 19:26:27 +00003658 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
3659 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
3660 (32-SplatBitSize));
3661 if (SextVal >= -16 && SextVal <= 15)
3662 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003663
3664
Bob Wilsonf2950b02009-03-03 19:26:27 +00003665 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00003666
Bob Wilsonf2950b02009-03-03 19:26:27 +00003667 // If this value is in the range [-32,30] and is even, use:
3668 // tmp = VSPLTI[bhw], result = add tmp, tmp
3669 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003670 SDValue Res = BuildSplatI(SextVal >> 1, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003671 Res = DAG.getNode(ISD::ADD, dl, Res.getValueType(), Res, Res);
3672 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3673 }
3674
3675 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
3676 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
3677 // for fneg/fabs.
3678 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
3679 // Make -1 and vspltisw -1:
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003681
3682 // Make the VSLW intrinsic, computing 0x8000_0000.
3683 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
3684 OnesV, DAG, dl);
3685
3686 // xor by OnesV to invert it.
Owen Anderson825b72b2009-08-11 20:47:22 +00003687 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003688 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3689 }
3690
3691 // Check to see if this is a wide variety of vsplti*, binop self cases.
3692 static const signed char SplatCsts[] = {
3693 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
3694 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
3695 };
3696
3697 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
3698 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
3699 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
3700 int i = SplatCsts[idx];
3701
3702 // Figure out what shift amount will be used by altivec if shifted by i in
3703 // this splat size.
3704 unsigned TypeShiftAmt = i & (SplatBitSize-1);
3705
3706 // vsplti + shl self.
3707 if (SextVal == (i << (int)TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003708 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003709 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3710 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
3711 Intrinsic::ppc_altivec_vslw
3712 };
3713 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Dale Johannesened2eee62009-02-06 01:31:28 +00003714 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003715 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003716
Bob Wilsonf2950b02009-03-03 19:26:27 +00003717 // vsplti + srl self.
3718 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003719 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003720 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3721 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
3722 Intrinsic::ppc_altivec_vsrw
3723 };
3724 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Dale Johannesened2eee62009-02-06 01:31:28 +00003725 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003726 }
3727
Bob Wilsonf2950b02009-03-03 19:26:27 +00003728 // vsplti + sra self.
3729 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003730 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003731 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3732 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
3733 Intrinsic::ppc_altivec_vsraw
3734 };
3735 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
3736 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00003737 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003738
Bob Wilsonf2950b02009-03-03 19:26:27 +00003739 // vsplti + rol self.
3740 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
3741 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003742 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003743 static const unsigned IIDs[] = { // Intrinsic to use for each size.
3744 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
3745 Intrinsic::ppc_altivec_vrlw
3746 };
3747 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
3748 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
3749 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003750
Bob Wilsonf2950b02009-03-03 19:26:27 +00003751 // t = vsplti c, result = vsldoi t, t, 1
3752 if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003753 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003754 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00003755 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003756 // t = vsplti c, result = vsldoi t, t, 2
3757 if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003758 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003759 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003760 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003761 // t = vsplti c, result = vsldoi t, t, 3
3762 if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003763 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003764 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
3765 }
3766 }
3767
3768 // Three instruction sequences.
3769
3770 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
3771 if (SextVal >= 0 && SextVal <= 31) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003772 SDValue LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG, dl);
3773 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003774 LHS = DAG.getNode(ISD::SUB, dl, LHS.getValueType(), LHS, RHS);
3775 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
3776 }
3777 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
3778 if (SextVal >= -31 && SextVal <= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003779 SDValue LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG, dl);
3780 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00003781 LHS = DAG.getNode(ISD::ADD, dl, LHS.getValueType(), LHS, RHS);
3782 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003783 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003784
Dan Gohman475871a2008-07-27 21:46:04 +00003785 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00003786}
3787
Chris Lattner59138102006-04-17 05:28:54 +00003788/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
3789/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00003790static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00003791 SDValue RHS, SelectionDAG &DAG,
Dale Johannesened2eee62009-02-06 01:31:28 +00003792 DebugLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00003793 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00003794 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00003795 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003796
Chris Lattner59138102006-04-17 05:28:54 +00003797 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00003798 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 +00003799 OP_VMRGHW,
3800 OP_VMRGLW,
3801 OP_VSPLTISW0,
3802 OP_VSPLTISW1,
3803 OP_VSPLTISW2,
3804 OP_VSPLTISW3,
3805 OP_VSLDOI4,
3806 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00003807 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00003808 };
Scott Michelfdc40a02009-02-17 22:15:04 +00003809
Chris Lattner59138102006-04-17 05:28:54 +00003810 if (OpNum == OP_COPY) {
3811 if (LHSID == (1*9+2)*9+3) return LHS;
3812 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
3813 return RHS;
3814 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003815
Dan Gohman475871a2008-07-27 21:46:04 +00003816 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00003817 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
3818 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003819
Nate Begeman9008ca62009-04-27 18:41:29 +00003820 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00003821 switch (OpNum) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003822 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner59138102006-04-17 05:28:54 +00003823 case OP_VMRGHW:
3824 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
3825 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
3826 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
3827 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
3828 break;
3829 case OP_VMRGLW:
3830 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
3831 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
3832 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
3833 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
3834 break;
3835 case OP_VSPLTISW0:
3836 for (unsigned i = 0; i != 16; ++i)
3837 ShufIdxs[i] = (i&3)+0;
3838 break;
3839 case OP_VSPLTISW1:
3840 for (unsigned i = 0; i != 16; ++i)
3841 ShufIdxs[i] = (i&3)+4;
3842 break;
3843 case OP_VSPLTISW2:
3844 for (unsigned i = 0; i != 16; ++i)
3845 ShufIdxs[i] = (i&3)+8;
3846 break;
3847 case OP_VSPLTISW3:
3848 for (unsigned i = 0; i != 16; ++i)
3849 ShufIdxs[i] = (i&3)+12;
3850 break;
3851 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00003852 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003853 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00003854 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003855 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00003856 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003857 }
Owen Andersone50ed302009-08-10 22:56:29 +00003858 EVT VT = OpLHS.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00003859 OpLHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpLHS);
3860 OpRHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpRHS);
3861 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Nate Begeman9008ca62009-04-27 18:41:29 +00003862 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00003863}
3864
Chris Lattnerf1b47082006-04-14 05:19:18 +00003865/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
3866/// is a shuffle we can handle in a single instruction, return it. Otherwise,
3867/// return the code it can be lowered into. Worst case, it can always be
3868/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00003869SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00003871 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003872 SDValue V1 = Op.getOperand(0);
3873 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00003875 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003876
Chris Lattnerf1b47082006-04-14 05:19:18 +00003877 // Cases that are handled by instructions that take permute immediates
3878 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
3879 // selected by the instruction selector.
3880 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003881 if (PPC::isSplatShuffleMask(SVOp, 1) ||
3882 PPC::isSplatShuffleMask(SVOp, 2) ||
3883 PPC::isSplatShuffleMask(SVOp, 4) ||
3884 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
3885 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
3886 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
3887 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
3888 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
3889 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
3890 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
3891 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
3892 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00003893 return Op;
3894 }
3895 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003896
Chris Lattnerf1b47082006-04-14 05:19:18 +00003897 // Altivec has a variety of "shuffle immediates" that take two vector inputs
3898 // and produce a fixed permutation. If any of these match, do not lower to
3899 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
3901 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
3902 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
3903 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
3904 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
3905 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
3906 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
3907 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
3908 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00003909 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003910
Chris Lattner59138102006-04-17 05:28:54 +00003911 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
3912 // perfect shuffle table to emit an optimal matching sequence.
Nate Begeman9008ca62009-04-27 18:41:29 +00003913 SmallVector<int, 16> PermMask;
3914 SVOp->getMask(PermMask);
3915
Chris Lattner59138102006-04-17 05:28:54 +00003916 unsigned PFIndexes[4];
3917 bool isFourElementShuffle = true;
3918 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
3919 unsigned EltNo = 8; // Start out undef.
3920 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00003922 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00003923
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00003925 if ((ByteSource & 3) != j) {
3926 isFourElementShuffle = false;
3927 break;
3928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003929
Chris Lattner59138102006-04-17 05:28:54 +00003930 if (EltNo == 8) {
3931 EltNo = ByteSource/4;
3932 } else if (EltNo != ByteSource/4) {
3933 isFourElementShuffle = false;
3934 break;
3935 }
3936 }
3937 PFIndexes[i] = EltNo;
3938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003939
3940 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00003941 // perfect shuffle vector to determine if it is cost effective to do this as
3942 // discrete instructions, or whether we should use a vperm.
3943 if (isFourElementShuffle) {
3944 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00003945 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00003946 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00003947
Chris Lattner59138102006-04-17 05:28:54 +00003948 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
3949 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00003950
Chris Lattner59138102006-04-17 05:28:54 +00003951 // Determining when to avoid vperm is tricky. Many things affect the cost
3952 // of vperm, particularly how many times the perm mask needs to be computed.
3953 // For example, if the perm mask can be hoisted out of a loop or is already
3954 // used (perhaps because there are multiple permutes with the same shuffle
3955 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
3956 // the loop requires an extra register.
3957 //
3958 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00003959 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00003960 // available, if this block is within a loop, we should avoid using vperm
3961 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00003962 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00003963 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00003964 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003965
Chris Lattnerf1b47082006-04-14 05:19:18 +00003966 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
3967 // vector that will get spilled to the constant pool.
3968 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00003969
Chris Lattnerf1b47082006-04-14 05:19:18 +00003970 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
3971 // that it is in input element units, not in bytes. Convert now.
Owen Andersone50ed302009-08-10 22:56:29 +00003972 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003973 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00003974
Dan Gohman475871a2008-07-27 21:46:04 +00003975 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00003976 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
3977 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00003978
Chris Lattnerf1b47082006-04-14 05:19:18 +00003979 for (unsigned j = 0; j != BytesPerElement; ++j)
3980 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson825b72b2009-08-11 20:47:22 +00003981 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00003982 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003983
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga87008d2009-02-25 22:49:59 +00003985 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00003986 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003987}
3988
Chris Lattner90564f22006-04-18 17:59:36 +00003989/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
3990/// altivec comparison. If it is, return true and fill in Opc/isDot with
3991/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00003992static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00003993 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003994 unsigned IntrinsicID =
3995 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00003996 CompareOpc = -1;
3997 isDot = false;
3998 switch (IntrinsicID) {
3999 default: return false;
4000 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00004001 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
4002 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
4003 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
4004 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
4005 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
4006 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
4007 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
4008 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
4009 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
4010 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
4011 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
4012 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
4013 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004014
Chris Lattner1a635d62006-04-14 06:01:58 +00004015 // Normal Comparisons.
4016 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
4017 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
4018 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
4019 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
4020 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
4021 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
4022 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
4023 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
4024 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
4025 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
4026 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
4027 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
4028 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
4029 }
Chris Lattner90564f22006-04-18 17:59:36 +00004030 return true;
4031}
4032
4033/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
4034/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00004035SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004036 SelectionDAG &DAG) {
Chris Lattner90564f22006-04-18 17:59:36 +00004037 // If this is a lowered altivec predicate compare, CompareOpc is set to the
4038 // opcode number of the comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00004039 DebugLoc dl = Op.getDebugLoc();
Chris Lattner90564f22006-04-18 17:59:36 +00004040 int CompareOpc;
4041 bool isDot;
4042 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00004043 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00004044
Chris Lattner90564f22006-04-18 17:59:36 +00004045 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00004046 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004047 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner1a635d62006-04-14 06:01:58 +00004048 Op.getOperand(1), Op.getOperand(2),
Owen Anderson825b72b2009-08-11 20:47:22 +00004049 DAG.getConstant(CompareOpc, MVT::i32));
Dale Johannesen3484c092009-02-05 22:07:54 +00004050 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00004051 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004052
Chris Lattner1a635d62006-04-14 06:01:58 +00004053 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00004054 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00004055 Op.getOperand(2), // LHS
4056 Op.getOperand(3), // RHS
Owen Anderson825b72b2009-08-11 20:47:22 +00004057 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00004058 };
Owen Andersone50ed302009-08-10 22:56:29 +00004059 std::vector<EVT> VTs;
Chris Lattner1a635d62006-04-14 06:01:58 +00004060 VTs.push_back(Op.getOperand(2).getValueType());
Owen Anderson825b72b2009-08-11 20:47:22 +00004061 VTs.push_back(MVT::Flag);
Dale Johannesen3484c092009-02-05 22:07:54 +00004062 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00004063
Chris Lattner1a635d62006-04-14 06:01:58 +00004064 // Now that we have the comparison, emit a copy from the CR to a GPR.
4065 // This is flagged to the above dot comparison.
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32,
4067 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00004068 CompNode.getValue(1));
4069
Chris Lattner1a635d62006-04-14 06:01:58 +00004070 // Unpack the result based on how the target uses it.
4071 unsigned BitNo; // Bit # of CR6.
4072 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004073 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00004074 default: // Can't happen, don't crash on invalid number though.
4075 case 0: // Return the value of the EQ bit of CR6.
4076 BitNo = 0; InvertBit = false;
4077 break;
4078 case 1: // Return the inverted value of the EQ bit of CR6.
4079 BitNo = 0; InvertBit = true;
4080 break;
4081 case 2: // Return the value of the LT bit of CR6.
4082 BitNo = 2; InvertBit = false;
4083 break;
4084 case 3: // Return the inverted value of the LT bit of CR6.
4085 BitNo = 2; InvertBit = true;
4086 break;
4087 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004088
Chris Lattner1a635d62006-04-14 06:01:58 +00004089 // Shift the bit into the low position.
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
4091 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00004092 // Isolate the bit.
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
4094 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00004095
Chris Lattner1a635d62006-04-14 06:01:58 +00004096 // If we are supposed to, toggle the bit.
4097 if (InvertBit)
Owen Anderson825b72b2009-08-11 20:47:22 +00004098 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
4099 DAG.getConstant(1, MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00004100 return Flags;
4101}
4102
Scott Michelfdc40a02009-02-17 22:15:04 +00004103SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dale Johannesen5b3b6952008-03-04 23:17:14 +00004104 SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004105 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00004106 // Create a stack slot that is 16-byte aligned.
4107 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
4108 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
Owen Andersone50ed302009-08-10 22:56:29 +00004109 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004110 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004111
Chris Lattner1a635d62006-04-14 06:01:58 +00004112 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004113 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Evan Cheng8b2794a2006-10-13 21:14:26 +00004114 Op.getOperand(0), FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004115 // Load it out.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004116 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, NULL, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004117}
4118
Dan Gohman475871a2008-07-27 21:46:04 +00004119SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) {
Dale Johannesened2eee62009-02-06 01:31:28 +00004120 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004121 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00004122 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004123
Owen Anderson825b72b2009-08-11 20:47:22 +00004124 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
4125 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00004126
Dan Gohman475871a2008-07-27 21:46:04 +00004127 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00004128 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00004129
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004130 // Shrinkify inputs to v8i16.
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, LHS);
4132 RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHS);
4133 RHSSwap = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00004134
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004135 // Low parts multiplied together, generating 32-bit results (we ignore the
4136 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00004137 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004139
Dan Gohman475871a2008-07-27 21:46:04 +00004140 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson825b72b2009-08-11 20:47:22 +00004141 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004142 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00004143 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00004144 Neg16, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
4146 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004147 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004148
Owen Anderson825b72b2009-08-11 20:47:22 +00004149 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004150
Chris Lattnercea2aa72006-04-18 04:28:57 +00004151 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00004152 LHS, RHS, Zero, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004154 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004155
Chris Lattner19a81522006-04-18 03:57:35 +00004156 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004157 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson825b72b2009-08-11 20:47:22 +00004158 LHS, RHS, DAG, dl, MVT::v8i16);
4159 EvenParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004160
Chris Lattner19a81522006-04-18 03:57:35 +00004161 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004162 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 LHS, RHS, DAG, dl, MVT::v8i16);
4164 OddParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004165
Chris Lattner19a81522006-04-18 03:57:35 +00004166 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00004167 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00004168 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004169 Ops[i*2 ] = 2*i+1;
4170 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00004171 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004173 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004174 llvm_unreachable("Unknown mul to lower!");
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004175 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00004176}
4177
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004178/// LowerOperation - Provide custom lowering hooks for some operations.
4179///
Dan Gohman475871a2008-07-27 21:46:04 +00004180SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004181 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004182 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00004183 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4184 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00004185 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Nate Begeman37efe672006-04-22 18:53:45 +00004186 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00004187 case ISD::SETCC: return LowerSETCC(Op, DAG);
Bill Wendling77959322008-09-17 00:30:57 +00004188 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004189 case ISD::VASTART:
Nicolas Geoffray01119992007-04-03 13:59:52 +00004190 return LowerVASTART(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
4191 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00004192
4193 case ISD::VAARG:
Nicolas Geoffray01119992007-04-03 13:59:52 +00004194 return LowerVAARG(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
4195 VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
4196
Jim Laskeyefc7e522006-12-04 22:04:42 +00004197 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00004198 case ISD::DYNAMIC_STACKALLOC:
4199 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00004200
Chris Lattner1a635d62006-04-14 06:01:58 +00004201 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004202 case ISD::FP_TO_UINT:
4203 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00004204 Op.getDebugLoc());
Chris Lattner1a635d62006-04-14 06:01:58 +00004205 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00004206 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004207
Chris Lattner1a635d62006-04-14 06:01:58 +00004208 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00004209 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
4210 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
4211 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004212
Chris Lattner1a635d62006-04-14 06:01:58 +00004213 // Vector-related lowering.
4214 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4215 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4216 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4217 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00004218 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004219
Chris Lattner3fc027d2007-12-08 06:59:59 +00004220 // Frame & Return address.
4221 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004222 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00004223 }
Dan Gohman475871a2008-07-27 21:46:04 +00004224 return SDValue();
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004225}
4226
Duncan Sands1607f052008-12-01 11:39:25 +00004227void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
4228 SmallVectorImpl<SDValue>&Results,
4229 SelectionDAG &DAG) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004230 DebugLoc dl = N->getDebugLoc();
Chris Lattner1f873002007-11-28 18:44:47 +00004231 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00004232 default:
Duncan Sands1607f052008-12-01 11:39:25 +00004233 assert(false && "Do not know how to custom type legalize this operation!");
4234 return;
4235 case ISD::FP_ROUND_INREG: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 assert(N->getValueType(0) == MVT::ppcf128);
4237 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00004238 SDValue Lo = 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(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00004241 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004243 DAG.getIntPtrConstant(1));
4244
4245 // This sequence changes FPSCR to do round-to-zero, adds the two halves
4246 // of the long double, and puts FPSCR back the way it was. We do not
4247 // actually model FPSCR.
Owen Andersone50ed302009-08-10 22:56:29 +00004248 std::vector<EVT> NodeTys;
Duncan Sands1607f052008-12-01 11:39:25 +00004249 SDValue Ops[4], Result, MFFSreg, InFlag, FPreg;
4250
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 NodeTys.push_back(MVT::f64); // Return register
4252 NodeTys.push_back(MVT::Flag); // Returns a flag for later insns
Dale Johannesen3484c092009-02-05 22:07:54 +00004253 Result = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Duncan Sands1607f052008-12-01 11:39:25 +00004254 MFFSreg = Result.getValue(0);
4255 InFlag = Result.getValue(1);
4256
4257 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004258 NodeTys.push_back(MVT::Flag); // Returns a flag
4259 Ops[0] = DAG.getConstant(31, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004260 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004261 Result = DAG.getNode(PPCISD::MTFSB1, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004262 InFlag = Result.getValue(0);
4263
4264 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004265 NodeTys.push_back(MVT::Flag); // Returns a flag
4266 Ops[0] = DAG.getConstant(30, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004267 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004268 Result = DAG.getNode(PPCISD::MTFSB0, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004269 InFlag = Result.getValue(0);
4270
4271 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004272 NodeTys.push_back(MVT::f64); // result of add
4273 NodeTys.push_back(MVT::Flag); // Returns a flag
Duncan Sands1607f052008-12-01 11:39:25 +00004274 Ops[0] = Lo;
4275 Ops[1] = Hi;
4276 Ops[2] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004277 Result = DAG.getNode(PPCISD::FADDRTZ, dl, NodeTys, Ops, 3);
Duncan Sands1607f052008-12-01 11:39:25 +00004278 FPreg = Result.getValue(0);
4279 InFlag = Result.getValue(1);
4280
4281 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004282 NodeTys.push_back(MVT::f64);
4283 Ops[0] = DAG.getConstant(1, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004284 Ops[1] = MFFSreg;
4285 Ops[2] = FPreg;
4286 Ops[3] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004287 Result = DAG.getNode(PPCISD::MTFSF, dl, NodeTys, Ops, 4);
Duncan Sands1607f052008-12-01 11:39:25 +00004288 FPreg = Result.getValue(0);
4289
4290 // We know the low half is about to be thrown away, so just use something
4291 // convenient.
Owen Anderson825b72b2009-08-11 20:47:22 +00004292 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00004293 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00004294 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00004295 }
Duncan Sands1607f052008-12-01 11:39:25 +00004296 case ISD::FP_TO_SINT:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004297 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00004298 return;
Chris Lattner1f873002007-11-28 18:44:47 +00004299 }
4300}
4301
4302
Chris Lattner1a635d62006-04-14 06:01:58 +00004303//===----------------------------------------------------------------------===//
4304// Other Lowering Code
4305//===----------------------------------------------------------------------===//
4306
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004307MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004308PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004309 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004310 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004311 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4312
4313 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4314 MachineFunction *F = BB->getParent();
4315 MachineFunction::iterator It = BB;
4316 ++It;
4317
4318 unsigned dest = MI->getOperand(0).getReg();
4319 unsigned ptrA = MI->getOperand(1).getReg();
4320 unsigned ptrB = MI->getOperand(2).getReg();
4321 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004322 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004323
4324 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4325 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4326 F->insert(It, loopMBB);
4327 F->insert(It, exitMBB);
4328 exitMBB->transferSuccessors(BB);
4329
4330 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00004331 unsigned TmpReg = (!BinOpcode) ? incr :
4332 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00004333 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4334 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004335
4336 // thisMBB:
4337 // ...
4338 // fallthrough --> loopMBB
4339 BB->addSuccessor(loopMBB);
4340
4341 // loopMBB:
4342 // l[wd]arx dest, ptr
4343 // add r0, dest, incr
4344 // st[wd]cx. r0, ptr
4345 // bne- loopMBB
4346 // fallthrough --> exitMBB
4347 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004348 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004349 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004350 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004351 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
4352 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004353 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004354 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004355 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004356 BB->addSuccessor(loopMBB);
4357 BB->addSuccessor(exitMBB);
4358
4359 // exitMBB:
4360 // ...
4361 BB = exitMBB;
4362 return BB;
4363}
4364
4365MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00004366PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00004367 MachineBasicBlock *BB,
4368 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004369 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004370 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00004371 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4372 // In 64 bit mode we have to use 64 bits for addresses, even though the
4373 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
4374 // registers without caring whether they're 32 or 64, but here we're
4375 // doing actual arithmetic on the addresses.
4376 bool is64bit = PPCSubTarget.isPPC64();
4377
4378 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4379 MachineFunction *F = BB->getParent();
4380 MachineFunction::iterator It = BB;
4381 ++It;
4382
4383 unsigned dest = MI->getOperand(0).getReg();
4384 unsigned ptrA = MI->getOperand(1).getReg();
4385 unsigned ptrB = MI->getOperand(2).getReg();
4386 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004387 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00004388
4389 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4390 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4391 F->insert(It, loopMBB);
4392 F->insert(It, exitMBB);
4393 exitMBB->transferSuccessors(BB);
4394
4395 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004396 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004397 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4398 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00004399 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4400 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4401 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4402 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
4403 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4404 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4405 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4406 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4407 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
4408 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004409 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004410 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00004411 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004412
4413 // thisMBB:
4414 // ...
4415 // fallthrough --> loopMBB
4416 BB->addSuccessor(loopMBB);
4417
4418 // The 4-byte load must be aligned, while a char or short may be
4419 // anywhere in the word. Hence all this nasty bookkeeping code.
4420 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4421 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004422 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00004423 // rlwinm ptr, ptr1, 0, 0, 29
4424 // slw incr2, incr, shift
4425 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4426 // slw mask, mask2, shift
4427 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004428 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00004429 // add tmp, tmpDest, incr2
4430 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00004431 // and tmp3, tmp, mask
4432 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004433 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00004434 // bne- loopMBB
4435 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00004436 // srw dest, tmpDest, shift
Dale Johannesen97efa362008-08-28 17:53:09 +00004437
4438 if (ptrA!=PPC::R0) {
4439 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004440 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004441 .addReg(ptrA).addReg(ptrB);
4442 } else {
4443 Ptr1Reg = ptrB;
4444 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004445 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004446 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004447 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004448 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4449 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004450 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004451 .addReg(Ptr1Reg).addImm(0).addImm(61);
4452 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004453 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004454 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004455 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004456 .addReg(incr).addReg(ShiftReg);
4457 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004458 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00004459 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004460 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4461 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00004462 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004463 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004464 .addReg(Mask2Reg).addReg(ShiftReg);
4465
4466 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004467 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004468 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004469 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004470 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004471 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004472 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004473 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004474 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004475 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004476 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004477 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004478 BuildMI(BB, dl, TII->get(PPC::STWCX))
Dale Johannesen97efa362008-08-28 17:53:09 +00004479 .addReg(Tmp4Reg).addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004480 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004481 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00004482 BB->addSuccessor(loopMBB);
4483 BB->addSuccessor(exitMBB);
4484
4485 // exitMBB:
4486 // ...
4487 BB = exitMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004488 BuildMI(BB, dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg).addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00004489 return BB;
4490}
4491
4492MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004493PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004494 MachineBasicBlock *BB) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004495 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00004496
4497 // To "insert" these instructions we actually have to insert their
4498 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004499 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004500 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004501 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00004502
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004503 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00004504
4505 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
4506 MI->getOpcode() == PPC::SELECT_CC_I8 ||
4507 MI->getOpcode() == PPC::SELECT_CC_F4 ||
4508 MI->getOpcode() == PPC::SELECT_CC_F8 ||
4509 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
4510
4511 // The incoming instruction knows the destination vreg to set, the
4512 // condition code register to branch on, the true/false values to
4513 // select between, and a branch opcode to use.
4514
4515 // thisMBB:
4516 // ...
4517 // TrueVal = ...
4518 // cmpTY ccX, r1, r2
4519 // bCC copy1MBB
4520 // fallthrough --> copy0MBB
4521 MachineBasicBlock *thisMBB = BB;
4522 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4523 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4524 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004525 DebugLoc dl = MI->getDebugLoc();
4526 BuildMI(BB, dl, TII->get(PPC::BCC))
Evan Cheng53301922008-07-12 02:23:19 +00004527 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
4528 F->insert(It, copy0MBB);
4529 F->insert(It, sinkMBB);
4530 // Update machine-CFG edges by transferring all successors of the current
4531 // block to the new block which will contain the Phi node for the select.
4532 sinkMBB->transferSuccessors(BB);
4533 // Next, add the true and fallthrough blocks as its successors.
4534 BB->addSuccessor(copy0MBB);
4535 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004536
Evan Cheng53301922008-07-12 02:23:19 +00004537 // copy0MBB:
4538 // %FalseValue = ...
4539 // # fallthrough to sinkMBB
4540 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00004541
Evan Cheng53301922008-07-12 02:23:19 +00004542 // Update machine-CFG edges
4543 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004544
Evan Cheng53301922008-07-12 02:23:19 +00004545 // sinkMBB:
4546 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4547 // ...
4548 BB = sinkMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004549 BuildMI(BB, dl, TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00004550 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
4551 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4552 }
Dale Johannesen97efa362008-08-28 17:53:09 +00004553 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
4554 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
4555 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
4556 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004557 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
4558 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
4559 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
4560 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004561
4562 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
4563 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
4564 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
4565 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004566 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
4567 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
4568 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
4569 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004570
4571 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
4572 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
4573 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
4574 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004575 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
4576 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
4577 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
4578 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004579
4580 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
4581 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
4582 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
4583 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004584 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
4585 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
4586 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
4587 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004588
4589 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00004590 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004591 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00004592 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004593 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00004594 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004595 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00004596 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004597
4598 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
4599 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
4600 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
4601 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004602 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
4603 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
4604 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
4605 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004606
Dale Johannesen0e55f062008-08-29 18:29:46 +00004607 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
4608 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
4609 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
4610 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
4611 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
4612 BB = EmitAtomicBinary(MI, BB, false, 0);
4613 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
4614 BB = EmitAtomicBinary(MI, BB, true, 0);
4615
Evan Cheng53301922008-07-12 02:23:19 +00004616 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
4617 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
4618 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
4619
4620 unsigned dest = MI->getOperand(0).getReg();
4621 unsigned ptrA = MI->getOperand(1).getReg();
4622 unsigned ptrB = MI->getOperand(2).getReg();
4623 unsigned oldval = MI->getOperand(3).getReg();
4624 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004625 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00004626
Dale Johannesen65e39732008-08-25 18:53:26 +00004627 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4628 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4629 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00004630 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004631 F->insert(It, loop1MBB);
4632 F->insert(It, loop2MBB);
4633 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00004634 F->insert(It, exitMBB);
4635 exitMBB->transferSuccessors(BB);
4636
4637 // thisMBB:
4638 // ...
4639 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00004640 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00004641
Dale Johannesen65e39732008-08-25 18:53:26 +00004642 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00004643 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00004644 // cmp[wd] dest, oldval
4645 // bne- midMBB
4646 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00004647 // st[wd]cx. newval, ptr
4648 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00004649 // b exitBB
4650 // midMBB:
4651 // st[wd]cx. dest, ptr
4652 // exitBB:
4653 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004654 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00004655 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004656 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00004657 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004658 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00004659 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
4660 BB->addSuccessor(loop2MBB);
4661 BB->addSuccessor(midMBB);
4662
4663 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004664 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00004665 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004666 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00004667 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004668 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004669 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00004670 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004671
Dale Johannesen65e39732008-08-25 18:53:26 +00004672 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004673 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00004674 .addReg(dest).addReg(ptrA).addReg(ptrB);
4675 BB->addSuccessor(exitMBB);
4676
Evan Cheng53301922008-07-12 02:23:19 +00004677 // exitMBB:
4678 // ...
4679 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004680 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
4681 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
4682 // We must use 64-bit registers for addresses when targeting 64-bit,
4683 // since we're actually doing arithmetic on them. Other registers
4684 // can be 32-bit.
4685 bool is64bit = PPCSubTarget.isPPC64();
4686 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
4687
4688 unsigned dest = MI->getOperand(0).getReg();
4689 unsigned ptrA = MI->getOperand(1).getReg();
4690 unsigned ptrB = MI->getOperand(2).getReg();
4691 unsigned oldval = MI->getOperand(3).getReg();
4692 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004693 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004694
4695 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4696 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4697 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
4698 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4699 F->insert(It, loop1MBB);
4700 F->insert(It, loop2MBB);
4701 F->insert(It, midMBB);
4702 F->insert(It, exitMBB);
4703 exitMBB->transferSuccessors(BB);
4704
4705 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004706 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004707 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4708 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004709 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4710 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4711 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4712 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
4713 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
4714 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
4715 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
4716 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4717 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4718 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4719 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4720 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
4721 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
4722 unsigned Ptr1Reg;
4723 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
4724 // thisMBB:
4725 // ...
4726 // fallthrough --> loopMBB
4727 BB->addSuccessor(loop1MBB);
4728
4729 // The 4-byte load must be aligned, while a char or short may be
4730 // anywhere in the word. Hence all this nasty bookkeeping code.
4731 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4732 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004733 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004734 // rlwinm ptr, ptr1, 0, 0, 29
4735 // slw newval2, newval, shift
4736 // slw oldval2, oldval,shift
4737 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4738 // slw mask, mask2, shift
4739 // and newval3, newval2, mask
4740 // and oldval3, oldval2, mask
4741 // loop1MBB:
4742 // lwarx tmpDest, ptr
4743 // and tmp, tmpDest, mask
4744 // cmpw tmp, oldval3
4745 // bne- midMBB
4746 // loop2MBB:
4747 // andc tmp2, tmpDest, mask
4748 // or tmp4, tmp2, newval3
4749 // stwcx. tmp4, ptr
4750 // bne- loop1MBB
4751 // b exitBB
4752 // midMBB:
4753 // stwcx. tmpDest, ptr
4754 // exitBB:
4755 // srw dest, tmpDest, shift
4756 if (ptrA!=PPC::R0) {
4757 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004758 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004759 .addReg(ptrA).addReg(ptrB);
4760 } else {
4761 Ptr1Reg = ptrB;
4762 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004763 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004764 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004765 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004766 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4767 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004768 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004769 .addReg(Ptr1Reg).addImm(0).addImm(61);
4770 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004771 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004772 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004773 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004774 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004775 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004776 .addReg(oldval).addReg(ShiftReg);
4777 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004778 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004779 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004780 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4781 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
4782 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004783 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004784 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004785 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004786 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004787 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004788 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004789 .addReg(OldVal2Reg).addReg(MaskReg);
4790
4791 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004792 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004793 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004794 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
4795 .addReg(TmpDestReg).addReg(MaskReg);
4796 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004797 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004798 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004799 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
4800 BB->addSuccessor(loop2MBB);
4801 BB->addSuccessor(midMBB);
4802
4803 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004804 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
4805 .addReg(TmpDestReg).addReg(MaskReg);
4806 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
4807 .addReg(Tmp2Reg).addReg(NewVal3Reg);
4808 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004809 .addReg(PPC::R0).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004810 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004811 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004812 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004813 BB->addSuccessor(loop1MBB);
4814 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004815
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004816 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004817 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004818 .addReg(PPC::R0).addReg(PtrReg);
4819 BB->addSuccessor(exitMBB);
4820
4821 // exitMBB:
4822 // ...
4823 BB = exitMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004824 BuildMI(BB, dl, TII->get(PPC::SRW),dest).addReg(TmpReg).addReg(ShiftReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004825 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004826 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng53301922008-07-12 02:23:19 +00004827 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004828
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004829 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004830 return BB;
4831}
4832
Chris Lattner1a635d62006-04-14 06:01:58 +00004833//===----------------------------------------------------------------------===//
4834// Target Optimization Hooks
4835//===----------------------------------------------------------------------===//
4836
Duncan Sands25cf2272008-11-24 14:53:14 +00004837SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
4838 DAGCombinerInfo &DCI) const {
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004839 TargetMachine &TM = getTargetMachine();
4840 SelectionDAG &DAG = DCI.DAG;
Dale Johannesen3484c092009-02-05 22:07:54 +00004841 DebugLoc dl = N->getDebugLoc();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004842 switch (N->getOpcode()) {
4843 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004844 case PPCISD::SHL:
4845 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004846 if (C->getZExtValue() == 0) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004847 return N->getOperand(0);
4848 }
4849 break;
4850 case PPCISD::SRL:
4851 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004852 if (C->getZExtValue() == 0) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004853 return N->getOperand(0);
4854 }
4855 break;
4856 case PPCISD::SRA:
4857 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004858 if (C->getZExtValue() == 0 || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00004859 C->isAllOnesValue()) // -1 >>s V -> -1.
4860 return N->getOperand(0);
4861 }
4862 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004863
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004864 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00004865 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004866 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
4867 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
4868 // We allow the src/dst to be either f32/f64, but the intermediate
4869 // type must be i64.
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 if (N->getOperand(0).getValueType() == MVT::i64 &&
4871 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004872 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00004873 if (Val.getValueType() == MVT::f32) {
4874 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004875 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004876 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004877
Owen Anderson825b72b2009-08-11 20:47:22 +00004878 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004879 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00004880 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004881 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00004882 if (N->getValueType(0) == MVT::f32) {
4883 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00004884 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004885 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004886 }
4887 return Val;
Owen Anderson825b72b2009-08-11 20:47:22 +00004888 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004889 // If the intermediate type is i32, we can avoid the load/store here
4890 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004891 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00004892 }
4893 }
4894 break;
Chris Lattner51269842006-03-01 05:50:56 +00004895 case ISD::STORE:
4896 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
4897 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00004898 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00004899 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004900 N->getOperand(1).getValueType() == MVT::i32 &&
4901 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004902 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00004903 if (Val.getValueType() == MVT::f32) {
4904 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004905 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004906 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00004908 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004909
Owen Anderson825b72b2009-08-11 20:47:22 +00004910 Val = DAG.getNode(PPCISD::STFIWX, dl, MVT::Other, N->getOperand(0), Val,
Chris Lattner51269842006-03-01 05:50:56 +00004911 N->getOperand(2), N->getOperand(3));
Gabor Greifba36cb52008-08-28 21:40:38 +00004912 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00004913 return Val;
4914 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004915
Chris Lattnerd9989382006-07-10 20:56:58 +00004916 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
4917 if (N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004918 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004919 (N->getOperand(1).getValueType() == MVT::i32 ||
4920 N->getOperand(1).getValueType() == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004921 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00004922 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson825b72b2009-08-11 20:47:22 +00004923 if (BSwapOp.getValueType() == MVT::i16)
4924 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00004925
Owen Anderson825b72b2009-08-11 20:47:22 +00004926 return DAG.getNode(PPCISD::STBRX, dl, MVT::Other, N->getOperand(0),
Dale Johannesen3484c092009-02-05 22:07:54 +00004927 BSwapOp, N->getOperand(2), N->getOperand(3),
Chris Lattnerd9989382006-07-10 20:56:58 +00004928 DAG.getValueType(N->getOperand(1).getValueType()));
4929 }
4930 break;
4931 case ISD::BSWAP:
4932 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00004933 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00004934 N->getOperand(0).hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004935 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004936 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00004937 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00004938 // Create the byte-swapping load.
Owen Andersone50ed302009-08-10 22:56:29 +00004939 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 VTs.push_back(MVT::i32);
4941 VTs.push_back(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004942 SDValue MO = DAG.getMemOperand(LD->getMemOperand());
4943 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00004944 LD->getChain(), // Chain
4945 LD->getBasePtr(), // Ptr
Dan Gohman69de1932008-02-06 22:27:42 +00004946 MO, // MemOperand
Chris Lattner79e490a2006-08-11 17:18:05 +00004947 DAG.getValueType(N->getValueType(0)) // VT
4948 };
Dale Johannesen3484c092009-02-05 22:07:54 +00004949 SDValue BSLoad = DAG.getNode(PPCISD::LBRX, dl, VTs, Ops, 4);
Chris Lattnerd9989382006-07-10 20:56:58 +00004950
Scott Michelfdc40a02009-02-17 22:15:04 +00004951 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00004952 SDValue ResVal = BSLoad;
Owen Anderson825b72b2009-08-11 20:47:22 +00004953 if (N->getValueType(0) == MVT::i16)
4954 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00004955
Chris Lattnerd9989382006-07-10 20:56:58 +00004956 // First, combine the bswap away. This makes the value produced by the
4957 // load dead.
4958 DCI.CombineTo(N, ResVal);
4959
4960 // Next, combine the load away, we give it a bogus result value but a real
4961 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00004962 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004963
Chris Lattnerd9989382006-07-10 20:56:58 +00004964 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00004965 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00004966 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004967
Chris Lattner51269842006-03-01 05:50:56 +00004968 break;
Chris Lattner4468c222006-03-31 06:02:07 +00004969 case PPCISD::VCMP: {
4970 // If a VCMPo node already exists with exactly the same operands as this
4971 // node, use its result instead of this node (VCMPo computes both a CR6 and
4972 // a normal output).
4973 //
4974 if (!N->getOperand(0).hasOneUse() &&
4975 !N->getOperand(1).hasOneUse() &&
4976 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004977
Chris Lattner4468c222006-03-31 06:02:07 +00004978 // Scan all of the users of the LHS, looking for VCMPo's that match.
4979 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00004980
Gabor Greifba36cb52008-08-28 21:40:38 +00004981 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00004982 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
4983 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00004984 if (UI->getOpcode() == PPCISD::VCMPo &&
4985 UI->getOperand(1) == N->getOperand(1) &&
4986 UI->getOperand(2) == N->getOperand(2) &&
4987 UI->getOperand(0) == N->getOperand(0)) {
4988 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00004989 break;
4990 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004991
Chris Lattner00901202006-04-18 18:28:22 +00004992 // If there is no VCMPo node, or if the flag value has a single use, don't
4993 // transform this.
4994 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
4995 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004996
4997 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00004998 // chain, this transformation is more complex. Note that multiple things
4999 // could use the value result, which we should ignore.
5000 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005001 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00005002 FlagUser == 0; ++UI) {
5003 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00005004 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00005005 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005006 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00005007 FlagUser = User;
5008 break;
5009 }
5010 }
5011 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005012
Chris Lattner00901202006-04-18 18:28:22 +00005013 // If the user is a MFCR instruction, we know this is safe. Otherwise we
5014 // give up for right now.
5015 if (FlagUser->getOpcode() == PPCISD::MFCR)
Dan Gohman475871a2008-07-27 21:46:04 +00005016 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00005017 }
5018 break;
5019 }
Chris Lattner90564f22006-04-18 17:59:36 +00005020 case ISD::BR_CC: {
5021 // If this is a branch on an altivec predicate comparison, lower this so
5022 // that we don't have to do a MFCR: instead, branch directly on CR6. This
5023 // lowering is done pre-legalize, because the legalizer lowers the predicate
5024 // compare down to code that is difficult to reassemble.
5025 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00005026 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Chris Lattner90564f22006-04-18 17:59:36 +00005027 int CompareOpc;
5028 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00005029
Chris Lattner90564f22006-04-18 17:59:36 +00005030 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
5031 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
5032 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
5033 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005034
Chris Lattner90564f22006-04-18 17:59:36 +00005035 // If this is a comparison against something other than 0/1, then we know
5036 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005037 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005038 if (Val != 0 && Val != 1) {
5039 if (CC == ISD::SETEQ) // Cond never true, remove branch.
5040 return N->getOperand(0);
5041 // Always !=, turn it into an unconditional branch.
Owen Anderson825b72b2009-08-11 20:47:22 +00005042 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00005043 N->getOperand(0), N->getOperand(4));
5044 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005045
Chris Lattner90564f22006-04-18 17:59:36 +00005046 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005047
Chris Lattner90564f22006-04-18 17:59:36 +00005048 // Create the PPCISD altivec 'dot' comparison node.
Owen Andersone50ed302009-08-10 22:56:29 +00005049 std::vector<EVT> VTs;
Dan Gohman475871a2008-07-27 21:46:04 +00005050 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005051 LHS.getOperand(2), // LHS of compare
5052 LHS.getOperand(3), // RHS of compare
Owen Anderson825b72b2009-08-11 20:47:22 +00005053 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00005054 };
Chris Lattner90564f22006-04-18 17:59:36 +00005055 VTs.push_back(LHS.getOperand(2).getValueType());
Owen Anderson825b72b2009-08-11 20:47:22 +00005056 VTs.push_back(MVT::Flag);
Dale Johannesen3484c092009-02-05 22:07:54 +00005057 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005058
Chris Lattner90564f22006-04-18 17:59:36 +00005059 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005060 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005061 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00005062 default: // Can't happen, don't crash on invalid number though.
5063 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005064 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00005065 break;
5066 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005067 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00005068 break;
5069 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005070 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00005071 break;
5072 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005073 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00005074 break;
5075 }
5076
Owen Anderson825b72b2009-08-11 20:47:22 +00005077 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
5078 DAG.getConstant(CompOpc, MVT::i32),
5079 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00005080 N->getOperand(4), CompNode.getValue(1));
5081 }
5082 break;
5083 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005084 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005085
Dan Gohman475871a2008-07-27 21:46:04 +00005086 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005087}
5088
Chris Lattner1a635d62006-04-14 06:01:58 +00005089//===----------------------------------------------------------------------===//
5090// Inline Assembly Support
5091//===----------------------------------------------------------------------===//
5092
Dan Gohman475871a2008-07-27 21:46:04 +00005093void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005094 const APInt &Mask,
Scott Michelfdc40a02009-02-17 22:15:04 +00005095 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005096 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005097 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005098 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005099 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005100 switch (Op.getOpcode()) {
5101 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00005102 case PPCISD::LBRX: {
5103 // lhbrx is known to have the top bits cleared out.
Owen Anderson825b72b2009-08-11 20:47:22 +00005104 if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16)
Chris Lattnerd9989382006-07-10 20:56:58 +00005105 KnownZero = 0xFFFF0000;
5106 break;
5107 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005108 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005109 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005110 default: break;
5111 case Intrinsic::ppc_altivec_vcmpbfp_p:
5112 case Intrinsic::ppc_altivec_vcmpeqfp_p:
5113 case Intrinsic::ppc_altivec_vcmpequb_p:
5114 case Intrinsic::ppc_altivec_vcmpequh_p:
5115 case Intrinsic::ppc_altivec_vcmpequw_p:
5116 case Intrinsic::ppc_altivec_vcmpgefp_p:
5117 case Intrinsic::ppc_altivec_vcmpgtfp_p:
5118 case Intrinsic::ppc_altivec_vcmpgtsb_p:
5119 case Intrinsic::ppc_altivec_vcmpgtsh_p:
5120 case Intrinsic::ppc_altivec_vcmpgtsw_p:
5121 case Intrinsic::ppc_altivec_vcmpgtub_p:
5122 case Intrinsic::ppc_altivec_vcmpgtuh_p:
5123 case Intrinsic::ppc_altivec_vcmpgtuw_p:
5124 KnownZero = ~1U; // All bits but the low one are known to be zero.
5125 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005126 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005127 }
5128 }
5129}
5130
5131
Chris Lattner4234f572007-03-25 02:14:49 +00005132/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005133/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00005134PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005135PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
5136 if (Constraint.size() == 1) {
5137 switch (Constraint[0]) {
5138 default: break;
5139 case 'b':
5140 case 'r':
5141 case 'f':
5142 case 'v':
5143 case 'y':
5144 return C_RegisterClass;
5145 }
5146 }
5147 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005148}
5149
Scott Michelfdc40a02009-02-17 22:15:04 +00005150std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00005151PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005152 EVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00005153 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00005154 // GCC RS6000 Constraint Letters
5155 switch (Constraint[0]) {
5156 case 'b': // R1-R31
5157 case 'r': // R0-R31
Owen Anderson825b72b2009-08-11 20:47:22 +00005158 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Chris Lattner331d1bc2006-11-02 01:44:04 +00005159 return std::make_pair(0U, PPC::G8RCRegisterClass);
5160 return std::make_pair(0U, PPC::GPRCRegisterClass);
5161 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00005162 if (VT == MVT::f32)
Chris Lattner331d1bc2006-11-02 01:44:04 +00005163 return std::make_pair(0U, PPC::F4RCRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00005164 else if (VT == MVT::f64)
Chris Lattner331d1bc2006-11-02 01:44:04 +00005165 return std::make_pair(0U, PPC::F8RCRegisterClass);
5166 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005167 case 'v':
Chris Lattner331d1bc2006-11-02 01:44:04 +00005168 return std::make_pair(0U, PPC::VRRCRegisterClass);
5169 case 'y': // crrc
5170 return std::make_pair(0U, PPC::CRRCRegisterClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005171 }
5172 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005173
Chris Lattner331d1bc2006-11-02 01:44:04 +00005174 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005175}
Chris Lattner763317d2006-02-07 00:47:13 +00005176
Chris Lattner331d1bc2006-11-02 01:44:04 +00005177
Chris Lattner48884cd2007-08-25 00:47:38 +00005178/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Evan Chengda43bcf2008-09-24 00:05:32 +00005179/// vector. If it is invalid, don't add anything to Ops. If hasMemory is true
5180/// it means one of the asm constraint of the inline asm instruction being
5181/// processed is 'm'.
Dan Gohman475871a2008-07-27 21:46:04 +00005182void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, char Letter,
Evan Chengda43bcf2008-09-24 00:05:32 +00005183 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00005184 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00005185 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00005186 SDValue Result(0,0);
Chris Lattner763317d2006-02-07 00:47:13 +00005187 switch (Letter) {
5188 default: break;
5189 case 'I':
5190 case 'J':
5191 case 'K':
5192 case 'L':
5193 case 'M':
5194 case 'N':
5195 case 'O':
5196 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00005197 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00005198 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005199 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00005200 switch (Letter) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005201 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner763317d2006-02-07 00:47:13 +00005202 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005203 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005204 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005205 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005206 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
5207 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005208 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005209 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005210 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005211 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005212 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005213 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005214 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005215 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005216 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00005217 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005218 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005219 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005220 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00005221 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005222 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005223 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005224 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005225 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005226 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005227 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005228 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005229 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005230 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005231 }
5232 break;
5233 }
5234 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005235
Gabor Greifba36cb52008-08-28 21:40:38 +00005236 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005237 Ops.push_back(Result);
5238 return;
5239 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005240
Chris Lattner763317d2006-02-07 00:47:13 +00005241 // Handle standard constraint letters.
Evan Chengda43bcf2008-09-24 00:05:32 +00005242 TargetLowering::LowerAsmOperandForConstraint(Op, Letter, hasMemory, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00005243}
Evan Chengc4c62572006-03-13 23:20:37 +00005244
Chris Lattnerc9addb72007-03-30 23:15:24 +00005245// isLegalAddressingMode - Return true if the addressing mode represented
5246// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00005247bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005248 const Type *Ty) const {
5249 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00005250
Chris Lattnerc9addb72007-03-30 23:15:24 +00005251 // PPC allows a sign-extended 16-bit immediate field.
5252 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
5253 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005254
Chris Lattnerc9addb72007-03-30 23:15:24 +00005255 // No global is ever allowed as a base.
5256 if (AM.BaseGV)
5257 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005258
5259 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005260 switch (AM.Scale) {
5261 case 0: // "r+i" or just "i", depending on HasBaseReg.
5262 break;
5263 case 1:
5264 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
5265 return false;
5266 // Otherwise we have r+r or r+i.
5267 break;
5268 case 2:
5269 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
5270 return false;
5271 // Allow 2*r as r+r.
5272 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00005273 default:
5274 // No other scales are supported.
5275 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00005276 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005277
Chris Lattnerc9addb72007-03-30 23:15:24 +00005278 return true;
5279}
5280
Evan Chengc4c62572006-03-13 23:20:37 +00005281/// isLegalAddressImmediate - Return true if the integer value can be used
Evan Cheng86193912007-03-12 23:29:01 +00005282/// as the offset of the target addressing mode for load / store of the
5283/// given type.
5284bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
Evan Chengc4c62572006-03-13 23:20:37 +00005285 // PPC allows a sign-extended 16-bit immediate field.
5286 return (V > -(1 << 16) && V < (1 << 16)-1);
5287}
Reid Spencer3a9ec242006-08-28 01:02:49 +00005288
5289bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michelfdc40a02009-02-17 22:15:04 +00005290 return false;
Reid Spencer3a9ec242006-08-28 01:02:49 +00005291}
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005292
Dan Gohman475871a2008-07-27 21:46:04 +00005293SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005294 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005295 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005296 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005297 return SDValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005298
5299 MachineFunction &MF = DAG.getMachineFunction();
5300 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005301
Chris Lattner3fc027d2007-12-08 06:59:59 +00005302 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00005303 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00005304
5305 // Make sure the function really does not optimize away the store of the RA
5306 // to the stack.
5307 FuncInfo->setLRStoreRequired();
Scott Michelfdc40a02009-02-17 22:15:04 +00005308 return DAG.getLoad(getPointerTy(), dl,
Dale Johannesen33c960f2009-02-04 20:06:27 +00005309 DAG.getEntryNode(), RetAddrFI, NULL, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00005310}
5311
Dan Gohman475871a2008-07-27 21:46:04 +00005312SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesena05dca42009-02-04 23:02:30 +00005313 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005314 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005315 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005316 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005317
Owen Andersone50ed302009-08-10 22:56:29 +00005318 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00005319 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00005320
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005321 MachineFunction &MF = DAG.getMachineFunction();
5322 MachineFrameInfo *MFI = MF.getFrameInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005323 bool is31 = (NoFramePointerElim || MFI->hasVarSizedObjects())
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005324 && MFI->getStackSize();
5325
5326 if (isPPC64)
Dale Johannesena05dca42009-02-04 23:02:30 +00005327 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::X31 : PPC::X1,
Owen Anderson825b72b2009-08-11 20:47:22 +00005328 MVT::i64);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005329 else
Dale Johannesena05dca42009-02-04 23:02:30 +00005330 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::R31 : PPC::R1,
Owen Anderson825b72b2009-08-11 20:47:22 +00005331 MVT::i32);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005332}
Dan Gohman54aeea32008-10-21 03:41:46 +00005333
5334bool
5335PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5336 // The PowerPC target isn't yet aware of offsets.
5337 return false;
5338}
Tilmann Schellerffd02002009-07-03 06:45:56 +00005339
Owen Andersone50ed302009-08-10 22:56:29 +00005340EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Tilmann Schellerffd02002009-07-03 06:45:56 +00005341 bool isSrcConst, bool isSrcStr,
5342 SelectionDAG &DAG) const {
5343 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005344 return MVT::i64;
Tilmann Schellerffd02002009-07-03 06:45:56 +00005345 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00005346 return MVT::i32;
Tilmann Schellerffd02002009-07-03 06:45:56 +00005347 }
5348}