blob: ae48574055f55352aad5532da01a90ca98c4ebdb [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
15#include "PPCTargetMachine.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000016#include "llvm/ADT/VectorExtras.h"
Evan Chengc4c62572006-03-13 23:20:37 +000017#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000021#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000022#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000023#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000024#include "llvm/Function.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000025#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000027using namespace llvm;
28
Nate Begeman21e463b2005-10-16 05:39:50 +000029PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000030 : TargetLowering(TM) {
31
32 // Fold away setcc operations if possible.
33 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000034 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000035
Chris Lattnerd145a612005-09-27 22:18:25 +000036 // Use _setjmp/_longjmp instead of setjmp/longjmp.
37 setUseUnderscoreSetJmpLongJmp(true);
38
Chris Lattner7c5a3d32005-08-16 17:14:42 +000039 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000040 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
41 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
42 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000043
Chris Lattnera54aa942006-01-29 06:26:08 +000044 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
45 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
46
Chris Lattner7c5a3d32005-08-16 17:14:42 +000047 // PowerPC has no intrinsics for these particular operations
48 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
49 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
50 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
51
52 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
53 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
54 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
55
56 // PowerPC has no SREM/UREM instructions
57 setOperationAction(ISD::SREM, MVT::i32, Expand);
58 setOperationAction(ISD::UREM, MVT::i32, Expand);
59
60 // We don't support sin/cos/sqrt/fmod
61 setOperationAction(ISD::FSIN , MVT::f64, Expand);
62 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000063 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000064 setOperationAction(ISD::FSIN , MVT::f32, Expand);
65 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000066 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000067
68 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000069 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000070 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
71 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
72 }
73
Chris Lattner9601a862006-03-05 05:08:37 +000074 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
75 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
76
Nate Begemand88fc032006-01-14 03:14:10 +000077 // PowerPC does not have BSWAP, CTPOP or CTTZ
78 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000079 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
80 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
81
Nate Begeman35ef9132006-01-11 21:21:00 +000082 // PowerPC does not have ROTR
83 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
84
Chris Lattner7c5a3d32005-08-16 17:14:42 +000085 // PowerPC does not have Select
86 setOperationAction(ISD::SELECT, MVT::i32, Expand);
87 setOperationAction(ISD::SELECT, MVT::f32, Expand);
88 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000089
Chris Lattner0b1e4e52005-08-26 17:36:52 +000090 // PowerPC wants to turn select_cc of FP into fsel when possible.
91 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
92 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000093
Nate Begeman750ac1b2006-02-01 07:19:44 +000094 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000095 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000096
Nate Begeman81e80972006-03-17 01:40:33 +000097 // PowerPC does not have BRCOND which requires SetCC
98 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000099
Chris Lattnerf7605322005-08-31 21:09:52 +0000100 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
101 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000102
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000103 // PowerPC does not have [U|S]INT_TO_FP
104 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
105 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
106
Chris Lattner53e88452005-12-23 05:13:35 +0000107 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
108 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
109
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000110 // PowerPC does not have truncstore for i1.
111 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000112
Jim Laskeyabf6d172006-01-05 01:25:28 +0000113 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000114 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000115 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000116 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000117 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000118 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000119
Nate Begeman28a6b022005-12-10 02:36:00 +0000120 // We want to legalize GlobalAddress and ConstantPool nodes into the
121 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000122 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000123 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000124
Nate Begemanee625572006-01-27 21:09:22 +0000125 // RET must be custom lowered, to meet ABI requirements
126 setOperationAction(ISD::RET , MVT::Other, Custom);
127
Nate Begemanacc398c2006-01-25 18:21:52 +0000128 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
129 setOperationAction(ISD::VASTART , MVT::Other, Custom);
130
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000131 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000132 setOperationAction(ISD::VAARG , MVT::Other, Expand);
133 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
134 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000135 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
136 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
137 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000138
Nate Begemanc09eeec2005-09-06 22:03:27 +0000139 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000140 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000141 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
142 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner7fbcef72006-03-24 07:53:47 +0000143
144 // FIXME: disable this lowered code. This generates 64-bit register values,
145 // and we don't model the fact that the top part is clobbered by calls. We
146 // need to flag these together so that the value isn't live across a call.
147 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
148
Nate Begemanae749a92005-10-25 23:48:36 +0000149 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
150 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
151 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000152 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000153 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000154 }
155
156 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
157 // 64 bit PowerPC implementations can support i64 types directly
158 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000159 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
160 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000161 } else {
162 // 32 bit PowerPC wants to expand i64 shifts itself.
163 setOperationAction(ISD::SHL, MVT::i64, Custom);
164 setOperationAction(ISD::SRL, MVT::i64, Custom);
165 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000166 }
167
Evan Chengd30bf012006-03-01 01:11:20 +0000168 // First set operation action for all vector types to expand. Then we
169 // will selectively turn on ones that can be effectively codegen'd.
170 for (unsigned VT = (unsigned)MVT::Vector + 1;
171 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
172 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
173 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
174 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
175 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000176 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000177 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000178 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000179 }
180
Nate Begeman425a9692005-11-29 08:17:20 +0000181 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000182 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000183 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000184
Evan Chengd30bf012006-03-01 01:11:20 +0000185 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
186 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
187 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
188 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
189 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
190 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000191 setOperationAction(ISD::LOAD , MVT::v16i8, Legal);
192
193 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
194 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
195
Chris Lattnerb2177b92006-03-19 06:55:52 +0000196 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
197 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000198
199 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
200 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000201 }
202
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000203 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000204 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000205
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000206 // We have target-specific dag combine patterns for the following nodes:
207 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000208 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000209
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000210 computeRegisterProperties();
211}
212
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000213const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
214 switch (Opcode) {
215 default: return 0;
216 case PPCISD::FSEL: return "PPCISD::FSEL";
217 case PPCISD::FCFID: return "PPCISD::FCFID";
218 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
219 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000220 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000221 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
222 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerb2177b92006-03-19 06:55:52 +0000223 case PPCISD::LVE_X: return "PPCISD::LVE_X";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000224 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000225 case PPCISD::Hi: return "PPCISD::Hi";
226 case PPCISD::Lo: return "PPCISD::Lo";
227 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
228 case PPCISD::SRL: return "PPCISD::SRL";
229 case PPCISD::SRA: return "PPCISD::SRA";
230 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000231 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
232 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000233 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000234 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
235 }
236}
237
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000238/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
239static bool isFloatingPointZero(SDOperand Op) {
240 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
241 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
242 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
243 // Maybe this has already been legalized into the constant pool?
244 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
245 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
246 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
247 }
248 return false;
249}
250
Chris Lattneref819f82006-03-20 06:33:01 +0000251
252/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
253/// specifies a splat of a single element that is suitable for input to
254/// VSPLTB/VSPLTH/VSPLTW.
255bool PPC::isSplatShuffleMask(SDNode *N) {
256 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000257
258 // We can only splat 8-bit, 16-bit, and 32-bit quantities.
259 if (N->getNumOperands() != 4 && N->getNumOperands() != 8 &&
260 N->getNumOperands() != 16)
261 return false;
262
Chris Lattner88a99ef2006-03-20 06:37:44 +0000263 // This is a splat operation if each element of the permute is the same, and
264 // if the value doesn't reference the second vector.
265 SDOperand Elt = N->getOperand(0);
266 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
267 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
268 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
269 "Invalid VECTOR_SHUFFLE mask!");
270 if (N->getOperand(i) != Elt) return false;
271 }
272
273 // Make sure it is a splat of the first vector operand.
274 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
Chris Lattneref819f82006-03-20 06:33:01 +0000275}
276
277/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
278/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
279unsigned PPC::getVSPLTImmediate(SDNode *N) {
280 assert(isSplatShuffleMask(N));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000281 return cast<ConstantSDNode>(N->getOperand(0))->getValue();
Chris Lattneref819f82006-03-20 06:33:01 +0000282}
283
Chris Lattner64b3a082006-03-24 07:48:08 +0000284/// isZeroVector - Return true if this build_vector is an all-zero vector.
285///
286bool PPC::isZeroVector(SDNode *N) {
287 if (MVT::isInteger(N->getOperand(0).getValueType())) {
288 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
289 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
290 cast<ConstantSDNode>(N->getOperand(i))->getValue() != 0)
291 return false;
292 } else {
293 assert(MVT::isFloatingPoint(N->getOperand(0).getValueType()) &&
294 "Vector of non-int, non-float values?");
295 // See if this is all zeros.
296 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
297 if (!isa<ConstantFPSDNode>(N->getOperand(i)) ||
298 !cast<ConstantFPSDNode>(N->getOperand(i))->isExactlyValue(0.0))
299 return false;
300 }
301 return true;
302}
303
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000304/// isVecSplatImm - Return true if this is a build_vector of constants which
305/// can be formed by using a vspltis[bhw] instruction. The ByteSize field
306/// indicates the number of bytes of each element [124] -> [bhw].
307bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) {
308 SDOperand OpVal(0, 0);
309 // Check to see if this buildvec has a single non-undef value in its elements.
310 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
311 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
312 if (OpVal.Val == 0)
313 OpVal = N->getOperand(i);
314 else if (OpVal != N->getOperand(i))
315 return false;
316 }
317
318 if (OpVal.Val == 0) return false; // All UNDEF: use implicit def.
319
320 unsigned ValSizeInBytes;
321 uint64_t Value;
322 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
323 Value = CN->getValue();
324 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
325 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
326 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
327 Value = FloatToBits(CN->getValue());
328 ValSizeInBytes = 4;
329 }
330
331 // If the splat value is larger than the element value, then we can never do
332 // this splat. The only case that we could fit the replicated bits into our
333 // immediate field for would be zero, and we prefer to use vxor for it.
334 if (ValSizeInBytes < ByteSize) return false;
335
336 // If the element value is larger than the splat value, cut it in half and
337 // check to see if the two halves are equal. Continue doing this until we
338 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
339 while (ValSizeInBytes > ByteSize) {
340 ValSizeInBytes >>= 1;
341
342 // If the top half equals the bottom half, we're still ok.
343 if (((Value >> (ValSizeInBytes*8)) & ((8 << ValSizeInBytes)-1)) !=
344 (Value & ((8 << ValSizeInBytes)-1)))
345 return false;
346 }
347
348 // Properly sign extend the value.
349 int ShAmt = (4-ByteSize)*8;
350 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
351
352 // If this is zero, don't match, zero matches isZeroVector.
353 if (MaskVal == 0) return false;
354
355 if (Val) *Val = MaskVal;
356
357 // Finally, if this value fits in a 5 bit sext field, return true.
358 return ((MaskVal << (32-5)) >> (32-5)) == MaskVal;
359}
360
Chris Lattneref819f82006-03-20 06:33:01 +0000361
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000362/// LowerOperation - Provide custom lowering hooks for some operations.
363///
Nate Begeman21e463b2005-10-16 05:39:50 +0000364SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000365 switch (Op.getOpcode()) {
366 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000367 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000368 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000369 SDOperand Src = Op.getOperand(0);
370 if (Src.getValueType() == MVT::f32)
371 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
372
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000373 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000374 switch (Op.getValueType()) {
375 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
376 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000377 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000378 break;
379 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000380 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000381 break;
382 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000383
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000384 // Convert the FP value to an int value through memory.
385 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
386 if (Op.getValueType() == MVT::i32)
387 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
388 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000389 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000390 case ISD::SINT_TO_FP:
391 if (Op.getOperand(0).getValueType() == MVT::i64) {
392 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
393 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
394 if (Op.getValueType() == MVT::f32)
395 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
396 return FP;
397 } else {
398 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
399 "Unhandled SINT_TO_FP type in custom expander!");
400 // Since we only generate this in 64-bit mode, we can take advantage of
401 // 64-bit registers. In particular, sign extend the input value into the
402 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
403 // then lfd it and fcfid it.
404 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
405 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
406 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
407
408 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
409 Op.getOperand(0));
410
411 // STD the extended value into the stack slot.
412 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
413 DAG.getEntryNode(), Ext64, FIdx,
414 DAG.getSrcValue(NULL));
415 // Load the value as a double.
416 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
417
418 // FCFID it and return it.
419 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
420 if (Op.getValueType() == MVT::f32)
421 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
422 return FP;
423 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000424 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000425
Chris Lattnerf7605322005-08-31 21:09:52 +0000426 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000427 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000428 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
429 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
430 break;
431
432 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
433
434 // Cannot handle SETEQ/SETNE.
435 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
436
437 MVT::ValueType ResVT = Op.getValueType();
438 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
439 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
440 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000441
Chris Lattnerf7605322005-08-31 21:09:52 +0000442 // If the RHS of the comparison is a 0.0, we don't need to do the
443 // subtraction at all.
444 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000445 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000446 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000447 case ISD::SETULT:
448 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000449 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000450 case ISD::SETUGE:
451 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000452 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
453 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000454 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000455 case ISD::SETUGT:
456 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000457 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000458 case ISD::SETULE:
459 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000460 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
461 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000462 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000463 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000464 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000465
Chris Lattnereb255f22005-10-25 20:54:57 +0000466 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000467 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000468 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000469 case ISD::SETULT:
470 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000471 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
472 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
473 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
474 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000475 case ISD::SETUGE:
476 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000477 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
478 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
479 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
480 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000481 case ISD::SETUGT:
482 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000483 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
484 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
485 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
486 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000487 case ISD::SETULE:
488 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000489 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
490 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
491 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
492 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000493 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000494 break;
495 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000496 case ISD::SHL: {
497 assert(Op.getValueType() == MVT::i64 &&
498 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
499 // The generic code does a fine job expanding shift by a constant.
500 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
501
502 // Otherwise, expand into a bunch of logical ops. Note that these ops
503 // depend on the PPC behavior for oversized shift amounts.
504 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
505 DAG.getConstant(0, MVT::i32));
506 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
507 DAG.getConstant(1, MVT::i32));
508 SDOperand Amt = Op.getOperand(1);
509
510 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
511 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000512 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
513 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000514 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
515 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
516 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000517 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000518 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000519 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000520 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
521 }
522 case ISD::SRL: {
523 assert(Op.getValueType() == MVT::i64 &&
524 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
525 // The generic code does a fine job expanding shift by a constant.
526 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
527
528 // Otherwise, expand into a bunch of logical ops. Note that these ops
529 // depend on the PPC behavior for oversized shift amounts.
530 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
531 DAG.getConstant(0, MVT::i32));
532 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
533 DAG.getConstant(1, MVT::i32));
534 SDOperand Amt = Op.getOperand(1);
535
536 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
537 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000538 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
539 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000540 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
541 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
542 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000543 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000544 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000545 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000546 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
547 }
548 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000549 assert(Op.getValueType() == MVT::i64 &&
550 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
551 // The generic code does a fine job expanding shift by a constant.
552 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
553
554 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
555 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
556 DAG.getConstant(0, MVT::i32));
557 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
558 DAG.getConstant(1, MVT::i32));
559 SDOperand Amt = Op.getOperand(1);
560
561 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
562 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000563 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
564 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000565 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
566 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
567 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000568 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
569 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000570 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
571 Tmp4, Tmp6, ISD::SETLE);
572 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000573 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000574 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000575 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
576 Constant *C = CP->get();
577 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000578 SDOperand Zero = DAG.getConstant(0, MVT::i32);
579
Evan Cheng4c1aa862006-02-22 20:19:42 +0000580 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000581 // Generate non-pic code that has direct accesses to the constant pool.
582 // The address of the global is just (hi(&g)+lo(&g)).
583 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
584 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
585 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
586 }
587
588 // Only lower ConstantPool on Darwin.
589 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
590 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000591 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000592 // With PIC, the first instruction is actually "GR+hi(&G)".
593 Hi = DAG.getNode(ISD::ADD, MVT::i32,
594 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
595 }
596
597 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
598 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
599 return Lo;
600 }
Chris Lattner860e8862005-11-17 07:30:41 +0000601 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000602 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
603 GlobalValue *GV = GSDN->getGlobal();
604 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000605 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000606
Evan Cheng4c1aa862006-02-22 20:19:42 +0000607 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000608 // Generate non-pic code that has direct accesses to globals.
609 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000610 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
611 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
612 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
613 }
Chris Lattner860e8862005-11-17 07:30:41 +0000614
Chris Lattner1d05cb42005-11-17 18:55:48 +0000615 // Only lower GlobalAddress on Darwin.
616 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000617
Chris Lattner860e8862005-11-17 07:30:41 +0000618 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000619 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000620 // With PIC, the first instruction is actually "GR+hi(&G)".
621 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000622 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000623 }
624
625 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
626 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
627
Chris Lattner37dd6f12006-01-29 20:49:17 +0000628 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
629 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000630 return Lo;
631
632 // If the global is weak or external, we have to go through the lazy
633 // resolution stub.
634 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
635 }
Nate Begeman44775902006-01-31 08:17:29 +0000636 case ISD::SETCC: {
637 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000638
639 // If we're comparing for equality to zero, expose the fact that this is
640 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
641 // fold the new nodes.
642 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
643 if (C->isNullValue() && CC == ISD::SETEQ) {
644 MVT::ValueType VT = Op.getOperand(0).getValueType();
645 SDOperand Zext = Op.getOperand(0);
646 if (VT < MVT::i32) {
647 VT = MVT::i32;
648 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
649 }
650 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
651 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
652 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
653 DAG.getConstant(Log2b, getShiftAmountTy()));
654 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
655 }
656 // Leave comparisons against 0 and -1 alone for now, since they're usually
657 // optimized. FIXME: revisit this when we can custom lower all setcc
658 // optimizations.
659 if (C->isAllOnesValue() || C->isNullValue())
660 break;
661 }
662
663 // If we have an integer seteq/setne, turn it into a compare against zero
664 // by subtracting the rhs from the lhs, which is faster than setting a
665 // condition register, reading it back out, and masking the correct bit.
666 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
667 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
668 MVT::ValueType VT = Op.getValueType();
669 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
670 Op.getOperand(1));
671 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
672 }
Nate Begeman44775902006-01-31 08:17:29 +0000673 break;
674 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000675 case ISD::VASTART: {
676 // vastart just stores the address of the VarArgsFrameIndex slot into the
677 // memory location argument.
678 // FIXME: Replace MVT::i32 with PointerTy
679 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
680 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
681 Op.getOperand(1), Op.getOperand(2));
682 }
Nate Begemanee625572006-01-27 21:09:22 +0000683 case ISD::RET: {
684 SDOperand Copy;
685
686 switch(Op.getNumOperands()) {
687 default:
688 assert(0 && "Do not know how to return this many arguments!");
689 abort();
690 case 1:
691 return SDOperand(); // ret void is legal
692 case 2: {
693 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
694 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
695 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
696 SDOperand());
697 break;
698 }
699 case 3:
700 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
701 SDOperand());
702 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
703 break;
704 }
705 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
706 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000707 case ISD::SCALAR_TO_VECTOR: {
708 // Create a stack slot that is 16-byte aligned.
709 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
710 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
711 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
712
713 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000714 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
715 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner23baa1b2006-03-20 22:37:23 +0000716 // LVE_X it out.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000717 return DAG.getNode(PPCISD::LVE_X, Op.getValueType(), Store, FIdx,
718 DAG.getSrcValue(NULL));
719 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000720 case ISD::BUILD_VECTOR:
721 // If this is a case we can't handle, return null and let the default
722 // expansion code take care of it. If we CAN select this case, return Op.
723
724 // See if this is all zeros.
725 // FIXME: We should handle splat(-0.0), and other cases here.
726 if (PPC::isZeroVector(Op.Val))
727 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000728
729 if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb
730 PPC::isVecSplatImm(Op.Val, 2) || // vspltish
731 PPC::isVecSplatImm(Op.Val, 4)) // vspltisw
732 return Op;
733
Chris Lattner64b3a082006-03-24 07:48:08 +0000734 return SDOperand();
735
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000736 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000737 SDOperand V1 = Op.getOperand(0);
738 SDOperand V2 = Op.getOperand(1);
739 SDOperand PermMask = Op.getOperand(2);
740
741 // Cases that are handled by instructions that take permute immediates
742 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
743 // selected by the instruction selector.
744 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
745 break;
746
747 // TODO: Handle more cases, and also handle cases that are cheaper to do as
748 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000749
750 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
751 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000752 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000753
754 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
755 // that it is in input element units, not in bytes. Convert now.
756 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
757 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
758
759 std::vector<SDOperand> ResultMask;
760 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
761 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
762
763 for (unsigned j = 0; j != BytesPerElement; ++j)
764 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
765 MVT::i8));
766 }
767
768 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
769 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
770 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000771 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000772 return SDOperand();
773}
774
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000775std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000776PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000777 //
778 // add beautiful description of PPC stack frame format, or at least some docs
779 //
780 MachineFunction &MF = DAG.getMachineFunction();
781 MachineFrameInfo *MFI = MF.getFrameInfo();
782 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000783 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000784 std::vector<SDOperand> ArgValues;
785
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000786 unsigned ArgOffset = 24;
787 unsigned GPR_remaining = 8;
788 unsigned FPR_remaining = 13;
789 unsigned GPR_idx = 0, FPR_idx = 0;
790 static const unsigned GPR[] = {
791 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
792 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
793 };
794 static const unsigned FPR[] = {
795 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
796 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
797 };
798
799 // Add DAG nodes to load the arguments... On entry to a function on PPC,
800 // the arguments start at offset 24, although they are likely to be passed
801 // in registers.
802 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
803 SDOperand newroot, argt;
804 unsigned ObjSize;
805 bool needsLoad = false;
806 bool ArgLive = !I->use_empty();
807 MVT::ValueType ObjectVT = getValueType(I->getType());
808
809 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000810 default: assert(0 && "Unhandled argument type!");
811 case MVT::i1:
812 case MVT::i8:
813 case MVT::i16:
814 case MVT::i32:
815 ObjSize = 4;
816 if (!ArgLive) break;
817 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000818 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000819 MF.addLiveIn(GPR[GPR_idx], VReg);
820 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000821 if (ObjectVT != MVT::i32) {
822 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
823 : ISD::AssertZext;
824 argt = DAG.getNode(AssertOp, MVT::i32, argt,
825 DAG.getValueType(ObjectVT));
826 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
827 }
Chris Lattner915fb302005-08-30 00:19:00 +0000828 } else {
829 needsLoad = true;
830 }
831 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000832 case MVT::i64:
833 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000834 if (!ArgLive) break;
835 if (GPR_remaining > 0) {
836 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000837 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000838 MF.addLiveIn(GPR[GPR_idx], VReg);
839 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000840 // If we have two or more remaining argument registers, then both halves
841 // of the i64 can be sourced from there. Otherwise, the lower half will
842 // have to come off the stack. This can happen when an i64 is preceded
843 // by 28 bytes of arguments.
844 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000845 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000846 MF.addLiveIn(GPR[GPR_idx+1], VReg);
847 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000848 } else {
849 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
850 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
851 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
852 DAG.getSrcValue(NULL));
853 }
854 // Build the outgoing arg thingy
855 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
856 newroot = argLo;
857 } else {
858 needsLoad = true;
859 }
860 break;
861 case MVT::f32:
862 case MVT::f64:
863 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000864 if (!ArgLive) {
865 if (FPR_remaining > 0) {
866 --FPR_remaining;
867 ++FPR_idx;
868 }
869 break;
870 }
Chris Lattner915fb302005-08-30 00:19:00 +0000871 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000872 unsigned VReg;
873 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000874 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000875 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000876 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000877 MF.addLiveIn(FPR[FPR_idx], VReg);
878 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000879 --FPR_remaining;
880 ++FPR_idx;
881 } else {
882 needsLoad = true;
883 }
884 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000885 }
886
887 // We need to load the argument to a virtual register if we determined above
888 // that we ran out of physical registers of the appropriate type
889 if (needsLoad) {
890 unsigned SubregOffset = 0;
891 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
892 if (ObjectVT == MVT::i16) SubregOffset = 2;
893 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
894 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
895 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
896 DAG.getConstant(SubregOffset, MVT::i32));
897 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
898 DAG.getSrcValue(NULL));
899 }
900
901 // Every 4 bytes of argument space consumes one of the GPRs available for
902 // argument passing.
903 if (GPR_remaining > 0) {
904 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
905 GPR_remaining -= delta;
906 GPR_idx += delta;
907 }
908 ArgOffset += ObjSize;
909 if (newroot.Val)
910 DAG.setRoot(newroot.getValue(1));
911
912 ArgValues.push_back(argt);
913 }
914
915 // If the function takes variable number of arguments, make a frame index for
916 // the start of the first vararg value... for expansion of llvm.va_start.
917 if (F.isVarArg()) {
918 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
919 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
920 // If this function is vararg, store any remaining integer argument regs
921 // to their spots on the stack so that they may be loaded by deferencing the
922 // result of va_next.
923 std::vector<SDOperand> MemOps;
924 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000925 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000926 MF.addLiveIn(GPR[GPR_idx], VReg);
927 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000928 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
929 Val, FIN, DAG.getSrcValue(NULL));
930 MemOps.push_back(Store);
931 // Increment the address by four for the next argument to store
932 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
933 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
934 }
Chris Lattner80720a92005-11-30 20:40:54 +0000935 if (!MemOps.empty()) {
936 MemOps.push_back(DAG.getRoot());
937 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
938 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000939 }
940
941 // Finally, inform the code generator which regs we return values in.
942 switch (getValueType(F.getReturnType())) {
943 default: assert(0 && "Unknown type!");
944 case MVT::isVoid: break;
945 case MVT::i1:
946 case MVT::i8:
947 case MVT::i16:
948 case MVT::i32:
949 MF.addLiveOut(PPC::R3);
950 break;
951 case MVT::i64:
952 MF.addLiveOut(PPC::R3);
953 MF.addLiveOut(PPC::R4);
954 break;
955 case MVT::f32:
956 case MVT::f64:
957 MF.addLiveOut(PPC::F1);
958 break;
959 }
960
961 return ArgValues;
962}
963
964std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000965PPCTargetLowering::LowerCallTo(SDOperand Chain,
966 const Type *RetTy, bool isVarArg,
967 unsigned CallingConv, bool isTailCall,
968 SDOperand Callee, ArgListTy &Args,
969 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000970 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000971 // SelectExpr to use to put the arguments in the appropriate registers.
972 std::vector<SDOperand> args_to_use;
973
974 // Count how many bytes are to be pushed on the stack, including the linkage
975 // area, and parameter passing area.
976 unsigned NumBytes = 24;
977
978 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000979 Chain = DAG.getCALLSEQ_START(Chain,
980 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000981 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000982 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000983 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000984 default: assert(0 && "Unknown value type!");
985 case MVT::i1:
986 case MVT::i8:
987 case MVT::i16:
988 case MVT::i32:
989 case MVT::f32:
990 NumBytes += 4;
991 break;
992 case MVT::i64:
993 case MVT::f64:
994 NumBytes += 8;
995 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000996 }
Chris Lattner915fb302005-08-30 00:19:00 +0000997 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000998
Chris Lattner915fb302005-08-30 00:19:00 +0000999 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1000 // plus 32 bytes of argument space in case any called code gets funky on us.
1001 // (Required by ABI to support var arg)
1002 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001003
1004 // Adjust the stack pointer for the new arguments...
1005 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001006 Chain = DAG.getCALLSEQ_START(Chain,
1007 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001008
1009 // Set up a copy of the stack pointer for use loading and storing any
1010 // arguments that may not fit in the registers available for argument
1011 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001012 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001013
1014 // Figure out which arguments are going to go in registers, and which in
1015 // memory. Also, if this is a vararg function, floating point operations
1016 // must be stored to our stack, and loaded into integer regs as well, if
1017 // any integer regs are available for argument passing.
1018 unsigned ArgOffset = 24;
1019 unsigned GPR_remaining = 8;
1020 unsigned FPR_remaining = 13;
1021
1022 std::vector<SDOperand> MemOps;
1023 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1024 // PtrOff will be used to store the current argument to the stack if a
1025 // register cannot be found for it.
1026 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1027 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1028 MVT::ValueType ArgVT = getValueType(Args[i].second);
1029
1030 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001031 default: assert(0 && "Unexpected ValueType for argument!");
1032 case MVT::i1:
1033 case MVT::i8:
1034 case MVT::i16:
1035 // Promote the integer to 32 bits. If the input type is signed use a
1036 // sign extend, otherwise use a zero extend.
1037 if (Args[i].second->isSigned())
1038 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1039 else
1040 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1041 // FALL THROUGH
1042 case MVT::i32:
1043 if (GPR_remaining > 0) {
1044 args_to_use.push_back(Args[i].first);
1045 --GPR_remaining;
1046 } else {
1047 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1048 Args[i].first, PtrOff,
1049 DAG.getSrcValue(NULL)));
1050 }
1051 ArgOffset += 4;
1052 break;
1053 case MVT::i64:
1054 // If we have one free GPR left, we can place the upper half of the i64
1055 // in it, and store the other half to the stack. If we have two or more
1056 // free GPRs, then we can pass both halves of the i64 in registers.
1057 if (GPR_remaining > 0) {
1058 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1059 Args[i].first, DAG.getConstant(1, MVT::i32));
1060 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1061 Args[i].first, DAG.getConstant(0, MVT::i32));
1062 args_to_use.push_back(Hi);
1063 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001064 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001065 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001066 --GPR_remaining;
1067 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001068 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1069 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001070 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001071 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001072 }
Chris Lattner915fb302005-08-30 00:19:00 +00001073 } else {
1074 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1075 Args[i].first, PtrOff,
1076 DAG.getSrcValue(NULL)));
1077 }
1078 ArgOffset += 8;
1079 break;
1080 case MVT::f32:
1081 case MVT::f64:
1082 if (FPR_remaining > 0) {
1083 args_to_use.push_back(Args[i].first);
1084 --FPR_remaining;
1085 if (isVarArg) {
1086 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1087 Args[i].first, PtrOff,
1088 DAG.getSrcValue(NULL));
1089 MemOps.push_back(Store);
1090 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001091 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001092 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1093 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001094 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001095 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001096 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001097 }
1098 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001099 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1100 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001101 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1102 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001103 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001104 args_to_use.push_back(Load);
1105 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001106 }
1107 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001108 // If we have any FPRs remaining, we may also have GPRs remaining.
1109 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1110 // GPRs.
1111 if (GPR_remaining > 0) {
1112 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1113 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001114 }
Chris Lattner915fb302005-08-30 00:19:00 +00001115 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1116 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1117 --GPR_remaining;
1118 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001119 }
Chris Lattner915fb302005-08-30 00:19:00 +00001120 } else {
1121 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1122 Args[i].first, PtrOff,
1123 DAG.getSrcValue(NULL)));
1124 }
1125 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1126 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001127 }
1128 }
1129 if (!MemOps.empty())
1130 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1131 }
1132
1133 std::vector<MVT::ValueType> RetVals;
1134 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001135 MVT::ValueType ActualRetTyVT = RetTyVT;
1136 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1137 ActualRetTyVT = MVT::i32; // Promote result to i32.
1138
Chris Lattnere00ebf02006-01-28 07:33:03 +00001139 if (RetTyVT == MVT::i64) {
1140 RetVals.push_back(MVT::i32);
1141 RetVals.push_back(MVT::i32);
1142 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001143 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001144 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001145 RetVals.push_back(MVT::Other);
1146
Chris Lattner2823b3e2005-11-17 05:56:14 +00001147 // If the callee is a GlobalAddress node (quite common, every direct call is)
1148 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1149 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1150 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1151
Chris Lattner281b55e2006-01-27 23:34:02 +00001152 std::vector<SDOperand> Ops;
1153 Ops.push_back(Chain);
1154 Ops.push_back(Callee);
1155 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1156 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001157 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001158 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1159 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001160 SDOperand RetVal = TheCall;
1161
1162 // If the result is a small value, add a note so that we keep track of the
1163 // information about whether it is sign or zero extended.
1164 if (RetTyVT != ActualRetTyVT) {
1165 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1166 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1167 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001168 } else if (RetTyVT == MVT::i64) {
1169 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001170 }
1171
1172 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001173}
1174
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001175MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001176PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1177 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001178 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001179 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1180 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001181 "Unexpected instr type to insert");
1182
1183 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1184 // control-flow pattern. The incoming instruction knows the destination vreg
1185 // to set, the condition code register to branch on, the true/false values to
1186 // select between, and a branch opcode to use.
1187 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1188 ilist<MachineBasicBlock>::iterator It = BB;
1189 ++It;
1190
1191 // thisMBB:
1192 // ...
1193 // TrueVal = ...
1194 // cmpTY ccX, r1, r2
1195 // bCC copy1MBB
1196 // fallthrough --> copy0MBB
1197 MachineBasicBlock *thisMBB = BB;
1198 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1199 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1200 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1201 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1202 MachineFunction *F = BB->getParent();
1203 F->getBasicBlockList().insert(It, copy0MBB);
1204 F->getBasicBlockList().insert(It, sinkMBB);
1205 // Update machine-CFG edges
1206 BB->addSuccessor(copy0MBB);
1207 BB->addSuccessor(sinkMBB);
1208
1209 // copy0MBB:
1210 // %FalseValue = ...
1211 // # fallthrough to sinkMBB
1212 BB = copy0MBB;
1213
1214 // Update machine-CFG edges
1215 BB->addSuccessor(sinkMBB);
1216
1217 // sinkMBB:
1218 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1219 // ...
1220 BB = sinkMBB;
1221 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1222 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1223 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1224
1225 delete MI; // The pseudo instruction is gone now.
1226 return BB;
1227}
1228
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001229SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1230 DAGCombinerInfo &DCI) const {
1231 TargetMachine &TM = getTargetMachine();
1232 SelectionDAG &DAG = DCI.DAG;
1233 switch (N->getOpcode()) {
1234 default: break;
1235 case ISD::SINT_TO_FP:
1236 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001237 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1238 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1239 // We allow the src/dst to be either f32/f64, but the intermediate
1240 // type must be i64.
1241 if (N->getOperand(0).getValueType() == MVT::i64) {
1242 SDOperand Val = N->getOperand(0).getOperand(0);
1243 if (Val.getValueType() == MVT::f32) {
1244 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1245 DCI.AddToWorklist(Val.Val);
1246 }
1247
1248 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001249 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001250 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001251 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001252 if (N->getValueType(0) == MVT::f32) {
1253 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1254 DCI.AddToWorklist(Val.Val);
1255 }
1256 return Val;
1257 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1258 // If the intermediate type is i32, we can avoid the load/store here
1259 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001260 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001261 }
1262 }
1263 break;
Chris Lattner51269842006-03-01 05:50:56 +00001264 case ISD::STORE:
1265 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1266 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1267 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1268 N->getOperand(1).getValueType() == MVT::i32) {
1269 SDOperand Val = N->getOperand(1).getOperand(0);
1270 if (Val.getValueType() == MVT::f32) {
1271 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1272 DCI.AddToWorklist(Val.Val);
1273 }
1274 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1275 DCI.AddToWorklist(Val.Val);
1276
1277 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1278 N->getOperand(2), N->getOperand(3));
1279 DCI.AddToWorklist(Val.Val);
1280 return Val;
1281 }
1282 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001283 }
1284
1285 return SDOperand();
1286}
1287
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001288/// getConstraintType - Given a constraint letter, return the type of
1289/// constraint it is for this target.
1290PPCTargetLowering::ConstraintType
1291PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1292 switch (ConstraintLetter) {
1293 default: break;
1294 case 'b':
1295 case 'r':
1296 case 'f':
1297 case 'v':
1298 case 'y':
1299 return C_RegisterClass;
1300 }
1301 return TargetLowering::getConstraintType(ConstraintLetter);
1302}
1303
1304
Chris Lattnerddc787d2006-01-31 19:20:21 +00001305std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001306getRegClassForInlineAsmConstraint(const std::string &Constraint,
1307 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001308 if (Constraint.size() == 1) {
1309 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1310 default: break; // Unknown constriant letter
1311 case 'b':
1312 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1313 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1314 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1315 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1316 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1317 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1318 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1319 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1320 0);
1321 case 'r':
1322 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1323 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1324 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1325 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1326 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1327 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1328 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1329 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1330 0);
1331 case 'f':
1332 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1333 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1334 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1335 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1336 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1337 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1338 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1339 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1340 0);
1341 case 'v':
1342 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1343 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1344 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1345 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1346 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1347 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1348 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1349 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1350 0);
1351 case 'y':
1352 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1353 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1354 0);
1355 }
1356 }
1357
Chris Lattner1efa40f2006-02-22 00:56:39 +00001358 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001359}
Chris Lattner763317d2006-02-07 00:47:13 +00001360
1361// isOperandValidForConstraint
1362bool PPCTargetLowering::
1363isOperandValidForConstraint(SDOperand Op, char Letter) {
1364 switch (Letter) {
1365 default: break;
1366 case 'I':
1367 case 'J':
1368 case 'K':
1369 case 'L':
1370 case 'M':
1371 case 'N':
1372 case 'O':
1373 case 'P': {
1374 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1375 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1376 switch (Letter) {
1377 default: assert(0 && "Unknown constraint letter!");
1378 case 'I': // "I" is a signed 16-bit constant.
1379 return (short)Value == (int)Value;
1380 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1381 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1382 return (short)Value == 0;
1383 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1384 return (Value >> 16) == 0;
1385 case 'M': // "M" is a constant that is greater than 31.
1386 return Value > 31;
1387 case 'N': // "N" is a positive constant that is an exact power of two.
1388 return (int)Value > 0 && isPowerOf2_32(Value);
1389 case 'O': // "O" is the constant zero.
1390 return Value == 0;
1391 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1392 return (short)-Value == (int)-Value;
1393 }
1394 break;
1395 }
1396 }
1397
1398 // Handle standard constraint letters.
1399 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1400}
Evan Chengc4c62572006-03-13 23:20:37 +00001401
1402/// isLegalAddressImmediate - Return true if the integer value can be used
1403/// as the offset of the target addressing mode.
1404bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1405 // PPC allows a sign-extended 16-bit immediate field.
1406 return (V > -(1 << 16) && V < (1 << 16)-1);
1407}