blob: 2df86cfc0f629534217d0fa10a6b45e6e201d574 [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"
Bill Wendling53351a12010-03-12 02:00:43 +000016#include "PPCPerfectShuffle.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000017#include "PPCTargetMachine.h"
Evan Cheng94b95502011-07-26 00:24:13 +000018#include "MCTargetDesc/PPCPredicates.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"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +000028#include "llvm/CallingConv.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000029#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000030#include "llvm/Function.h"
Chris Lattner6d92cad2006-03-26 10:06:40 +000031#include "llvm/Intrinsics.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000032#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner4eab7142006-11-10 02:08:47 +000034#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Jay Foad8d730fb2009-05-11 19:38:09 +000037#include "llvm/DerivedTypes.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000038using namespace llvm;
39
Duncan Sands1e96bab2010-11-04 10:49:57 +000040static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +000041 CCValAssign::LocInfo &LocInfo,
42 ISD::ArgFlagsTy &ArgFlags,
43 CCState &State);
Duncan Sands1e96bab2010-11-04 10:49:57 +000044static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +000045 MVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +000046 CCValAssign::LocInfo &LocInfo,
47 ISD::ArgFlagsTy &ArgFlags,
48 CCState &State);
Duncan Sands1e96bab2010-11-04 10:49:57 +000049static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +000050 MVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +000051 CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags,
53 CCState &State);
54
Scott Michelfdc40a02009-02-17 22:15:04 +000055static cl::opt<bool> EnablePPCPreinc("enable-ppc-preinc",
Chris Lattner3ee77402007-06-19 05:46:06 +000056cl::desc("enable preincrement load/store generation on PPC (experimental)"),
57 cl::Hidden);
Chris Lattner4eab7142006-11-10 02:08:47 +000058
Chris Lattnerf0144122009-07-28 03:13:23 +000059static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
60 if (TM.getSubtargetImpl()->isDarwin())
Bill Wendling505ad8b2010-03-15 21:09:38 +000061 return new TargetLoweringObjectFileMachO();
Bill Wendling53351a12010-03-12 02:00:43 +000062
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000063 return new TargetLoweringObjectFileELF();
Chris Lattnerf0144122009-07-28 03:13:23 +000064}
65
Chris Lattner331d1bc2006-11-02 01:44:04 +000066PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000067 : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
Scott Michelfdc40a02009-02-17 22:15:04 +000068
Nate Begeman405e3ec2005-10-21 00:02:42 +000069 setPow2DivIsCheap();
Dale Johannesen72324642008-07-31 18:13:12 +000070
Chris Lattnerd145a612005-09-27 22:18:25 +000071 // Use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(true);
Scott Michelfdc40a02009-02-17 22:15:04 +000074
Chris Lattner749dc722010-10-10 18:34:00 +000075 // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all
76 // arguments are at least 4/8 bytes aligned.
77 setMinStackArgumentAlignment(TM.getSubtarget<PPCSubtarget>().isPPC64() ? 8:4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000078
Chris Lattner7c5a3d32005-08-16 17:14:42 +000079 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000080 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
81 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
82 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +000083
Evan Chengc5484282006-10-04 00:56:09 +000084 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
Owen Anderson825b72b2009-08-11 20:47:22 +000085 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
86 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
Duncan Sandsf9c98e62008-01-23 20:39:46 +000087
Owen Anderson825b72b2009-08-11 20:47:22 +000088 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +000089
Chris Lattner94e509c2006-11-10 23:58:45 +000090 // PowerPC has pre-inc load and store's.
Owen Anderson825b72b2009-08-11 20:47:22 +000091 setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
92 setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
93 setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
94 setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
95 setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
96 setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
97 setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
98 setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
99 setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
100 setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
Evan Chengcd633192006-11-09 19:11:50 +0000101
Dale Johannesen6eaeff22007-10-10 01:01:31 +0000102 // This is used in the ppcf128->int sequence. Note it has different semantics
103 // from FP_ROUND: that rounds to nearest, this rounds to zero.
Owen Anderson825b72b2009-08-11 20:47:22 +0000104 setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
Dale Johannesen638ccd52007-10-06 01:24:11 +0000105
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000106 // PowerPC has no SREM/UREM instructions
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 setOperationAction(ISD::SREM, MVT::i32, Expand);
108 setOperationAction(ISD::UREM, MVT::i32, Expand);
109 setOperationAction(ISD::SREM, MVT::i64, Expand);
110 setOperationAction(ISD::UREM, MVT::i64, Expand);
Dan Gohman3ce990d2007-10-08 17:28:24 +0000111
112 // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
114 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
115 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
116 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
117 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
118 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
119 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
120 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000121
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000122 // We don't support sin/cos/sqrt/fmod/pow
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setOperationAction(ISD::FSIN , MVT::f64, Expand);
124 setOperationAction(ISD::FCOS , MVT::f64, Expand);
125 setOperationAction(ISD::FREM , MVT::f64, Expand);
126 setOperationAction(ISD::FPOW , MVT::f64, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000127 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 setOperationAction(ISD::FSIN , MVT::f32, Expand);
129 setOperationAction(ISD::FCOS , MVT::f32, Expand);
130 setOperationAction(ISD::FREM , MVT::f32, Expand);
131 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000132 setOperationAction(ISD::FMA , MVT::f32, Expand);
Dale Johannesen5c5eb802008-01-18 19:55:37 +0000133
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000135
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000136 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +0000137 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
139 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000140 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000141
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
143 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000144
Nate Begemand88fc032006-01-14 03:14:10 +0000145 // PowerPC does not have BSWAP, CTPOP or CTTZ
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
147 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
148 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
149 setOperationAction(ISD::BSWAP, MVT::i64 , Expand);
150 setOperationAction(ISD::CTPOP, MVT::i64 , Expand);
151 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000152
Nate Begeman35ef9132006-01-11 21:21:00 +0000153 // PowerPC does not have ROTR
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
155 setOperationAction(ISD::ROTR, MVT::i64 , Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000156
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000157 // PowerPC does not have Select
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::SELECT, MVT::i32, Expand);
159 setOperationAction(ISD::SELECT, MVT::i64, Expand);
160 setOperationAction(ISD::SELECT, MVT::f32, Expand);
161 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000162
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000163 // PowerPC wants to turn select_cc of FP into fsel when possible.
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
165 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +0000166
Nate Begeman750ac1b2006-02-01 07:19:44 +0000167 // PowerPC wants to optimize integer setcc a bit
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000169
Nate Begeman81e80972006-03-17 01:40:33 +0000170 // PowerPC does not have BRCOND which requires SetCC
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Evan Chengc35497f2006-10-30 08:02:39 +0000172
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000174
Chris Lattnerf7605322005-08-31 21:09:52 +0000175 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000177
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000178 // PowerPC does not have [U|S]INT_TO_FP
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
180 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000181
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000182 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
183 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
184 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
185 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
Chris Lattner53e88452005-12-23 05:13:35 +0000186
Chris Lattner25b8b8c2006-04-28 21:56:10 +0000187 // We cannot sextinreg(i1). Expand to shifts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000189
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
191 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
192 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
193 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000194
195
196 // We want to legalize GlobalAddress and ConstantPool nodes into the
Nate Begeman28a6b022005-12-10 02:36:00 +0000197 // appropriate instructions to materialize the address.
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
199 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000200 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
202 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
203 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
204 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000205 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
207 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000208
Nate Begeman1db3c922008-08-11 17:36:31 +0000209 // TRAP is legal.
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Bill Wendling77959322008-09-17 00:30:57 +0000211
212 // TRAMPOLINE is custom lowered.
Duncan Sands4a544a72011-09-06 13:37:06 +0000213 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
214 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
Bill Wendling77959322008-09-17 00:30:57 +0000215
Nate Begemanacc398c2006-01-25 18:21:52 +0000216 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000218
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000219 // VAARG is custom lowered with the 32-bit SVR4 ABI.
Roman Divackybdb226e2011-06-28 15:30:42 +0000220 if (TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
221 && !TM.getSubtarget<PPCSubtarget>().isPPC64()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 setOperationAction(ISD::VAARG, MVT::Other, Custom);
Roman Divackybdb226e2011-06-28 15:30:42 +0000223 setOperationAction(ISD::VAARG, MVT::i64, Custom);
224 } else
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::VAARG, MVT::Other, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000226
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000227 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
229 setOperationAction(ISD::VAEND , MVT::Other, Expand);
230 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
231 setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom);
232 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
233 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom);
Chris Lattner56a752e2006-10-18 01:18:48 +0000234
Chris Lattner6d92cad2006-03-26 10:06:40 +0000235 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000237
Dale Johannesen53e4e442008-11-07 22:54:33 +0000238 // Comparisons that require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
240 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
241 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
242 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
243 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
244 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
245 setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
246 setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
247 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
248 setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
249 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
250 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
Scott Michelfdc40a02009-02-17 22:15:04 +0000251
Chris Lattnera7a58542006-06-16 17:34:12 +0000252 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000253 // They also have instructions for converting between i64 and fp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
255 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
256 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
257 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Dale Johannesen4c9369d2009-06-04 20:53:52 +0000258 // This is just the low 32 bits of a (signed) fp->i64 conversion.
259 // We cannot do this with Promote because i64 is not a legal type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000261
Chris Lattner7fbcef72006-03-24 07:53:47 +0000262 // FIXME: disable this lowered code. This generates 64-bit register values,
263 // and we don't model the fact that the top part is clobbered by calls. We
264 // need to flag these together so that the value isn't live across a call.
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000266 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000267 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000269 }
270
Chris Lattnera7a58542006-06-16 17:34:12 +0000271 if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) {
Chris Lattner26cb2862007-10-19 04:08:28 +0000272 // 64-bit PowerPC implementations can support i64 types directly
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000274 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Dan Gohman9ed06db2008-03-07 20:36:53 +0000276 // 64-bit PowerPC wants to expand i128 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
278 setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
279 setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000280 } else {
Chris Lattner26cb2862007-10-19 04:08:28 +0000281 // 32-bit PowerPC wants to expand i64 shifts itself.
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
283 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
284 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000285 }
Evan Chengd30bf012006-03-01 01:11:20 +0000286
Nate Begeman425a9692005-11-29 08:17:20 +0000287 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000288 // First set operation action for all vector types to expand. Then we
289 // will selectively turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
291 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
292 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000293
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000294 // add/sub are legal for all supported vector VT's.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000295 setOperationAction(ISD::ADD , VT, Legal);
296 setOperationAction(ISD::SUB , VT, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000297
Chris Lattner7ff7e672006-04-04 17:25:31 +0000298 // We promote all shuffles to v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000299 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000301
302 // We promote all non-typed operations to v4i32.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000303 setOperationAction(ISD::AND , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 AddPromotedToType (ISD::AND , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000305 setOperationAction(ISD::OR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 AddPromotedToType (ISD::OR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000307 setOperationAction(ISD::XOR , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 AddPromotedToType (ISD::XOR , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000309 setOperationAction(ISD::LOAD , VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000310 AddPromotedToType (ISD::LOAD , VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000311 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000312 AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000313 setOperationAction(ISD::STORE, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000315
Chris Lattnerf3f69de2006-04-16 01:37:57 +0000316 // No other operations are legal.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000317 setOperationAction(ISD::MUL , VT, Expand);
318 setOperationAction(ISD::SDIV, VT, Expand);
319 setOperationAction(ISD::SREM, VT, Expand);
320 setOperationAction(ISD::UDIV, VT, Expand);
321 setOperationAction(ISD::UREM, VT, Expand);
322 setOperationAction(ISD::FDIV, VT, Expand);
323 setOperationAction(ISD::FNEG, VT, Expand);
324 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
325 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
326 setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
327 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
328 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
329 setOperationAction(ISD::UDIVREM, VT, Expand);
330 setOperationAction(ISD::SDIVREM, VT, Expand);
331 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
332 setOperationAction(ISD::FPOW, VT, Expand);
333 setOperationAction(ISD::CTPOP, VT, Expand);
334 setOperationAction(ISD::CTLZ, VT, Expand);
335 setOperationAction(ISD::CTTZ, VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000336 }
337
Chris Lattner7ff7e672006-04-04 17:25:31 +0000338 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
339 // with merges, splats, etc.
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000341
Owen Anderson825b72b2009-08-11 20:47:22 +0000342 setOperationAction(ISD::AND , MVT::v4i32, Legal);
343 setOperationAction(ISD::OR , MVT::v4i32, Legal);
344 setOperationAction(ISD::XOR , MVT::v4i32, Legal);
345 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
346 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
347 setOperationAction(ISD::STORE , MVT::v4i32, Legal);
Scott Michelfdc40a02009-02-17 22:15:04 +0000348
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
350 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
351 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
352 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +0000353
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
355 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
356 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
357 setOperationAction(ISD::MUL, MVT::v16i8, Custom);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000358
Owen Anderson825b72b2009-08-11 20:47:22 +0000359 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
360 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000361
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
363 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
364 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
365 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000366 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000367
Eli Friedman4db5aca2011-08-29 18:23:02 +0000368 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
369 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
370
Duncan Sands03228082008-11-23 15:47:28 +0000371 setBooleanContents(ZeroOrOneBooleanContent);
Duncan Sands28b77e92011-09-06 19:07:46 +0000372 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
Scott Michelfdc40a02009-02-17 22:15:04 +0000373
Jim Laskey2ad9f172007-02-22 14:56:36 +0000374 if (TM.getSubtarget<PPCSubtarget>().isPPC64()) {
Chris Lattner10da9572006-10-18 01:20:43 +0000375 setStackPointerRegisterToSaveRestore(PPC::X1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000376 setExceptionPointerRegister(PPC::X3);
377 setExceptionSelectorRegister(PPC::X4);
378 } else {
Chris Lattner10da9572006-10-18 01:20:43 +0000379 setStackPointerRegisterToSaveRestore(PPC::R1);
Jim Laskey2ad9f172007-02-22 14:56:36 +0000380 setExceptionPointerRegister(PPC::R3);
381 setExceptionSelectorRegister(PPC::R4);
382 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000383
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000384 // We have target-specific dag combine patterns for the following nodes:
385 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000386 setTargetDAGCombine(ISD::STORE);
Chris Lattner90564f22006-04-18 17:59:36 +0000387 setTargetDAGCombine(ISD::BR_CC);
Chris Lattnerd9989382006-07-10 20:56:58 +0000388 setTargetDAGCombine(ISD::BSWAP);
Scott Michelfdc40a02009-02-17 22:15:04 +0000389
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000390 // Darwin long double math library functions have $LDBL128 appended.
391 if (TM.getSubtarget<PPCSubtarget>().isDarwin()) {
Duncan Sands007f9842008-01-10 10:28:30 +0000392 setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000393 setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
394 setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
Duncan Sands007f9842008-01-10 10:28:30 +0000395 setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
396 setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000397 setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
398 setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
399 setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
400 setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
401 setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
Dale Johannesenfabd32d2007-10-19 00:59:18 +0000402 }
403
Hal Finkelc6129162011-10-17 18:53:03 +0000404 setMinFunctionAlignment(2);
405 if (PPCSubTarget.isDarwin())
406 setPrefFunctionAlignment(4);
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000407
Eli Friedman26689ac2011-08-03 21:06:02 +0000408 setInsertFencesForAtomic(true);
409
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000410 computeRegisterProperties();
411}
412
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000413/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
414/// function arguments in the caller parameter area.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000415unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +0000416 const TargetMachine &TM = getTargetMachine();
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000417 // Darwin passes everything on 4 byte boundary.
418 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
419 return 4;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000420 // FIXME SVR4 TBD
Dale Johannesen28d08fd2008-02-28 22:31:51 +0000421 return 4;
422}
423
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000424const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
425 switch (Opcode) {
426 default: return 0;
Evan Cheng53301922008-07-12 02:23:19 +0000427 case PPCISD::FSEL: return "PPCISD::FSEL";
428 case PPCISD::FCFID: return "PPCISD::FCFID";
429 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
430 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
431 case PPCISD::STFIWX: return "PPCISD::STFIWX";
432 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
433 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
434 case PPCISD::VPERM: return "PPCISD::VPERM";
435 case PPCISD::Hi: return "PPCISD::Hi";
436 case PPCISD::Lo: return "PPCISD::Lo";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000437 case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY";
Tilmann Scheller3a84dae2009-12-18 13:00:15 +0000438 case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE";
439 case PPCISD::LOAD: return "PPCISD::LOAD";
440 case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC";
Evan Cheng53301922008-07-12 02:23:19 +0000441 case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC";
442 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
443 case PPCISD::SRL: return "PPCISD::SRL";
444 case PPCISD::SRA: return "PPCISD::SRA";
445 case PPCISD::SHL: return "PPCISD::SHL";
446 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
447 case PPCISD::STD_32: return "PPCISD::STD_32";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000448 case PPCISD::CALL_SVR4: return "PPCISD::CALL_SVR4";
449 case PPCISD::CALL_Darwin: return "PPCISD::CALL_Darwin";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000450 case PPCISD::NOP: return "PPCISD::NOP";
Evan Cheng53301922008-07-12 02:23:19 +0000451 case PPCISD::MTCTR: return "PPCISD::MTCTR";
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000452 case PPCISD::BCTRL_Darwin: return "PPCISD::BCTRL_Darwin";
453 case PPCISD::BCTRL_SVR4: return "PPCISD::BCTRL_SVR4";
Evan Cheng53301922008-07-12 02:23:19 +0000454 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
455 case PPCISD::MFCR: return "PPCISD::MFCR";
456 case PPCISD::VCMP: return "PPCISD::VCMP";
457 case PPCISD::VCMPo: return "PPCISD::VCMPo";
458 case PPCISD::LBRX: return "PPCISD::LBRX";
459 case PPCISD::STBRX: return "PPCISD::STBRX";
Evan Cheng53301922008-07-12 02:23:19 +0000460 case PPCISD::LARX: return "PPCISD::LARX";
461 case PPCISD::STCX: return "PPCISD::STCX";
462 case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH";
463 case PPCISD::MFFS: return "PPCISD::MFFS";
464 case PPCISD::MTFSB0: return "PPCISD::MTFSB0";
465 case PPCISD::MTFSB1: return "PPCISD::MTFSB1";
466 case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ";
467 case PPCISD::MTFSF: return "PPCISD::MTFSF";
Evan Cheng53301922008-07-12 02:23:19 +0000468 case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000469 }
470}
471
Duncan Sands28b77e92011-09-06 19:07:46 +0000472EVT PPCTargetLowering::getSetCCResultType(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 return MVT::i32;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000474}
475
Chris Lattner1a635d62006-04-14 06:01:58 +0000476//===----------------------------------------------------------------------===//
477// Node matching predicates, for use by the tblgen matching code.
478//===----------------------------------------------------------------------===//
479
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000480/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
Dan Gohman475871a2008-07-27 21:46:04 +0000481static bool isFloatingPointZero(SDValue Op) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000482 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000483 return CFP->getValueAPF().isZero();
Gabor Greifba36cb52008-08-28 21:40:38 +0000484 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000485 // Maybe this has already been legalized into the constant pool?
486 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
Dan Gohman46510a72010-04-15 01:51:59 +0000487 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000488 return CFP->getValueAPF().isZero();
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000489 }
490 return false;
491}
492
Chris Lattnerddb739e2006-04-06 17:23:16 +0000493/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
494/// true if Op is undef or if it matches the specified value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000495static bool isConstantOrUndef(int Op, int Val) {
496 return Op < 0 || Op == Val;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000497}
498
499/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
500/// VPKUHUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000501bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000502 if (!isUnary) {
503 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000504 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000505 return false;
506 } else {
507 for (unsigned i = 0; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000508 if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) ||
509 !isConstantOrUndef(N->getMaskElt(i+8), i*2+1))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000510 return false;
511 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000512 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000513}
514
515/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
516/// VPKUWUM instruction.
Nate Begeman9008ca62009-04-27 18:41:29 +0000517bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
Chris Lattnerf24380e2006-04-06 22:28:36 +0000518 if (!isUnary) {
519 for (unsigned i = 0; i != 16; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000520 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
521 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000522 return false;
523 } else {
524 for (unsigned i = 0; i != 8; i += 2)
Nate Begeman9008ca62009-04-27 18:41:29 +0000525 if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) ||
526 !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) ||
527 !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) ||
528 !isConstantOrUndef(N->getMaskElt(i+9), i*2+3))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000529 return false;
530 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000531 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000532}
533
Chris Lattnercaad1632006-04-06 22:02:42 +0000534/// isVMerge - Common function, used to match vmrg* shuffles.
535///
Nate Begeman9008ca62009-04-27 18:41:29 +0000536static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
Chris Lattnercaad1632006-04-06 22:02:42 +0000537 unsigned LHSStart, unsigned RHSStart) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000539 "PPC only supports shuffles by bytes!");
Chris Lattner116cc482006-04-06 21:11:54 +0000540 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
541 "Unsupported merge size!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000542
Chris Lattner116cc482006-04-06 21:11:54 +0000543 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
544 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
Nate Begeman9008ca62009-04-27 18:41:29 +0000545 if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000546 LHSStart+j+i*UnitSize) ||
Nate Begeman9008ca62009-04-27 18:41:29 +0000547 !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000548 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000549 return false;
550 }
Nate Begeman9008ca62009-04-27 18:41:29 +0000551 return true;
Chris Lattnercaad1632006-04-06 22:02:42 +0000552}
553
554/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
555/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000556bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000557 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000558 if (!isUnary)
559 return isVMerge(N, UnitSize, 8, 24);
560 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000561}
562
563/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
564/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000565bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize,
Nate Begeman9008ca62009-04-27 18:41:29 +0000566 bool isUnary) {
Chris Lattnercaad1632006-04-06 22:02:42 +0000567 if (!isUnary)
568 return isVMerge(N, UnitSize, 0, 16);
569 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000570}
571
572
Chris Lattnerd0608e12006-04-06 18:26:28 +0000573/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
574/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000575int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000576 assert(N->getValueType(0) == MVT::v16i8 &&
Nate Begeman9008ca62009-04-27 18:41:29 +0000577 "PPC only supports shuffles by bytes!");
578
579 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000580
Chris Lattnerd0608e12006-04-06 18:26:28 +0000581 // Find the first non-undef value in the shuffle mask.
582 unsigned i;
Nate Begeman9008ca62009-04-27 18:41:29 +0000583 for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
Chris Lattnerd0608e12006-04-06 18:26:28 +0000584 /*search*/;
Scott Michelfdc40a02009-02-17 22:15:04 +0000585
Chris Lattnerd0608e12006-04-06 18:26:28 +0000586 if (i == 16) return -1; // all undef.
Scott Michelfdc40a02009-02-17 22:15:04 +0000587
Nate Begeman9008ca62009-04-27 18:41:29 +0000588 // Otherwise, check to see if the rest of the elements are consecutively
Chris Lattnerd0608e12006-04-06 18:26:28 +0000589 // numbered from this value.
Nate Begeman9008ca62009-04-27 18:41:29 +0000590 unsigned ShiftAmt = SVOp->getMaskElt(i);
Chris Lattnerd0608e12006-04-06 18:26:28 +0000591 if (ShiftAmt < i) return -1;
592 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000593
Chris Lattnerf24380e2006-04-06 22:28:36 +0000594 if (!isUnary) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000595 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000596 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000597 if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000598 return -1;
599 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000600 // Check the rest of the elements to see if they are consecutive.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000601 for (++i; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +0000602 if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
Chris Lattnerf24380e2006-04-06 22:28:36 +0000603 return -1;
604 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000605 return ShiftAmt;
606}
Chris Lattneref819f82006-03-20 06:33:01 +0000607
608/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
609/// specifies a splat of a single element that is suitable for input to
610/// VSPLTB/VSPLTH/VSPLTW.
Nate Begeman9008ca62009-04-27 18:41:29 +0000611bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000612 assert(N->getValueType(0) == MVT::v16i8 &&
Chris Lattner7ff7e672006-04-04 17:25:31 +0000613 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Scott Michelfdc40a02009-02-17 22:15:04 +0000614
Chris Lattner88a99ef2006-03-20 06:37:44 +0000615 // This is a splat operation if each element of the permute is the same, and
616 // if the value doesn't reference the second vector.
Nate Begeman9008ca62009-04-27 18:41:29 +0000617 unsigned ElementBase = N->getMaskElt(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000618
Nate Begeman9008ca62009-04-27 18:41:29 +0000619 // FIXME: Handle UNDEF elements too!
620 if (ElementBase >= 16)
Chris Lattner7ff7e672006-04-04 17:25:31 +0000621 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000622
Nate Begeman9008ca62009-04-27 18:41:29 +0000623 // Check that the indices are consecutive, in the case of a multi-byte element
624 // splatted with a v16i8 mask.
625 for (unsigned i = 1; i != EltSize; ++i)
626 if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000627 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000628
Chris Lattner7ff7e672006-04-04 17:25:31 +0000629 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000630 if (N->getMaskElt(i) < 0) continue;
Chris Lattner7ff7e672006-04-04 17:25:31 +0000631 for (unsigned j = 0; j != EltSize; ++j)
Nate Begeman9008ca62009-04-27 18:41:29 +0000632 if (N->getMaskElt(i+j) != N->getMaskElt(j))
Chris Lattner7ff7e672006-04-04 17:25:31 +0000633 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000634 }
Chris Lattner7ff7e672006-04-04 17:25:31 +0000635 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000636}
637
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000638/// isAllNegativeZeroVector - Returns true if all elements of build_vector
639/// are -0.0.
640bool PPC::isAllNegativeZeroVector(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000641 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
642
643 APInt APVal, APUndef;
644 unsigned BitSize;
645 bool HasAnyUndefs;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000646
Dale Johannesen1e608812009-11-13 01:45:18 +0000647 if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true))
Nate Begeman9008ca62009-04-27 18:41:29 +0000648 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
Dale Johanneseneaf08942007-08-31 04:03:46 +0000649 return CFP->getValueAPF().isNegZero();
Nate Begeman9008ca62009-04-27 18:41:29 +0000650
Evan Cheng66ffe6b2007-07-30 07:51:22 +0000651 return false;
652}
653
Chris Lattneref819f82006-03-20 06:33:01 +0000654/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
655/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000656unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
Nate Begeman9008ca62009-04-27 18:41:29 +0000657 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
658 assert(isSplatShuffleMask(SVOp, EltSize));
659 return SVOp->getMaskElt(0) / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000660}
661
Chris Lattnere87192a2006-04-12 17:37:20 +0000662/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000663/// by using a vspltis[bhw] instruction of the specified element size, return
664/// the constant being splatted. The ByteSize field indicates the number of
665/// bytes of each element [124] -> [bhw].
Dan Gohman475871a2008-07-27 21:46:04 +0000666SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
667 SDValue OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000668
669 // If ByteSize of the splat is bigger than the element size of the
670 // build_vector, then we have a case where we are checking for a splat where
671 // multiple elements of the buildvector are folded together into a single
672 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
673 unsigned EltSize = 16/N->getNumOperands();
674 if (EltSize < ByteSize) {
675 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
Dan Gohman475871a2008-07-27 21:46:04 +0000676 SDValue UniquedVals[4];
Chris Lattner79d9a882006-04-08 07:14:26 +0000677 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
Scott Michelfdc40a02009-02-17 22:15:04 +0000678
Chris Lattner79d9a882006-04-08 07:14:26 +0000679 // See if all of the elements in the buildvector agree across.
680 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
681 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
682 // If the element isn't a constant, bail fully out.
Dan Gohman475871a2008-07-27 21:46:04 +0000683 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000684
Scott Michelfdc40a02009-02-17 22:15:04 +0000685
Gabor Greifba36cb52008-08-28 21:40:38 +0000686 if (UniquedVals[i&(Multiple-1)].getNode() == 0)
Chris Lattner79d9a882006-04-08 07:14:26 +0000687 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
688 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000689 return SDValue(); // no match.
Chris Lattner79d9a882006-04-08 07:14:26 +0000690 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000691
Chris Lattner79d9a882006-04-08 07:14:26 +0000692 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
693 // either constant or undef values that are identical for each chunk. See
694 // if these chunks can form into a larger vspltis*.
Scott Michelfdc40a02009-02-17 22:15:04 +0000695
Chris Lattner79d9a882006-04-08 07:14:26 +0000696 // Check to see if all of the leading entries are either 0 or -1. If
697 // neither, then this won't fit into the immediate field.
698 bool LeadingZero = true;
699 bool LeadingOnes = true;
700 for (unsigned i = 0; i != Multiple-1; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000701 if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs.
Scott Michelfdc40a02009-02-17 22:15:04 +0000702
Chris Lattner79d9a882006-04-08 07:14:26 +0000703 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
704 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
705 }
706 // Finally, check the least significant entry.
707 if (LeadingZero) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000708 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000710 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000711 if (Val < 16)
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
Chris Lattner79d9a882006-04-08 07:14:26 +0000713 }
714 if (LeadingOnes) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000715 if (UniquedVals[Multiple-1].getNode() == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
Dan Gohman7810bfe2008-09-26 21:54:37 +0000717 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000718 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
Owen Anderson825b72b2009-08-11 20:47:22 +0000719 return DAG.getTargetConstant(Val, MVT::i32);
Chris Lattner79d9a882006-04-08 07:14:26 +0000720 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000721
Dan Gohman475871a2008-07-27 21:46:04 +0000722 return SDValue();
Chris Lattner79d9a882006-04-08 07:14:26 +0000723 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000724
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000725 // Check to see if this buildvec has a single non-undef value in its elements.
726 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
727 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Gabor Greifba36cb52008-08-28 21:40:38 +0000728 if (OpVal.getNode() == 0)
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000729 OpVal = N->getOperand(i);
730 else if (OpVal != N->getOperand(i))
Dan Gohman475871a2008-07-27 21:46:04 +0000731 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000732 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000733
Gabor Greifba36cb52008-08-28 21:40:38 +0000734 if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def.
Scott Michelfdc40a02009-02-17 22:15:04 +0000735
Eli Friedman1a8229b2009-05-24 02:03:36 +0000736 unsigned ValSizeInBytes = EltSize;
Nate Begeman98e70cc2006-03-28 04:15:58 +0000737 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000738 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000739 Value = CN->getZExtValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000740 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
Dale Johanneseneaf08942007-08-31 04:03:46 +0000742 Value = FloatToBits(CN->getValueAPF().convertToFloat());
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000743 }
744
745 // If the splat value is larger than the element value, then we can never do
746 // this splat. The only case that we could fit the replicated bits into our
747 // immediate field for would be zero, and we prefer to use vxor for it.
Dan Gohman475871a2008-07-27 21:46:04 +0000748 if (ValSizeInBytes < ByteSize) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000749
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000750 // If the element value is larger than the splat value, cut it in half and
751 // check to see if the two halves are equal. Continue doing this until we
752 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
753 while (ValSizeInBytes > ByteSize) {
754 ValSizeInBytes >>= 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000755
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000756 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000757 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
758 (Value & ((1 << (8*ValSizeInBytes))-1)))
Dan Gohman475871a2008-07-27 21:46:04 +0000759 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000760 }
761
762 // Properly sign extend the value.
763 int ShAmt = (4-ByteSize)*8;
764 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
Scott Michelfdc40a02009-02-17 22:15:04 +0000765
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000766 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Dan Gohman475871a2008-07-27 21:46:04 +0000767 if (MaskVal == 0) return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000768
Chris Lattner140a58f2006-04-08 06:46:53 +0000769 // Finally, if this value fits in a 5 bit sext field, return it
770 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 return DAG.getTargetConstant(MaskVal, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000772 return SDValue();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000773}
774
Chris Lattner1a635d62006-04-14 06:01:58 +0000775//===----------------------------------------------------------------------===//
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000776// Addressing Mode Selection
777//===----------------------------------------------------------------------===//
778
779/// isIntS16Immediate - This method tests to see if the node is either a 32-bit
780/// or 64-bit immediate, and if the value can be accurately represented as a
781/// sign extension from a 16-bit value. If so, this returns true and the
782/// immediate.
783static bool isIntS16Immediate(SDNode *N, short &Imm) {
784 if (N->getOpcode() != ISD::Constant)
785 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000786
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000787 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 if (N->getValueType(0) == MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000789 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000790 else
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000791 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000792}
Dan Gohman475871a2008-07-27 21:46:04 +0000793static bool isIntS16Immediate(SDValue Op, short &Imm) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000794 return isIntS16Immediate(Op.getNode(), Imm);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000795}
796
797
798/// SelectAddressRegReg - Given the specified addressed, check to see if it
799/// can be represented as an indexed [r+r] operation. Returns false if it
800/// can be more efficiently represented with [r+imm].
Dan Gohman475871a2008-07-27 21:46:04 +0000801bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
802 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000803 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000804 short imm = 0;
805 if (N.getOpcode() == ISD::ADD) {
806 if (isIntS16Immediate(N.getOperand(1), imm))
807 return false; // r+i
808 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
809 return false; // r+i
Scott Michelfdc40a02009-02-17 22:15:04 +0000810
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000811 Base = N.getOperand(0);
812 Index = N.getOperand(1);
813 return true;
814 } else if (N.getOpcode() == ISD::OR) {
815 if (isIntS16Immediate(N.getOperand(1), imm))
816 return false; // r+i can fold it if we can.
Scott Michelfdc40a02009-02-17 22:15:04 +0000817
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000818 // If this is an or of disjoint bitfields, we can codegen this as an add
819 // (for better address arithmetic) if the LHS and RHS of the OR are provably
820 // disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000821 APInt LHSKnownZero, LHSKnownOne;
822 APInt RHSKnownZero, RHSKnownOne;
823 DAG.ComputeMaskedBits(N.getOperand(0),
Dan Gohmanec59b952008-02-27 21:12:32 +0000824 APInt::getAllOnesValue(N.getOperand(0)
825 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000826 LHSKnownZero, LHSKnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +0000827
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000828 if (LHSKnownZero.getBoolValue()) {
829 DAG.ComputeMaskedBits(N.getOperand(1),
Dan Gohmanec59b952008-02-27 21:12:32 +0000830 APInt::getAllOnesValue(N.getOperand(1)
831 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000832 RHSKnownZero, RHSKnownOne);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000833 // If all of the bits are known zero on the LHS or RHS, the add won't
834 // carry.
Dan Gohmanec59b952008-02-27 21:12:32 +0000835 if (~(LHSKnownZero | RHSKnownZero) == 0) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000836 Base = N.getOperand(0);
837 Index = N.getOperand(1);
838 return true;
839 }
840 }
841 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000842
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000843 return false;
844}
845
846/// Returns true if the address N can be represented by a base register plus
847/// a signed 16-bit displacement [r+imm], and if it is not better
848/// represented as reg+reg.
Dan Gohman475871a2008-07-27 21:46:04 +0000849bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
Dan Gohman73e09142009-01-15 16:29:45 +0000850 SDValue &Base,
851 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000852 // FIXME dl should come from parent load or store, not from address
853 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000854 // If this can be more profitably realized as r+r, fail.
855 if (SelectAddressRegReg(N, Disp, Base, DAG))
856 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000857
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000858 if (N.getOpcode() == ISD::ADD) {
859 short imm = 0;
860 if (isIntS16Immediate(N.getOperand(1), imm)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000862 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
863 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
864 } else {
865 Base = N.getOperand(0);
866 }
867 return true; // [r+i]
868 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
869 // Match LOAD (ADD (X, Lo(G))).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000870 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000871 && "Cannot handle constant offsets yet!");
872 Disp = N.getOperand(1).getOperand(0); // The global address.
873 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
874 Disp.getOpcode() == ISD::TargetConstantPool ||
875 Disp.getOpcode() == ISD::TargetJumpTable);
876 Base = N.getOperand(0);
877 return true; // [&g+r]
878 }
879 } else if (N.getOpcode() == ISD::OR) {
880 short imm = 0;
881 if (isIntS16Immediate(N.getOperand(1), imm)) {
882 // If this is an or of disjoint bitfields, we can codegen this as an add
883 // (for better address arithmetic) if the LHS and RHS of the OR are
884 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000885 APInt LHSKnownZero, LHSKnownOne;
886 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +0000887 APInt::getAllOnesValue(N.getOperand(0)
888 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000889 LHSKnownZero, LHSKnownOne);
Bill Wendling3e98c302008-03-24 23:16:37 +0000890
Dan Gohmanb3564aa2008-02-27 01:23:58 +0000891 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000892 // If all of the bits are known zero on the LHS or RHS, the add won't
893 // carry.
894 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000896 return true;
897 }
898 }
899 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
900 // Loading from a constant address.
Scott Michelfdc40a02009-02-17 22:15:04 +0000901
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000902 // If this address fits entirely in a 16-bit sext immediate field, codegen
903 // this as "d, 0"
904 short Imm;
905 if (isIntS16Immediate(CN, Imm)) {
906 Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +0000907 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::X0 : PPC::R0,
908 CN->getValueType(0));
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000909 return true;
910 }
Chris Lattnerbc681d62007-02-17 06:44:03 +0000911
912 // Handle 32-bit sext immediates with LIS + addr mode.
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000914 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
915 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +0000916
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000917 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +0000919
Owen Anderson825b72b2009-08-11 20:47:22 +0000920 Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
921 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman602b0c82009-09-25 18:54:59 +0000922 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000923 return true;
924 }
925 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000926
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000927 Disp = DAG.getTargetConstant(0, getPointerTy());
928 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
929 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
930 else
931 Base = N;
932 return true; // [r+0]
933}
934
935/// SelectAddressRegRegOnly - Given the specified addressed, force it to be
936/// represented as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000937bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
938 SDValue &Index,
Dan Gohman73e09142009-01-15 16:29:45 +0000939 SelectionDAG &DAG) const {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000940 // Check to see if we can easily represent this as an [r+r] address. This
941 // will fail if it thinks that the address is more profitably represented as
942 // reg+imm, e.g. where imm = 0.
943 if (SelectAddressRegReg(N, Base, Index, DAG))
944 return true;
Scott Michelfdc40a02009-02-17 22:15:04 +0000945
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000946 // If the operand is an addition, always emit this as [r+r], since this is
947 // better (for code size, and execution, as the memop does the add for free)
948 // than emitting an explicit add.
949 if (N.getOpcode() == ISD::ADD) {
950 Base = N.getOperand(0);
951 Index = N.getOperand(1);
952 return true;
953 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000954
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000955 // Otherwise, do it the hard way, using R0 as the base register.
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +0000956 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::X0 : PPC::R0,
957 N.getValueType());
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000958 Index = N;
959 return true;
960}
961
962/// SelectAddressRegImmShift - Returns true if the address N can be
963/// represented by a base register plus a signed 14-bit displacement
964/// [r+imm*4]. Suitable for use by STD and friends.
Dan Gohman475871a2008-07-27 21:46:04 +0000965bool PPCTargetLowering::SelectAddressRegImmShift(SDValue N, SDValue &Disp,
966 SDValue &Base,
Dan Gohman73e09142009-01-15 16:29:45 +0000967 SelectionDAG &DAG) const {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000968 // FIXME dl should come from the parent load or store, not the address
969 DebugLoc dl = N.getDebugLoc();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000970 // If this can be more profitably realized as r+r, fail.
971 if (SelectAddressRegReg(N, Disp, Base, DAG))
972 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000973
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000974 if (N.getOpcode() == ISD::ADD) {
975 short imm = 0;
976 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000978 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
979 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
980 } else {
981 Base = N.getOperand(0);
982 }
983 return true; // [r+i]
984 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
985 // Match LOAD (ADD (X, Lo(G))).
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000986 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +0000987 && "Cannot handle constant offsets yet!");
988 Disp = N.getOperand(1).getOperand(0); // The global address.
989 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
990 Disp.getOpcode() == ISD::TargetConstantPool ||
991 Disp.getOpcode() == ISD::TargetJumpTable);
992 Base = N.getOperand(0);
993 return true; // [&g+r]
994 }
995 } else if (N.getOpcode() == ISD::OR) {
996 short imm = 0;
997 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
998 // If this is an or of disjoint bitfields, we can codegen this as an add
999 // (for better address arithmetic) if the LHS and RHS of the OR are
1000 // provably disjoint.
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001001 APInt LHSKnownZero, LHSKnownOne;
1002 DAG.ComputeMaskedBits(N.getOperand(0),
Bill Wendling3e98c302008-03-24 23:16:37 +00001003 APInt::getAllOnesValue(N.getOperand(0)
1004 .getValueSizeInBits()),
Dan Gohmanb3564aa2008-02-27 01:23:58 +00001005 LHSKnownZero, LHSKnownOne);
1006 if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001007 // If all of the bits are known zero on the LHS or RHS, the add won't
1008 // carry.
1009 Base = N.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001011 return true;
1012 }
1013 }
1014 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001015 // Loading from a constant address. Verify low two bits are clear.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001016 if ((CN->getZExtValue() & 3) == 0) {
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001017 // If this address fits entirely in a 14-bit sext immediate field, codegen
1018 // this as "d, 0"
1019 short Imm;
1020 if (isIntS16Immediate(CN, Imm)) {
1021 Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
Cameron Zwarichd76773a2011-05-19 03:11:06 +00001022 Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::X0 : PPC::R0,
1023 CN->getValueType(0));
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001024 return true;
1025 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001026
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001027 // Fold the low-part of 32-bit absolute addresses into addr mode.
Owen Anderson825b72b2009-08-11 20:47:22 +00001028 if (CN->getValueType(0) == MVT::i32 ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001029 (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
1030 int Addr = (int)CN->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001032 // Otherwise, break this down into an LIS + disp.
Owen Anderson825b72b2009-08-11 20:47:22 +00001033 Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
1034 Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
1035 unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
Dan Gohman602b0c82009-09-25 18:54:59 +00001036 Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base),0);
Chris Lattnerdee5a5a2007-02-17 06:57:26 +00001037 return true;
1038 }
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001039 }
1040 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001041
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001042 Disp = DAG.getTargetConstant(0, getPointerTy());
1043 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
1044 Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
1045 else
1046 Base = N;
1047 return true; // [r+0]
1048}
1049
1050
1051/// getPreIndexedAddressParts - returns true by value, base pointer and
1052/// offset pointer and addressing mode by reference if the node's address
1053/// can be legally represented as pre-indexed load / store address.
Dan Gohman475871a2008-07-27 21:46:04 +00001054bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
1055 SDValue &Offset,
Evan Cheng144d8f02006-11-09 17:55:04 +00001056 ISD::MemIndexedMode &AM,
Dan Gohman73e09142009-01-15 16:29:45 +00001057 SelectionDAG &DAG) const {
Chris Lattner4eab7142006-11-10 02:08:47 +00001058 // Disabled by default for now.
1059 if (!EnablePPCPreinc) return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Dan Gohman475871a2008-07-27 21:46:04 +00001061 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00001062 EVT VT;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001063 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1064 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001065 VT = LD->getMemoryVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001066
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001067 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001068 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001069 VT = ST->getMemoryVT();
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001070 } else
1071 return false;
1072
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001073 // PowerPC doesn't have preinc load/store instructions for vectors.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001074 if (VT.isVector())
Chris Lattner2fe4bf42006-11-14 01:38:31 +00001075 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001076
Chris Lattner0851b4f2006-11-15 19:55:13 +00001077 // TODO: Check reg+reg first.
Scott Michelfdc40a02009-02-17 22:15:04 +00001078
Chris Lattner0851b4f2006-11-15 19:55:13 +00001079 // LDU/STU use reg+imm*4, others use reg+imm.
Owen Anderson825b72b2009-08-11 20:47:22 +00001080 if (VT != MVT::i64) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001081 // reg + imm
1082 if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
1083 return false;
1084 } else {
1085 // reg + imm * 4.
1086 if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
1087 return false;
1088 }
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001089
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001090 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner0851b4f2006-11-15 19:55:13 +00001091 // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of
1092 // sext i32 to i64 when addr mode is r+i.
Owen Anderson825b72b2009-08-11 20:47:22 +00001093 if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
Chris Lattnerf6edf4d2006-11-11 00:08:42 +00001094 LD->getExtensionType() == ISD::SEXTLOAD &&
1095 isa<ConstantSDNode>(Offset))
1096 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00001097 }
1098
Chris Lattner4eab7142006-11-10 02:08:47 +00001099 AM = ISD::PRE_INC;
1100 return true;
Chris Lattnerfc5b1ab2006-11-08 02:15:41 +00001101}
1102
1103//===----------------------------------------------------------------------===//
Chris Lattner1a635d62006-04-14 06:01:58 +00001104// LowerOperation implementation
1105//===----------------------------------------------------------------------===//
1106
Chris Lattner1e61e692010-11-15 02:46:57 +00001107/// GetLabelAccessInfo - Return true if we should reference labels using a
1108/// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags.
1109static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags,
Chris Lattner6d2ff122010-11-15 03:13:19 +00001110 unsigned &LoOpFlags, const GlobalValue *GV = 0) {
1111 HiOpFlags = PPCII::MO_HA16;
1112 LoOpFlags = PPCII::MO_LO16;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001113
Chris Lattner1e61e692010-11-15 02:46:57 +00001114 // Don't use the pic base if not in PIC relocation model. Or if we are on a
1115 // non-darwin platform. We don't support PIC on other platforms yet.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001116 bool isPIC = TM.getRelocationModel() == Reloc::PIC_ &&
Chris Lattner1e61e692010-11-15 02:46:57 +00001117 TM.getSubtarget<PPCSubtarget>().isDarwin();
Chris Lattner6d2ff122010-11-15 03:13:19 +00001118 if (isPIC) {
1119 HiOpFlags |= PPCII::MO_PIC_FLAG;
1120 LoOpFlags |= PPCII::MO_PIC_FLAG;
1121 }
1122
1123 // If this is a reference to a global value that requires a non-lazy-ptr, make
1124 // sure that instruction lowering adds it.
1125 if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) {
1126 HiOpFlags |= PPCII::MO_NLP_FLAG;
1127 LoOpFlags |= PPCII::MO_NLP_FLAG;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001128
Chris Lattner6d2ff122010-11-15 03:13:19 +00001129 if (GV->hasHiddenVisibility()) {
1130 HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1131 LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG;
1132 }
1133 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001134
Chris Lattner1e61e692010-11-15 02:46:57 +00001135 return isPIC;
1136}
1137
1138static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC,
1139 SelectionDAG &DAG) {
1140 EVT PtrVT = HiPart.getValueType();
1141 SDValue Zero = DAG.getConstant(0, PtrVT);
1142 DebugLoc DL = HiPart.getDebugLoc();
1143
1144 SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero);
1145 SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001146
Chris Lattner1e61e692010-11-15 02:46:57 +00001147 // With PIC, the first instruction is actually "GR+hi(&G)".
1148 if (isPIC)
1149 Hi = DAG.getNode(ISD::ADD, DL, PtrVT,
1150 DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001151
Chris Lattner1e61e692010-11-15 02:46:57 +00001152 // Generate non-pic code that has direct accesses to the constant pool.
1153 // The address of the global is just (hi(&g)+lo(&g)).
1154 return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1155}
1156
Scott Michelfdc40a02009-02-17 22:15:04 +00001157SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00001158 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001159 EVT PtrVT = Op.getValueType();
Chris Lattner1a635d62006-04-14 06:01:58 +00001160 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman46510a72010-04-15 01:51:59 +00001161 const Constant *C = CP->getConstVal();
Chris Lattner1a635d62006-04-14 06:01:58 +00001162
Chris Lattner1e61e692010-11-15 02:46:57 +00001163 unsigned MOHiFlag, MOLoFlag;
1164 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1165 SDValue CPIHi =
1166 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag);
1167 SDValue CPILo =
1168 DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag);
1169 return LowerLabelRef(CPIHi, CPILo, isPIC, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00001170}
1171
Dan Gohmand858e902010-04-17 15:26:15 +00001172SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00001173 EVT PtrVT = Op.getValueType();
Nate Begeman37efe672006-04-22 18:53:45 +00001174 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001175
Chris Lattner1e61e692010-11-15 02:46:57 +00001176 unsigned MOHiFlag, MOLoFlag;
1177 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1178 SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag);
1179 SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag);
1180 return LowerLabelRef(JTIHi, JTILo, isPIC, DAG);
Lauro Ramos Venancio75ce0102007-07-11 17:19:51 +00001181}
1182
Dan Gohmand858e902010-04-17 15:26:15 +00001183SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op,
1184 SelectionDAG &DAG) const {
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001185 EVT PtrVT = Op.getValueType();
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001186
Dan Gohman46510a72010-04-15 01:51:59 +00001187 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001188
Chris Lattner1e61e692010-11-15 02:46:57 +00001189 unsigned MOHiFlag, MOLoFlag;
1190 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag);
1191 SDValue TgtBAHi = DAG.getBlockAddress(BA, PtrVT, /*isTarget=*/true, MOHiFlag);
1192 SDValue TgtBALo = DAG.getBlockAddress(BA, PtrVT, /*isTarget=*/true, MOLoFlag);
1193 return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG);
1194}
1195
1196SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
1197 SelectionDAG &DAG) const {
1198 EVT PtrVT = Op.getValueType();
1199 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
1200 DebugLoc DL = GSDN->getDebugLoc();
1201 const GlobalValue *GV = GSDN->getGlobal();
1202
Chris Lattner1e61e692010-11-15 02:46:57 +00001203 // 64-bit SVR4 ABI code is always position-independent.
1204 // The actual address of the GlobalValue is stored in the TOC.
1205 if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
1206 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset());
1207 return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA,
1208 DAG.getRegister(PPC::X2, MVT::i64));
1209 }
1210
Chris Lattner6d2ff122010-11-15 03:13:19 +00001211 unsigned MOHiFlag, MOLoFlag;
1212 bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV);
Chris Lattner1e61e692010-11-15 02:46:57 +00001213
Chris Lattner6d2ff122010-11-15 03:13:19 +00001214 SDValue GAHi =
1215 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag);
1216 SDValue GALo =
1217 DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001218
Chris Lattner6d2ff122010-11-15 03:13:19 +00001219 SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00001220
Chris Lattner6d2ff122010-11-15 03:13:19 +00001221 // If the global reference is actually to a non-lazy-pointer, we have to do an
1222 // extra load to get the address of the global.
1223 if (MOHiFlag & PPCII::MO_NLP_FLAG)
1224 Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001225 false, false, false, 0);
Chris Lattner6d2ff122010-11-15 03:13:19 +00001226 return Ptr;
Chris Lattner1a635d62006-04-14 06:01:58 +00001227}
1228
Dan Gohmand858e902010-04-17 15:26:15 +00001229SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00001230 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001231 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001232
Chris Lattner1a635d62006-04-14 06:01:58 +00001233 // If we're comparing for equality to zero, expose the fact that this is
1234 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1235 // fold the new nodes.
1236 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1237 if (C->isNullValue() && CC == ISD::SETEQ) {
Owen Andersone50ed302009-08-10 22:56:29 +00001238 EVT VT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001239 SDValue Zext = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 if (VT.bitsLT(MVT::i32)) {
1241 VT = MVT::i32;
Dale Johannesenf5d97892009-02-04 01:48:28 +00001242 Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001243 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00001244 unsigned Log2b = Log2_32(VT.getSizeInBits());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001245 SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
1246 SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
Owen Anderson825b72b2009-08-11 20:47:22 +00001247 DAG.getConstant(Log2b, MVT::i32));
1248 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
Chris Lattner1a635d62006-04-14 06:01:58 +00001249 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001250 // Leave comparisons against 0 and -1 alone for now, since they're usually
Chris Lattner1a635d62006-04-14 06:01:58 +00001251 // optimized. FIXME: revisit this when we can custom lower all setcc
1252 // optimizations.
1253 if (C->isAllOnesValue() || C->isNullValue())
Dan Gohman475871a2008-07-27 21:46:04 +00001254 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001255 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001256
Chris Lattner1a635d62006-04-14 06:01:58 +00001257 // If we have an integer seteq/setne, turn it into a compare against zero
Chris Lattnerac011bc2006-11-14 05:28:08 +00001258 // by xor'ing the rhs with the lhs, which is faster than setting a
1259 // condition register, reading it back out, and masking the correct bit. The
1260 // normal approach here uses sub to do this instead of xor. Using xor exposes
1261 // the result to other bit-twiddling opportunities.
Owen Andersone50ed302009-08-10 22:56:29 +00001262 EVT LHSVT = Op.getOperand(0).getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001263 if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001264 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001265 SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
Chris Lattner1a635d62006-04-14 06:01:58 +00001266 Op.getOperand(1));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001267 return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
Chris Lattner1a635d62006-04-14 06:01:58 +00001268 }
Dan Gohman475871a2008-07-27 21:46:04 +00001269 return SDValue();
Chris Lattner1a635d62006-04-14 06:01:58 +00001270}
1271
Dan Gohman475871a2008-07-27 21:46:04 +00001272SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001273 const PPCSubtarget &Subtarget) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00001274 SDNode *Node = Op.getNode();
1275 EVT VT = Node->getValueType(0);
1276 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1277 SDValue InChain = Node->getOperand(0);
1278 SDValue VAListPtr = Node->getOperand(1);
1279 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1280 DebugLoc dl = Node->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Roman Divackybdb226e2011-06-28 15:30:42 +00001282 assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only");
1283
1284 // gpr_index
1285 SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1286 VAListPtr, MachinePointerInfo(SV), MVT::i8,
1287 false, false, 0);
1288 InChain = GprIndex.getValue(1);
1289
1290 if (VT == MVT::i64) {
1291 // Check if GprIndex is even
1292 SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex,
1293 DAG.getConstant(1, MVT::i32));
1294 SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd,
1295 DAG.getConstant(0, MVT::i32), ISD::SETNE);
1296 SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex,
1297 DAG.getConstant(1, MVT::i32));
1298 // Align GprIndex to be even if it isn't
1299 GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne,
1300 GprIndex);
1301 }
1302
1303 // fpr index is 1 byte after gpr
1304 SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1305 DAG.getConstant(1, MVT::i32));
1306
1307 // fpr
1308 SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain,
1309 FprPtr, MachinePointerInfo(SV), MVT::i8,
1310 false, false, 0);
1311 InChain = FprIndex.getValue(1);
1312
1313 SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1314 DAG.getConstant(8, MVT::i32));
1315
1316 SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr,
1317 DAG.getConstant(4, MVT::i32));
1318
1319 // areas
1320 SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001321 MachinePointerInfo(), false, false,
1322 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001323 InChain = OverflowArea.getValue(1);
1324
1325 SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001326 MachinePointerInfo(), false, false,
1327 false, 0);
Roman Divackybdb226e2011-06-28 15:30:42 +00001328 InChain = RegSaveArea.getValue(1);
1329
1330 // select overflow_area if index > 8
1331 SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex,
1332 DAG.getConstant(8, MVT::i32), ISD::SETLT);
1333
Roman Divackybdb226e2011-06-28 15:30:42 +00001334 // adjustment constant gpr_index * 4/8
1335 SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32,
1336 VT.isInteger() ? GprIndex : FprIndex,
1337 DAG.getConstant(VT.isInteger() ? 4 : 8,
1338 MVT::i32));
1339
1340 // OurReg = RegSaveArea + RegConstant
1341 SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea,
1342 RegConstant);
1343
1344 // Floating types are 32 bytes into RegSaveArea
1345 if (VT.isFloatingPoint())
1346 OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg,
1347 DAG.getConstant(32, MVT::i32));
1348
1349 // increase {f,g}pr_index by 1 (or 2 if VT is i64)
1350 SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32,
1351 VT.isInteger() ? GprIndex : FprIndex,
1352 DAG.getConstant(VT == MVT::i64 ? 2 : 1,
1353 MVT::i32));
1354
1355 InChain = DAG.getTruncStore(InChain, dl, IndexPlus1,
1356 VT.isInteger() ? VAListPtr : FprPtr,
1357 MachinePointerInfo(SV),
1358 MVT::i8, false, false, 0);
1359
1360 // determine if we should load from reg_save_area or overflow_area
1361 SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea);
1362
1363 // increase overflow_area by 4/8 if gpr/fpr > 8
1364 SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea,
1365 DAG.getConstant(VT.isInteger() ? 4 : 8,
1366 MVT::i32));
1367
1368 OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea,
1369 OverflowAreaPlusN);
1370
1371 InChain = DAG.getTruncStore(InChain, dl, OverflowArea,
1372 OverflowAreaPtr,
1373 MachinePointerInfo(),
1374 MVT::i32, false, false, 0);
1375
Pete Cooperd752e0f2011-11-08 18:42:53 +00001376 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(),
1377 false, false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001378}
1379
Duncan Sands4a544a72011-09-06 13:37:06 +00001380SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
1381 SelectionDAG &DAG) const {
1382 return Op.getOperand(0);
1383}
1384
1385SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
1386 SelectionDAG &DAG) const {
Bill Wendling77959322008-09-17 00:30:57 +00001387 SDValue Chain = Op.getOperand(0);
1388 SDValue Trmp = Op.getOperand(1); // trampoline
1389 SDValue FPtr = Op.getOperand(2); // nested function
1390 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001391 DebugLoc dl = Op.getDebugLoc();
Bill Wendling77959322008-09-17 00:30:57 +00001392
Owen Andersone50ed302009-08-10 22:56:29 +00001393 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00001394 bool isPPC64 = (PtrVT == MVT::i64);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001395 Type *IntPtrTy =
Owen Anderson1d0be152009-08-13 21:58:54 +00001396 DAG.getTargetLoweringInfo().getTargetData()->getIntPtrType(
1397 *DAG.getContext());
Bill Wendling77959322008-09-17 00:30:57 +00001398
Scott Michelfdc40a02009-02-17 22:15:04 +00001399 TargetLowering::ArgListTy Args;
Bill Wendling77959322008-09-17 00:30:57 +00001400 TargetLowering::ArgListEntry Entry;
1401
1402 Entry.Ty = IntPtrTy;
1403 Entry.Node = Trmp; Args.push_back(Entry);
1404
1405 // TrampSize == (isPPC64 ? 48 : 40);
1406 Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
Owen Anderson825b72b2009-08-11 20:47:22 +00001407 isPPC64 ? MVT::i64 : MVT::i32);
Bill Wendling77959322008-09-17 00:30:57 +00001408 Args.push_back(Entry);
1409
1410 Entry.Node = FPtr; Args.push_back(Entry);
1411 Entry.Node = Nest; Args.push_back(Entry);
Scott Michelfdc40a02009-02-17 22:15:04 +00001412
Bill Wendling77959322008-09-17 00:30:57 +00001413 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
1414 std::pair<SDValue, SDValue> CallResult =
Duncan Sands4a544a72011-09-06 13:37:06 +00001415 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
Owen Andersond1474d02009-07-09 17:57:24 +00001416 false, false, false, false, 0, CallingConv::C, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001417 /*isReturnValueUsed=*/true,
Bill Wendling77959322008-09-17 00:30:57 +00001418 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
Bill Wendling46ada192010-03-02 01:55:18 +00001419 Args, DAG, dl);
Bill Wendling77959322008-09-17 00:30:57 +00001420
Duncan Sands4a544a72011-09-06 13:37:06 +00001421 return CallResult.second;
Bill Wendling77959322008-09-17 00:30:57 +00001422}
1423
Dan Gohman475871a2008-07-27 21:46:04 +00001424SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001425 const PPCSubtarget &Subtarget) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001426 MachineFunction &MF = DAG.getMachineFunction();
1427 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
1428
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001429 DebugLoc dl = Op.getDebugLoc();
Nicolas Geoffray01119992007-04-03 13:59:52 +00001430
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001431 if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
Nicolas Geoffray01119992007-04-03 13:59:52 +00001432 // vastart just stores the address of the VarArgsFrameIndex slot into the
1433 // memory location argument.
Owen Andersone50ed302009-08-10 22:56:29 +00001434 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman1e93df62010-04-17 14:41:14 +00001435 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001436 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +00001437 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
1438 MachinePointerInfo(SV),
David Greene534502d12010-02-15 16:56:53 +00001439 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001440 }
1441
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001442 // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
Nicolas Geoffray01119992007-04-03 13:59:52 +00001443 // We suppose the given va_list is already allocated.
1444 //
1445 // typedef struct {
1446 // char gpr; /* index into the array of 8 GPRs
1447 // * stored in the register save area
1448 // * gpr=0 corresponds to r3,
1449 // * gpr=1 to r4, etc.
1450 // */
1451 // char fpr; /* index into the array of 8 FPRs
1452 // * stored in the register save area
1453 // * fpr=0 corresponds to f1,
1454 // * fpr=1 to f2, etc.
1455 // */
1456 // char *overflow_arg_area;
1457 // /* location on stack that holds
1458 // * the next overflow argument
1459 // */
1460 // char *reg_save_area;
1461 // /* where r3:r10 and f1:f8 (if saved)
1462 // * are stored
1463 // */
1464 // } va_list[1];
1465
1466
Dan Gohman1e93df62010-04-17 14:41:14 +00001467 SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32);
1468 SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00001469
Nicolas Geoffray01119992007-04-03 13:59:52 +00001470
Owen Andersone50ed302009-08-10 22:56:29 +00001471 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Scott Michelfdc40a02009-02-17 22:15:04 +00001472
Dan Gohman1e93df62010-04-17 14:41:14 +00001473 SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(),
1474 PtrVT);
1475 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1476 PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001477
Duncan Sands83ec4b62008-06-06 12:08:01 +00001478 uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
Dan Gohman475871a2008-07-27 21:46:04 +00001479 SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001480
Duncan Sands83ec4b62008-06-06 12:08:01 +00001481 uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001482 SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
Dan Gohman69de1932008-02-06 22:27:42 +00001483
1484 uint64_t FPROffset = 1;
Dan Gohman475871a2008-07-27 21:46:04 +00001485 SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001486
Dan Gohman69de1932008-02-06 22:27:42 +00001487 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00001488
Nicolas Geoffray01119992007-04-03 13:59:52 +00001489 // Store first byte : number of int regs
Tilmann Schellerffd02002009-07-03 06:45:56 +00001490 SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001491 Op.getOperand(1),
1492 MachinePointerInfo(SV),
1493 MVT::i8, false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001494 uint64_t nextOffset = FPROffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001495 SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
Nicolas Geoffray01119992007-04-03 13:59:52 +00001496 ConstFPROffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001497
Nicolas Geoffray01119992007-04-03 13:59:52 +00001498 // Store second byte : number of float regs
Dan Gohman475871a2008-07-27 21:46:04 +00001499 SDValue secondStore =
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001500 DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr,
1501 MachinePointerInfo(SV, nextOffset), MVT::i8,
David Greene534502d12010-02-15 16:56:53 +00001502 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001503 nextOffset += StackOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001504 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
Scott Michelfdc40a02009-02-17 22:15:04 +00001505
Nicolas Geoffray01119992007-04-03 13:59:52 +00001506 // Store second word : arguments given on stack
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SDValue thirdStore =
Chris Lattner6229d0a2010-09-21 18:41:36 +00001508 DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr,
1509 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001510 false, false, 0);
Dan Gohman69de1932008-02-06 22:27:42 +00001511 nextOffset += FrameOffset;
Dale Johannesen33c960f2009-02-04 20:06:27 +00001512 nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001513
1514 // Store third word : arguments given in registers
Chris Lattner6229d0a2010-09-21 18:41:36 +00001515 return DAG.getStore(thirdStore, dl, FR, nextPtr,
1516 MachinePointerInfo(SV, nextOffset),
David Greene534502d12010-02-15 16:56:53 +00001517 false, false, 0);
Nicolas Geoffray01119992007-04-03 13:59:52 +00001518
Chris Lattner1a635d62006-04-14 06:01:58 +00001519}
1520
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00001521#include "PPCGenCallingConv.inc"
1522
Duncan Sands1e96bab2010-11-04 10:49:57 +00001523static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001524 CCValAssign::LocInfo &LocInfo,
1525 ISD::ArgFlagsTy &ArgFlags,
1526 CCState &State) {
1527 return true;
1528}
1529
Duncan Sands1e96bab2010-11-04 10:49:57 +00001530static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001531 MVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001532 CCValAssign::LocInfo &LocInfo,
1533 ISD::ArgFlagsTy &ArgFlags,
1534 CCState &State) {
1535 static const unsigned ArgRegs[] = {
1536 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1537 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1538 };
1539 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001540
Tilmann Schellerffd02002009-07-03 06:45:56 +00001541 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1542
1543 // Skip one register if the first unallocated register has an even register
1544 // number and there are still argument registers available which have not been
1545 // allocated yet. RegNum is actually an index into ArgRegs, which means we
1546 // need to skip a register if RegNum is odd.
1547 if (RegNum != NumArgRegs && RegNum % 2 == 1) {
1548 State.AllocateReg(ArgRegs[RegNum]);
1549 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001550
Tilmann Schellerffd02002009-07-03 06:45:56 +00001551 // Always return false here, as this function only makes sure that the first
1552 // unallocated register has an odd register number and does not actually
1553 // allocate a register for the current argument.
1554 return false;
1555}
1556
Duncan Sands1e96bab2010-11-04 10:49:57 +00001557static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT,
Duncan Sands1440e8b2010-11-03 11:35:31 +00001558 MVT &LocVT,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001559 CCValAssign::LocInfo &LocInfo,
1560 ISD::ArgFlagsTy &ArgFlags,
1561 CCState &State) {
1562 static const unsigned ArgRegs[] = {
1563 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1564 PPC::F8
1565 };
1566
1567 const unsigned NumArgRegs = array_lengthof(ArgRegs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001568
Tilmann Schellerffd02002009-07-03 06:45:56 +00001569 unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
1570
1571 // If there is only one Floating-point register left we need to put both f64
1572 // values of a split ppc_fp128 value on the stack.
1573 if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
1574 State.AllocateReg(ArgRegs[RegNum]);
1575 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001576
Tilmann Schellerffd02002009-07-03 06:45:56 +00001577 // Always return false here, as this function only makes sure that the two f64
1578 // values a ppc_fp128 value is split into are both passed in registers or both
1579 // passed on the stack and does not actually allocate a register for the
1580 // current argument.
1581 return false;
1582}
1583
Chris Lattner9f0bc652007-02-25 05:34:32 +00001584/// GetFPR - Get the set of FP registers that should be allocated for arguments,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001585/// on Darwin.
1586static const unsigned *GetFPR() {
Chris Lattner9f0bc652007-02-25 05:34:32 +00001587 static const unsigned FPR[] = {
1588 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001589 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
Chris Lattner9f0bc652007-02-25 05:34:32 +00001590 };
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001591
Chris Lattner9f0bc652007-02-25 05:34:32 +00001592 return FPR;
1593}
1594
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001595/// CalculateStackSlotSize - Calculates the size reserved for this argument on
1596/// the stack.
Owen Andersone50ed302009-08-10 22:56:29 +00001597static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001598 unsigned PtrByteSize) {
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00001599 unsigned ArgSize = ArgVT.getSizeInBits()/8;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001600 if (Flags.isByVal())
1601 ArgSize = Flags.getByValSize();
1602 ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1603
1604 return ArgSize;
1605}
1606
Dan Gohman475871a2008-07-27 21:46:04 +00001607SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001608PPCTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001609 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610 const SmallVectorImpl<ISD::InputArg>
1611 &Ins,
1612 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001613 SmallVectorImpl<SDValue> &InVals)
1614 const {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001615 if (PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001616 return LowerFormalArguments_SVR4(Chain, CallConv, isVarArg, Ins,
1617 dl, DAG, InVals);
1618 } else {
1619 return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
1620 dl, DAG, InVals);
1621 }
1622}
1623
1624SDValue
1625PPCTargetLowering::LowerFormalArguments_SVR4(
1626 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001627 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001628 const SmallVectorImpl<ISD::InputArg>
1629 &Ins,
1630 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001631 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001633 // 32-bit SVR4 ABI Stack Frame Layout:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001634 // +-----------------------------------+
1635 // +--> | Back chain |
1636 // | +-----------------------------------+
1637 // | | Floating-point register save area |
1638 // | +-----------------------------------+
1639 // | | General register save area |
1640 // | +-----------------------------------+
1641 // | | CR save word |
1642 // | +-----------------------------------+
1643 // | | VRSAVE save word |
1644 // | +-----------------------------------+
1645 // | | Alignment padding |
1646 // | +-----------------------------------+
1647 // | | Vector register save area |
1648 // | +-----------------------------------+
1649 // | | Local variable space |
1650 // | +-----------------------------------+
1651 // | | Parameter list area |
1652 // | +-----------------------------------+
1653 // | | LR save word |
1654 // | +-----------------------------------+
1655 // SP--> +--- | Back chain |
1656 // +-----------------------------------+
1657 //
1658 // Specifications:
1659 // System V Application Binary Interface PowerPC Processor Supplement
1660 // AltiVec Technology Programming Interface Manual
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001661
Tilmann Schellerffd02002009-07-03 06:45:56 +00001662 MachineFunction &MF = DAG.getMachineFunction();
1663 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00001664 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001665
Owen Andersone50ed302009-08-10 22:56:29 +00001666 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Tilmann Schellerffd02002009-07-03 06:45:56 +00001667 // Potential tail calls could cause overwriting of argument stack slots.
Dan Gohman1797ed52010-02-08 20:27:50 +00001668 bool isImmutable = !(GuaranteedTailCallOpt && (CallConv==CallingConv::Fast));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001669 unsigned PtrByteSize = 4;
1670
1671 // Assign locations to all of the incoming arguments.
1672 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001673 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1674 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001675
1676 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001677 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001678
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679 CCInfo.AnalyzeFormalArguments(Ins, CC_PPC_SVR4);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001680
Tilmann Schellerffd02002009-07-03 06:45:56 +00001681 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1682 CCValAssign &VA = ArgLocs[i];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001683
Tilmann Schellerffd02002009-07-03 06:45:56 +00001684 // Arguments stored in registers.
1685 if (VA.isRegLoc()) {
1686 TargetRegisterClass *RC;
Owen Andersone50ed302009-08-10 22:56:29 +00001687 EVT ValVT = VA.getValVT();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001688
Owen Anderson825b72b2009-08-11 20:47:22 +00001689 switch (ValVT.getSimpleVT().SimpleTy) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00001690 default:
Dan Gohman98ca4f22009-08-05 01:29:28 +00001691 llvm_unreachable("ValVT not supported by formal arguments Lowering");
Owen Anderson825b72b2009-08-11 20:47:22 +00001692 case MVT::i32:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001693 RC = PPC::GPRCRegisterClass;
1694 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001695 case MVT::f32:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001696 RC = PPC::F4RCRegisterClass;
1697 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 case MVT::f64:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001699 RC = PPC::F8RCRegisterClass;
1700 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 case MVT::v16i8:
1702 case MVT::v8i16:
1703 case MVT::v4i32:
1704 case MVT::v4f32:
Tilmann Schellerffd02002009-07-03 06:45:56 +00001705 RC = PPC::VRRCRegisterClass;
1706 break;
1707 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001708
Tilmann Schellerffd02002009-07-03 06:45:56 +00001709 // Transform the arguments stored in physical registers into virtual ones.
Devang Patel68e6bee2011-02-21 23:21:26 +00001710 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001712
Dan Gohman98ca4f22009-08-05 01:29:28 +00001713 InVals.push_back(ArgValue);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001714 } else {
1715 // Argument stored in memory.
1716 assert(VA.isMemLoc());
1717
1718 unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
1719 int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
Evan Chenged2ae132010-07-03 00:40:23 +00001720 isImmutable);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001721
1722 // Create load nodes to retrieve arguments from the stack.
1723 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001724 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1725 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001726 false, false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001727 }
1728 }
1729
1730 // Assign locations to all of the incoming aggregate by value arguments.
1731 // Aggregates passed by value are stored in the local variable space of the
1732 // caller's stack frame, right above the parameter list area.
1733 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001734 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1735 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001736
1737 // Reserve stack space for the allocations in CCInfo.
1738 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
1739
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001741
1742 // Area that is at least reserved in the caller of this function.
1743 unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001744
Tilmann Schellerffd02002009-07-03 06:45:56 +00001745 // Set the size that is at least reserved in caller of this function. Tail
1746 // call optimized function's reserved stack space needs to be aligned so that
1747 // taking the difference between two stack areas will result in an aligned
1748 // stack.
1749 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
1750
1751 MinReservedArea =
1752 std::max(MinReservedArea,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001753 PPCFrameLowering::getMinCallFrameSize(false, false));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001754
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001755 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Tilmann Schellerffd02002009-07-03 06:45:56 +00001756 getStackAlignment();
1757 unsigned AlignMask = TargetAlign-1;
1758 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001759
Tilmann Schellerffd02002009-07-03 06:45:56 +00001760 FI->setMinReservedArea(MinReservedArea);
1761
1762 SmallVector<SDValue, 8> MemOps;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001763
Tilmann Schellerffd02002009-07-03 06:45:56 +00001764 // If the function takes variable number of arguments, make a frame index for
1765 // the start of the first vararg value... for expansion of llvm.va_start.
1766 if (isVarArg) {
1767 static const unsigned GPArgRegs[] = {
1768 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1769 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1770 };
1771 const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
1772
1773 static const unsigned FPArgRegs[] = {
1774 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1775 PPC::F8
1776 };
1777 const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
1778
Dan Gohman1e93df62010-04-17 14:41:14 +00001779 FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs,
1780 NumGPArgRegs));
1781 FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs,
1782 NumFPArgRegs));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001783
1784 // Make room for NumGPArgRegs and NumFPArgRegs.
1785 int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
Owen Anderson825b72b2009-08-11 20:47:22 +00001786 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001787
Dan Gohman1e93df62010-04-17 14:41:14 +00001788 FuncInfo->setVarArgsStackOffset(
1789 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001790 CCInfo.getNextStackOffset(), true));
Tilmann Schellerffd02002009-07-03 06:45:56 +00001791
Dan Gohman1e93df62010-04-17 14:41:14 +00001792 FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false));
1793 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001794
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00001795 // The fixed integer arguments of a variadic function are stored to the
1796 // VarArgsFrameIndex on the stack so that they may be loaded by deferencing
1797 // the result of va_next.
1798 for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) {
1799 // Get an existing live-in vreg, or add a new one.
1800 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]);
1801 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00001802 VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001803
Dan Gohman98ca4f22009-08-05 01:29:28 +00001804 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00001805 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1806 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001807 MemOps.push_back(Store);
1808 // Increment the address by four for the next argument to store
1809 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
1810 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1811 }
1812
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001813 // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
1814 // is set.
Tilmann Schellerffd02002009-07-03 06:45:56 +00001815 // The double arguments are stored to the VarArgsFrameIndex
1816 // on the stack.
Jakob Stoklund Olesen4f9af2e2010-10-11 20:43:09 +00001817 for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) {
1818 // Get an existing live-in vreg, or add a new one.
1819 unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]);
1820 if (!VReg)
Devang Patel68e6bee2011-02-21 23:21:26 +00001821 VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001822
Owen Anderson825b72b2009-08-11 20:47:22 +00001823 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
Chris Lattner6229d0a2010-09-21 18:41:36 +00001824 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
1825 MachinePointerInfo(), false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00001826 MemOps.push_back(Store);
1827 // Increment the address by eight for the next argument to store
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
Tilmann Schellerffd02002009-07-03 06:45:56 +00001829 PtrVT);
1830 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
1831 }
1832 }
1833
1834 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00001835 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001836 MVT::Other, &MemOps[0], MemOps.size());
Tilmann Schellerffd02002009-07-03 06:45:56 +00001837
Dan Gohman98ca4f22009-08-05 01:29:28 +00001838 return Chain;
Tilmann Schellerffd02002009-07-03 06:45:56 +00001839}
1840
1841SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001842PPCTargetLowering::LowerFormalArguments_Darwin(
1843 SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001844 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845 const SmallVectorImpl<ISD::InputArg>
1846 &Ins,
1847 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001848 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001849 // TODO: add description of PPC stack frame format, or at least some docs.
1850 //
1851 MachineFunction &MF = DAG.getMachineFunction();
1852 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +00001853 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001854
Owen Andersone50ed302009-08-10 22:56:29 +00001855 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 bool isPPC64 = PtrVT == MVT::i64;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001857 // Potential tail calls could cause overwriting of argument stack slots.
Dan Gohman1797ed52010-02-08 20:27:50 +00001858 bool isImmutable = !(GuaranteedTailCallOpt && (CallConv==CallingConv::Fast));
Jim Laskeye9bd7b22006-11-28 14:53:52 +00001859 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Jim Laskey2f616bf2006-11-16 22:43:37 +00001860
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001861 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001862 // Area that is at least reserved in caller of this function.
1863 unsigned MinReservedArea = ArgOffset;
1864
Chris Lattnerc91a4752006-06-26 22:48:35 +00001865 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001866 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1867 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1868 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001869 static const unsigned GPR_64[] = { // 64-bit registers.
1870 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
1871 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
1872 };
Scott Michelfdc40a02009-02-17 22:15:04 +00001873
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00001874 static const unsigned *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00001875
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001876 static const unsigned VR[] = {
1877 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
1878 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
1879 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00001880
Owen Anderson718cb662007-09-07 04:06:50 +00001881 const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001882 const unsigned Num_FPR_Regs = 13;
Owen Anderson718cb662007-09-07 04:06:50 +00001883 const unsigned Num_VR_Regs = array_lengthof( VR);
Jim Laskey2f616bf2006-11-16 22:43:37 +00001884
1885 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00001886
Chris Lattnerc91a4752006-06-26 22:48:35 +00001887 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
Scott Michelfdc40a02009-02-17 22:15:04 +00001888
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001889 // In 32-bit non-varargs functions, the stack space for vectors is after the
1890 // stack space for non-vectors. We do not use this space unless we have
1891 // too many vectors to fit in registers, something that only occurs in
Scott Michelfdc40a02009-02-17 22:15:04 +00001892 // constructed examples:), but we have to walk the arglist to figure
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001893 // that out...for the pathological case, compute VecArgOffset as the
1894 // start of the vector parameter area. Computing VecArgOffset is the
1895 // entire point of the following loop.
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001896 unsigned VecArgOffset = ArgOffset;
1897 if (!isVarArg && !isPPC64) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001898 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001899 ++ArgNo) {
Owen Andersone50ed302009-08-10 22:56:29 +00001900 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001901 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001902 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001903
Duncan Sands276dcbd2008-03-21 09:14:45 +00001904 if (Flags.isByVal()) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001905 // ObjSize is the true size, ArgSize rounded up to multiple of regs.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001906 ObjSize = Flags.getByValSize();
Scott Michelfdc40a02009-02-17 22:15:04 +00001907 unsigned ArgSize =
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001908 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
1909 VecArgOffset += ArgSize;
1910 continue;
1911 }
1912
Owen Anderson825b72b2009-08-11 20:47:22 +00001913 switch(ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001914 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001915 case MVT::i32:
1916 case MVT::f32:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001917 VecArgOffset += isPPC64 ? 8 : 4;
1918 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001919 case MVT::i64: // PPC64
1920 case MVT::f64:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001921 VecArgOffset += 8;
1922 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001923 case MVT::v4f32:
1924 case MVT::v4i32:
1925 case MVT::v8i16:
1926 case MVT::v16i8:
Dale Johannesen8f5422c2008-03-14 17:41:26 +00001927 // Nothing to do, we're only looking at Nonvector args here.
1928 break;
1929 }
1930 }
1931 }
1932 // We've found where the vector parameter area in memory is. Skip the
1933 // first 12 parameters; these don't use that memory.
1934 VecArgOffset = ((VecArgOffset+15)/16)*16;
1935 VecArgOffset += 12*16;
1936
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001937 // Add DAG nodes to load the arguments or copy them out of registers. On
Jim Laskey2f616bf2006-11-16 22:43:37 +00001938 // entry to a function on PPC, the arguments start after the linkage area,
1939 // although the first ones are often in registers.
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00001940
Dan Gohman475871a2008-07-27 21:46:04 +00001941 SmallVector<SDValue, 8> MemOps;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001942 unsigned nAltivecParamsAtEnd = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001943 for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SDValue ArgVal;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001945 bool needsLoad = false;
Owen Andersone50ed302009-08-10 22:56:29 +00001946 EVT ObjectVT = Ins[ArgNo].VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001947 unsigned ObjSize = ObjectVT.getSizeInBits()/8;
Jim Laskey619965d2006-11-29 13:37:09 +00001948 unsigned ArgSize = ObjSize;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001949 ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00001950
Chris Lattnerbe4849a2006-05-16 18:51:52 +00001951 unsigned CurArgOffset = ArgOffset;
Dale Johannesen8419dd62008-03-07 20:27:40 +00001952
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001953 // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
1955 ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001956 if (isVarArg || isPPC64) {
1957 MinReservedArea = ((MinReservedArea+15)/16)*16;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001958 MinReservedArea += CalculateStackSlotSize(ObjectVT,
Dan Gohman095cc292008-09-13 01:54:27 +00001959 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001960 PtrByteSize);
1961 } else nAltivecParamsAtEnd++;
1962 } else
1963 // Calculate min reserved area.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001964 MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
Dan Gohman095cc292008-09-13 01:54:27 +00001965 Flags,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001966 PtrByteSize);
1967
Dale Johannesen8419dd62008-03-07 20:27:40 +00001968 // FIXME the codegen can be much improved in some cases.
1969 // We do not have to keep everything in memory.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001970 if (Flags.isByVal()) {
Dale Johannesen8419dd62008-03-07 20:27:40 +00001971 // ObjSize is the true size, ArgSize rounded up to multiple of registers.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001972 ObjSize = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00001973 ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001974 // Objects of size 1 and 2 are right justified, everything else is
1975 // left justified. This means the memory address is adjusted forwards.
1976 if (ObjSize==1 || ObjSize==2) {
1977 CurArgOffset = CurArgOffset + (4 - ObjSize);
1978 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00001979 // The value of the object is its address.
Evan Chenged2ae132010-07-03 00:40:23 +00001980 int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00001981 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001982 InVals.push_back(FIN);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001983 if (ObjSize==1 || ObjSize==2) {
1984 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00001985 unsigned VReg;
1986 if (isPPC64)
1987 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
1988 else
1989 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001991 SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00001992 MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00001993 ObjSize==1 ? MVT::i8 : MVT::i16,
1994 false, false, 0);
Dale Johannesen7f96f392008-03-08 01:41:42 +00001995 MemOps.push_back(Store);
1996 ++GPR_idx;
Dale Johannesen7f96f392008-03-08 01:41:42 +00001997 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001998
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00001999 ArgOffset += PtrByteSize;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002000
Dale Johannesen7f96f392008-03-08 01:41:42 +00002001 continue;
2002 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002003 for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
2004 // Store whatever pieces of the object are in registers
2005 // to memory. ArgVal will be address of the beginning of
2006 // the object.
2007 if (GPR_idx != Num_GPR_Regs) {
Roman Divacky951cd022011-06-17 15:21:10 +00002008 unsigned VReg;
2009 if (isPPC64)
2010 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
2011 else
2012 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Evan Chenged2ae132010-07-03 00:40:23 +00002013 int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true);
Dan Gohman475871a2008-07-27 21:46:04 +00002014 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002015 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002016 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2017 MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00002018 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00002019 MemOps.push_back(Store);
2020 ++GPR_idx;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002021 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00002022 } else {
2023 ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
2024 break;
2025 }
2026 }
2027 continue;
2028 }
2029
Owen Anderson825b72b2009-08-11 20:47:22 +00002030 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002031 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00002032 case MVT::i32:
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002033 if (!isPPC64) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002034 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002035 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002036 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002037 ++GPR_idx;
2038 } else {
2039 needsLoad = true;
2040 ArgSize = PtrByteSize;
2041 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002042 // All int arguments reserve stack space in the Darwin ABI.
2043 ArgOffset += PtrByteSize;
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002044 break;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002045 }
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002046 // FALLTHROUGH
Owen Anderson825b72b2009-08-11 20:47:22 +00002047 case MVT::i64: // PPC64
Chris Lattnerc91a4752006-06-26 22:48:35 +00002048 if (GPR_idx != Num_GPR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002049 unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002051
Owen Anderson825b72b2009-08-11 20:47:22 +00002052 if (ObjectVT == MVT::i32) {
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002053 // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
Owen Anderson825b72b2009-08-11 20:47:22 +00002054 // value to MVT::i64 and then truncate to the correct register size.
Duncan Sands276dcbd2008-03-21 09:14:45 +00002055 if (Flags.isSExt())
Owen Anderson825b72b2009-08-11 20:47:22 +00002056 ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002057 DAG.getValueType(ObjectVT));
Duncan Sands276dcbd2008-03-21 09:14:45 +00002058 else if (Flags.isZExt())
Owen Anderson825b72b2009-08-11 20:47:22 +00002059 ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002060 DAG.getValueType(ObjectVT));
2061
Owen Anderson825b72b2009-08-11 20:47:22 +00002062 ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
Bill Wendling5f5bf3a2008-03-07 20:49:02 +00002063 }
2064
Chris Lattnerc91a4752006-06-26 22:48:35 +00002065 ++GPR_idx;
2066 } else {
2067 needsLoad = true;
Evan Cheng982a0592008-07-24 08:17:07 +00002068 ArgSize = PtrByteSize;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002069 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002070 // All int arguments reserve stack space in the Darwin ABI.
2071 ArgOffset += 8;
Chris Lattnerc91a4752006-06-26 22:48:35 +00002072 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00002073
Owen Anderson825b72b2009-08-11 20:47:22 +00002074 case MVT::f32:
2075 case MVT::f64:
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002076 // Every 4 bytes of argument space consumes one of the GPRs available for
2077 // argument passing.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002078 if (GPR_idx != Num_GPR_Regs) {
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002079 ++GPR_idx;
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002080 if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002081 ++GPR_idx;
Chris Lattnerbe4849a2006-05-16 18:51:52 +00002082 }
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002083 if (FPR_idx != Num_FPR_Regs) {
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002084 unsigned VReg;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002085
Owen Anderson825b72b2009-08-11 20:47:22 +00002086 if (ObjectVT == MVT::f32)
Devang Patel68e6bee2011-02-21 23:21:26 +00002087 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002088 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002089 VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002090
Dan Gohman98ca4f22009-08-05 01:29:28 +00002091 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002092 ++FPR_idx;
2093 } else {
2094 needsLoad = true;
2095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002096
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002097 // All FP arguments reserve stack space in the Darwin ABI.
2098 ArgOffset += isPPC64 ? 8 : ObjSize;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002099 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002100 case MVT::v4f32:
2101 case MVT::v4i32:
2102 case MVT::v8i16:
2103 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00002104 // Note that vector arguments in registers don't reserve stack space,
2105 // except in varargs functions.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002106 if (VR_idx != Num_VR_Regs) {
Devang Patel68e6bee2011-02-21 23:21:26 +00002107 unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002108 ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
Dale Johannesen75092de2008-03-12 00:22:17 +00002109 if (isVarArg) {
2110 while ((ArgOffset % 16) != 0) {
2111 ArgOffset += PtrByteSize;
2112 if (GPR_idx != Num_GPR_Regs)
2113 GPR_idx++;
2114 }
2115 ArgOffset += 16;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002116 GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
Dale Johannesen75092de2008-03-12 00:22:17 +00002117 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002118 ++VR_idx;
2119 } else {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00002120 if (!isVarArg && !isPPC64) {
2121 // Vectors go after all the nonvectors.
2122 CurArgOffset = VecArgOffset;
2123 VecArgOffset += 16;
2124 } else {
2125 // Vectors are aligned.
2126 ArgOffset = ((ArgOffset+15)/16)*16;
2127 CurArgOffset = ArgOffset;
2128 ArgOffset += 16;
Dale Johannesen404d9902008-03-12 00:49:20 +00002129 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002130 needsLoad = true;
2131 }
2132 break;
2133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002134
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002135 // We need to load the argument to a virtual register if we determined above
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002136 // that we ran out of physical registers of the appropriate type.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002137 if (needsLoad) {
Chris Lattner9f72d1a2008-02-13 07:35:30 +00002138 int FI = MFI->CreateFixedObject(ObjSize,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002139 CurArgOffset + (ArgSize - ObjSize),
Evan Chenged2ae132010-07-03 00:40:23 +00002140 isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00002141 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002142 ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002143 false, false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Dan Gohman98ca4f22009-08-05 01:29:28 +00002146 InVals.push_back(ArgVal);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002147 }
Dale Johannesen8419dd62008-03-07 20:27:40 +00002148
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002149 // Set the size that is at least reserved in caller of this function. Tail
2150 // call optimized function's reserved stack space needs to be aligned so that
2151 // taking the difference between two stack areas will result in an aligned
2152 // stack.
2153 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
2154 // Add the Altivec parameters at the end, if needed.
2155 if (nAltivecParamsAtEnd) {
2156 MinReservedArea = ((MinReservedArea+15)/16)*16;
2157 MinReservedArea += 16*nAltivecParamsAtEnd;
2158 }
2159 MinReservedArea =
2160 std::max(MinReservedArea,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002161 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
2162 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002163 getStackAlignment();
2164 unsigned AlignMask = TargetAlign-1;
2165 MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
2166 FI->setMinReservedArea(MinReservedArea);
2167
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002168 // If the function takes variable number of arguments, make a frame index for
2169 // the start of the first vararg value... for expansion of llvm.va_start.
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002170 if (isVarArg) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002171 int Depth = ArgOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00002172
Dan Gohman1e93df62010-04-17 14:41:14 +00002173 FuncInfo->setVarArgsFrameIndex(
2174 MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00002175 Depth, true));
Dan Gohman1e93df62010-04-17 14:41:14 +00002176 SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00002177
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002178 // If this function is vararg, store any remaining integer argument regs
2179 // to their spots on the stack so that they may be loaded by deferencing the
2180 // result of va_next.
Chris Lattneraf4ec0c2006-05-16 18:58:15 +00002181 for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002182 unsigned VReg;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002183
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002184 if (isPPC64)
Devang Patel68e6bee2011-02-21 23:21:26 +00002185 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002186 else
Devang Patel68e6bee2011-02-21 23:21:26 +00002187 VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
Chris Lattnerb1eb9872006-11-18 01:57:19 +00002188
Dan Gohman98ca4f22009-08-05 01:29:28 +00002189 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
Chris Lattner6229d0a2010-09-21 18:41:36 +00002190 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
2191 MachinePointerInfo(), false, false, 0);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002192 MemOps.push_back(Store);
2193 // Increment the address by four for the next argument to store
Dan Gohman475871a2008-07-27 21:46:04 +00002194 SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
Dale Johannesen39355f92009-02-04 02:34:38 +00002195 FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002196 }
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002197 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002198
Dale Johannesen8419dd62008-03-07 20:27:40 +00002199 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002200 Chain = DAG.getNode(ISD::TokenFactor, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002201 MVT::Other, &MemOps[0], MemOps.size());
Dale Johannesen8419dd62008-03-07 20:27:40 +00002202
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 return Chain;
Chris Lattner8ab5fe52006-05-16 18:18:50 +00002204}
2205
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002206/// CalculateParameterAndLinkageAreaSize - Get the size of the paramter plus
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002207/// linkage area for the Darwin ABI.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002208static unsigned
2209CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
2210 bool isPPC64,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002211 bool isVarArg,
2212 unsigned CC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002213 const SmallVectorImpl<ISD::OutputArg>
2214 &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002215 const SmallVectorImpl<SDValue> &OutVals,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002216 unsigned &nAltivecParamsAtEnd) {
2217 // Count how many bytes are to be pushed on the stack, including the linkage
2218 // area, and parameter passing area. We start with 24/48 bytes, which is
2219 // prereserved space for [SP][CR][LR][3 x unused].
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002220 unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002221 unsigned NumOps = Outs.size();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002222 unsigned PtrByteSize = isPPC64 ? 8 : 4;
2223
2224 // Add up all the space actually used.
2225 // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
2226 // they all go in registers, but we must reserve stack space for them for
2227 // possible use by the caller. In varargs or 64-bit calls, parameters are
2228 // assigned stack space in order, with padding so Altivec parameters are
2229 // 16-byte aligned.
2230 nAltivecParamsAtEnd = 0;
2231 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002232 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohmanc9403652010-07-07 15:54:55 +00002233 EVT ArgVT = Outs[i].VT;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002234 // Varargs Altivec parameters are padded to a 16 byte boundary.
Owen Anderson825b72b2009-08-11 20:47:22 +00002235 if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
2236 ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002237 if (!isVarArg && !isPPC64) {
2238 // Non-varargs Altivec parameters go after all the non-Altivec
2239 // parameters; handle those later so we know how much padding we need.
2240 nAltivecParamsAtEnd++;
2241 continue;
2242 }
2243 // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
2244 NumBytes = ((NumBytes+15)/16)*16;
2245 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002246 NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002247 }
2248
2249 // Allow for Altivec parameters at the end, if needed.
2250 if (nAltivecParamsAtEnd) {
2251 NumBytes = ((NumBytes+15)/16)*16;
2252 NumBytes += 16*nAltivecParamsAtEnd;
2253 }
2254
2255 // The prolog code of the callee may store up to 8 GPR argument registers to
2256 // the stack, allowing va_start to index over them in memory if its varargs.
2257 // Because we cannot tell if this is needed on the caller side, we have to
2258 // conservatively assume that it is needed. As such, make sure we have at
2259 // least enough stack space for the caller to store the 8 GPRs.
2260 NumBytes = std::max(NumBytes,
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002261 PPCFrameLowering::getMinCallFrameSize(isPPC64, true));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002262
2263 // Tail call needs the stack to be aligned.
Dan Gohman1797ed52010-02-08 20:27:50 +00002264 if (CC==CallingConv::Fast && GuaranteedTailCallOpt) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002265 unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()->
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002266 getStackAlignment();
2267 unsigned AlignMask = TargetAlign-1;
2268 NumBytes = (NumBytes + AlignMask) & ~AlignMask;
2269 }
2270
2271 return NumBytes;
2272}
2273
2274/// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002275/// adjusted to accommodate the arguments for the tailcall.
Dale Johannesenb60d5192009-11-24 01:09:07 +00002276static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002277 unsigned ParamSize) {
2278
Dale Johannesenb60d5192009-11-24 01:09:07 +00002279 if (!isTailCall) return 0;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002280
2281 PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
2282 unsigned CallerMinReservedArea = FI->getMinReservedArea();
2283 int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
2284 // Remember only if the new adjustement is bigger.
2285 if (SPDiff < FI->getTailCallSPDelta())
2286 FI->setTailCallSPDelta(SPDiff);
2287
2288 return SPDiff;
2289}
2290
Dan Gohman98ca4f22009-08-05 01:29:28 +00002291/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2292/// for tail call optimization. Targets which want to do tail call
2293/// optimization should implement this function.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002294bool
Dan Gohman98ca4f22009-08-05 01:29:28 +00002295PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002296 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002297 bool isVarArg,
2298 const SmallVectorImpl<ISD::InputArg> &Ins,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002299 SelectionDAG& DAG) const {
Dan Gohman1797ed52010-02-08 20:27:50 +00002300 if (!GuaranteedTailCallOpt)
Evan Cheng6c2e8a92010-01-29 23:05:56 +00002301 return false;
2302
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002303 // Variable argument functions are not supported.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002304 if (isVarArg)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002305 return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002306
Dan Gohman98ca4f22009-08-05 01:29:28 +00002307 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002308 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002309 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
2310 // Functions containing by val parameters are not supported.
2311 for (unsigned i = 0; i != Ins.size(); i++) {
2312 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2313 if (Flags.isByVal()) return false;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002314 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002315
2316 // Non PIC/GOT tail calls are supported.
2317 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
2318 return true;
2319
2320 // At the moment we can only do local tail calls (in same module, hidden
2321 // or protected) if we are generating PIC.
2322 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2323 return G->getGlobal()->hasHiddenVisibility()
2324 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002325 }
2326
2327 return false;
2328}
2329
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002330/// isCallCompatibleAddress - Return the immediate to use if the specified
2331/// 32-bit value is representable in the immediate field of a BxA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00002332static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002333 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
2334 if (!C) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00002335
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002336 int Addr = C->getZExtValue();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002337 if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero.
2338 (Addr << 6 >> 6) != Addr)
2339 return 0; // Top 6 bits have to be sext of immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00002340
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002341 return DAG.getConstant((int)C->getZExtValue() >> 2,
Gabor Greifba36cb52008-08-28 21:40:38 +00002342 DAG.getTargetLoweringInfo().getPointerTy()).getNode();
Chris Lattnerc703a8f2006-05-17 19:00:46 +00002343}
2344
Dan Gohman844731a2008-05-13 00:00:25 +00002345namespace {
2346
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002347struct TailCallArgumentInfo {
Dan Gohman475871a2008-07-27 21:46:04 +00002348 SDValue Arg;
2349 SDValue FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002350 int FrameIdx;
2351
2352 TailCallArgumentInfo() : FrameIdx(0) {}
2353};
2354
Dan Gohman844731a2008-05-13 00:00:25 +00002355}
2356
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002357/// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
2358static void
2359StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
Evan Chengff89dcb2009-10-18 18:16:27 +00002360 SDValue Chain,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002361 const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002362 SmallVector<SDValue, 8> &MemOpChains,
2363 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002364 for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002365 SDValue Arg = TailCallArgs[i].Arg;
2366 SDValue FIN = TailCallArgs[i].FrameIdxOp;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002367 int FI = TailCallArgs[i].FrameIdx;
2368 // Store relative to framepointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00002369 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002370 MachinePointerInfo::getFixedStack(FI),
2371 false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002372 }
2373}
2374
2375/// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
2376/// the appropriate stack slot for the tail call optimized function call.
Dan Gohman475871a2008-07-27 21:46:04 +00002377static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002378 MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00002379 SDValue Chain,
2380 SDValue OldRetAddr,
2381 SDValue OldFP,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002382 int SPDiff,
2383 bool isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002384 bool isDarwinABI,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002385 DebugLoc dl) {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002386 if (SPDiff) {
2387 // Calculate the new stack slot for the return address.
2388 int SlotSize = isPPC64 ? 8 : 4;
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002389 int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002390 isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002391 int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002392 NewRetAddrLoc, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00002393 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002394 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002395 Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002396 MachinePointerInfo::getFixedStack(NewRetAddr),
David Greene534502d12010-02-15 16:56:53 +00002397 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002398
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002399 // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
2400 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002401 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002402 int NewFPLoc =
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002403 SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI);
David Greene3f2bf852009-11-12 20:49:22 +00002404 int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc,
Evan Chenged2ae132010-07-03 00:40:23 +00002405 true);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002406 SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
2407 Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002408 MachinePointerInfo::getFixedStack(NewFPIdx),
David Greene534502d12010-02-15 16:56:53 +00002409 false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002410 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002411 }
2412 return Chain;
2413}
2414
2415/// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
2416/// the position of the argument.
2417static void
2418CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
Dan Gohman475871a2008-07-27 21:46:04 +00002419 SDValue Arg, int SPDiff, unsigned ArgOffset,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002420 SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
2421 int Offset = ArgOffset + SPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002422 uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002423 int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Owen Anderson825b72b2009-08-11 20:47:22 +00002424 EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00002425 SDValue FIN = DAG.getFrameIndex(FI, VT);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002426 TailCallArgumentInfo Info;
2427 Info.Arg = Arg;
2428 Info.FrameIdxOp = FIN;
2429 Info.FrameIdx = FI;
2430 TailCallArguments.push_back(Info);
2431}
2432
2433/// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
2434/// stack slot. Returns the chain as result and the loaded frame pointers in
2435/// LROpOut/FPOpout. Used when tail calling.
Dan Gohman475871a2008-07-27 21:46:04 +00002436SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002437 int SPDiff,
2438 SDValue Chain,
2439 SDValue &LROpOut,
2440 SDValue &FPOpOut,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002441 bool isDarwinABI,
Dan Gohmand858e902010-04-17 15:26:15 +00002442 DebugLoc dl) const {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002443 if (SPDiff) {
2444 // Load the LR and FP stack slot for later adjusting.
Owen Anderson825b72b2009-08-11 20:47:22 +00002445 EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002446 LROpOut = getReturnAddrFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002447 LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002448 false, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00002449 Chain = SDValue(LROpOut.getNode(), 1);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002450
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002451 // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
2452 // slot as the FP is never overwritten.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002453 if (isDarwinABI) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002454 FPOpOut = getFramePointerFrameIndex(DAG);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00002455 FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00002456 false, false, false, 0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002457 Chain = SDValue(FPOpOut.getNode(), 1);
2458 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002459 }
2460 return Chain;
2461}
2462
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002463/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
Scott Michelfdc40a02009-02-17 22:15:04 +00002464/// by "Src" to address "Dst" of size "Size". Alignment information is
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002465/// specified by the specific parameter attribute. The copy will be passed as
2466/// a byval function parameter.
2467/// Sometimes what we are copying is the end of a larger object, the part that
2468/// does not fit in registers.
Scott Michelfdc40a02009-02-17 22:15:04 +00002469static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00002470CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00002471 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00002472 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002473 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesen8ad9b432009-02-04 01:17:06 +00002474 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Chris Lattnere72f2022010-09-21 05:40:29 +00002475 false, false, MachinePointerInfo(0),
2476 MachinePointerInfo(0));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00002477}
Chris Lattner9f0bc652007-02-25 05:34:32 +00002478
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002479/// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
2480/// tail calls.
2481static void
Dan Gohman475871a2008-07-27 21:46:04 +00002482LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
2483 SDValue Arg, SDValue PtrOff, int SPDiff,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002484 unsigned ArgOffset, bool isPPC64, bool isTailCall,
Dan Gohman475871a2008-07-27 21:46:04 +00002485 bool isVector, SmallVector<SDValue, 8> &MemOpChains,
Chris Lattner6229d0a2010-09-21 18:41:36 +00002486 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments,
Dale Johannesen33c960f2009-02-04 20:06:27 +00002487 DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00002488 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002489 if (!isTailCall) {
2490 if (isVector) {
Dan Gohman475871a2008-07-27 21:46:04 +00002491 SDValue StackPtr;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002492 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00002493 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002494 else
Owen Anderson825b72b2009-08-11 20:47:22 +00002495 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +00002496 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002497 DAG.getConstant(ArgOffset, PtrVT));
2498 }
Chris Lattner6229d0a2010-09-21 18:41:36 +00002499 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
2500 MachinePointerInfo(), false, false, 0));
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002501 // Calculate and remember argument location.
2502 } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
2503 TailCallArguments);
2504}
2505
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002506static
2507void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
2508 DebugLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
2509 SDValue LROp, SDValue FPOp, bool isDarwinABI,
2510 SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) {
2511 MachineFunction &MF = DAG.getMachineFunction();
2512
2513 // Emit a sequence of copyto/copyfrom virtual registers for arguments that
2514 // might overwrite each other in case of tail call optimization.
2515 SmallVector<SDValue, 8> MemOpChains2;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00002516 // Do not flag preceding copytoreg stuff together with the following stuff.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002517 InFlag = SDValue();
2518 StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
2519 MemOpChains2, dl);
2520 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002521 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002522 &MemOpChains2[0], MemOpChains2.size());
2523
2524 // Store the return address to the appropriate stack slot.
2525 Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
2526 isPPC64, isDarwinABI, dl);
2527
2528 // Emit callseq_end just before tailcall node.
2529 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2530 DAG.getIntPtrConstant(0, true), InFlag);
2531 InFlag = Chain.getValue(1);
2532}
2533
2534static
2535unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
2536 SDValue &Chain, DebugLoc dl, int SPDiff, bool isTailCall,
2537 SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
Owen Andersone50ed302009-08-10 22:56:29 +00002538 SmallVector<SDValue, 8> &Ops, std::vector<EVT> &NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00002539 const PPCSubtarget &PPCSubTarget) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002540
Chris Lattnerb9082582010-11-14 23:42:06 +00002541 bool isPPC64 = PPCSubTarget.isPPC64();
2542 bool isSVR4ABI = PPCSubTarget.isSVR4ABI();
2543
Owen Andersone50ed302009-08-10 22:56:29 +00002544 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00002545 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002546 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002547
2548 unsigned CallOpc = isSVR4ABI ? PPCISD::CALL_SVR4 : PPCISD::CALL_Darwin;
2549
Torok Edwin0e3a1a82010-08-04 20:47:44 +00002550 bool needIndirectCall = true;
2551 if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002552 // If this is an absolute destination address, use the munged value.
2553 Callee = SDValue(Dest, 0);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00002554 needIndirectCall = false;
2555 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002556
Chris Lattnerb9082582010-11-14 23:42:06 +00002557 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2558 // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201
2559 // Use indirect calls for ALL functions calls in JIT mode, since the
2560 // far-call stubs may be outside relocation limits for a BL instruction.
2561 if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) {
2562 unsigned OpFlags = 0;
2563 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00002564 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002565 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) &&
Chris Lattnerb9082582010-11-14 23:42:06 +00002566 (G->getGlobal()->isDeclaration() ||
2567 G->getGlobal()->isWeakForLinker())) {
2568 // PC-relative references to external symbols should go through $stub,
2569 // unless we're building with the leopard linker or later, which
2570 // automatically synthesizes these stubs.
2571 OpFlags = PPCII::MO_DARWIN_STUB;
2572 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002573
Chris Lattnerb9082582010-11-14 23:42:06 +00002574 // If the callee is a GlobalAddress/ExternalSymbol node (quite common,
2575 // every direct call is) turn it into a TargetGlobalAddress /
2576 // TargetExternalSymbol node so that legalize doesn't hack it.
Torok Edwin0e3a1a82010-08-04 20:47:44 +00002577 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
Chris Lattnerb9082582010-11-14 23:42:06 +00002578 Callee.getValueType(),
2579 0, OpFlags);
Torok Edwin0e3a1a82010-08-04 20:47:44 +00002580 needIndirectCall = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002581 }
Torok Edwin0e3a1a82010-08-04 20:47:44 +00002582 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002583
Torok Edwin0e3a1a82010-08-04 20:47:44 +00002584 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattnerb9082582010-11-14 23:42:06 +00002585 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002586
Chris Lattnerb9082582010-11-14 23:42:06 +00002587 if (DAG.getTarget().getRelocationModel() != Reloc::Static &&
Roman Divackyd5601cc2011-07-24 08:22:56 +00002588 (PPCSubTarget.getTargetTriple().isMacOSX() &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002589 PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerb9082582010-11-14 23:42:06 +00002590 // PC-relative references to external symbols should go through $stub,
2591 // unless we're building with the leopard linker or later, which
2592 // automatically synthesizes these stubs.
2593 OpFlags = PPCII::MO_DARWIN_STUB;
2594 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002595
Chris Lattnerb9082582010-11-14 23:42:06 +00002596 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(),
2597 OpFlags);
2598 needIndirectCall = false;
Torok Edwin0e3a1a82010-08-04 20:47:44 +00002599 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002600
Torok Edwin0e3a1a82010-08-04 20:47:44 +00002601 if (needIndirectCall) {
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002602 // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair
2603 // to do the call, we can't use PPCISD::CALL.
2604 SDValue MTCTROps[] = {Chain, Callee, InFlag};
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00002605
2606 if (isSVR4ABI && isPPC64) {
2607 // Function pointers in the 64-bit SVR4 ABI do not point to the function
2608 // entry point, but to the function descriptor (the function entry point
2609 // address is part of the function descriptor though).
2610 // The function descriptor is a three doubleword structure with the
2611 // following fields: function entry point, TOC base address and
2612 // environment pointer.
2613 // Thus for a call through a function pointer, the following actions need
2614 // to be performed:
2615 // 1. Save the TOC of the caller in the TOC save area of its stack
2616 // frame (this is done in LowerCall_Darwin()).
2617 // 2. Load the address of the function entry point from the function
2618 // descriptor.
2619 // 3. Load the TOC of the callee from the function descriptor into r2.
2620 // 4. Load the environment pointer from the function descriptor into
2621 // r11.
2622 // 5. Branch to the function entry point address.
2623 // 6. On return of the callee, the TOC of the caller needs to be
2624 // restored (this is done in FinishCall()).
2625 //
2626 // All those operations are flagged together to ensure that no other
2627 // operations can be scheduled in between. E.g. without flagging the
2628 // operations together, a TOC access in the caller could be scheduled
2629 // between the load of the callee TOC and the branch to the callee, which
2630 // results in the TOC access going through the TOC of the callee instead
2631 // of going through the TOC of the caller, which leads to incorrect code.
2632
2633 // Load the address of the function entry point from the function
2634 // descriptor.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002635 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00002636 SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps,
2637 InFlag.getNode() ? 3 : 2);
2638 Chain = LoadFuncPtr.getValue(1);
2639 InFlag = LoadFuncPtr.getValue(2);
2640
2641 // Load environment pointer into r11.
2642 // Offset of the environment pointer within the function descriptor.
2643 SDValue PtrOff = DAG.getIntPtrConstant(16);
2644
2645 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff);
2646 SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr,
2647 InFlag);
2648 Chain = LoadEnvPtr.getValue(1);
2649 InFlag = LoadEnvPtr.getValue(2);
2650
2651 SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr,
2652 InFlag);
2653 Chain = EnvVal.getValue(0);
2654 InFlag = EnvVal.getValue(1);
2655
2656 // Load TOC of the callee into r2. We are using a target-specific load
2657 // with r2 hard coded, because the result of a target-independent load
2658 // would never go directly into r2, since r2 is a reserved register (which
2659 // prevents the register allocator from allocating it), resulting in an
2660 // additional register being allocated and an unnecessary move instruction
2661 // being generated.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002662 VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00002663 SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain,
2664 Callee, InFlag);
2665 Chain = LoadTOCPtr.getValue(0);
2666 InFlag = LoadTOCPtr.getValue(1);
2667
2668 MTCTROps[0] = Chain;
2669 MTCTROps[1] = LoadFuncPtr;
2670 MTCTROps[2] = InFlag;
2671 }
2672
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002673 Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
2674 2 + (InFlag.getNode() != 0));
2675 InFlag = Chain.getValue(1);
2676
2677 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00002678 NodeTys.push_back(MVT::Other);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002679 NodeTys.push_back(MVT::Glue);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002680 Ops.push_back(Chain);
2681 CallOpc = isSVR4ABI ? PPCISD::BCTRL_SVR4 : PPCISD::BCTRL_Darwin;
2682 Callee.setNode(0);
2683 // Add CTR register as callee so a bctr can be emitted later.
2684 if (isTailCall)
Roman Divacky0c9b5592011-06-03 15:47:49 +00002685 Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002686 }
2687
2688 // If this is a direct call, pass the chain and the callee.
2689 if (Callee.getNode()) {
2690 Ops.push_back(Chain);
2691 Ops.push_back(Callee);
2692 }
2693 // If this is a tail call add stack pointer delta.
2694 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002695 Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002696
2697 // Add argument registers to the end of the list so that they are known live
2698 // into the call.
2699 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2700 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2701 RegsToPass[i].second.getValueType()));
2702
2703 return CallOpc;
2704}
2705
Dan Gohman98ca4f22009-08-05 01:29:28 +00002706SDValue
2707PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002708 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002709 const SmallVectorImpl<ISD::InputArg> &Ins,
2710 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002711 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002712
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002713 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002714 CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2715 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002716 CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002717
2718 // Copy all of the result registers out of their specified physreg.
2719 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
2720 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00002721 EVT VT = VA.getValVT();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002722 assert(VA.isRegLoc() && "Can only return in registers!");
2723 Chain = DAG.getCopyFromReg(Chain, dl,
2724 VA.getLocReg(), VT, InFlag).getValue(1);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002725 InVals.push_back(Chain.getValue(0));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002726 InFlag = Chain.getValue(2);
2727 }
2728
Dan Gohman98ca4f22009-08-05 01:29:28 +00002729 return Chain;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002730}
2731
Dan Gohman98ca4f22009-08-05 01:29:28 +00002732SDValue
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002733PPCTargetLowering::FinishCall(CallingConv::ID CallConv, DebugLoc dl,
2734 bool isTailCall, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002735 SelectionDAG &DAG,
2736 SmallVector<std::pair<unsigned, SDValue>, 8>
2737 &RegsToPass,
2738 SDValue InFlag, SDValue Chain,
2739 SDValue &Callee,
2740 int SPDiff, unsigned NumBytes,
2741 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohmand858e902010-04-17 15:26:15 +00002742 SmallVectorImpl<SDValue> &InVals) const {
Owen Andersone50ed302009-08-10 22:56:29 +00002743 std::vector<EVT> NodeTys;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002744 SmallVector<SDValue, 8> Ops;
2745 unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
2746 isTailCall, RegsToPass, Ops, NodeTys,
Chris Lattnerb9082582010-11-14 23:42:06 +00002747 PPCSubTarget);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002748
2749 // When performing tail call optimization the callee pops its arguments off
2750 // the stack. Account for this here so these bytes can be pushed back on in
2751 // PPCRegisterInfo::eliminateCallFramePseudoInstr.
2752 int BytesCalleePops =
Dan Gohman1797ed52010-02-08 20:27:50 +00002753 (CallConv==CallingConv::Fast && GuaranteedTailCallOpt) ? NumBytes : 0;
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002754
2755 if (InFlag.getNode())
2756 Ops.push_back(InFlag);
2757
2758 // Emit tail call.
2759 if (isTailCall) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002760 // If this is the first return lowered for this function, add the regs
2761 // to the liveout set for the function.
2762 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
2763 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002764 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2765 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002766 CCInfo.AnalyzeCallResult(Ins, RetCC_PPC);
2767 for (unsigned i = 0; i != RVLocs.size(); ++i)
2768 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2769 }
2770
2771 assert(((Callee.getOpcode() == ISD::Register &&
2772 cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
2773 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2774 Callee.getOpcode() == ISD::TargetGlobalAddress ||
2775 isa<ConstantSDNode>(Callee)) &&
2776 "Expecting an global address, external symbol, absolute value or register");
2777
Owen Anderson825b72b2009-08-11 20:47:22 +00002778 return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002779 }
2780
2781 Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
2782 InFlag = Chain.getValue(1);
2783
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002784 // Add a NOP immediately after the branch instruction when using the 64-bit
2785 // SVR4 ABI. At link time, if caller and callee are in a different module and
2786 // thus have a different TOC, the call will be replaced with a call to a stub
2787 // function which saves the current TOC, loads the TOC of the callee and
2788 // branches to the callee. The NOP will be replaced with a load instruction
2789 // which restores the TOC of the caller from the TOC save slot of the current
2790 // stack frame. If caller and callee belong to the same module (and have the
2791 // same TOC), the NOP will remain unchanged.
2792 if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002793 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00002794 if (CallOpc == PPCISD::BCTRL_SVR4) {
2795 // This is a call through a function pointer.
2796 // Restore the caller TOC from the save area into R2.
2797 // See PrepareCall() for more information about calls through function
2798 // pointers in the 64-bit SVR4 ABI.
2799 // We are using a target-specific load with r2 hard coded, because the
2800 // result of a target-independent load would never go directly into r2,
2801 // since r2 is a reserved register (which prevents the register allocator
2802 // from allocating it), resulting in an additional register being
2803 // allocated and an unnecessary move instruction being generated.
2804 Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag);
2805 InFlag = Chain.getValue(1);
2806 } else {
2807 // Otherwise insert NOP.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002808 InFlag = DAG.getNode(PPCISD::NOP, dl, MVT::Glue, InFlag);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00002809 }
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002810 }
2811
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002812 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2813 DAG.getIntPtrConstant(BytesCalleePops, true),
2814 InFlag);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002815 if (!Ins.empty())
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002816 InFlag = Chain.getValue(1);
2817
Dan Gohman98ca4f22009-08-05 01:29:28 +00002818 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2819 Ins, dl, DAG, InVals);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00002820}
2821
Dan Gohman98ca4f22009-08-05 01:29:28 +00002822SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00002823PPCTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002824 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00002825 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002826 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002827 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002828 const SmallVectorImpl<ISD::InputArg> &Ins,
2829 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002830 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002831 if (isTailCall)
2832 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
2833 Ins, DAG);
2834
Chris Lattnerb9082582010-11-14 23:42:06 +00002835 if (PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64())
Dan Gohman98ca4f22009-08-05 01:29:28 +00002836 return LowerCall_SVR4(Chain, Callee, CallConv, isVarArg,
Dan Gohmanc9403652010-07-07 15:54:55 +00002837 isTailCall, Outs, OutVals, Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002838 dl, DAG, InVals);
Chris Lattnerb9082582010-11-14 23:42:06 +00002839
2840 return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
2841 isTailCall, Outs, OutVals, Ins,
2842 dl, DAG, InVals);
Dan Gohman98ca4f22009-08-05 01:29:28 +00002843}
2844
2845SDValue
2846PPCTargetLowering::LowerCall_SVR4(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002847 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002848 bool isTailCall,
2849 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002850 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002851 const SmallVectorImpl<ISD::InputArg> &Ins,
2852 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00002853 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00002854 // See PPCTargetLowering::LowerFormalArguments_SVR4() for a description
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00002855 // of the 32-bit SVR4 ABI stack frame layout.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002856
Dan Gohman98ca4f22009-08-05 01:29:28 +00002857 assert((CallConv == CallingConv::C ||
2858 CallConv == CallingConv::Fast) && "Unknown calling convention!");
Tilmann Schellerffd02002009-07-03 06:45:56 +00002859
Tilmann Schellerffd02002009-07-03 06:45:56 +00002860 unsigned PtrByteSize = 4;
2861
2862 MachineFunction &MF = DAG.getMachineFunction();
2863
2864 // Mark this function as potentially containing a function that contains a
2865 // tail call. As a consequence the frame pointer will be used for dynamicalloc
2866 // and restoring the callers stack pointer in this functions epilog. This is
2867 // done because by tail calling the called function might overwrite the value
2868 // in this function's (MF) stack pointer stack slot 0(SP).
Dan Gohman1797ed52010-02-08 20:27:50 +00002869 if (GuaranteedTailCallOpt && CallConv==CallingConv::Fast)
Tilmann Schellerffd02002009-07-03 06:45:56 +00002870 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002871
Tilmann Schellerffd02002009-07-03 06:45:56 +00002872 // Count how many bytes are to be pushed on the stack, including the linkage
2873 // area, parameter list area and the part of the local variable space which
2874 // contains copies of aggregates which are passed by value.
2875
2876 // Assign locations to all of the outgoing arguments.
2877 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002878 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2879 getTargetMachine(), ArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002880
2881 // Reserve space for the linkage area on the stack.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002882 CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002883
2884 if (isVarArg) {
2885 // Handle fixed and variable vector arguments differently.
2886 // Fixed vector arguments go into registers as long as registers are
2887 // available. Variable vector arguments always go into memory.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002888 unsigned NumArgs = Outs.size();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002889
Tilmann Schellerffd02002009-07-03 06:45:56 +00002890 for (unsigned i = 0; i != NumArgs; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002891 MVT ArgVT = Outs[i].VT;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002892 ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002893 bool Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002894
Dan Gohman98ca4f22009-08-05 01:29:28 +00002895 if (Outs[i].IsFixed) {
Tilmann Schellerffd02002009-07-03 06:45:56 +00002896 Result = CC_PPC_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
2897 CCInfo);
2898 } else {
2899 Result = CC_PPC_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
2900 ArgFlags, CCInfo);
2901 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002902
Tilmann Schellerffd02002009-07-03 06:45:56 +00002903 if (Result) {
Torok Edwindac237e2009-07-08 20:53:28 +00002904#ifndef NDEBUG
Chris Lattner45cfe542009-08-23 06:03:38 +00002905 errs() << "Call operand #" << i << " has unhandled type "
Duncan Sands1440e8b2010-11-03 11:35:31 +00002906 << EVT(ArgVT).getEVTString() << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +00002907#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00002908 llvm_unreachable(0);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002909 }
2910 }
2911 } else {
2912 // All arguments are treated the same.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002913 CCInfo.AnalyzeCallOperands(Outs, CC_PPC_SVR4);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002914 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002915
Tilmann Schellerffd02002009-07-03 06:45:56 +00002916 // Assign locations to all of the outgoing aggregate by value arguments.
2917 SmallVector<CCValAssign, 16> ByValArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002918 CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2919 getTargetMachine(), ByValArgLocs, *DAG.getContext());
Tilmann Schellerffd02002009-07-03 06:45:56 +00002920
2921 // Reserve stack space for the allocations in CCInfo.
2922 CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
2923
Dan Gohman98ca4f22009-08-05 01:29:28 +00002924 CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC_SVR4_ByVal);
Tilmann Schellerffd02002009-07-03 06:45:56 +00002925
2926 // Size of the linkage area, parameter list area and the part of the local
2927 // space variable where copies of aggregates which are passed by value are
2928 // stored.
2929 unsigned NumBytes = CCByValInfo.getNextStackOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002930
Tilmann Schellerffd02002009-07-03 06:45:56 +00002931 // Calculate by how many bytes the stack has to be adjusted in case of tail
2932 // call optimization.
2933 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
2934
2935 // Adjust the stack pointer for the new arguments...
2936 // These operations are automatically eliminated by the prolog/epilog pass
2937 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
2938 SDValue CallSeqStart = Chain;
2939
2940 // Load the return address and frame pointer so it can be moved somewhere else
2941 // later.
2942 SDValue LROp, FPOp;
2943 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
2944 dl);
2945
2946 // Set up a copy of the stack pointer for use loading and storing any
2947 // arguments that may not fit in the registers available for argument
2948 // passing.
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002950
Tilmann Schellerffd02002009-07-03 06:45:56 +00002951 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
2952 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
2953 SmallVector<SDValue, 8> MemOpChains;
2954
Roman Divacky0aaa9192011-08-30 17:04:16 +00002955 bool seenFloatArg = false;
Tilmann Schellerffd02002009-07-03 06:45:56 +00002956 // Walk the register/memloc assignments, inserting copies/loads.
2957 for (unsigned i = 0, j = 0, e = ArgLocs.size();
2958 i != e;
2959 ++i) {
2960 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002961 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002962 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002963
Tilmann Schellerffd02002009-07-03 06:45:56 +00002964 if (Flags.isByVal()) {
2965 // Argument is an aggregate which is passed by value, thus we need to
2966 // create a copy of it in the local variable space of the current stack
2967 // frame (which is the stack frame of the caller) and pass the address of
2968 // this copy to the callee.
2969 assert((j < ByValArgLocs.size()) && "Index out of bounds!");
2970 CCValAssign &ByValVA = ByValArgLocs[j++];
2971 assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002972
Tilmann Schellerffd02002009-07-03 06:45:56 +00002973 // Memory reserved in the local variable space of the callers stack frame.
2974 unsigned LocMemOffset = ByValVA.getLocMemOffset();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002975
Tilmann Schellerffd02002009-07-03 06:45:56 +00002976 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
2977 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002978
Tilmann Schellerffd02002009-07-03 06:45:56 +00002979 // Create a copy of the argument in the local area of the current
2980 // stack frame.
2981 SDValue MemcpyCall =
2982 CreateCopyOfByValArgument(Arg, PtrOff,
2983 CallSeqStart.getNode()->getOperand(0),
2984 Flags, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002985
Tilmann Schellerffd02002009-07-03 06:45:56 +00002986 // This must go outside the CALLSEQ_START..END.
2987 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
2988 CallSeqStart.getNode()->getOperand(1));
2989 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
2990 NewCallSeqStart.getNode());
2991 Chain = CallSeqStart = NewCallSeqStart;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002992
Tilmann Schellerffd02002009-07-03 06:45:56 +00002993 // Pass the address of the aggregate copy on the stack either in a
2994 // physical register or in the parameter list area of the current stack
2995 // frame to the callee.
2996 Arg = PtrOff;
2997 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002998
Tilmann Schellerffd02002009-07-03 06:45:56 +00002999 if (VA.isRegLoc()) {
Roman Divacky0aaa9192011-08-30 17:04:16 +00003000 seenFloatArg |= VA.getLocVT().isFloatingPoint();
Tilmann Schellerffd02002009-07-03 06:45:56 +00003001 // Put argument in a physical register.
3002 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3003 } else {
3004 // Put argument in the parameter list area of the current stack frame.
3005 assert(VA.isMemLoc());
3006 unsigned LocMemOffset = VA.getLocMemOffset();
3007
3008 if (!isTailCall) {
3009 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
3010 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
3011
3012 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003013 MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00003014 false, false, 0));
Tilmann Schellerffd02002009-07-03 06:45:56 +00003015 } else {
3016 // Calculate and remember argument location.
3017 CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
3018 TailCallArguments);
3019 }
3020 }
3021 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003022
Tilmann Schellerffd02002009-07-03 06:45:56 +00003023 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003024 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Tilmann Schellerffd02002009-07-03 06:45:56 +00003025 &MemOpChains[0], MemOpChains.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003026
Roman Divacky0aaa9192011-08-30 17:04:16 +00003027 // Set CR6 to true if this is a vararg call with floating args passed in
3028 // registers.
Eli Friedman4e3adfd2011-06-14 22:16:20 +00003029 if (isVarArg) {
Roman Divacky0aaa9192011-08-30 17:04:16 +00003030 SDValue SetCR(DAG.getMachineNode(seenFloatArg ? PPC::CRSET : PPC::CRUNSET,
3031 dl, MVT::i32), 0);
Eli Friedman4e3adfd2011-06-14 22:16:20 +00003032 RegsToPass.push_back(std::make_pair(unsigned(PPC::CR1EQ), SetCR));
3033 }
3034
Tilmann Schellerffd02002009-07-03 06:45:56 +00003035 // Build a sequence of copy-to-reg nodes chained together with token chain
3036 // and flag operands which copy the outgoing args into the appropriate regs.
3037 SDValue InFlag;
3038 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3039 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3040 RegsToPass[i].second, InFlag);
3041 InFlag = Chain.getValue(1);
3042 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003043
Chris Lattnerb9082582010-11-14 23:42:06 +00003044 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003045 PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
3046 false, TailCallArguments);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003047
Dan Gohman98ca4f22009-08-05 01:29:28 +00003048 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3049 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3050 Ins, InVals);
Tilmann Schellerffd02002009-07-03 06:45:56 +00003051}
3052
Dan Gohman98ca4f22009-08-05 01:29:28 +00003053SDValue
3054PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003055 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003056 bool isTailCall,
3057 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003058 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003059 const SmallVectorImpl<ISD::InputArg> &Ins,
3060 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003061 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003062
3063 unsigned NumOps = Outs.size();
Scott Michelfdc40a02009-02-17 22:15:04 +00003064
Owen Andersone50ed302009-08-10 22:56:29 +00003065 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00003066 bool isPPC64 = PtrVT == MVT::i64;
Chris Lattnerc91a4752006-06-26 22:48:35 +00003067 unsigned PtrByteSize = isPPC64 ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00003068
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003069 MachineFunction &MF = DAG.getMachineFunction();
3070
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003071 // Mark this function as potentially containing a function that contains a
3072 // tail call. As a consequence the frame pointer will be used for dynamicalloc
3073 // and restoring the callers stack pointer in this functions epilog. This is
3074 // done because by tail calling the called function might overwrite the value
3075 // in this function's (MF) stack pointer stack slot 0(SP).
Dan Gohman1797ed52010-02-08 20:27:50 +00003076 if (GuaranteedTailCallOpt && CallConv==CallingConv::Fast)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003077 MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
3078
3079 unsigned nAltivecParamsAtEnd = 0;
3080
Chris Lattnerabde4602006-05-16 22:56:08 +00003081 // Count how many bytes are to be pushed on the stack, including the linkage
Chris Lattnerc91a4752006-06-26 22:48:35 +00003082 // area, and parameter passing area. We start with 24/48 bytes, which is
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003083 // prereserved space for [SP][CR][LR][3 x unused].
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003084 unsigned NumBytes =
Dan Gohman98ca4f22009-08-05 01:29:28 +00003085 CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
Dan Gohmanc9403652010-07-07 15:54:55 +00003086 Outs, OutVals,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003087 nAltivecParamsAtEnd);
Dale Johannesen75092de2008-03-12 00:22:17 +00003088
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003089 // Calculate by how many bytes the stack has to be adjusted in case of tail
3090 // call optimization.
3091 int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
Scott Michelfdc40a02009-02-17 22:15:04 +00003092
Dan Gohman98ca4f22009-08-05 01:29:28 +00003093 // To protect arguments on the stack from being clobbered in a tail call,
3094 // force all the loads to happen before doing any other lowering.
3095 if (isTailCall)
3096 Chain = DAG.getStackArgumentTokenFactor(Chain);
3097
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003098 // Adjust the stack pointer for the new arguments...
3099 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattnere563bbc2008-10-11 22:08:30 +00003100 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohman475871a2008-07-27 21:46:04 +00003101 SDValue CallSeqStart = Chain;
Scott Michelfdc40a02009-02-17 22:15:04 +00003102
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003103 // Load the return address and frame pointer so it can be move somewhere else
3104 // later.
Dan Gohman475871a2008-07-27 21:46:04 +00003105 SDValue LROp, FPOp;
Tilmann Schellerffd02002009-07-03 06:45:56 +00003106 Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
3107 dl);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003108
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003109 // Set up a copy of the stack pointer for use loading and storing any
3110 // arguments that may not fit in the registers available for argument
3111 // passing.
Dan Gohman475871a2008-07-27 21:46:04 +00003112 SDValue StackPtr;
Chris Lattnerc91a4752006-06-26 22:48:35 +00003113 if (isPPC64)
Owen Anderson825b72b2009-08-11 20:47:22 +00003114 StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
Chris Lattnerc91a4752006-06-26 22:48:35 +00003115 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00003117
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003118 // Figure out which arguments are going to go in registers, and which in
3119 // memory. Also, if this is a vararg function, floating point operations
3120 // must be stored to our stack, and loaded into integer regs as well, if
3121 // any integer regs are available for argument passing.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003122 unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003123 unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003124
Chris Lattnerc91a4752006-06-26 22:48:35 +00003125 static const unsigned GPR_32[] = { // 32-bit registers.
Chris Lattner9a2a4972006-05-17 06:01:33 +00003126 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
3127 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
3128 };
Chris Lattnerc91a4752006-06-26 22:48:35 +00003129 static const unsigned GPR_64[] = { // 64-bit registers.
3130 PPC::X3, PPC::X4, PPC::X5, PPC::X6,
3131 PPC::X7, PPC::X8, PPC::X9, PPC::X10,
3132 };
Tilmann Scheller6b16eff2009-08-15 11:54:46 +00003133 static const unsigned *FPR = GetFPR();
Scott Michelfdc40a02009-02-17 22:15:04 +00003134
Chris Lattner9a2a4972006-05-17 06:01:33 +00003135 static const unsigned VR[] = {
3136 PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
3137 PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
3138 };
Owen Anderson718cb662007-09-07 04:06:50 +00003139 const unsigned NumGPRs = array_lengthof(GPR_32);
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003140 const unsigned NumFPRs = 13;
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00003141 const unsigned NumVRs = array_lengthof(VR);
Scott Michelfdc40a02009-02-17 22:15:04 +00003142
Chris Lattnerc91a4752006-06-26 22:48:35 +00003143 const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
3144
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003145 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003146 SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
3147
Dan Gohman475871a2008-07-27 21:46:04 +00003148 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng4360bdc2006-05-25 00:57:32 +00003149 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00003150 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00003151 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00003152
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003153 // PtrOff will be used to store the current argument to the stack if a
3154 // register cannot be found for it.
Dan Gohman475871a2008-07-27 21:46:04 +00003155 SDValue PtrOff;
Scott Michelfdc40a02009-02-17 22:15:04 +00003156
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003157 PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
Nicolas Geoffrayb2ec1cc2007-03-13 15:02:46 +00003158
Dale Johannesen39355f92009-02-04 02:34:38 +00003159 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattnerc91a4752006-06-26 22:48:35 +00003160
3161 // On PPC64, promote integers to 64-bit values.
Owen Anderson825b72b2009-08-11 20:47:22 +00003162 if (isPPC64 && Arg.getValueType() == MVT::i32) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00003163 // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
3164 unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Owen Anderson825b72b2009-08-11 20:47:22 +00003165 Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
Chris Lattnerc91a4752006-06-26 22:48:35 +00003166 }
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003167
Dale Johannesen8419dd62008-03-07 20:27:40 +00003168 // FIXME memcpy is used way more than necessary. Correctness first.
Duncan Sands276dcbd2008-03-21 09:14:45 +00003169 if (Flags.isByVal()) {
3170 unsigned Size = Flags.getByValSize();
Dale Johannesen8419dd62008-03-07 20:27:40 +00003171 if (Size==1 || Size==2) {
3172 // Very small objects are passed right-justified.
3173 // Everything else is passed left-justified.
Owen Anderson825b72b2009-08-11 20:47:22 +00003174 EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
Dale Johannesen8419dd62008-03-07 20:27:40 +00003175 if (GPR_idx != NumGPRs) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00003176 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00003177 MachinePointerInfo(), VT,
3178 false, false, 0);
Dale Johannesen8419dd62008-03-07 20:27:40 +00003179 MemOpChains.push_back(Load.getValue(1));
3180 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003181
3182 ArgOffset += PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00003183 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00003184 SDValue Const = DAG.getConstant(4 - Size, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00003185 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
Dan Gohman475871a2008-07-27 21:46:04 +00003186 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr,
Scott Michelfdc40a02009-02-17 22:15:04 +00003187 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00003188 Flags, DAG, dl);
Dale Johannesen8419dd62008-03-07 20:27:40 +00003189 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00003190 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00003191 CallSeqStart.getNode()->getOperand(1));
Gabor Greif93c53e52008-08-31 15:37:04 +00003192 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
3193 NewCallSeqStart.getNode());
Dale Johannesen8419dd62008-03-07 20:27:40 +00003194 Chain = CallSeqStart = NewCallSeqStart;
3195 ArgOffset += PtrByteSize;
3196 }
3197 continue;
3198 }
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00003199 // Copy entire object into memory. There are cases where gcc-generated
3200 // code assumes it is there, even if it could be put entirely into
3201 // registers. (This is not what the doc says.)
Dan Gohman475871a2008-07-27 21:46:04 +00003202 SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00003203 CallSeqStart.getNode()->getOperand(0),
Tilmann Scheller667ee3c2009-07-03 06:43:35 +00003204 Flags, DAG, dl);
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00003205 // This must go outside the CALLSEQ_START..END.
Dan Gohman475871a2008-07-27 21:46:04 +00003206 SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
Gabor Greifba36cb52008-08-28 21:40:38 +00003207 CallSeqStart.getNode()->getOperand(1));
3208 DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), NewCallSeqStart.getNode());
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00003209 Chain = CallSeqStart = NewCallSeqStart;
3210 // And copy the pieces of it that fit into registers.
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003211 for (unsigned j=0; j<Size; j+=PtrByteSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003212 SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00003213 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003214 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003215 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
3216 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003217 false, false, false, 0);
Dale Johannesen1f797a32008-03-05 23:31:27 +00003218 MemOpChains.push_back(Load.getValue(1));
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003219 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003220 ArgOffset += PtrByteSize;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003221 } else {
Dale Johannesenfdd3ade2008-03-17 02:13:43 +00003222 ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
Dale Johannesen8419dd62008-03-07 20:27:40 +00003223 break;
Dale Johannesen5b3b6952008-03-04 23:17:14 +00003224 }
3225 }
3226 continue;
3227 }
3228
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 switch (Arg.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003230 default: llvm_unreachable("Unexpected ValueType for argument!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 case MVT::i32:
3232 case MVT::i64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00003233 if (GPR_idx != NumGPRs) {
3234 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003235 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003236 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3237 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003238 TailCallArguments, dl);
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003239 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003240 ArgOffset += PtrByteSize;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003241 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00003242 case MVT::f32:
3243 case MVT::f64:
Chris Lattner9a2a4972006-05-17 06:01:33 +00003244 if (FPR_idx != NumFPRs) {
3245 RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
3246
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003247 if (isVarArg) {
Chris Lattner6229d0a2010-09-21 18:41:36 +00003248 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
3249 MachinePointerInfo(), false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003250 MemOpChains.push_back(Store);
3251
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003252 // Float varargs are always shadowed in available integer registers
Chris Lattner9a2a4972006-05-17 06:01:33 +00003253 if (GPR_idx != NumGPRs) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003254 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
Pete Cooperd752e0f2011-11-08 18:42:53 +00003255 MachinePointerInfo(), false, false,
3256 false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003257 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003258 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003259 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003260 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
Dan Gohman475871a2008-07-27 21:46:04 +00003261 SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
Dale Johannesen39355f92009-02-04 02:34:38 +00003262 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003263 SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff,
3264 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003265 false, false, false, 0);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003266 MemOpChains.push_back(Load.getValue(1));
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003267 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
Chris Lattnerabde4602006-05-16 22:56:08 +00003268 }
3269 } else {
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003270 // If we have any FPRs remaining, we may also have GPRs remaining.
3271 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
3272 // GPRs.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003273 if (GPR_idx != NumGPRs)
3274 ++GPR_idx;
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003276 !isPPC64) // PPC64 has 64-bit GPR's obviously :)
3277 ++GPR_idx;
Chris Lattnerabde4602006-05-16 22:56:08 +00003278 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003279 } else {
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003280 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3281 isPPC64, isTailCall, false, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003282 TailCallArguments, dl);
Chris Lattnerabde4602006-05-16 22:56:08 +00003283 }
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003284 if (isPPC64)
3285 ArgOffset += 8;
3286 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003287 ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003288 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 case MVT::v4f32:
3290 case MVT::v4i32:
3291 case MVT::v8i16:
3292 case MVT::v16i8:
Dale Johannesen75092de2008-03-12 00:22:17 +00003293 if (isVarArg) {
3294 // These go aligned on the stack, or in the corresponding R registers
Scott Michelfdc40a02009-02-17 22:15:04 +00003295 // when within range. The Darwin PPC ABI doc claims they also go in
Dale Johannesen75092de2008-03-12 00:22:17 +00003296 // V registers; in fact gcc does this only for arguments that are
3297 // prototyped, not for those that match the ... We do it for all
3298 // arguments, seems to work.
3299 while (ArgOffset % 16 !=0) {
3300 ArgOffset += PtrByteSize;
3301 if (GPR_idx != NumGPRs)
3302 GPR_idx++;
3303 }
3304 // We could elide this store in the case where the object fits
3305 // entirely in R registers. Maybe later.
Scott Michelfdc40a02009-02-17 22:15:04 +00003306 PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
Dale Johannesen75092de2008-03-12 00:22:17 +00003307 DAG.getConstant(ArgOffset, PtrVT));
Chris Lattner6229d0a2010-09-21 18:41:36 +00003308 SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff,
3309 MachinePointerInfo(), false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003310 MemOpChains.push_back(Store);
3311 if (VR_idx != NumVRs) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003312 SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003313 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003314 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003315 MemOpChains.push_back(Load.getValue(1));
3316 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
3317 }
3318 ArgOffset += 16;
3319 for (unsigned i=0; i<16; i+=PtrByteSize) {
3320 if (GPR_idx == NumGPRs)
3321 break;
Dale Johannesen39355f92009-02-04 02:34:38 +00003322 SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
Dale Johannesen75092de2008-03-12 00:22:17 +00003323 DAG.getConstant(i, PtrVT));
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003324 SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003325 false, false, false, 0);
Dale Johannesen75092de2008-03-12 00:22:17 +00003326 MemOpChains.push_back(Load.getValue(1));
3327 RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
3328 }
3329 break;
3330 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003331
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003332 // Non-varargs Altivec params generally go in registers, but have
3333 // stack space allocated at the end.
3334 if (VR_idx != NumVRs) {
3335 // Doesn't have GPR space allocated.
3336 RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
3337 } else if (nAltivecParamsAtEnd==0) {
3338 // We are emitting Altivec params in order.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003339 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3340 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003341 TailCallArguments, dl);
Dale Johannesen75092de2008-03-12 00:22:17 +00003342 ArgOffset += 16;
Dale Johannesen75092de2008-03-12 00:22:17 +00003343 }
Chris Lattnerc8b682c2006-05-17 00:15:40 +00003344 break;
Chris Lattnerabde4602006-05-16 22:56:08 +00003345 }
Chris Lattnerabde4602006-05-16 22:56:08 +00003346 }
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003347 // If all Altivec parameters fit in registers, as they usually do,
3348 // they get stack space following the non-Altivec parameters. We
3349 // don't track this here because nobody below needs it.
3350 // If there are more Altivec parameters than fit in registers emit
3351 // the stores here.
3352 if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
3353 unsigned j = 0;
3354 // Offset is aligned; skip 1st 12 params which go in V registers.
3355 ArgOffset = ((ArgOffset+15)/16)*16;
3356 ArgOffset += 12*16;
3357 for (unsigned i = 0; i != NumOps; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +00003358 SDValue Arg = OutVals[i];
3359 EVT ArgType = Outs[i].VT;
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
3361 ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003362 if (++j > NumVRs) {
Dan Gohman475871a2008-07-27 21:46:04 +00003363 SDValue PtrOff;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003364 // We are emitting Altivec params in order.
3365 LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
3366 isPPC64, isTailCall, true, MemOpChains,
Dale Johannesen33c960f2009-02-04 20:06:27 +00003367 TailCallArguments, dl);
Dale Johannesen8f5422c2008-03-14 17:41:26 +00003368 ArgOffset += 16;
3369 }
3370 }
3371 }
3372 }
3373
Chris Lattner9a2a4972006-05-17 06:01:33 +00003374 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnere2199452006-08-11 17:38:39 +00003376 &MemOpChains[0], MemOpChains.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00003377
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003378 // Check if this is an indirect call (MTCTR/BCTRL).
3379 // See PrepareCall() for more information about calls through function
3380 // pointers in the 64-bit SVR4 ABI.
3381 if (!isTailCall && isPPC64 && PPCSubTarget.isSVR4ABI() &&
3382 !dyn_cast<GlobalAddressSDNode>(Callee) &&
3383 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
3384 !isBLACompatibleAddress(Callee, DAG)) {
3385 // Load r2 into a virtual register and store it to the TOC save area.
3386 SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64);
3387 // TOC save area offset.
3388 SDValue PtrOff = DAG.getIntPtrConstant(40);
3389 SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
Chris Lattner6229d0a2010-09-21 18:41:36 +00003390 Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00003391 false, false, 0);
Tilmann Scheller3a84dae2009-12-18 13:00:15 +00003392 }
3393
Dale Johannesenf7b73042010-03-09 20:15:42 +00003394 // On Darwin, R12 must contain the address of an indirect callee. This does
3395 // not mean the MTCTR instruction must use R12; it's easier to model this as
3396 // an extra parameter, so do that.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003397 if (!isTailCall &&
Dale Johannesenf7b73042010-03-09 20:15:42 +00003398 !dyn_cast<GlobalAddressSDNode>(Callee) &&
3399 !dyn_cast<ExternalSymbolSDNode>(Callee) &&
3400 !isBLACompatibleAddress(Callee, DAG))
3401 RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 :
3402 PPC::R12), Callee));
3403
Chris Lattner9a2a4972006-05-17 06:01:33 +00003404 // Build a sequence of copy-to-reg nodes chained together with token chain
3405 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman475871a2008-07-27 21:46:04 +00003406 SDValue InFlag;
Chris Lattner9a2a4972006-05-17 06:01:33 +00003407 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003408 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesen39355f92009-02-04 02:34:38 +00003409 RegsToPass[i].second, InFlag);
Chris Lattner9a2a4972006-05-17 06:01:33 +00003410 InFlag = Chain.getValue(1);
3411 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003412
Chris Lattnerb9082582010-11-14 23:42:06 +00003413 if (isTailCall)
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003414 PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
3415 FPOp, true, TailCallArguments);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003416
Dan Gohman98ca4f22009-08-05 01:29:28 +00003417 return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
3418 RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
3419 Ins, InVals);
Chris Lattnerabde4602006-05-16 22:56:08 +00003420}
3421
Hal Finkeld712f932011-10-14 19:51:36 +00003422bool
3423PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3424 MachineFunction &MF, bool isVarArg,
3425 const SmallVectorImpl<ISD::OutputArg> &Outs,
3426 LLVMContext &Context) const {
3427 SmallVector<CCValAssign, 16> RVLocs;
3428 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
3429 RVLocs, Context);
3430 return CCInfo.CheckReturn(Outs, RetCC_PPC);
3431}
3432
Dan Gohman98ca4f22009-08-05 01:29:28 +00003433SDValue
3434PPCTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003435 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00003436 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00003437 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00003438 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00003439
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003440 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00003441 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3442 getTargetMachine(), RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00003443 CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
Scott Michelfdc40a02009-02-17 22:15:04 +00003444
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003445 // If this is the first return lowered for this function, add the regs to the
3446 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00003447 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003448 for (unsigned i = 0; i != RVLocs.size(); ++i)
Chris Lattner84bc5422007-12-31 04:13:23 +00003449 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003450 }
3451
Dan Gohman475871a2008-07-27 21:46:04 +00003452 SDValue Flag;
Scott Michelfdc40a02009-02-17 22:15:04 +00003453
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003454 // Copy the result values into the output registers.
3455 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3456 CCValAssign &VA = RVLocs[i];
3457 assert(VA.isRegLoc() && "Can only return in registers!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003458 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +00003459 OutVals[i], Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003460 Flag = Chain.getValue(1);
3461 }
3462
Gabor Greifba36cb52008-08-28 21:40:38 +00003463 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Chris Lattnerb9a7bea2007-03-06 00:59:59 +00003465 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner1a635d62006-04-14 06:01:58 +00003467}
3468
Dan Gohman475871a2008-07-27 21:46:04 +00003469SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003470 const PPCSubtarget &Subtarget) const {
Jim Laskeyefc7e522006-12-04 22:04:42 +00003471 // When we pop the dynamic allocation we need to restore the SP link.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003472 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003473
Jim Laskeyefc7e522006-12-04 22:04:42 +00003474 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00003475 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskeyefc7e522006-12-04 22:04:42 +00003476
3477 // Construct the stack pointer operand.
Dale Johannesenb60d5192009-11-24 01:09:07 +00003478 bool isPPC64 = Subtarget.isPPC64();
3479 unsigned SP = isPPC64 ? PPC::X1 : PPC::R1;
Dan Gohman475871a2008-07-27 21:46:04 +00003480 SDValue StackPtr = DAG.getRegister(SP, PtrVT);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003481
3482 // Get the operands for the STACKRESTORE.
Dan Gohman475871a2008-07-27 21:46:04 +00003483 SDValue Chain = Op.getOperand(0);
3484 SDValue SaveSP = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003485
Jim Laskeyefc7e522006-12-04 22:04:42 +00003486 // Load the old link SP.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003487 SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr,
3488 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003489 false, false, false, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003490
Jim Laskeyefc7e522006-12-04 22:04:42 +00003491 // Restore the stack pointer.
Dale Johannesen33c960f2009-02-04 20:06:27 +00003492 Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
Scott Michelfdc40a02009-02-17 22:15:04 +00003493
Jim Laskeyefc7e522006-12-04 22:04:42 +00003494 // Store the old link SP.
Chris Lattner6229d0a2010-09-21 18:41:36 +00003495 return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00003496 false, false, 0);
Jim Laskeyefc7e522006-12-04 22:04:42 +00003497}
3498
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003499
3500
Dan Gohman475871a2008-07-27 21:46:04 +00003501SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003502PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003503 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00003504 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003505 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00003506 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003507
3508 // Get current frame pointer save index. The users of this index will be
3509 // primarily DYNALLOC instructions.
3510 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3511 int RASI = FI->getReturnAddrSaveIndex();
3512
3513 // If the frame pointer save index hasn't been defined yet.
3514 if (!RASI) {
3515 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003516 int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003517 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00003518 RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003519 // Save the result.
3520 FI->setReturnAddrSaveIndex(RASI);
3521 }
3522 return DAG.getFrameIndex(RASI, PtrVT);
3523}
3524
Dan Gohman475871a2008-07-27 21:46:04 +00003525SDValue
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003526PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
3527 MachineFunction &MF = DAG.getMachineFunction();
Dale Johannesenb60d5192009-11-24 01:09:07 +00003528 bool isPPC64 = PPCSubTarget.isPPC64();
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003529 bool isDarwinABI = PPCSubTarget.isDarwinABI();
Owen Andersone50ed302009-08-10 22:56:29 +00003530 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003531
3532 // Get current frame pointer save index. The users of this index will be
3533 // primarily DYNALLOC instructions.
3534 PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
3535 int FPSI = FI->getFramePointerSaveIndex();
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003536
Jim Laskey2f616bf2006-11-16 22:43:37 +00003537 // If the frame pointer save index hasn't been defined yet.
3538 if (!FPSI) {
3539 // Find out what the fix offset of the frame pointer save area.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003540 int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64,
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +00003541 isDarwinABI);
Scott Michelfdc40a02009-02-17 22:15:04 +00003542
Jim Laskey2f616bf2006-11-16 22:43:37 +00003543 // Allocate the frame index for frame pointer save area.
Evan Chenged2ae132010-07-03 00:40:23 +00003544 FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003545 // Save the result.
Scott Michelfdc40a02009-02-17 22:15:04 +00003546 FI->setFramePointerSaveIndex(FPSI);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003547 }
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003548 return DAG.getFrameIndex(FPSI, PtrVT);
3549}
Jim Laskey2f616bf2006-11-16 22:43:37 +00003550
Dan Gohman475871a2008-07-27 21:46:04 +00003551SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00003552 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003553 const PPCSubtarget &Subtarget) const {
Jim Laskey2f616bf2006-11-16 22:43:37 +00003554 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00003555 SDValue Chain = Op.getOperand(0);
3556 SDValue Size = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00003557 DebugLoc dl = Op.getDebugLoc();
3558
Jim Laskey2f616bf2006-11-16 22:43:37 +00003559 // Get the corect type for pointers.
Owen Andersone50ed302009-08-10 22:56:29 +00003560 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Jim Laskey2f616bf2006-11-16 22:43:37 +00003561 // Negate the size.
Dale Johannesende064702009-02-06 21:50:26 +00003562 SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
Jim Laskey2f616bf2006-11-16 22:43:37 +00003563 DAG.getConstant(0, PtrVT), Size);
3564 // Construct a node for the frame pointer save index.
Dan Gohman475871a2008-07-27 21:46:04 +00003565 SDValue FPSIdx = getFramePointerFrameIndex(DAG);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003566 // Build a DYNALLOC node.
Dan Gohman475871a2008-07-27 21:46:04 +00003567 SDValue Ops[3] = { Chain, NegSize, FPSIdx };
Owen Anderson825b72b2009-08-11 20:47:22 +00003568 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
Dale Johannesende064702009-02-06 21:50:26 +00003569 return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
Jim Laskey2f616bf2006-11-16 22:43:37 +00003570}
3571
Chris Lattner1a635d62006-04-14 06:01:58 +00003572/// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
3573/// possible.
Dan Gohmand858e902010-04-17 15:26:15 +00003574SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner1a635d62006-04-14 06:01:58 +00003575 // Not FP? Not a fsel.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003576 if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
3577 !Op.getOperand(2).getValueType().isFloatingPoint())
Eli Friedmanc06441e2009-05-28 04:31:08 +00003578 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003579
Chris Lattner1a635d62006-04-14 06:01:58 +00003580 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003581
Chris Lattner1a635d62006-04-14 06:01:58 +00003582 // Cannot handle SETEQ/SETNE.
Eli Friedmanc06441e2009-05-28 04:31:08 +00003583 if (CC == ISD::SETEQ || CC == ISD::SETNE) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00003584
Owen Andersone50ed302009-08-10 22:56:29 +00003585 EVT ResVT = Op.getValueType();
3586 EVT CmpVT = Op.getOperand(0).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
3588 SDValue TV = Op.getOperand(2), FV = Op.getOperand(3);
Dale Johannesende064702009-02-06 21:50:26 +00003589 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00003590
Chris Lattner1a635d62006-04-14 06:01:58 +00003591 // If the RHS of the comparison is a 0.0, we don't need to do the
3592 // subtraction at all.
3593 if (isFloatingPointZero(RHS))
3594 switch (CC) {
3595 default: break; // SETUO etc aren't handled by fsel.
3596 case ISD::SETULT:
3597 case ISD::SETLT:
3598 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003599 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003600 case ISD::SETGE:
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
3602 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00003603 return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003604 case ISD::SETUGT:
3605 case ISD::SETGT:
3606 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattner57340122006-05-24 00:06:44 +00003607 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003608 case ISD::SETLE:
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
3610 LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
Dale Johannesende064702009-02-06 21:50:26 +00003611 return DAG.getNode(PPCISD::FSEL, dl, ResVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003613 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003614
Dan Gohman475871a2008-07-27 21:46:04 +00003615 SDValue Cmp;
Chris Lattner1a635d62006-04-14 06:01:58 +00003616 switch (CC) {
3617 default: break; // SETUO etc aren't handled by fsel.
3618 case ISD::SETULT:
3619 case ISD::SETLT:
Dale Johannesende064702009-02-06 21:50:26 +00003620 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003621 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3622 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003623 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003624 case ISD::SETOGE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003625 case ISD::SETGE:
Dale Johannesende064702009-02-06 21:50:26 +00003626 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003627 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3628 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003629 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003630 case ISD::SETUGT:
3631 case ISD::SETGT:
Dale Johannesende064702009-02-06 21:50:26 +00003632 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003633 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3634 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003635 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
Chris Lattner57340122006-05-24 00:06:44 +00003636 case ISD::SETOLE:
Chris Lattner1a635d62006-04-14 06:01:58 +00003637 case ISD::SETLE:
Dale Johannesende064702009-02-06 21:50:26 +00003638 Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00003639 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
3640 Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
Dale Johannesende064702009-02-06 21:50:26 +00003641 return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
Chris Lattner1a635d62006-04-14 06:01:58 +00003642 }
Eli Friedmanc06441e2009-05-28 04:31:08 +00003643 return Op;
Chris Lattner1a635d62006-04-14 06:01:58 +00003644}
3645
Chris Lattner1f873002007-11-28 18:44:47 +00003646// FIXME: Split this code up when LegalizeDAGTypes lands.
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003647SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00003648 DebugLoc dl) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003649 assert(Op.getOperand(0).getValueType().isFloatingPoint());
Dan Gohman475871a2008-07-27 21:46:04 +00003650 SDValue Src = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00003651 if (Src.getValueType() == MVT::f32)
3652 Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003653
Dan Gohman475871a2008-07-27 21:46:04 +00003654 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003656 default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003657 case MVT::i32:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00003658 Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003659 PPCISD::FCTIDZ,
Owen Anderson825b72b2009-08-11 20:47:22 +00003660 dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003661 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00003662 case MVT::i64:
3663 Tmp = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Src);
Chris Lattner1a635d62006-04-14 06:01:58 +00003664 break;
3665 }
Duncan Sandsa7360f02008-07-19 16:26:02 +00003666
Chris Lattner1a635d62006-04-14 06:01:58 +00003667 // Convert the FP value to an int value through memory.
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 SDValue FIPtr = DAG.CreateStackTemporary(MVT::f64);
Duncan Sandsa7360f02008-07-19 16:26:02 +00003669
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003670 // Emit a store to the stack slot.
Chris Lattner6229d0a2010-09-21 18:41:36 +00003671 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr,
3672 MachinePointerInfo(), false, false, 0);
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003673
3674 // Result is a load from the stack slot. If loading 4 bytes, make sure to
3675 // add in a bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00003676 if (Op.getValueType() == MVT::i32)
Dale Johannesen33c960f2009-02-04 20:06:27 +00003677 FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
Chris Lattner1de7c1d2007-10-15 20:14:52 +00003678 DAG.getConstant(4, FIPtr.getValueType()));
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003679 return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003680 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00003681}
3682
Dan Gohmand858e902010-04-17 15:26:15 +00003683SDValue PPCTargetLowering::LowerSINT_TO_FP(SDValue Op,
3684 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003685 DebugLoc dl = Op.getDebugLoc();
Dan Gohman034f60e2008-03-11 01:59:03 +00003686 // Don't handle ppc_fp128 here; let it be lowered to a libcall.
Owen Anderson825b72b2009-08-11 20:47:22 +00003687 if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
Dan Gohman475871a2008-07-27 21:46:04 +00003688 return SDValue();
Dan Gohman034f60e2008-03-11 01:59:03 +00003689
Owen Anderson825b72b2009-08-11 20:47:22 +00003690 if (Op.getOperand(0).getValueType() == MVT::i64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003691 SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op.getOperand(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003692 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Bits);
3693 if (Op.getValueType() == MVT::f32)
Scott Michelfdc40a02009-02-17 22:15:04 +00003694 FP = DAG.getNode(ISD::FP_ROUND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003695 MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003696 return FP;
3697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003698
Owen Anderson825b72b2009-08-11 20:47:22 +00003699 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
Chris Lattner1a635d62006-04-14 06:01:58 +00003700 "Unhandled SINT_TO_FP type in custom expander!");
3701 // Since we only generate this in 64-bit mode, we can take advantage of
3702 // 64-bit registers. In particular, sign extend the input value into the
3703 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
3704 // then lfd it and fcfid it.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003705 MachineFunction &MF = DAG.getMachineFunction();
3706 MachineFrameInfo *FrameInfo = MF.getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00003707 int FrameIdx = FrameInfo->CreateStackObject(8, 8, false);
Owen Andersone50ed302009-08-10 22:56:29 +00003708 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003709 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003710
Owen Anderson825b72b2009-08-11 20:47:22 +00003711 SDValue Ext64 = DAG.getNode(PPCISD::EXTSW_32, dl, MVT::i32,
Chris Lattner1a635d62006-04-14 06:01:58 +00003712 Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00003713
Chris Lattner1a635d62006-04-14 06:01:58 +00003714 // STD the extended value into the stack slot.
Dan Gohmanc76909a2009-09-25 20:36:54 +00003715 MachineMemOperand *MMO =
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003716 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
Chris Lattner59db5492010-09-21 04:39:43 +00003717 MachineMemOperand::MOStore, 8, 8);
Dan Gohmanc76909a2009-09-25 20:36:54 +00003718 SDValue Ops[] = { DAG.getEntryNode(), Ext64, FIdx };
3719 SDValue Store =
3720 DAG.getMemIntrinsicNode(PPCISD::STD_32, dl, DAG.getVTList(MVT::Other),
3721 Ops, 4, MVT::i64, MMO);
Chris Lattner1a635d62006-04-14 06:01:58 +00003722 // Load the value as a double.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003723 SDValue Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003724 false, false, false, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003725
Chris Lattner1a635d62006-04-14 06:01:58 +00003726 // FCFID it and return it.
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Ld);
3728 if (Op.getValueType() == MVT::f32)
3729 FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
Chris Lattner1a635d62006-04-14 06:01:58 +00003730 return FP;
3731}
3732
Dan Gohmand858e902010-04-17 15:26:15 +00003733SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
3734 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003735 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003736 /*
3737 The rounding mode is in bits 30:31 of FPSR, and has the following
3738 settings:
3739 00 Round to nearest
3740 01 Round to 0
3741 10 Round to +inf
3742 11 Round to -inf
3743
3744 FLT_ROUNDS, on the other hand, expects the following:
3745 -1 Undefined
3746 0 Round to 0
3747 1 Round to nearest
3748 2 Round to +inf
3749 3 Round to -inf
3750
3751 To perform the conversion, we do:
3752 ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
3753 */
3754
3755 MachineFunction &MF = DAG.getMachineFunction();
Owen Andersone50ed302009-08-10 22:56:29 +00003756 EVT VT = Op.getValueType();
3757 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
3758 std::vector<EVT> NodeTys;
Dan Gohman475871a2008-07-27 21:46:04 +00003759 SDValue MFFSreg, InFlag;
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003760
3761 // Save FP Control Word to register
Owen Anderson825b72b2009-08-11 20:47:22 +00003762 NodeTys.push_back(MVT::f64); // return register
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00003763 NodeTys.push_back(MVT::Glue); // unused in this context
Dale Johannesen33c960f2009-02-04 20:06:27 +00003764 SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003765
3766 // Save FP register to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00003767 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00003768 SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003769 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
Chris Lattner6229d0a2010-09-21 18:41:36 +00003770 StackSlot, MachinePointerInfo(), false, false,0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003771
3772 // Load FP Control Word from low 32 bits of stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00003773 SDValue Four = DAG.getConstant(4, PtrVT);
Dale Johannesen33c960f2009-02-04 20:06:27 +00003774 SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00003775 SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00003776 false, false, false, 0);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003777
3778 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00003779 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00003780 DAG.getNode(ISD::AND, dl, MVT::i32,
3781 CWD, DAG.getConstant(3, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00003782 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00003783 DAG.getNode(ISD::SRL, dl, MVT::i32,
3784 DAG.getNode(ISD::AND, dl, MVT::i32,
3785 DAG.getNode(ISD::XOR, dl, MVT::i32,
3786 CWD, DAG.getConstant(3, MVT::i32)),
3787 DAG.getConstant(3, MVT::i32)),
3788 DAG.getConstant(1, MVT::i32));
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003789
Dan Gohman475871a2008-07-27 21:46:04 +00003790 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00003791 DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003792
Duncan Sands83ec4b62008-06-06 12:08:01 +00003793 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen33c960f2009-02-04 20:06:27 +00003794 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Dale Johannesen5c5eb802008-01-18 19:55:37 +00003795}
3796
Dan Gohmand858e902010-04-17 15:26:15 +00003797SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003798 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003799 unsigned BitWidth = VT.getSizeInBits();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003800 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003801 assert(Op.getNumOperands() == 3 &&
3802 VT == Op.getOperand(1).getValueType() &&
3803 "Unexpected SHL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003804
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00003805 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003806 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003807 SDValue Lo = Op.getOperand(0);
3808 SDValue Hi = Op.getOperand(1);
3809 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003810 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003811
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003812 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003813 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003814 SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
3815 SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
3816 SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
3817 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003818 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003819 SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
3820 SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3821 SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003822 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003823 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003824}
3825
Dan Gohmand858e902010-04-17 15:26:15 +00003826SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00003827 EVT VT = Op.getValueType();
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003828 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003830 assert(Op.getNumOperands() == 3 &&
3831 VT == Op.getOperand(1).getValueType() &&
3832 "Unexpected SRL!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003833
Dan Gohman9ed06db2008-03-07 20:36:53 +00003834 // Expand into a bunch of logical ops. Note that these ops
Chris Lattner1a635d62006-04-14 06:01:58 +00003835 // depend on the PPC behavior for oversized shift amounts.
Dan Gohman475871a2008-07-27 21:46:04 +00003836 SDValue Lo = Op.getOperand(0);
3837 SDValue Hi = Op.getOperand(1);
3838 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003839 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003840
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003841 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003842 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003843 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3844 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3845 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3846 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003847 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003848 SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
3849 SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
3850 SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
Dan Gohman475871a2008-07-27 21:46:04 +00003851 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003852 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003853}
3854
Dan Gohmand858e902010-04-17 15:26:15 +00003855SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003856 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00003857 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003858 unsigned BitWidth = VT.getSizeInBits();
Dan Gohman9ed06db2008-03-07 20:36:53 +00003859 assert(Op.getNumOperands() == 3 &&
3860 VT == Op.getOperand(1).getValueType() &&
3861 "Unexpected SRA!");
Scott Michelfdc40a02009-02-17 22:15:04 +00003862
Dan Gohman9ed06db2008-03-07 20:36:53 +00003863 // Expand into a bunch of logical ops, followed by a select_cc.
Dan Gohman475871a2008-07-27 21:46:04 +00003864 SDValue Lo = Op.getOperand(0);
3865 SDValue Hi = Op.getOperand(1);
3866 SDValue Amt = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00003867 EVT AmtVT = Amt.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003868
Dale Johannesenf5d97892009-02-04 01:48:28 +00003869 SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003870 DAG.getConstant(BitWidth, AmtVT), Amt);
Dale Johannesenf5d97892009-02-04 01:48:28 +00003871 SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
3872 SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
3873 SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
3874 SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003875 DAG.getConstant(-BitWidth, AmtVT));
Dale Johannesenf5d97892009-02-04 01:48:28 +00003876 SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
3877 SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
3878 SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
Duncan Sands2fbfbd22008-10-30 19:28:32 +00003879 Tmp4, Tmp6, ISD::SETLE);
Dan Gohman475871a2008-07-27 21:46:04 +00003880 SDValue OutOps[] = { OutLo, OutHi };
Dale Johannesen4be0bdf2009-02-05 00:20:09 +00003881 return DAG.getMergeValues(OutOps, 2, dl);
Chris Lattner1a635d62006-04-14 06:01:58 +00003882}
3883
3884//===----------------------------------------------------------------------===//
3885// Vector related lowering.
3886//
3887
Chris Lattner4a998b92006-04-17 06:00:21 +00003888/// BuildSplatI - Build a canonical splati of Val with an element size of
3889/// SplatSize. Cast the result to VT.
Owen Andersone50ed302009-08-10 22:56:29 +00003890static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
Dale Johannesened2eee62009-02-06 01:31:28 +00003891 SelectionDAG &DAG, DebugLoc dl) {
Chris Lattner4a998b92006-04-17 06:00:21 +00003892 assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
Chris Lattner70fa4932006-12-01 01:45:39 +00003893
Owen Andersone50ed302009-08-10 22:56:29 +00003894 static const EVT VTys[] = { // canonical VT to use for each size.
Owen Anderson825b72b2009-08-11 20:47:22 +00003895 MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
Chris Lattner4a998b92006-04-17 06:00:21 +00003896 };
Chris Lattner70fa4932006-12-01 01:45:39 +00003897
Owen Anderson825b72b2009-08-11 20:47:22 +00003898 EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003899
Chris Lattner70fa4932006-12-01 01:45:39 +00003900 // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
3901 if (Val == -1)
3902 SplatSize = 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00003903
Owen Andersone50ed302009-08-10 22:56:29 +00003904 EVT CanonicalVT = VTys[SplatSize-1];
Scott Michelfdc40a02009-02-17 22:15:04 +00003905
Chris Lattner4a998b92006-04-17 06:00:21 +00003906 // Build a canonical splat for this value.
Owen Anderson825b72b2009-08-11 20:47:22 +00003907 SDValue Elt = DAG.getConstant(Val, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003908 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003909 Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
Evan Chenga87008d2009-02-25 22:49:59 +00003910 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
3911 &Ops[0], Ops.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003912 return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00003913}
3914
Chris Lattnere7c768e2006-04-18 03:24:30 +00003915/// BuildIntrinsicOp - Return a binary operator intrinsic node with the
Chris Lattner6876e662006-04-17 06:58:41 +00003916/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003917static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
Dale Johannesened2eee62009-02-06 01:31:28 +00003918 SelectionDAG &DAG, DebugLoc dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003919 EVT DestVT = MVT::Other) {
3920 if (DestVT == MVT::Other) DestVT = LHS.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003921 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 DAG.getConstant(IID, MVT::i32), LHS, RHS);
Chris Lattner6876e662006-04-17 06:58:41 +00003923}
3924
Chris Lattnere7c768e2006-04-18 03:24:30 +00003925/// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
3926/// specified intrinsic ID.
Dan Gohman475871a2008-07-27 21:46:04 +00003927static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
Dale Johannesened2eee62009-02-06 01:31:28 +00003928 SDValue Op2, SelectionDAG &DAG,
Owen Anderson825b72b2009-08-11 20:47:22 +00003929 DebugLoc dl, EVT DestVT = MVT::Other) {
3930 if (DestVT == MVT::Other) DestVT = Op0.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00003931 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
Owen Anderson825b72b2009-08-11 20:47:22 +00003932 DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
Chris Lattnere7c768e2006-04-18 03:24:30 +00003933}
3934
3935
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003936/// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
3937/// amount. The result has the specified value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003938static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
Owen Andersone50ed302009-08-10 22:56:29 +00003939 EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003940 // Force LHS/RHS to be the right type.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003941 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS);
3942 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS);
Duncan Sandsd038e042008-07-21 10:20:31 +00003943
Nate Begeman9008ca62009-04-27 18:41:29 +00003944 int Ops[16];
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003945 for (unsigned i = 0; i != 16; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 Ops[i] = i + Amt;
Owen Anderson825b72b2009-08-11 20:47:22 +00003947 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003948 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattnerbdd558c2006-04-17 17:55:10 +00003949}
3950
Chris Lattnerf1b47082006-04-14 05:19:18 +00003951// If this is a case we can't handle, return null and let the default
3952// expansion code take care of it. If we CAN select this case, and if it
3953// selects to a single instruction, return Op. Otherwise, if we can codegen
3954// this case more efficiently than a constant pool load, lower it to the
3955// sequence of ops that should be used.
Dan Gohmand858e902010-04-17 15:26:15 +00003956SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op,
3957 SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00003958 DebugLoc dl = Op.getDebugLoc();
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003959 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
3960 assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
Scott Micheldf380432009-02-25 03:12:50 +00003961
Bob Wilson24e338e2009-03-02 23:24:16 +00003962 // Check if this is a splat of a constant value.
3963 APInt APSplatBits, APSplatUndef;
3964 unsigned SplatBitSize;
Bob Wilsona27ea9e2009-03-01 01:13:55 +00003965 bool HasAnyUndefs;
Bob Wilsonf2950b02009-03-03 19:26:27 +00003966 if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
Dale Johannesen1e608812009-11-13 01:45:18 +00003967 HasAnyUndefs, 0, true) || SplatBitSize > 32)
Bob Wilsonf2950b02009-03-03 19:26:27 +00003968 return SDValue();
Evan Chenga87008d2009-02-25 22:49:59 +00003969
Bob Wilsonf2950b02009-03-03 19:26:27 +00003970 unsigned SplatBits = APSplatBits.getZExtValue();
3971 unsigned SplatUndef = APSplatUndef.getZExtValue();
3972 unsigned SplatSize = SplatBitSize / 8;
Scott Michelfdc40a02009-02-17 22:15:04 +00003973
Bob Wilsonf2950b02009-03-03 19:26:27 +00003974 // First, handle single instruction cases.
3975
3976 // All zeros?
3977 if (SplatBits == 0) {
3978 // Canonicalize all zero vectors to be v4i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00003979 if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
3980 SDValue Z = DAG.getConstant(0, MVT::i32);
3981 Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003982 Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z);
Chris Lattnerf1b47082006-04-14 05:19:18 +00003983 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00003984 return Op;
3985 }
Chris Lattnerb17f1672006-04-16 01:01:29 +00003986
Bob Wilsonf2950b02009-03-03 19:26:27 +00003987 // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
3988 int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
3989 (32-SplatBitSize));
3990 if (SextVal >= -16 && SextVal <= 15)
3991 return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00003992
3993
Bob Wilsonf2950b02009-03-03 19:26:27 +00003994 // Two instruction sequences.
Scott Michelfdc40a02009-02-17 22:15:04 +00003995
Bob Wilsonf2950b02009-03-03 19:26:27 +00003996 // If this value is in the range [-32,30] and is even, use:
3997 // tmp = VSPLTI[bhw], result = add tmp, tmp
3998 if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003999 SDValue Res = BuildSplatI(SextVal >> 1, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004000 Res = DAG.getNode(ISD::ADD, dl, Res.getValueType(), Res, Res);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004001 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004002 }
4003
4004 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
4005 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important
4006 // for fneg/fabs.
4007 if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
4008 // Make -1 and vspltisw -1:
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004010
4011 // Make the VSLW intrinsic, computing 0x8000_0000.
4012 SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
4013 OnesV, DAG, dl);
4014
4015 // xor by OnesV to invert it.
Owen Anderson825b72b2009-08-11 20:47:22 +00004016 Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004017 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004018 }
4019
4020 // Check to see if this is a wide variety of vsplti*, binop self cases.
4021 static const signed char SplatCsts[] = {
4022 -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
4023 -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
4024 };
4025
4026 for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
4027 // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
4028 // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1'
4029 int i = SplatCsts[idx];
4030
4031 // Figure out what shift amount will be used by altivec if shifted by i in
4032 // this splat size.
4033 unsigned TypeShiftAmt = i & (SplatBitSize-1);
4034
4035 // vsplti + shl self.
4036 if (SextVal == (i << (int)TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004037 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004038 static const unsigned IIDs[] = { // Intrinsic to use for each size.
4039 Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
4040 Intrinsic::ppc_altivec_vslw
4041 };
4042 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004043 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner4a998b92006-04-17 06:00:21 +00004044 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004045
Bob Wilsonf2950b02009-03-03 19:26:27 +00004046 // vsplti + srl self.
4047 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004048 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004049 static const unsigned IIDs[] = { // Intrinsic to use for each size.
4050 Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
4051 Intrinsic::ppc_altivec_vsrw
4052 };
4053 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004054 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00004055 }
4056
Bob Wilsonf2950b02009-03-03 19:26:27 +00004057 // vsplti + sra self.
4058 if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004059 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004060 static const unsigned IIDs[] = { // Intrinsic to use for each size.
4061 Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
4062 Intrinsic::ppc_altivec_vsraw
4063 };
4064 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004065 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Chris Lattner6876e662006-04-17 06:58:41 +00004066 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004067
Bob Wilsonf2950b02009-03-03 19:26:27 +00004068 // vsplti + rol self.
4069 if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
4070 ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004071 SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004072 static const unsigned IIDs[] = { // Intrinsic to use for each size.
4073 Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
4074 Intrinsic::ppc_altivec_vrlw
4075 };
4076 Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004077 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004079
Bob Wilsonf2950b02009-03-03 19:26:27 +00004080 // t = vsplti c, result = vsldoi t, t, 1
Eli Friedmane3837012010-08-02 00:18:19 +00004081 if (SextVal == ((i << 8) | (i < 0 ? 0xFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004082 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004083 return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
Chris Lattnerdbce85d2006-04-17 18:09:22 +00004084 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00004085 // t = vsplti c, result = vsldoi t, t, 2
Eli Friedmane3837012010-08-02 00:18:19 +00004086 if (SextVal == ((i << 16) | (i < 0 ? 0xFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004087 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004088 return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
Chris Lattnerf1b47082006-04-14 05:19:18 +00004089 }
Bob Wilsonf2950b02009-03-03 19:26:27 +00004090 // t = vsplti c, result = vsldoi t, t, 3
Eli Friedmane3837012010-08-02 00:18:19 +00004091 if (SextVal == ((i << 24) | (i < 0 ? 0xFFFFFF : 0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004092 SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004093 return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
4094 }
4095 }
4096
4097 // Three instruction sequences.
4098
4099 // Odd, in range [17,31]: (vsplti C)-(vsplti -16).
4100 if (SextVal >= 0 && SextVal <= 31) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004101 SDValue LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG, dl);
4102 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004103 LHS = DAG.getNode(ISD::SUB, dl, LHS.getValueType(), LHS, RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004104 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), LHS);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004105 }
4106 // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16).
4107 if (SextVal >= -31 && SextVal <= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004108 SDValue LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG, dl);
4109 SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
Bob Wilsonf2950b02009-03-03 19:26:27 +00004110 LHS = DAG.getNode(ISD::ADD, dl, LHS.getValueType(), LHS, RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004111 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), LHS);
Chris Lattnerf1b47082006-04-14 05:19:18 +00004112 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004113
Dan Gohman475871a2008-07-27 21:46:04 +00004114 return SDValue();
Chris Lattnerf1b47082006-04-14 05:19:18 +00004115}
4116
Chris Lattner59138102006-04-17 05:28:54 +00004117/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
4118/// the specified operations to build the shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004119static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
Scott Michelfdc40a02009-02-17 22:15:04 +00004120 SDValue RHS, SelectionDAG &DAG,
Dale Johannesened2eee62009-02-06 01:31:28 +00004121 DebugLoc dl) {
Chris Lattner59138102006-04-17 05:28:54 +00004122 unsigned OpNum = (PFEntry >> 26) & 0x0F;
Bill Wendling77959322008-09-17 00:30:57 +00004123 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
Chris Lattner59138102006-04-17 05:28:54 +00004124 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004125
Chris Lattner59138102006-04-17 05:28:54 +00004126 enum {
Chris Lattner00402c72006-05-16 04:20:24 +00004127 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 +00004128 OP_VMRGHW,
4129 OP_VMRGLW,
4130 OP_VSPLTISW0,
4131 OP_VSPLTISW1,
4132 OP_VSPLTISW2,
4133 OP_VSPLTISW3,
4134 OP_VSLDOI4,
4135 OP_VSLDOI8,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +00004136 OP_VSLDOI12
Chris Lattner59138102006-04-17 05:28:54 +00004137 };
Scott Michelfdc40a02009-02-17 22:15:04 +00004138
Chris Lattner59138102006-04-17 05:28:54 +00004139 if (OpNum == OP_COPY) {
4140 if (LHSID == (1*9+2)*9+3) return LHS;
4141 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
4142 return RHS;
4143 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004144
Dan Gohman475871a2008-07-27 21:46:04 +00004145 SDValue OpLHS, OpRHS;
Dale Johannesened2eee62009-02-06 01:31:28 +00004146 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
4147 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00004148
Nate Begeman9008ca62009-04-27 18:41:29 +00004149 int ShufIdxs[16];
Chris Lattner59138102006-04-17 05:28:54 +00004150 switch (OpNum) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004151 default: llvm_unreachable("Unknown i32 permute!");
Chris Lattner59138102006-04-17 05:28:54 +00004152 case OP_VMRGHW:
4153 ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3;
4154 ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
4155 ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7;
4156 ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
4157 break;
4158 case OP_VMRGLW:
4159 ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
4160 ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
4161 ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
4162 ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
4163 break;
4164 case OP_VSPLTISW0:
4165 for (unsigned i = 0; i != 16; ++i)
4166 ShufIdxs[i] = (i&3)+0;
4167 break;
4168 case OP_VSPLTISW1:
4169 for (unsigned i = 0; i != 16; ++i)
4170 ShufIdxs[i] = (i&3)+4;
4171 break;
4172 case OP_VSPLTISW2:
4173 for (unsigned i = 0; i != 16; ++i)
4174 ShufIdxs[i] = (i&3)+8;
4175 break;
4176 case OP_VSPLTISW3:
4177 for (unsigned i = 0; i != 16; ++i)
4178 ShufIdxs[i] = (i&3)+12;
4179 break;
4180 case OP_VSLDOI4:
Dale Johannesened2eee62009-02-06 01:31:28 +00004181 return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00004182 case OP_VSLDOI8:
Dale Johannesened2eee62009-02-06 01:31:28 +00004183 return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00004184 case OP_VSLDOI12:
Dale Johannesened2eee62009-02-06 01:31:28 +00004185 return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00004186 }
Owen Andersone50ed302009-08-10 22:56:29 +00004187 EVT VT = OpLHS.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004188 OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS);
4189 OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00004190 SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004191 return DAG.getNode(ISD::BITCAST, dl, VT, T);
Chris Lattner59138102006-04-17 05:28:54 +00004192}
4193
Chris Lattnerf1b47082006-04-14 05:19:18 +00004194/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
4195/// is a shuffle we can handle in a single instruction, return it. Otherwise,
4196/// return the code it can be lowered into. Worst case, it can always be
4197/// lowered into a vperm.
Scott Michelfdc40a02009-02-17 22:15:04 +00004198SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004199 SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00004200 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004201 SDValue V1 = Op.getOperand(0);
4202 SDValue V2 = Op.getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004203 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Owen Andersone50ed302009-08-10 22:56:29 +00004204 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00004205
Chris Lattnerf1b47082006-04-14 05:19:18 +00004206 // Cases that are handled by instructions that take permute immediates
4207 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
4208 // selected by the instruction selector.
4209 if (V2.getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004210 if (PPC::isSplatShuffleMask(SVOp, 1) ||
4211 PPC::isSplatShuffleMask(SVOp, 2) ||
4212 PPC::isSplatShuffleMask(SVOp, 4) ||
4213 PPC::isVPKUWUMShuffleMask(SVOp, true) ||
4214 PPC::isVPKUHUMShuffleMask(SVOp, true) ||
4215 PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
4216 PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
4217 PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
4218 PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
4219 PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
4220 PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
4221 PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
Chris Lattnerf1b47082006-04-14 05:19:18 +00004222 return Op;
4223 }
4224 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004225
Chris Lattnerf1b47082006-04-14 05:19:18 +00004226 // Altivec has a variety of "shuffle immediates" that take two vector inputs
4227 // and produce a fixed permutation. If any of these match, do not lower to
4228 // VPERM.
Nate Begeman9008ca62009-04-27 18:41:29 +00004229 if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
4230 PPC::isVPKUHUMShuffleMask(SVOp, false) ||
4231 PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
4232 PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
4233 PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
4234 PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
4235 PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
4236 PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
4237 PPC::isVMRGHShuffleMask(SVOp, 4, false))
Chris Lattnerf1b47082006-04-14 05:19:18 +00004238 return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004239
Chris Lattner59138102006-04-17 05:28:54 +00004240 // Check to see if this is a shuffle of 4-byte values. If so, we can use our
4241 // perfect shuffle table to emit an optimal matching sequence.
Nate Begeman9008ca62009-04-27 18:41:29 +00004242 SmallVector<int, 16> PermMask;
4243 SVOp->getMask(PermMask);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004244
Chris Lattner59138102006-04-17 05:28:54 +00004245 unsigned PFIndexes[4];
4246 bool isFourElementShuffle = true;
4247 for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
4248 unsigned EltNo = 8; // Start out undef.
4249 for (unsigned j = 0; j != 4; ++j) { // Intra-element byte.
Nate Begeman9008ca62009-04-27 18:41:29 +00004250 if (PermMask[i*4+j] < 0)
Chris Lattner59138102006-04-17 05:28:54 +00004251 continue; // Undef, ignore it.
Scott Michelfdc40a02009-02-17 22:15:04 +00004252
Nate Begeman9008ca62009-04-27 18:41:29 +00004253 unsigned ByteSource = PermMask[i*4+j];
Chris Lattner59138102006-04-17 05:28:54 +00004254 if ((ByteSource & 3) != j) {
4255 isFourElementShuffle = false;
4256 break;
4257 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004258
Chris Lattner59138102006-04-17 05:28:54 +00004259 if (EltNo == 8) {
4260 EltNo = ByteSource/4;
4261 } else if (EltNo != ByteSource/4) {
4262 isFourElementShuffle = false;
4263 break;
4264 }
4265 }
4266 PFIndexes[i] = EltNo;
4267 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004268
4269 // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
Chris Lattner59138102006-04-17 05:28:54 +00004270 // perfect shuffle vector to determine if it is cost effective to do this as
4271 // discrete instructions, or whether we should use a vperm.
4272 if (isFourElementShuffle) {
4273 // Compute the index in the perfect shuffle table.
Scott Michelfdc40a02009-02-17 22:15:04 +00004274 unsigned PFTableIndex =
Chris Lattner59138102006-04-17 05:28:54 +00004275 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Scott Michelfdc40a02009-02-17 22:15:04 +00004276
Chris Lattner59138102006-04-17 05:28:54 +00004277 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
4278 unsigned Cost = (PFEntry >> 30);
Scott Michelfdc40a02009-02-17 22:15:04 +00004279
Chris Lattner59138102006-04-17 05:28:54 +00004280 // Determining when to avoid vperm is tricky. Many things affect the cost
4281 // of vperm, particularly how many times the perm mask needs to be computed.
4282 // For example, if the perm mask can be hoisted out of a loop or is already
4283 // used (perhaps because there are multiple permutes with the same shuffle
4284 // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of
4285 // the loop requires an extra register.
4286 //
4287 // As a compromise, we only emit discrete instructions if the shuffle can be
Scott Michelfdc40a02009-02-17 22:15:04 +00004288 // generated in 3 or fewer operations. When we have loop information
Chris Lattner59138102006-04-17 05:28:54 +00004289 // available, if this block is within a loop, we should avoid using vperm
4290 // for 3-operation perms and use a constant pool load instead.
Scott Michelfdc40a02009-02-17 22:15:04 +00004291 if (Cost < 3)
Dale Johannesened2eee62009-02-06 01:31:28 +00004292 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
Chris Lattner59138102006-04-17 05:28:54 +00004293 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004294
Chris Lattnerf1b47082006-04-14 05:19:18 +00004295 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
4296 // vector that will get spilled to the constant pool.
4297 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Scott Michelfdc40a02009-02-17 22:15:04 +00004298
Chris Lattnerf1b47082006-04-14 05:19:18 +00004299 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
4300 // that it is in input element units, not in bytes. Convert now.
Owen Andersone50ed302009-08-10 22:56:29 +00004301 EVT EltVT = V1.getValueType().getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004302 unsigned BytesPerElement = EltVT.getSizeInBits()/8;
Scott Michelfdc40a02009-02-17 22:15:04 +00004303
Dan Gohman475871a2008-07-27 21:46:04 +00004304 SmallVector<SDValue, 16> ResultMask;
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
4306 unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00004307
Chris Lattnerf1b47082006-04-14 05:19:18 +00004308 for (unsigned j = 0; j != BytesPerElement; ++j)
4309 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
Owen Anderson825b72b2009-08-11 20:47:22 +00004310 MVT::i32));
Chris Lattnerf1b47082006-04-14 05:19:18 +00004311 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004312
Owen Anderson825b72b2009-08-11 20:47:22 +00004313 SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
Evan Chenga87008d2009-02-25 22:49:59 +00004314 &ResultMask[0], ResultMask.size());
Dale Johannesened2eee62009-02-06 01:31:28 +00004315 return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
Chris Lattnerf1b47082006-04-14 05:19:18 +00004316}
4317
Chris Lattner90564f22006-04-18 17:59:36 +00004318/// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
4319/// altivec comparison. If it is, return true and fill in Opc/isDot with
4320/// information about the intrinsic.
Dan Gohman475871a2008-07-27 21:46:04 +00004321static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
Chris Lattner90564f22006-04-18 17:59:36 +00004322 bool &isDot) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004323 unsigned IntrinsicID =
4324 cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00004325 CompareOpc = -1;
4326 isDot = false;
4327 switch (IntrinsicID) {
4328 default: return false;
4329 // Comparison predicates.
Chris Lattner1a635d62006-04-14 06:01:58 +00004330 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
4331 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
4332 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
4333 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
4334 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
4335 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
4336 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
4337 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
4338 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
4339 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
4340 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
4341 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
4342 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00004343
Chris Lattner1a635d62006-04-14 06:01:58 +00004344 // Normal Comparisons.
4345 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
4346 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
4347 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
4348 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
4349 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
4350 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
4351 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
4352 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
4353 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
4354 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
4355 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
4356 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
4357 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
4358 }
Chris Lattner90564f22006-04-18 17:59:36 +00004359 return true;
4360}
4361
4362/// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
4363/// lower, do it, otherwise return null.
Scott Michelfdc40a02009-02-17 22:15:04 +00004364SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004365 SelectionDAG &DAG) const {
Chris Lattner90564f22006-04-18 17:59:36 +00004366 // If this is a lowered altivec predicate compare, CompareOpc is set to the
4367 // opcode number of the comparison.
Dale Johannesen3484c092009-02-05 22:07:54 +00004368 DebugLoc dl = Op.getDebugLoc();
Chris Lattner90564f22006-04-18 17:59:36 +00004369 int CompareOpc;
4370 bool isDot;
4371 if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
Dan Gohman475871a2008-07-27 21:46:04 +00004372 return SDValue(); // Don't custom lower most intrinsics.
Scott Michelfdc40a02009-02-17 22:15:04 +00004373
Chris Lattner90564f22006-04-18 17:59:36 +00004374 // If this is a non-dot comparison, make the VCMP node and we are done.
Chris Lattner1a635d62006-04-14 06:01:58 +00004375 if (!isDot) {
Dale Johannesen3484c092009-02-05 22:07:54 +00004376 SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
Chris Lattner149add02010-03-14 22:44:11 +00004377 Op.getOperand(1), Op.getOperand(2),
4378 DAG.getConstant(CompareOpc, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004379 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp);
Chris Lattner1a635d62006-04-14 06:01:58 +00004380 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004381
Chris Lattner1a635d62006-04-14 06:01:58 +00004382 // Create the PPCISD altivec 'dot' comparison node.
Dan Gohman475871a2008-07-27 21:46:04 +00004383 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00004384 Op.getOperand(2), // LHS
4385 Op.getOperand(3), // RHS
Owen Anderson825b72b2009-08-11 20:47:22 +00004386 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00004387 };
Owen Andersone50ed302009-08-10 22:56:29 +00004388 std::vector<EVT> VTs;
Chris Lattner1a635d62006-04-14 06:01:58 +00004389 VTs.push_back(Op.getOperand(2).getValueType());
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004390 VTs.push_back(MVT::Glue);
Dale Johannesen3484c092009-02-05 22:07:54 +00004391 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00004392
Chris Lattner1a635d62006-04-14 06:01:58 +00004393 // Now that we have the comparison, emit a copy from the CR to a GPR.
4394 // This is flagged to the above dot comparison.
Owen Anderson825b72b2009-08-11 20:47:22 +00004395 SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32,
4396 DAG.getRegister(PPC::CR6, MVT::i32),
Scott Michelfdc40a02009-02-17 22:15:04 +00004397 CompNode.getValue(1));
4398
Chris Lattner1a635d62006-04-14 06:01:58 +00004399 // Unpack the result based on how the target uses it.
4400 unsigned BitNo; // Bit # of CR6.
4401 bool InvertBit; // Invert result?
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004402 switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
Chris Lattner1a635d62006-04-14 06:01:58 +00004403 default: // Can't happen, don't crash on invalid number though.
4404 case 0: // Return the value of the EQ bit of CR6.
4405 BitNo = 0; InvertBit = false;
4406 break;
4407 case 1: // Return the inverted value of the EQ bit of CR6.
4408 BitNo = 0; InvertBit = true;
4409 break;
4410 case 2: // Return the value of the LT bit of CR6.
4411 BitNo = 2; InvertBit = false;
4412 break;
4413 case 3: // Return the inverted value of the LT bit of CR6.
4414 BitNo = 2; InvertBit = true;
4415 break;
4416 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004417
Chris Lattner1a635d62006-04-14 06:01:58 +00004418 // Shift the bit into the low position.
Owen Anderson825b72b2009-08-11 20:47:22 +00004419 Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
4420 DAG.getConstant(8-(3-BitNo), MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00004421 // Isolate the bit.
Owen Anderson825b72b2009-08-11 20:47:22 +00004422 Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
4423 DAG.getConstant(1, MVT::i32));
Scott Michelfdc40a02009-02-17 22:15:04 +00004424
Chris Lattner1a635d62006-04-14 06:01:58 +00004425 // If we are supposed to, toggle the bit.
4426 if (InvertBit)
Owen Anderson825b72b2009-08-11 20:47:22 +00004427 Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
4428 DAG.getConstant(1, MVT::i32));
Chris Lattner1a635d62006-04-14 06:01:58 +00004429 return Flags;
4430}
4431
Scott Michelfdc40a02009-02-17 22:15:04 +00004432SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004433 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004434 DebugLoc dl = Op.getDebugLoc();
Chris Lattner1a635d62006-04-14 06:01:58 +00004435 // Create a stack slot that is 16-byte aligned.
4436 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
David Greene3f2bf852009-11-12 20:49:22 +00004437 int FrameIdx = FrameInfo->CreateStackObject(16, 16, false);
Dale Johannesen08673d22010-05-03 22:59:34 +00004438 EVT PtrVT = getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00004439 SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004440
Chris Lattner1a635d62006-04-14 06:01:58 +00004441 // Store the input value into Value#0 of the stack slot.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004442 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +00004443 Op.getOperand(0), FIdx, MachinePointerInfo(),
David Greene534502d12010-02-15 16:56:53 +00004444 false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004445 // Load it out.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00004446 return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00004447 false, false, false, 0);
Chris Lattner1a635d62006-04-14 06:01:58 +00004448}
4449
Dan Gohmand858e902010-04-17 15:26:15 +00004450SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesened2eee62009-02-06 01:31:28 +00004451 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004452 if (Op.getValueType() == MVT::v4i32) {
Dan Gohman475871a2008-07-27 21:46:04 +00004453 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004454
Owen Anderson825b72b2009-08-11 20:47:22 +00004455 SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl);
4456 SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
Scott Michelfdc40a02009-02-17 22:15:04 +00004457
Dan Gohman475871a2008-07-27 21:46:04 +00004458 SDValue RHSSwap = // = vrlw RHS, 16
Dale Johannesened2eee62009-02-06 01:31:28 +00004459 BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
Scott Michelfdc40a02009-02-17 22:15:04 +00004460
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004461 // Shrinkify inputs to v8i16.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004462 LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS);
4463 RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS);
4464 RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap);
Scott Michelfdc40a02009-02-17 22:15:04 +00004465
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004466 // Low parts multiplied together, generating 32-bit results (we ignore the
4467 // top parts).
Dan Gohman475871a2008-07-27 21:46:04 +00004468 SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
Owen Anderson825b72b2009-08-11 20:47:22 +00004469 LHS, RHS, DAG, dl, MVT::v4i32);
Scott Michelfdc40a02009-02-17 22:15:04 +00004470
Dan Gohman475871a2008-07-27 21:46:04 +00004471 SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
Owen Anderson825b72b2009-08-11 20:47:22 +00004472 LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004473 // Shift the high parts up 16 bits.
Scott Michelfdc40a02009-02-17 22:15:04 +00004474 HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
Dale Johannesened2eee62009-02-06 01:31:28 +00004475 Neg16, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004476 return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
4477 } else if (Op.getValueType() == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004478 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004479
Owen Anderson825b72b2009-08-11 20:47:22 +00004480 SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004481
Chris Lattnercea2aa72006-04-18 04:28:57 +00004482 return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
Dale Johannesened2eee62009-02-06 01:31:28 +00004483 LHS, RHS, Zero, DAG, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004484 } else if (Op.getValueType() == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004485 SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004486
Chris Lattner19a81522006-04-18 03:57:35 +00004487 // Multiply the even 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004488 SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
Owen Anderson825b72b2009-08-11 20:47:22 +00004489 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004490 EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004491
Chris Lattner19a81522006-04-18 03:57:35 +00004492 // Multiply the odd 8-bit parts, producing 16-bit sums.
Dan Gohman475871a2008-07-27 21:46:04 +00004493 SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
Owen Anderson825b72b2009-08-11 20:47:22 +00004494 LHS, RHS, DAG, dl, MVT::v8i16);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004495 OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts);
Scott Michelfdc40a02009-02-17 22:15:04 +00004496
Chris Lattner19a81522006-04-18 03:57:35 +00004497 // Merge the results together.
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 int Ops[16];
Chris Lattner19a81522006-04-18 03:57:35 +00004499 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004500 Ops[i*2 ] = 2*i+1;
4501 Ops[i*2+1] = 2*i+1+16;
Chris Lattner19a81522006-04-18 03:57:35 +00004502 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004503 return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004504 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00004505 llvm_unreachable("Unknown mul to lower!");
Chris Lattner72dd9bd2006-04-18 03:43:48 +00004506 }
Chris Lattnere7c768e2006-04-18 03:24:30 +00004507}
4508
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004509/// LowerOperation - Provide custom lowering hooks for some operations.
4510///
Dan Gohmand858e902010-04-17 15:26:15 +00004511SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004512 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00004513 default: llvm_unreachable("Wasn't expecting to be able to lower this!");
Chris Lattner1a635d62006-04-14 06:01:58 +00004514 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson3d90dbe2009-11-04 21:31:18 +00004515 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00004516 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Chris Lattner1e61e692010-11-15 02:46:57 +00004517 case ISD::GlobalTLSAddress: llvm_unreachable("TLS not implemented for PPC");
Nate Begeman37efe672006-04-22 18:53:45 +00004518 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Chris Lattner1a635d62006-04-14 06:01:58 +00004519 case ISD::SETCC: return LowerSETCC(Op, DAG);
Duncan Sands4a544a72011-09-06 13:37:06 +00004520 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
4521 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004522 case ISD::VASTART:
Dan Gohman1e93df62010-04-17 14:41:14 +00004523 return LowerVASTART(Op, DAG, PPCSubTarget);
Scott Michelfdc40a02009-02-17 22:15:04 +00004524
4525 case ISD::VAARG:
Dan Gohman1e93df62010-04-17 14:41:14 +00004526 return LowerVAARG(Op, DAG, PPCSubTarget);
Nicolas Geoffray01119992007-04-03 13:59:52 +00004527
Jim Laskeyefc7e522006-12-04 22:04:42 +00004528 case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
Chris Lattner9f0bc652007-02-25 05:34:32 +00004529 case ISD::DYNAMIC_STACKALLOC:
4530 return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
Evan Cheng54fc97d2008-04-19 01:30:48 +00004531
Chris Lattner1a635d62006-04-14 06:01:58 +00004532 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004533 case ISD::FP_TO_UINT:
4534 case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG,
Dale Johannesen3484c092009-02-05 22:07:54 +00004535 Op.getDebugLoc());
Chris Lattner1a635d62006-04-14 06:01:58 +00004536 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00004537 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004538
Chris Lattner1a635d62006-04-14 06:01:58 +00004539 // Lower 64-bit shifts.
Chris Lattner3fe6c1d2006-09-20 03:47:40 +00004540 case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG);
4541 case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG);
4542 case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00004543
Chris Lattner1a635d62006-04-14 06:01:58 +00004544 // Vector-related lowering.
4545 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4546 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4547 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
4548 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
Chris Lattnere7c768e2006-04-18 03:24:30 +00004549 case ISD::MUL: return LowerMUL(Op, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004550
Chris Lattner3fc027d2007-12-08 06:59:59 +00004551 // Frame & Return address.
4552 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00004553 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerbc11c342005-08-31 20:23:54 +00004554 }
Dan Gohman475871a2008-07-27 21:46:04 +00004555 return SDValue();
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00004556}
4557
Duncan Sands1607f052008-12-01 11:39:25 +00004558void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
4559 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00004560 SelectionDAG &DAG) const {
Roman Divackybdb226e2011-06-28 15:30:42 +00004561 const TargetMachine &TM = getTargetMachine();
Dale Johannesen3484c092009-02-05 22:07:54 +00004562 DebugLoc dl = N->getDebugLoc();
Chris Lattner1f873002007-11-28 18:44:47 +00004563 switch (N->getOpcode()) {
Duncan Sands57760d92008-10-28 15:00:32 +00004564 default:
Duncan Sands1607f052008-12-01 11:39:25 +00004565 assert(false && "Do not know how to custom type legalize this operation!");
4566 return;
Roman Divackybdb226e2011-06-28 15:30:42 +00004567 case ISD::VAARG: {
4568 if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
4569 || TM.getSubtarget<PPCSubtarget>().isPPC64())
4570 return;
4571
4572 EVT VT = N->getValueType(0);
4573
4574 if (VT == MVT::i64) {
4575 SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget);
4576
4577 Results.push_back(NewNode);
4578 Results.push_back(NewNode.getValue(1));
4579 }
4580 return;
4581 }
Duncan Sands1607f052008-12-01 11:39:25 +00004582 case ISD::FP_ROUND_INREG: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004583 assert(N->getValueType(0) == MVT::ppcf128);
4584 assert(N->getOperand(0).getValueType() == MVT::ppcf128);
Scott Michelfdc40a02009-02-17 22:15:04 +00004585 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004586 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004587 DAG.getIntPtrConstant(0));
Dale Johannesen3484c092009-02-05 22:07:54 +00004588 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004589 MVT::f64, N->getOperand(0),
Duncan Sands1607f052008-12-01 11:39:25 +00004590 DAG.getIntPtrConstant(1));
4591
4592 // This sequence changes FPSCR to do round-to-zero, adds the two halves
4593 // of the long double, and puts FPSCR back the way it was. We do not
4594 // actually model FPSCR.
Owen Andersone50ed302009-08-10 22:56:29 +00004595 std::vector<EVT> NodeTys;
Duncan Sands1607f052008-12-01 11:39:25 +00004596 SDValue Ops[4], Result, MFFSreg, InFlag, FPreg;
4597
Owen Anderson825b72b2009-08-11 20:47:22 +00004598 NodeTys.push_back(MVT::f64); // Return register
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004599 NodeTys.push_back(MVT::Glue); // Returns a flag for later insns
Dale Johannesen3484c092009-02-05 22:07:54 +00004600 Result = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
Duncan Sands1607f052008-12-01 11:39:25 +00004601 MFFSreg = Result.getValue(0);
4602 InFlag = Result.getValue(1);
4603
4604 NodeTys.clear();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004605 NodeTys.push_back(MVT::Glue); // Returns a flag
Owen Anderson825b72b2009-08-11 20:47:22 +00004606 Ops[0] = DAG.getConstant(31, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004607 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004608 Result = DAG.getNode(PPCISD::MTFSB1, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004609 InFlag = Result.getValue(0);
4610
4611 NodeTys.clear();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004612 NodeTys.push_back(MVT::Glue); // Returns a flag
Owen Anderson825b72b2009-08-11 20:47:22 +00004613 Ops[0] = DAG.getConstant(30, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004614 Ops[1] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004615 Result = DAG.getNode(PPCISD::MTFSB0, dl, NodeTys, Ops, 2);
Duncan Sands1607f052008-12-01 11:39:25 +00004616 InFlag = Result.getValue(0);
4617
4618 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004619 NodeTys.push_back(MVT::f64); // result of add
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00004620 NodeTys.push_back(MVT::Glue); // Returns a flag
Duncan Sands1607f052008-12-01 11:39:25 +00004621 Ops[0] = Lo;
4622 Ops[1] = Hi;
4623 Ops[2] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004624 Result = DAG.getNode(PPCISD::FADDRTZ, dl, NodeTys, Ops, 3);
Duncan Sands1607f052008-12-01 11:39:25 +00004625 FPreg = Result.getValue(0);
4626 InFlag = Result.getValue(1);
4627
4628 NodeTys.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00004629 NodeTys.push_back(MVT::f64);
4630 Ops[0] = DAG.getConstant(1, MVT::i32);
Duncan Sands1607f052008-12-01 11:39:25 +00004631 Ops[1] = MFFSreg;
4632 Ops[2] = FPreg;
4633 Ops[3] = InFlag;
Dale Johannesen3484c092009-02-05 22:07:54 +00004634 Result = DAG.getNode(PPCISD::MTFSF, dl, NodeTys, Ops, 4);
Duncan Sands1607f052008-12-01 11:39:25 +00004635 FPreg = Result.getValue(0);
4636
4637 // We know the low half is about to be thrown away, so just use something
4638 // convenient.
Owen Anderson825b72b2009-08-11 20:47:22 +00004639 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
Dale Johannesen3484c092009-02-05 22:07:54 +00004640 FPreg, FPreg));
Duncan Sands1607f052008-12-01 11:39:25 +00004641 return;
Duncan Sandsa7360f02008-07-19 16:26:02 +00004642 }
Duncan Sands1607f052008-12-01 11:39:25 +00004643 case ISD::FP_TO_SINT:
Dale Johannesen4c9369d2009-06-04 20:53:52 +00004644 Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
Duncan Sands1607f052008-12-01 11:39:25 +00004645 return;
Chris Lattner1f873002007-11-28 18:44:47 +00004646 }
4647}
4648
4649
Chris Lattner1a635d62006-04-14 06:01:58 +00004650//===----------------------------------------------------------------------===//
4651// Other Lowering Code
4652//===----------------------------------------------------------------------===//
4653
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004654MachineBasicBlock *
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004655PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004656 bool is64bit, unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004657 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004658 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4659
4660 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4661 MachineFunction *F = BB->getParent();
4662 MachineFunction::iterator It = BB;
4663 ++It;
4664
4665 unsigned dest = MI->getOperand(0).getReg();
4666 unsigned ptrA = MI->getOperand(1).getReg();
4667 unsigned ptrB = MI->getOperand(2).getReg();
4668 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004669 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004670
4671 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4672 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4673 F->insert(It, loopMBB);
4674 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004675 exitMBB->splice(exitMBB->begin(), BB,
4676 llvm::next(MachineBasicBlock::iterator(MI)),
4677 BB->end());
4678 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004679
4680 MachineRegisterInfo &RegInfo = F->getRegInfo();
Dale Johannesen0e55f062008-08-29 18:29:46 +00004681 unsigned TmpReg = (!BinOpcode) ? incr :
4682 RegInfo.createVirtualRegister(
Dale Johannesena619d012008-09-02 20:30:23 +00004683 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4684 (const TargetRegisterClass *) &PPC::GPRCRegClass);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004685
4686 // thisMBB:
4687 // ...
4688 // fallthrough --> loopMBB
4689 BB->addSuccessor(loopMBB);
4690
4691 // loopMBB:
4692 // l[wd]arx dest, ptr
4693 // add r0, dest, incr
4694 // st[wd]cx. r0, ptr
4695 // bne- loopMBB
4696 // fallthrough --> exitMBB
4697 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004698 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004699 .addReg(ptrA).addReg(ptrB);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004700 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004701 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
4702 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004703 .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004704 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004705 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004706 BB->addSuccessor(loopMBB);
4707 BB->addSuccessor(exitMBB);
4708
4709 // exitMBB:
4710 // ...
4711 BB = exitMBB;
4712 return BB;
4713}
4714
4715MachineBasicBlock *
Scott Michelfdc40a02009-02-17 22:15:04 +00004716PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
Dale Johannesen97efa362008-08-28 17:53:09 +00004717 MachineBasicBlock *BB,
4718 bool is8bit, // operation
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00004719 unsigned BinOpcode) const {
Dale Johannesen0e55f062008-08-29 18:29:46 +00004720 // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
Dale Johannesen97efa362008-08-28 17:53:09 +00004721 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
4722 // In 64 bit mode we have to use 64 bits for addresses, even though the
4723 // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address
4724 // registers without caring whether they're 32 or 64, but here we're
4725 // doing actual arithmetic on the addresses.
4726 bool is64bit = PPCSubTarget.isPPC64();
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00004727 unsigned ZeroReg = is64bit ? PPC::X0 : PPC::R0;
Dale Johannesen97efa362008-08-28 17:53:09 +00004728
4729 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4730 MachineFunction *F = BB->getParent();
4731 MachineFunction::iterator It = BB;
4732 ++It;
4733
4734 unsigned dest = MI->getOperand(0).getReg();
4735 unsigned ptrA = MI->getOperand(1).getReg();
4736 unsigned ptrB = MI->getOperand(2).getReg();
4737 unsigned incr = MI->getOperand(3).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004738 DebugLoc dl = MI->getDebugLoc();
Dale Johannesen97efa362008-08-28 17:53:09 +00004739
4740 MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
4741 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
4742 F->insert(It, loopMBB);
4743 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004744 exitMBB->splice(exitMBB->begin(), BB,
4745 llvm::next(MachineBasicBlock::iterator(MI)),
4746 BB->end());
4747 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesen97efa362008-08-28 17:53:09 +00004748
4749 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00004750 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00004751 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
4752 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesen97efa362008-08-28 17:53:09 +00004753 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
4754 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
4755 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
4756 unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
4757 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
4758 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
4759 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
4760 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
4761 unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
4762 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004763 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004764 unsigned Ptr1Reg;
Dale Johannesen0e55f062008-08-29 18:29:46 +00004765 unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004766
4767 // thisMBB:
4768 // ...
4769 // fallthrough --> loopMBB
4770 BB->addSuccessor(loopMBB);
4771
4772 // The 4-byte load must be aligned, while a char or short may be
4773 // anywhere in the word. Hence all this nasty bookkeeping code.
4774 // add ptr1, ptrA, ptrB [copy if ptrA==0]
4775 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00004776 // xori shift, shift1, 24 [16]
Dale Johannesen97efa362008-08-28 17:53:09 +00004777 // rlwinm ptr, ptr1, 0, 0, 29
4778 // slw incr2, incr, shift
4779 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
4780 // slw mask, mask2, shift
4781 // loopMBB:
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004782 // lwarx tmpDest, ptr
Dale Johannesen0e55f062008-08-29 18:29:46 +00004783 // add tmp, tmpDest, incr2
4784 // andc tmp2, tmpDest, mask
Dale Johannesen97efa362008-08-28 17:53:09 +00004785 // and tmp3, tmp, mask
4786 // or tmp4, tmp3, tmp2
Dale Johannesenea9eedb2008-08-30 00:08:53 +00004787 // stwcx. tmp4, ptr
Dale Johannesen97efa362008-08-28 17:53:09 +00004788 // bne- loopMBB
4789 // fallthrough --> exitMBB
Dale Johannesen0e55f062008-08-29 18:29:46 +00004790 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00004791 if (ptrA != ZeroReg) {
Dale Johannesen97efa362008-08-28 17:53:09 +00004792 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004793 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004794 .addReg(ptrA).addReg(ptrB);
4795 } else {
4796 Ptr1Reg = ptrB;
4797 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004798 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004799 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004800 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004801 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
4802 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004803 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004804 .addReg(Ptr1Reg).addImm(0).addImm(61);
4805 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00004806 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004807 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004808 BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004809 .addReg(incr).addReg(ShiftReg);
4810 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004811 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesen97efa362008-08-28 17:53:09 +00004812 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00004813 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
4814 BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
Dale Johannesen97efa362008-08-28 17:53:09 +00004815 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00004816 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004817 .addReg(Mask2Reg).addReg(ShiftReg);
4818
4819 BB = loopMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00004820 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00004821 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen0e55f062008-08-29 18:29:46 +00004822 if (BinOpcode)
Dale Johannesen536a2f12009-02-13 02:27:39 +00004823 BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004824 .addReg(Incr2Reg).addReg(TmpDestReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004825 BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
Dale Johannesen0e55f062008-08-29 18:29:46 +00004826 .addReg(TmpDestReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004827 BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004828 .addReg(TmpReg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004829 BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
Dale Johannesen97efa362008-08-28 17:53:09 +00004830 .addReg(Tmp3Reg).addReg(Tmp2Reg);
Roman Divacky951cd022011-06-17 15:21:10 +00004831 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00004832 .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00004833 BuildMI(BB, dl, TII->get(PPC::BCC))
Scott Michelfdc40a02009-02-17 22:15:04 +00004834 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
Dale Johannesen97efa362008-08-28 17:53:09 +00004835 BB->addSuccessor(loopMBB);
4836 BB->addSuccessor(exitMBB);
4837
4838 // exitMBB:
4839 // ...
4840 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00004841 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg)
4842 .addReg(ShiftReg);
Dale Johannesen97efa362008-08-28 17:53:09 +00004843 return BB;
4844}
4845
4846MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00004847PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00004848 MachineBasicBlock *BB) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004849 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng53301922008-07-12 02:23:19 +00004850
4851 // To "insert" these instructions we actually have to insert their
4852 // control-flow patterns.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004853 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004854 MachineFunction::iterator It = BB;
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00004855 ++It;
Evan Cheng53301922008-07-12 02:23:19 +00004856
Dan Gohman8e5f2c62008-07-07 23:14:23 +00004857 MachineFunction *F = BB->getParent();
Evan Cheng53301922008-07-12 02:23:19 +00004858
4859 if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
4860 MI->getOpcode() == PPC::SELECT_CC_I8 ||
4861 MI->getOpcode() == PPC::SELECT_CC_F4 ||
4862 MI->getOpcode() == PPC::SELECT_CC_F8 ||
4863 MI->getOpcode() == PPC::SELECT_CC_VRRC) {
4864
4865 // The incoming instruction knows the destination vreg to set, the
4866 // condition code register to branch on, the true/false values to
4867 // select between, and a branch opcode to use.
4868
4869 // thisMBB:
4870 // ...
4871 // TrueVal = ...
4872 // cmpTY ccX, r1, r2
4873 // bCC copy1MBB
4874 // fallthrough --> copy0MBB
4875 MachineBasicBlock *thisMBB = BB;
4876 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
4877 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
4878 unsigned SelectPred = MI->getOperand(4).getImm();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004879 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00004880 F->insert(It, copy0MBB);
4881 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004882
4883 // Transfer the remainder of BB and its successor edges to sinkMBB.
4884 sinkMBB->splice(sinkMBB->begin(), BB,
4885 llvm::next(MachineBasicBlock::iterator(MI)),
4886 BB->end());
4887 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
4888
Evan Cheng53301922008-07-12 02:23:19 +00004889 // Next, add the true and fallthrough blocks as its successors.
4890 BB->addSuccessor(copy0MBB);
4891 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004892
Dan Gohman14152b42010-07-06 20:24:04 +00004893 BuildMI(BB, dl, TII->get(PPC::BCC))
4894 .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
4895
Evan Cheng53301922008-07-12 02:23:19 +00004896 // copy0MBB:
4897 // %FalseValue = ...
4898 // # fallthrough to sinkMBB
4899 BB = copy0MBB;
Scott Michelfdc40a02009-02-17 22:15:04 +00004900
Evan Cheng53301922008-07-12 02:23:19 +00004901 // Update machine-CFG edges
4902 BB->addSuccessor(sinkMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00004903
Evan Cheng53301922008-07-12 02:23:19 +00004904 // sinkMBB:
4905 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4906 // ...
4907 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00004908 BuildMI(*BB, BB->begin(), dl,
4909 TII->get(PPC::PHI), MI->getOperand(0).getReg())
Evan Cheng53301922008-07-12 02:23:19 +00004910 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
4911 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4912 }
Dale Johannesen97efa362008-08-28 17:53:09 +00004913 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
4914 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
4915 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
4916 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004917 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
4918 BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
4919 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
4920 BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004921
4922 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
4923 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
4924 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
4925 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004926 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
4927 BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
4928 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
4929 BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004930
4931 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
4932 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
4933 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
4934 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004935 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
4936 BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
4937 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
4938 BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004939
4940 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
4941 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
4942 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
4943 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004944 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
4945 BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
4946 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
4947 BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004948
4949 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
Dale Johannesen209a4092008-09-11 02:15:03 +00004950 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
Dale Johannesen97efa362008-08-28 17:53:09 +00004951 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
Dale Johannesen209a4092008-09-11 02:15:03 +00004952 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004953 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
Dale Johannesen209a4092008-09-11 02:15:03 +00004954 BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004955 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
Dale Johannesen209a4092008-09-11 02:15:03 +00004956 BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004957
4958 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
4959 BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
4960 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
4961 BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
Dale Johannesenbdab93a2008-08-25 22:34:37 +00004962 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
4963 BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
4964 else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
4965 BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
Dale Johannesen97efa362008-08-28 17:53:09 +00004966
Dale Johannesen0e55f062008-08-29 18:29:46 +00004967 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
4968 BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
4969 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
4970 BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
4971 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
4972 BB = EmitAtomicBinary(MI, BB, false, 0);
4973 else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
4974 BB = EmitAtomicBinary(MI, BB, true, 0);
4975
Evan Cheng53301922008-07-12 02:23:19 +00004976 else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
4977 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
4978 bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
4979
4980 unsigned dest = MI->getOperand(0).getReg();
4981 unsigned ptrA = MI->getOperand(1).getReg();
4982 unsigned ptrB = MI->getOperand(2).getReg();
4983 unsigned oldval = MI->getOperand(3).getReg();
4984 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00004985 DebugLoc dl = MI->getDebugLoc();
Evan Cheng53301922008-07-12 02:23:19 +00004986
Dale Johannesen65e39732008-08-25 18:53:26 +00004987 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
4988 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
4989 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
Evan Cheng53301922008-07-12 02:23:19 +00004990 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesen65e39732008-08-25 18:53:26 +00004991 F->insert(It, loop1MBB);
4992 F->insert(It, loop2MBB);
4993 F->insert(It, midMBB);
Evan Cheng53301922008-07-12 02:23:19 +00004994 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00004995 exitMBB->splice(exitMBB->begin(), BB,
4996 llvm::next(MachineBasicBlock::iterator(MI)),
4997 BB->end());
4998 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Evan Cheng53301922008-07-12 02:23:19 +00004999
5000 // thisMBB:
5001 // ...
5002 // fallthrough --> loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00005003 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00005004
Dale Johannesen65e39732008-08-25 18:53:26 +00005005 // loop1MBB:
Evan Cheng53301922008-07-12 02:23:19 +00005006 // l[wd]arx dest, ptr
Dale Johannesen65e39732008-08-25 18:53:26 +00005007 // cmp[wd] dest, oldval
5008 // bne- midMBB
5009 // loop2MBB:
Evan Cheng53301922008-07-12 02:23:19 +00005010 // st[wd]cx. newval, ptr
5011 // bne- loopMBB
Dale Johannesen65e39732008-08-25 18:53:26 +00005012 // b exitBB
5013 // midMBB:
5014 // st[wd]cx. dest, ptr
5015 // exitBB:
5016 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005017 BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
Evan Cheng53301922008-07-12 02:23:19 +00005018 .addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005019 BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
Evan Cheng53301922008-07-12 02:23:19 +00005020 .addReg(oldval).addReg(dest);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005021 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00005022 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
5023 BB->addSuccessor(loop2MBB);
5024 BB->addSuccessor(midMBB);
5025
5026 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005027 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Evan Cheng53301922008-07-12 02:23:19 +00005028 .addReg(newval).addReg(ptrA).addReg(ptrB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005029 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesen65e39732008-08-25 18:53:26 +00005030 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005031 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesen65e39732008-08-25 18:53:26 +00005032 BB->addSuccessor(loop1MBB);
Evan Cheng53301922008-07-12 02:23:19 +00005033 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00005034
Dale Johannesen65e39732008-08-25 18:53:26 +00005035 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005036 BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
Dale Johannesen65e39732008-08-25 18:53:26 +00005037 .addReg(dest).addReg(ptrA).addReg(ptrB);
5038 BB->addSuccessor(exitMBB);
5039
Evan Cheng53301922008-07-12 02:23:19 +00005040 // exitMBB:
5041 // ...
5042 BB = exitMBB;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005043 } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
5044 MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
5045 // We must use 64-bit registers for addresses when targeting 64-bit,
5046 // since we're actually doing arithmetic on them. Other registers
5047 // can be 32-bit.
5048 bool is64bit = PPCSubTarget.isPPC64();
5049 bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
5050
5051 unsigned dest = MI->getOperand(0).getReg();
5052 unsigned ptrA = MI->getOperand(1).getReg();
5053 unsigned ptrB = MI->getOperand(2).getReg();
5054 unsigned oldval = MI->getOperand(3).getReg();
5055 unsigned newval = MI->getOperand(4).getReg();
Dale Johannesen536a2f12009-02-13 02:27:39 +00005056 DebugLoc dl = MI->getDebugLoc();
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005057
5058 MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
5059 MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
5060 MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
5061 MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
5062 F->insert(It, loop1MBB);
5063 F->insert(It, loop2MBB);
5064 F->insert(It, midMBB);
5065 F->insert(It, exitMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00005066 exitMBB->splice(exitMBB->begin(), BB,
5067 llvm::next(MachineBasicBlock::iterator(MI)),
5068 BB->end());
5069 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005070
5071 MachineRegisterInfo &RegInfo = F->getRegInfo();
Scott Michelfdc40a02009-02-17 22:15:04 +00005072 const TargetRegisterClass *RC =
Dale Johannesena619d012008-09-02 20:30:23 +00005073 is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
5074 (const TargetRegisterClass *) &PPC::GPRCRegClass;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005075 unsigned PtrReg = RegInfo.createVirtualRegister(RC);
5076 unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
5077 unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
5078 unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
5079 unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
5080 unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
5081 unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
5082 unsigned MaskReg = RegInfo.createVirtualRegister(RC);
5083 unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
5084 unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
5085 unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
5086 unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
5087 unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
5088 unsigned Ptr1Reg;
5089 unsigned TmpReg = RegInfo.createVirtualRegister(RC);
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005090 unsigned ZeroReg = is64bit ? PPC::X0 : PPC::R0;
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005091 // thisMBB:
5092 // ...
5093 // fallthrough --> loopMBB
5094 BB->addSuccessor(loop1MBB);
5095
5096 // The 4-byte load must be aligned, while a char or short may be
5097 // anywhere in the word. Hence all this nasty bookkeeping code.
5098 // add ptr1, ptrA, ptrB [copy if ptrA==0]
5099 // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
Dale Johannesena619d012008-09-02 20:30:23 +00005100 // xori shift, shift1, 24 [16]
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005101 // rlwinm ptr, ptr1, 0, 0, 29
5102 // slw newval2, newval, shift
5103 // slw oldval2, oldval,shift
5104 // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
5105 // slw mask, mask2, shift
5106 // and newval3, newval2, mask
5107 // and oldval3, oldval2, mask
5108 // loop1MBB:
5109 // lwarx tmpDest, ptr
5110 // and tmp, tmpDest, mask
5111 // cmpw tmp, oldval3
5112 // bne- midMBB
5113 // loop2MBB:
5114 // andc tmp2, tmpDest, mask
5115 // or tmp4, tmp2, newval3
5116 // stwcx. tmp4, ptr
5117 // bne- loop1MBB
5118 // b exitBB
5119 // midMBB:
5120 // stwcx. tmpDest, ptr
5121 // exitBB:
5122 // srw dest, tmpDest, shift
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005123 if (ptrA != ZeroReg) {
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005124 Ptr1Reg = RegInfo.createVirtualRegister(RC);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005125 BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005126 .addReg(ptrA).addReg(ptrB);
5127 } else {
5128 Ptr1Reg = ptrB;
5129 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00005130 BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005131 .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005132 BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005133 .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
5134 if (is64bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005135 BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005136 .addReg(Ptr1Reg).addImm(0).addImm(61);
5137 else
Dale Johannesen536a2f12009-02-13 02:27:39 +00005138 BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005139 .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005140 BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005141 .addReg(newval).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005142 BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005143 .addReg(oldval).addReg(ShiftReg);
5144 if (is8bit)
Dale Johannesen536a2f12009-02-13 02:27:39 +00005145 BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005146 else {
Dale Johannesen536a2f12009-02-13 02:27:39 +00005147 BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
5148 BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
5149 .addReg(Mask3Reg).addImm(65535);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005150 }
Dale Johannesen536a2f12009-02-13 02:27:39 +00005151 BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005152 .addReg(Mask2Reg).addReg(ShiftReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005153 BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005154 .addReg(NewVal2Reg).addReg(MaskReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005155 BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005156 .addReg(OldVal2Reg).addReg(MaskReg);
5157
5158 BB = loop1MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005159 BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005160 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005161 BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
5162 .addReg(TmpDestReg).addReg(MaskReg);
5163 BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005164 .addReg(TmpReg).addReg(OldVal3Reg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005165 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005166 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
5167 BB->addSuccessor(loop2MBB);
5168 BB->addSuccessor(midMBB);
5169
5170 BB = loop2MBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005171 BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
5172 .addReg(TmpDestReg).addReg(MaskReg);
5173 BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
5174 .addReg(Tmp2Reg).addReg(NewVal3Reg);
5175 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005176 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005177 BuildMI(BB, dl, TII->get(PPC::BCC))
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005178 .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
Dale Johannesen536a2f12009-02-13 02:27:39 +00005179 BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005180 BB->addSuccessor(loop1MBB);
5181 BB->addSuccessor(exitMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00005182
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005183 BB = midMBB;
Dale Johannesen536a2f12009-02-13 02:27:39 +00005184 BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
Jakob Stoklund Olesen2684c5d2011-04-04 17:07:06 +00005185 .addReg(ZeroReg).addReg(PtrReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005186 BB->addSuccessor(exitMBB);
5187
5188 // exitMBB:
5189 // ...
5190 BB = exitMBB;
Jakob Stoklund Olesen5fcb81d2011-04-04 17:57:29 +00005191 BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg)
5192 .addReg(ShiftReg);
Dale Johannesenea9eedb2008-08-30 00:08:53 +00005193 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00005194 llvm_unreachable("Unexpected instr type to insert");
Evan Cheng53301922008-07-12 02:23:19 +00005195 }
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005196
Dan Gohman14152b42010-07-06 20:24:04 +00005197 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00005198 return BB;
5199}
5200
Chris Lattner1a635d62006-04-14 06:01:58 +00005201//===----------------------------------------------------------------------===//
5202// Target Optimization Hooks
5203//===----------------------------------------------------------------------===//
5204
Duncan Sands25cf2272008-11-24 14:53:14 +00005205SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
5206 DAGCombinerInfo &DCI) const {
Dan Gohmanf0757b02010-04-21 01:34:56 +00005207 const TargetMachine &TM = getTargetMachine();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005208 SelectionDAG &DAG = DCI.DAG;
Dale Johannesen3484c092009-02-05 22:07:54 +00005209 DebugLoc dl = N->getDebugLoc();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005210 switch (N->getOpcode()) {
5211 default: break;
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00005212 case PPCISD::SHL:
5213 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00005214 if (C->isNullValue()) // 0 << V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00005215 return N->getOperand(0);
5216 }
5217 break;
5218 case PPCISD::SRL:
5219 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00005220 if (C->isNullValue()) // 0 >>u V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00005221 return N->getOperand(0);
5222 }
5223 break;
5224 case PPCISD::SRA:
5225 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
Dan Gohmane368b462010-06-18 14:22:04 +00005226 if (C->isNullValue() || // 0 >>s V -> 0.
Chris Lattnercf9d0ac2006-09-19 05:22:59 +00005227 C->isAllOnesValue()) // -1 >>s V -> -1.
5228 return N->getOperand(0);
5229 }
5230 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005231
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005232 case ISD::SINT_TO_FP:
Chris Lattnera7a58542006-06-16 17:34:12 +00005233 if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005234 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
5235 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
5236 // We allow the src/dst to be either f32/f64, but the intermediate
5237 // type must be i64.
Owen Anderson825b72b2009-08-11 20:47:22 +00005238 if (N->getOperand(0).getValueType() == MVT::i64 &&
5239 N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00005240 SDValue Val = N->getOperand(0).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005241 if (Val.getValueType() == MVT::f32) {
5242 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005243 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005244 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005245
Owen Anderson825b72b2009-08-11 20:47:22 +00005246 Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005247 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00005248 Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005249 DCI.AddToWorklist(Val.getNode());
Owen Anderson825b72b2009-08-11 20:47:22 +00005250 if (N->getValueType(0) == MVT::f32) {
5251 Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
Chris Lattner0bd48932008-01-17 07:00:52 +00005252 DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005253 DCI.AddToWorklist(Val.getNode());
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005254 }
5255 return Val;
Owen Anderson825b72b2009-08-11 20:47:22 +00005256 } else if (N->getOperand(0).getValueType() == MVT::i32) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00005257 // If the intermediate type is i32, we can avoid the load/store here
5258 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005259 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005260 }
5261 }
5262 break;
Chris Lattner51269842006-03-01 05:50:56 +00005263 case ISD::STORE:
5264 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
5265 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
Chris Lattnera7a02fb2008-01-18 16:54:56 +00005266 !cast<StoreSDNode>(N)->isTruncatingStore() &&
Chris Lattner51269842006-03-01 05:50:56 +00005267 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005268 N->getOperand(1).getValueType() == MVT::i32 &&
5269 N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00005270 SDValue Val = N->getOperand(1).getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005271 if (Val.getValueType() == MVT::f32) {
5272 Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005273 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00005274 }
Owen Anderson825b72b2009-08-11 20:47:22 +00005275 Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
Gabor Greifba36cb52008-08-28 21:40:38 +00005276 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00005277
Owen Anderson825b72b2009-08-11 20:47:22 +00005278 Val = DAG.getNode(PPCISD::STFIWX, dl, MVT::Other, N->getOperand(0), Val,
Chris Lattner51269842006-03-01 05:50:56 +00005279 N->getOperand(2), N->getOperand(3));
Gabor Greifba36cb52008-08-28 21:40:38 +00005280 DCI.AddToWorklist(Val.getNode());
Chris Lattner51269842006-03-01 05:50:56 +00005281 return Val;
5282 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005283
Chris Lattnerd9989382006-07-10 20:56:58 +00005284 // Turn STORE (BSWAP) -> sthbrx/stwbrx.
Dan Gohman6acaaa82009-09-25 00:57:30 +00005285 if (cast<StoreSDNode>(N)->isUnindexed() &&
5286 N->getOperand(1).getOpcode() == ISD::BSWAP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005287 N->getOperand(1).getNode()->hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005288 (N->getOperand(1).getValueType() == MVT::i32 ||
5289 N->getOperand(1).getValueType() == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue BSwapOp = N->getOperand(1).getOperand(0);
Chris Lattnerd9989382006-07-10 20:56:58 +00005291 // Do an any-extend to 32-bits if this is a half-word input.
Owen Anderson825b72b2009-08-11 20:47:22 +00005292 if (BSwapOp.getValueType() == MVT::i16)
5293 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
Chris Lattnerd9989382006-07-10 20:56:58 +00005294
Dan Gohmanc76909a2009-09-25 20:36:54 +00005295 SDValue Ops[] = {
5296 N->getOperand(0), BSwapOp, N->getOperand(2),
5297 DAG.getValueType(N->getOperand(1).getValueType())
5298 };
5299 return
5300 DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other),
5301 Ops, array_lengthof(Ops),
5302 cast<StoreSDNode>(N)->getMemoryVT(),
5303 cast<StoreSDNode>(N)->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00005304 }
5305 break;
5306 case ISD::BSWAP:
5307 // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
Gabor Greifba36cb52008-08-28 21:40:38 +00005308 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
Chris Lattnerd9989382006-07-10 20:56:58 +00005309 N->getOperand(0).hasOneUse() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005310 (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
Dan Gohman475871a2008-07-27 21:46:04 +00005311 SDValue Load = N->getOperand(0);
Evan Cheng466685d2006-10-09 20:57:25 +00005312 LoadSDNode *LD = cast<LoadSDNode>(Load);
Chris Lattnerd9989382006-07-10 20:56:58 +00005313 // Create the byte-swapping load.
Dan Gohman475871a2008-07-27 21:46:04 +00005314 SDValue Ops[] = {
Evan Cheng466685d2006-10-09 20:57:25 +00005315 LD->getChain(), // Chain
5316 LD->getBasePtr(), // Ptr
Chris Lattner79e490a2006-08-11 17:18:05 +00005317 DAG.getValueType(N->getValueType(0)) // VT
5318 };
Dan Gohmanc76909a2009-09-25 20:36:54 +00005319 SDValue BSLoad =
5320 DAG.getMemIntrinsicNode(PPCISD::LBRX, dl,
5321 DAG.getVTList(MVT::i32, MVT::Other), Ops, 3,
5322 LD->getMemoryVT(), LD->getMemOperand());
Chris Lattnerd9989382006-07-10 20:56:58 +00005323
Scott Michelfdc40a02009-02-17 22:15:04 +00005324 // If this is an i16 load, insert the truncate.
Dan Gohman475871a2008-07-27 21:46:04 +00005325 SDValue ResVal = BSLoad;
Owen Anderson825b72b2009-08-11 20:47:22 +00005326 if (N->getValueType(0) == MVT::i16)
5327 ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
Scott Michelfdc40a02009-02-17 22:15:04 +00005328
Chris Lattnerd9989382006-07-10 20:56:58 +00005329 // First, combine the bswap away. This makes the value produced by the
5330 // load dead.
5331 DCI.CombineTo(N, ResVal);
5332
5333 // Next, combine the load away, we give it a bogus result value but a real
5334 // chain result. The result value is dead because the bswap is dead.
Gabor Greifba36cb52008-08-28 21:40:38 +00005335 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005336
Chris Lattnerd9989382006-07-10 20:56:58 +00005337 // Return N so it doesn't get rechecked!
Dan Gohman475871a2008-07-27 21:46:04 +00005338 return SDValue(N, 0);
Chris Lattnerd9989382006-07-10 20:56:58 +00005339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005340
Chris Lattner51269842006-03-01 05:50:56 +00005341 break;
Chris Lattner4468c222006-03-31 06:02:07 +00005342 case PPCISD::VCMP: {
5343 // If a VCMPo node already exists with exactly the same operands as this
5344 // node, use its result instead of this node (VCMPo computes both a CR6 and
5345 // a normal output).
5346 //
5347 if (!N->getOperand(0).hasOneUse() &&
5348 !N->getOperand(1).hasOneUse() &&
5349 !N->getOperand(2).hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005350
Chris Lattner4468c222006-03-31 06:02:07 +00005351 // Scan all of the users of the LHS, looking for VCMPo's that match.
5352 SDNode *VCMPoNode = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005353
Gabor Greifba36cb52008-08-28 21:40:38 +00005354 SDNode *LHSN = N->getOperand(0).getNode();
Chris Lattner4468c222006-03-31 06:02:07 +00005355 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
5356 UI != E; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +00005357 if (UI->getOpcode() == PPCISD::VCMPo &&
5358 UI->getOperand(1) == N->getOperand(1) &&
5359 UI->getOperand(2) == N->getOperand(2) &&
5360 UI->getOperand(0) == N->getOperand(0)) {
5361 VCMPoNode = *UI;
Chris Lattner4468c222006-03-31 06:02:07 +00005362 break;
5363 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005364
Chris Lattner00901202006-04-18 18:28:22 +00005365 // If there is no VCMPo node, or if the flag value has a single use, don't
5366 // transform this.
5367 if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
5368 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005369
5370 // Look at the (necessarily single) use of the flag value. If it has a
Chris Lattner00901202006-04-18 18:28:22 +00005371 // chain, this transformation is more complex. Note that multiple things
5372 // could use the value result, which we should ignore.
5373 SDNode *FlagUser = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005374 for (SDNode::use_iterator UI = VCMPoNode->use_begin();
Chris Lattner00901202006-04-18 18:28:22 +00005375 FlagUser == 0; ++UI) {
5376 assert(UI != VCMPoNode->use_end() && "Didn't find user!");
Dan Gohman89684502008-07-27 20:43:25 +00005377 SDNode *User = *UI;
Chris Lattner00901202006-04-18 18:28:22 +00005378 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005379 if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
Chris Lattner00901202006-04-18 18:28:22 +00005380 FlagUser = User;
5381 break;
5382 }
5383 }
5384 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005385
Chris Lattner00901202006-04-18 18:28:22 +00005386 // If the user is a MFCR instruction, we know this is safe. Otherwise we
5387 // give up for right now.
5388 if (FlagUser->getOpcode() == PPCISD::MFCR)
Dan Gohman475871a2008-07-27 21:46:04 +00005389 return SDValue(VCMPoNode, 0);
Chris Lattner4468c222006-03-31 06:02:07 +00005390 }
5391 break;
5392 }
Chris Lattner90564f22006-04-18 17:59:36 +00005393 case ISD::BR_CC: {
5394 // If this is a branch on an altivec predicate comparison, lower this so
5395 // that we don't have to do a MFCR: instead, branch directly on CR6. This
5396 // lowering is done pre-legalize, because the legalizer lowers the predicate
5397 // compare down to code that is difficult to reassemble.
5398 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +00005399 SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
Chris Lattner90564f22006-04-18 17:59:36 +00005400 int CompareOpc;
5401 bool isDot;
Scott Michelfdc40a02009-02-17 22:15:04 +00005402
Chris Lattner90564f22006-04-18 17:59:36 +00005403 if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
5404 isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
5405 getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
5406 assert(isDot && "Can't compare against a vector result!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005407
Chris Lattner90564f22006-04-18 17:59:36 +00005408 // If this is a comparison against something other than 0/1, then we know
5409 // that the condition is never/always true.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005410 unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
Chris Lattner90564f22006-04-18 17:59:36 +00005411 if (Val != 0 && Val != 1) {
5412 if (CC == ISD::SETEQ) // Cond never true, remove branch.
5413 return N->getOperand(0);
5414 // Always !=, turn it into an unconditional branch.
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 return DAG.getNode(ISD::BR, dl, MVT::Other,
Chris Lattner90564f22006-04-18 17:59:36 +00005416 N->getOperand(0), N->getOperand(4));
5417 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005418
Chris Lattner90564f22006-04-18 17:59:36 +00005419 bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005420
Chris Lattner90564f22006-04-18 17:59:36 +00005421 // Create the PPCISD altivec 'dot' comparison node.
Owen Andersone50ed302009-08-10 22:56:29 +00005422 std::vector<EVT> VTs;
Dan Gohman475871a2008-07-27 21:46:04 +00005423 SDValue Ops[] = {
Chris Lattner79e490a2006-08-11 17:18:05 +00005424 LHS.getOperand(2), // LHS of compare
5425 LHS.getOperand(3), // RHS of compare
Owen Anderson825b72b2009-08-11 20:47:22 +00005426 DAG.getConstant(CompareOpc, MVT::i32)
Chris Lattner79e490a2006-08-11 17:18:05 +00005427 };
Chris Lattner90564f22006-04-18 17:59:36 +00005428 VTs.push_back(LHS.getOperand(2).getValueType());
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00005429 VTs.push_back(MVT::Glue);
Dale Johannesen3484c092009-02-05 22:07:54 +00005430 SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
Scott Michelfdc40a02009-02-17 22:15:04 +00005431
Chris Lattner90564f22006-04-18 17:59:36 +00005432 // Unpack the result based on how the target uses it.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005433 PPC::Predicate CompOpc;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005434 switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
Chris Lattner90564f22006-04-18 17:59:36 +00005435 default: // Can't happen, don't crash on invalid number though.
5436 case 0: // Branch on the value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005437 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
Chris Lattner90564f22006-04-18 17:59:36 +00005438 break;
5439 case 1: // Branch on the inverted value of the EQ bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005440 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
Chris Lattner90564f22006-04-18 17:59:36 +00005441 break;
5442 case 2: // Branch on the value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005443 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
Chris Lattner90564f22006-04-18 17:59:36 +00005444 break;
5445 case 3: // Branch on the inverted value of the LT bit of CR6.
Chris Lattnerdf4ed632006-11-17 22:10:59 +00005446 CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
Chris Lattner90564f22006-04-18 17:59:36 +00005447 break;
5448 }
5449
Owen Anderson825b72b2009-08-11 20:47:22 +00005450 return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
5451 DAG.getConstant(CompOpc, MVT::i32),
5452 DAG.getRegister(PPC::CR6, MVT::i32),
Chris Lattner90564f22006-04-18 17:59:36 +00005453 N->getOperand(4), CompNode.getValue(1));
5454 }
5455 break;
5456 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005457 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005458
Dan Gohman475871a2008-07-27 21:46:04 +00005459 return SDValue();
Chris Lattner8c13d0a2006-03-01 04:57:39 +00005460}
5461
Chris Lattner1a635d62006-04-14 06:01:58 +00005462//===----------------------------------------------------------------------===//
5463// Inline Assembly Support
5464//===----------------------------------------------------------------------===//
5465
Dan Gohman475871a2008-07-27 21:46:04 +00005466void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00005467 const APInt &Mask,
Scott Michelfdc40a02009-02-17 22:15:04 +00005468 APInt &KnownZero,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005469 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005470 const SelectionDAG &DAG,
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005471 unsigned Depth) const {
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00005472 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005473 switch (Op.getOpcode()) {
5474 default: break;
Chris Lattnerd9989382006-07-10 20:56:58 +00005475 case PPCISD::LBRX: {
5476 // lhbrx is known to have the top bits cleared out.
Dan Gohmanae03af22009-09-27 23:17:47 +00005477 if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16)
Chris Lattnerd9989382006-07-10 20:56:58 +00005478 KnownZero = 0xFFFF0000;
5479 break;
5480 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005481 case ISD::INTRINSIC_WO_CHAIN: {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005482 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005483 default: break;
5484 case Intrinsic::ppc_altivec_vcmpbfp_p:
5485 case Intrinsic::ppc_altivec_vcmpeqfp_p:
5486 case Intrinsic::ppc_altivec_vcmpequb_p:
5487 case Intrinsic::ppc_altivec_vcmpequh_p:
5488 case Intrinsic::ppc_altivec_vcmpequw_p:
5489 case Intrinsic::ppc_altivec_vcmpgefp_p:
5490 case Intrinsic::ppc_altivec_vcmpgtfp_p:
5491 case Intrinsic::ppc_altivec_vcmpgtsb_p:
5492 case Intrinsic::ppc_altivec_vcmpgtsh_p:
5493 case Intrinsic::ppc_altivec_vcmpgtsw_p:
5494 case Intrinsic::ppc_altivec_vcmpgtub_p:
5495 case Intrinsic::ppc_altivec_vcmpgtuh_p:
5496 case Intrinsic::ppc_altivec_vcmpgtuw_p:
5497 KnownZero = ~1U; // All bits but the low one are known to be zero.
5498 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005499 }
Chris Lattnerbbe77de2006-04-02 06:26:07 +00005500 }
5501 }
5502}
5503
5504
Chris Lattner4234f572007-03-25 02:14:49 +00005505/// getConstraintType - Given a constraint, return the type of
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005506/// constraint it is for this target.
Scott Michelfdc40a02009-02-17 22:15:04 +00005507PPCTargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005508PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
5509 if (Constraint.size() == 1) {
5510 switch (Constraint[0]) {
5511 default: break;
5512 case 'b':
5513 case 'r':
5514 case 'f':
5515 case 'v':
5516 case 'y':
5517 return C_RegisterClass;
5518 }
5519 }
5520 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00005521}
5522
John Thompson44ab89e2010-10-29 17:29:13 +00005523/// Examine constraint type and operand type and determine a weight value.
5524/// This object must already have been set up with the operand type
5525/// and the current alternative constraint selected.
5526TargetLowering::ConstraintWeight
5527PPCTargetLowering::getSingleConstraintMatchWeight(
5528 AsmOperandInfo &info, const char *constraint) const {
5529 ConstraintWeight weight = CW_Invalid;
5530 Value *CallOperandVal = info.CallOperandVal;
5531 // If we don't have a value, we can't do a match,
5532 // but allow it at the lowest weight.
5533 if (CallOperandVal == NULL)
5534 return CW_Default;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005535 Type *type = CallOperandVal->getType();
John Thompson44ab89e2010-10-29 17:29:13 +00005536 // Look at the constraint type.
5537 switch (*constraint) {
5538 default:
5539 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
5540 break;
5541 case 'b':
5542 if (type->isIntegerTy())
5543 weight = CW_Register;
5544 break;
5545 case 'f':
5546 if (type->isFloatTy())
5547 weight = CW_Register;
5548 break;
5549 case 'd':
5550 if (type->isDoubleTy())
5551 weight = CW_Register;
5552 break;
5553 case 'v':
5554 if (type->isVectorTy())
5555 weight = CW_Register;
5556 break;
5557 case 'y':
5558 weight = CW_Register;
5559 break;
5560 }
5561 return weight;
5562}
5563
Scott Michelfdc40a02009-02-17 22:15:04 +00005564std::pair<unsigned, const TargetRegisterClass*>
Chris Lattner331d1bc2006-11-02 01:44:04 +00005565PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00005566 EVT VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00005567 if (Constraint.size() == 1) {
Chris Lattner331d1bc2006-11-02 01:44:04 +00005568 // GCC RS6000 Constraint Letters
5569 switch (Constraint[0]) {
5570 case 'b': // R1-R31
5571 case 'r': // R0-R31
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 if (VT == MVT::i64 && PPCSubTarget.isPPC64())
Chris Lattner331d1bc2006-11-02 01:44:04 +00005573 return std::make_pair(0U, PPC::G8RCRegisterClass);
5574 return std::make_pair(0U, PPC::GPRCRegisterClass);
5575 case 'f':
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 if (VT == MVT::f32)
Chris Lattner331d1bc2006-11-02 01:44:04 +00005577 return std::make_pair(0U, PPC::F4RCRegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00005578 else if (VT == MVT::f64)
Chris Lattner331d1bc2006-11-02 01:44:04 +00005579 return std::make_pair(0U, PPC::F8RCRegisterClass);
5580 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005581 case 'v':
Chris Lattner331d1bc2006-11-02 01:44:04 +00005582 return std::make_pair(0U, PPC::VRRCRegisterClass);
5583 case 'y': // crrc
5584 return std::make_pair(0U, PPC::CRRCRegisterClass);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005585 }
5586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005587
Chris Lattner331d1bc2006-11-02 01:44:04 +00005588 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattnerddc787d2006-01-31 19:20:21 +00005589}
Chris Lattner763317d2006-02-07 00:47:13 +00005590
Chris Lattner331d1bc2006-11-02 01:44:04 +00005591
Chris Lattner48884cd2007-08-25 00:47:38 +00005592/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
Dale Johannesen1784d162010-06-25 21:55:36 +00005593/// vector. If it is invalid, don't add anything to Ops.
Eric Christopher471e4222011-06-08 23:55:35 +00005594void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopher100c8332011-06-02 23:16:42 +00005595 std::string &Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00005596 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00005597 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00005598 SDValue Result(0,0);
Eric Christopher471e4222011-06-08 23:55:35 +00005599
Eric Christopher100c8332011-06-02 23:16:42 +00005600 // Only support length 1 constraints.
5601 if (Constraint.length() > 1) return;
Eric Christopher471e4222011-06-08 23:55:35 +00005602
Eric Christopher100c8332011-06-02 23:16:42 +00005603 char Letter = Constraint[0];
Chris Lattner763317d2006-02-07 00:47:13 +00005604 switch (Letter) {
5605 default: break;
5606 case 'I':
5607 case 'J':
5608 case 'K':
5609 case 'L':
5610 case 'M':
5611 case 'N':
5612 case 'O':
5613 case 'P': {
Chris Lattner9f5d5782007-05-15 01:31:05 +00005614 ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
Chris Lattner48884cd2007-08-25 00:47:38 +00005615 if (!CST) return; // Must be an immediate to match.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005616 unsigned Value = CST->getZExtValue();
Chris Lattner763317d2006-02-07 00:47:13 +00005617 switch (Letter) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005618 default: llvm_unreachable("Unknown constraint letter!");
Chris Lattner763317d2006-02-07 00:47:13 +00005619 case 'I': // "I" is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005620 if ((short)Value == (int)Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005621 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005622 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005623 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
5624 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005625 if ((short)Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005626 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005627 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005628 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005629 if ((Value >> 16) == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005630 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005631 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005632 case 'M': // "M" is a constant that is greater than 31.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005633 if (Value > 31)
Chris Lattner48884cd2007-08-25 00:47:38 +00005634 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005635 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005636 case 'N': // "N" is a positive constant that is an exact power of two.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005637 if ((int)Value > 0 && isPowerOf2_32(Value))
Chris Lattner48884cd2007-08-25 00:47:38 +00005638 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005639 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005640 case 'O': // "O" is the constant zero.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005641 if (Value == 0)
Chris Lattner48884cd2007-08-25 00:47:38 +00005642 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005643 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005644 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
Chris Lattner9f5d5782007-05-15 01:31:05 +00005645 if ((short)-Value == (int)-Value)
Chris Lattner48884cd2007-08-25 00:47:38 +00005646 Result = DAG.getTargetConstant(Value, Op.getValueType());
Chris Lattnerdba1aee2006-10-31 19:40:43 +00005647 break;
Chris Lattner763317d2006-02-07 00:47:13 +00005648 }
5649 break;
5650 }
5651 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005652
Gabor Greifba36cb52008-08-28 21:40:38 +00005653 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005654 Ops.push_back(Result);
5655 return;
5656 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005657
Chris Lattner763317d2006-02-07 00:47:13 +00005658 // Handle standard constraint letters.
Eric Christopher100c8332011-06-02 23:16:42 +00005659 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner763317d2006-02-07 00:47:13 +00005660}
Evan Chengc4c62572006-03-13 23:20:37 +00005661
Chris Lattnerc9addb72007-03-30 23:15:24 +00005662// isLegalAddressingMode - Return true if the addressing mode represented
5663// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00005664bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005665 Type *Ty) const {
Chris Lattnerc9addb72007-03-30 23:15:24 +00005666 // FIXME: PPC does not allow r+i addressing modes for vectors!
Scott Michelfdc40a02009-02-17 22:15:04 +00005667
Chris Lattnerc9addb72007-03-30 23:15:24 +00005668 // PPC allows a sign-extended 16-bit immediate field.
5669 if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
5670 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005671
Chris Lattnerc9addb72007-03-30 23:15:24 +00005672 // No global is ever allowed as a base.
5673 if (AM.BaseGV)
5674 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005675
5676 // PPC only support r+r,
Chris Lattnerc9addb72007-03-30 23:15:24 +00005677 switch (AM.Scale) {
5678 case 0: // "r+i" or just "i", depending on HasBaseReg.
5679 break;
5680 case 1:
5681 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
5682 return false;
5683 // Otherwise we have r+r or r+i.
5684 break;
5685 case 2:
5686 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
5687 return false;
5688 // Allow 2*r as r+r.
5689 break;
Chris Lattner7c7ba9d2007-04-09 22:10:05 +00005690 default:
5691 // No other scales are supported.
5692 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00005693 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005694
Chris Lattnerc9addb72007-03-30 23:15:24 +00005695 return true;
5696}
5697
Evan Chengc4c62572006-03-13 23:20:37 +00005698/// isLegalAddressImmediate - Return true if the integer value can be used
Evan Cheng86193912007-03-12 23:29:01 +00005699/// as the offset of the target addressing mode for load / store of the
5700/// given type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005701bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,Type *Ty) const{
Evan Chengc4c62572006-03-13 23:20:37 +00005702 // PPC allows a sign-extended 16-bit immediate field.
5703 return (V > -(1 << 16) && V < (1 << 16)-1);
5704}
Reid Spencer3a9ec242006-08-28 01:02:49 +00005705
5706bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
Scott Michelfdc40a02009-02-17 22:15:04 +00005707 return false;
Reid Spencer3a9ec242006-08-28 01:02:49 +00005708}
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005709
Dan Gohmand858e902010-04-17 15:26:15 +00005710SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op,
5711 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00005712 MachineFunction &MF = DAG.getMachineFunction();
5713 MachineFrameInfo *MFI = MF.getFrameInfo();
5714 MFI->setReturnAddressIsTaken(true);
5715
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005716 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen08673d22010-05-03 22:59:34 +00005717 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Chris Lattner3fc027d2007-12-08 06:59:59 +00005718
Dale Johannesen08673d22010-05-03 22:59:34 +00005719 // Make sure the function does not optimize away the store of the RA to
5720 // the stack.
Chris Lattner3fc027d2007-12-08 06:59:59 +00005721 PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
Dale Johannesen08673d22010-05-03 22:59:34 +00005722 FuncInfo->setLRStoreRequired();
5723 bool isPPC64 = PPCSubTarget.isPPC64();
5724 bool isDarwinABI = PPCSubTarget.isDarwinABI();
5725
5726 if (Depth > 0) {
5727 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
5728 SDValue Offset =
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005729
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00005730 DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI),
Dale Johannesen08673d22010-05-03 22:59:34 +00005731 isPPC64? MVT::i64 : MVT::i32);
5732 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
5733 DAG.getNode(ISD::ADD, dl, getPointerTy(),
5734 FrameAddr, Offset),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005735 MachinePointerInfo(), false, false, false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00005736 }
Chris Lattner3fc027d2007-12-08 06:59:59 +00005737
Chris Lattner3fc027d2007-12-08 06:59:59 +00005738 // Just load the return address off the stack.
Dan Gohman475871a2008-07-27 21:46:04 +00005739 SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
Dale Johannesen08673d22010-05-03 22:59:34 +00005740 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005741 RetAddrFI, MachinePointerInfo(), false, false, false, 0);
Chris Lattner3fc027d2007-12-08 06:59:59 +00005742}
5743
Dan Gohmand858e902010-04-17 15:26:15 +00005744SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op,
5745 SelectionDAG &DAG) const {
Dale Johannesena05dca42009-02-04 23:02:30 +00005746 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen08673d22010-05-03 22:59:34 +00005747 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005748
Owen Andersone50ed302009-08-10 22:56:29 +00005749 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 bool isPPC64 = PtrVT == MVT::i64;
Scott Michelfdc40a02009-02-17 22:15:04 +00005751
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005752 MachineFunction &MF = DAG.getMachineFunction();
5753 MachineFrameInfo *MFI = MF.getFrameInfo();
Dale Johannesen08673d22010-05-03 22:59:34 +00005754 MFI->setFrameAddressIsTaken(true);
5755 bool is31 = (DisableFramePointerElim(MF) || MFI->hasVarSizedObjects()) &&
5756 MFI->getStackSize() &&
5757 !MF.getFunction()->hasFnAttr(Attribute::Naked);
5758 unsigned FrameReg = isPPC64 ? (is31 ? PPC::X31 : PPC::X1) :
5759 (is31 ? PPC::R31 : PPC::R1);
5760 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg,
5761 PtrVT);
5762 while (Depth--)
5763 FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005764 FrameAddr, MachinePointerInfo(), false, false,
5765 false, 0);
Dale Johannesen08673d22010-05-03 22:59:34 +00005766 return FrameAddr;
Nicolas Geoffray43c6e7c2007-03-01 13:11:38 +00005767}
Dan Gohman54aeea32008-10-21 03:41:46 +00005768
5769bool
5770PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
5771 // The PowerPC target isn't yet aware of offsets.
5772 return false;
5773}
Tilmann Schellerffd02002009-07-03 06:45:56 +00005774
Evan Cheng42642d02010-04-01 20:10:42 +00005775/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengf28f8bc2010-04-02 19:36:14 +00005776/// and store operations as a result of memset, memcpy, and memmove
5777/// lowering. If DstAlign is zero that means it's safe to destination
5778/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
5779/// means there isn't a need to check it against alignment requirement,
5780/// probably because the source does not need to be loaded. If
Lang Hames15701f82011-10-26 23:50:43 +00005781/// 'IsZeroVal' is true, that means it's safe to return a
Evan Chengf28f8bc2010-04-02 19:36:14 +00005782/// non-scalar-integer type, e.g. empty string source, constant, or loaded
Evan Chengc3b0c342010-04-08 07:37:57 +00005783/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
5784/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00005785/// It returns EVT::Other if the type should be determined using generic
5786/// target-independent logic.
Evan Cheng255f20f2010-04-01 06:04:33 +00005787EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size,
5788 unsigned DstAlign, unsigned SrcAlign,
Lang Hames15701f82011-10-26 23:50:43 +00005789 bool IsZeroVal,
Evan Chengc3b0c342010-04-08 07:37:57 +00005790 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00005791 MachineFunction &MF) const {
Tilmann Schellerffd02002009-07-03 06:45:56 +00005792 if (this->PPCSubTarget.isPPC64()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005793 return MVT::i64;
Tilmann Schellerffd02002009-07-03 06:45:56 +00005794 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00005795 return MVT::i32;
Tilmann Schellerffd02002009-07-03 06:45:56 +00005796 }
5797}