blob: 01cb343a12fd6438dab2fcf420172cc179a112a7 [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);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000175 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000176 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000177 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000178 }
179
Nate Begeman425a9692005-11-29 08:17:20 +0000180 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000181 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000182 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000183 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
184 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000185
Evan Chengd30bf012006-03-01 01:11:20 +0000186 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
187 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
188 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
Evan Chengd30bf012006-03-01 01:11:20 +0000189 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000190
191 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
192 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
193
Chris Lattnerb2177b92006-03-19 06:55:52 +0000194 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
195 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000196
197 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
198 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000199 }
200
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000201 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000202 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000203
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000204 // We have target-specific dag combine patterns for the following nodes:
205 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000206 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000207
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000208 computeRegisterProperties();
209}
210
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000211const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
212 switch (Opcode) {
213 default: return 0;
214 case PPCISD::FSEL: return "PPCISD::FSEL";
215 case PPCISD::FCFID: return "PPCISD::FCFID";
216 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
217 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000218 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000219 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
220 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerb2177b92006-03-19 06:55:52 +0000221 case PPCISD::LVE_X: return "PPCISD::LVE_X";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000222 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000223 case PPCISD::Hi: return "PPCISD::Hi";
224 case PPCISD::Lo: return "PPCISD::Lo";
225 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
226 case PPCISD::SRL: return "PPCISD::SRL";
227 case PPCISD::SRA: return "PPCISD::SRA";
228 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000229 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
230 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000231 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000232 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
233 }
234}
235
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000236/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
237static bool isFloatingPointZero(SDOperand Op) {
238 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
239 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
240 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
241 // Maybe this has already been legalized into the constant pool?
242 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
243 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
244 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
245 }
246 return false;
247}
248
Chris Lattneref819f82006-03-20 06:33:01 +0000249
250/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
251/// specifies a splat of a single element that is suitable for input to
252/// VSPLTB/VSPLTH/VSPLTW.
253bool PPC::isSplatShuffleMask(SDNode *N) {
254 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000255
256 // We can only splat 8-bit, 16-bit, and 32-bit quantities.
257 if (N->getNumOperands() != 4 && N->getNumOperands() != 8 &&
258 N->getNumOperands() != 16)
259 return false;
260
Chris Lattner88a99ef2006-03-20 06:37:44 +0000261 // This is a splat operation if each element of the permute is the same, and
262 // if the value doesn't reference the second vector.
263 SDOperand Elt = N->getOperand(0);
264 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
265 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
266 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
267 "Invalid VECTOR_SHUFFLE mask!");
268 if (N->getOperand(i) != Elt) return false;
269 }
270
271 // Make sure it is a splat of the first vector operand.
272 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
Chris Lattneref819f82006-03-20 06:33:01 +0000273}
274
275/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
276/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
277unsigned PPC::getVSPLTImmediate(SDNode *N) {
278 assert(isSplatShuffleMask(N));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000279 return cast<ConstantSDNode>(N->getOperand(0))->getValue();
Chris Lattneref819f82006-03-20 06:33:01 +0000280}
281
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000282/// isVecSplatImm - Return true if this is a build_vector of constants which
283/// can be formed by using a vspltis[bhw] instruction. The ByteSize field
284/// indicates the number of bytes of each element [124] -> [bhw].
285bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) {
286 SDOperand OpVal(0, 0);
287 // Check to see if this buildvec has a single non-undef value in its elements.
288 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
289 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
290 if (OpVal.Val == 0)
291 OpVal = N->getOperand(i);
292 else if (OpVal != N->getOperand(i))
293 return false;
294 }
295
296 if (OpVal.Val == 0) return false; // All UNDEF: use implicit def.
297
298 unsigned ValSizeInBytes;
299 uint64_t Value;
300 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
301 Value = CN->getValue();
302 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
303 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
304 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
305 Value = FloatToBits(CN->getValue());
306 ValSizeInBytes = 4;
307 }
308
309 // If the splat value is larger than the element value, then we can never do
310 // this splat. The only case that we could fit the replicated bits into our
311 // immediate field for would be zero, and we prefer to use vxor for it.
312 if (ValSizeInBytes < ByteSize) return false;
313
314 // If the element value is larger than the splat value, cut it in half and
315 // check to see if the two halves are equal. Continue doing this until we
316 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
317 while (ValSizeInBytes > ByteSize) {
318 ValSizeInBytes >>= 1;
319
320 // If the top half equals the bottom half, we're still ok.
321 if (((Value >> (ValSizeInBytes*8)) & ((8 << ValSizeInBytes)-1)) !=
322 (Value & ((8 << ValSizeInBytes)-1)))
323 return false;
324 }
325
326 // Properly sign extend the value.
327 int ShAmt = (4-ByteSize)*8;
328 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
329
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000330 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000331 if (MaskVal == 0) return false;
332
333 if (Val) *Val = MaskVal;
334
335 // Finally, if this value fits in a 5 bit sext field, return true.
336 return ((MaskVal << (32-5)) >> (32-5)) == MaskVal;
337}
338
Chris Lattneref819f82006-03-20 06:33:01 +0000339
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000340/// LowerOperation - Provide custom lowering hooks for some operations.
341///
Nate Begeman21e463b2005-10-16 05:39:50 +0000342SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000343 switch (Op.getOpcode()) {
344 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000345 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000346 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000347 SDOperand Src = Op.getOperand(0);
348 if (Src.getValueType() == MVT::f32)
349 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
350
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000351 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000352 switch (Op.getValueType()) {
353 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
354 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000355 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000356 break;
357 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000358 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000359 break;
360 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000361
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000362 // Convert the FP value to an int value through memory.
363 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
364 if (Op.getValueType() == MVT::i32)
365 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
366 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000367 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000368 case ISD::SINT_TO_FP:
369 if (Op.getOperand(0).getValueType() == MVT::i64) {
370 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
371 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
372 if (Op.getValueType() == MVT::f32)
373 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
374 return FP;
375 } else {
376 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
377 "Unhandled SINT_TO_FP type in custom expander!");
378 // Since we only generate this in 64-bit mode, we can take advantage of
379 // 64-bit registers. In particular, sign extend the input value into the
380 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
381 // then lfd it and fcfid it.
382 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
383 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
384 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
385
386 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
387 Op.getOperand(0));
388
389 // STD the extended value into the stack slot.
390 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
391 DAG.getEntryNode(), Ext64, FIdx,
392 DAG.getSrcValue(NULL));
393 // Load the value as a double.
394 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
395
396 // FCFID it and return it.
397 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
398 if (Op.getValueType() == MVT::f32)
399 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
400 return FP;
401 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000402 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000403
Chris Lattnerf7605322005-08-31 21:09:52 +0000404 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000405 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000406 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
407 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
408 break;
409
410 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
411
412 // Cannot handle SETEQ/SETNE.
413 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
414
415 MVT::ValueType ResVT = Op.getValueType();
416 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
417 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
418 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000419
Chris Lattnerf7605322005-08-31 21:09:52 +0000420 // If the RHS of the comparison is a 0.0, we don't need to do the
421 // subtraction at all.
422 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000423 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000424 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000425 case ISD::SETULT:
426 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000427 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000428 case ISD::SETUGE:
429 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000430 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
431 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000432 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000433 case ISD::SETUGT:
434 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000435 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000436 case ISD::SETULE:
437 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000438 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
439 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000440 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000441 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000442 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000443
Chris Lattnereb255f22005-10-25 20:54:57 +0000444 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000445 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000446 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000447 case ISD::SETULT:
448 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000449 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
450 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
451 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
452 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000453 case ISD::SETUGE:
454 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000455 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
456 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
457 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
458 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000459 case ISD::SETUGT:
460 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000461 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
462 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
463 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
464 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000465 case ISD::SETULE:
466 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000467 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
468 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
469 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
470 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000471 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000472 break;
473 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000474 case ISD::SHL: {
475 assert(Op.getValueType() == MVT::i64 &&
476 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
477 // The generic code does a fine job expanding shift by a constant.
478 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
479
480 // Otherwise, expand into a bunch of logical ops. Note that these ops
481 // depend on the PPC behavior for oversized shift amounts.
482 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
483 DAG.getConstant(0, MVT::i32));
484 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
485 DAG.getConstant(1, MVT::i32));
486 SDOperand Amt = Op.getOperand(1);
487
488 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
489 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000490 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
491 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000492 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
493 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
494 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000495 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000496 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000497 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000498 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
499 }
500 case ISD::SRL: {
501 assert(Op.getValueType() == MVT::i64 &&
502 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
503 // The generic code does a fine job expanding shift by a constant.
504 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
505
506 // Otherwise, expand into a bunch of logical ops. Note that these ops
507 // depend on the PPC behavior for oversized shift amounts.
508 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
509 DAG.getConstant(0, MVT::i32));
510 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
511 DAG.getConstant(1, MVT::i32));
512 SDOperand Amt = Op.getOperand(1);
513
514 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
515 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000516 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
517 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000518 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
519 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
520 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000521 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000522 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000523 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000524 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
525 }
526 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000527 assert(Op.getValueType() == MVT::i64 &&
528 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
529 // The generic code does a fine job expanding shift by a constant.
530 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
531
532 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
533 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
534 DAG.getConstant(0, MVT::i32));
535 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
536 DAG.getConstant(1, MVT::i32));
537 SDOperand Amt = Op.getOperand(1);
538
539 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
540 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000541 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
542 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000543 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
544 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
545 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000546 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
547 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000548 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
549 Tmp4, Tmp6, ISD::SETLE);
550 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000551 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000552 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000553 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
554 Constant *C = CP->get();
555 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000556 SDOperand Zero = DAG.getConstant(0, MVT::i32);
557
Evan Cheng4c1aa862006-02-22 20:19:42 +0000558 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000559 // Generate non-pic code that has direct accesses to the constant pool.
560 // The address of the global is just (hi(&g)+lo(&g)).
561 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
562 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
563 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
564 }
565
566 // Only lower ConstantPool on Darwin.
567 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
568 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000569 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000570 // With PIC, the first instruction is actually "GR+hi(&G)".
571 Hi = DAG.getNode(ISD::ADD, MVT::i32,
572 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
573 }
574
575 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
576 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
577 return Lo;
578 }
Chris Lattner860e8862005-11-17 07:30:41 +0000579 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000580 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
581 GlobalValue *GV = GSDN->getGlobal();
582 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000583 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000584
Evan Cheng4c1aa862006-02-22 20:19:42 +0000585 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000586 // Generate non-pic code that has direct accesses to globals.
587 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000588 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
589 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
590 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
591 }
Chris Lattner860e8862005-11-17 07:30:41 +0000592
Chris Lattner1d05cb42005-11-17 18:55:48 +0000593 // Only lower GlobalAddress on Darwin.
594 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000595
Chris Lattner860e8862005-11-17 07:30:41 +0000596 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000597 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000598 // With PIC, the first instruction is actually "GR+hi(&G)".
599 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000600 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000601 }
602
603 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
604 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
605
Chris Lattner37dd6f12006-01-29 20:49:17 +0000606 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
607 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000608 return Lo;
609
610 // If the global is weak or external, we have to go through the lazy
611 // resolution stub.
612 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
613 }
Nate Begeman44775902006-01-31 08:17:29 +0000614 case ISD::SETCC: {
615 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000616
617 // If we're comparing for equality to zero, expose the fact that this is
618 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
619 // fold the new nodes.
620 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
621 if (C->isNullValue() && CC == ISD::SETEQ) {
622 MVT::ValueType VT = Op.getOperand(0).getValueType();
623 SDOperand Zext = Op.getOperand(0);
624 if (VT < MVT::i32) {
625 VT = MVT::i32;
626 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
627 }
628 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
629 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
630 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
631 DAG.getConstant(Log2b, getShiftAmountTy()));
632 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
633 }
634 // Leave comparisons against 0 and -1 alone for now, since they're usually
635 // optimized. FIXME: revisit this when we can custom lower all setcc
636 // optimizations.
637 if (C->isAllOnesValue() || C->isNullValue())
638 break;
639 }
640
641 // If we have an integer seteq/setne, turn it into a compare against zero
642 // by subtracting the rhs from the lhs, which is faster than setting a
643 // condition register, reading it back out, and masking the correct bit.
644 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
645 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
646 MVT::ValueType VT = Op.getValueType();
647 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
648 Op.getOperand(1));
649 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
650 }
Nate Begeman44775902006-01-31 08:17:29 +0000651 break;
652 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000653 case ISD::VASTART: {
654 // vastart just stores the address of the VarArgsFrameIndex slot into the
655 // memory location argument.
656 // FIXME: Replace MVT::i32 with PointerTy
657 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
658 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
659 Op.getOperand(1), Op.getOperand(2));
660 }
Nate Begemanee625572006-01-27 21:09:22 +0000661 case ISD::RET: {
662 SDOperand Copy;
663
664 switch(Op.getNumOperands()) {
665 default:
666 assert(0 && "Do not know how to return this many arguments!");
667 abort();
668 case 1:
669 return SDOperand(); // ret void is legal
670 case 2: {
671 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
672 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
673 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
674 SDOperand());
675 break;
676 }
677 case 3:
678 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
679 SDOperand());
680 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
681 break;
682 }
683 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
684 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000685 case ISD::SCALAR_TO_VECTOR: {
686 // Create a stack slot that is 16-byte aligned.
687 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
688 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
689 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
690
691 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000692 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
693 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner23baa1b2006-03-20 22:37:23 +0000694 // LVE_X it out.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000695 return DAG.getNode(PPCISD::LVE_X, Op.getValueType(), Store, FIdx,
696 DAG.getSrcValue(NULL));
697 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000698 case ISD::BUILD_VECTOR:
699 // If this is a case we can't handle, return null and let the default
700 // expansion code take care of it. If we CAN select this case, return Op.
701
702 // See if this is all zeros.
703 // FIXME: We should handle splat(-0.0), and other cases here.
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000704 if (ISD::isBuildVectorAllZeros(Op.Val))
Chris Lattner64b3a082006-03-24 07:48:08 +0000705 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000706
707 if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb
708 PPC::isVecSplatImm(Op.Val, 2) || // vspltish
709 PPC::isVecSplatImm(Op.Val, 4)) // vspltisw
710 return Op;
711
Chris Lattner64b3a082006-03-24 07:48:08 +0000712 return SDOperand();
713
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000714 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000715 SDOperand V1 = Op.getOperand(0);
716 SDOperand V2 = Op.getOperand(1);
717 SDOperand PermMask = Op.getOperand(2);
718
719 // Cases that are handled by instructions that take permute immediates
720 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
721 // selected by the instruction selector.
722 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
723 break;
724
725 // TODO: Handle more cases, and also handle cases that are cheaper to do as
726 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000727
728 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
729 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000730 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000731
732 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
733 // that it is in input element units, not in bytes. Convert now.
734 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
735 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
736
737 std::vector<SDOperand> ResultMask;
738 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
739 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
740
741 for (unsigned j = 0; j != BytesPerElement; ++j)
742 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
743 MVT::i8));
744 }
745
746 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
747 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
748 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000749 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000750 return SDOperand();
751}
752
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000753std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000754PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000755 //
756 // add beautiful description of PPC stack frame format, or at least some docs
757 //
758 MachineFunction &MF = DAG.getMachineFunction();
759 MachineFrameInfo *MFI = MF.getFrameInfo();
760 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000761 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000762 std::vector<SDOperand> ArgValues;
763
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000764 unsigned ArgOffset = 24;
765 unsigned GPR_remaining = 8;
766 unsigned FPR_remaining = 13;
767 unsigned GPR_idx = 0, FPR_idx = 0;
768 static const unsigned GPR[] = {
769 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
770 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
771 };
772 static const unsigned FPR[] = {
773 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
774 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
775 };
776
777 // Add DAG nodes to load the arguments... On entry to a function on PPC,
778 // the arguments start at offset 24, although they are likely to be passed
779 // in registers.
780 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
781 SDOperand newroot, argt;
782 unsigned ObjSize;
783 bool needsLoad = false;
784 bool ArgLive = !I->use_empty();
785 MVT::ValueType ObjectVT = getValueType(I->getType());
786
787 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000788 default: assert(0 && "Unhandled argument type!");
789 case MVT::i1:
790 case MVT::i8:
791 case MVT::i16:
792 case MVT::i32:
793 ObjSize = 4;
794 if (!ArgLive) break;
795 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000796 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000797 MF.addLiveIn(GPR[GPR_idx], VReg);
798 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000799 if (ObjectVT != MVT::i32) {
800 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
801 : ISD::AssertZext;
802 argt = DAG.getNode(AssertOp, MVT::i32, argt,
803 DAG.getValueType(ObjectVT));
804 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
805 }
Chris Lattner915fb302005-08-30 00:19:00 +0000806 } else {
807 needsLoad = true;
808 }
809 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000810 case MVT::i64:
811 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000812 if (!ArgLive) break;
813 if (GPR_remaining > 0) {
814 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000815 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000816 MF.addLiveIn(GPR[GPR_idx], VReg);
817 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000818 // If we have two or more remaining argument registers, then both halves
819 // of the i64 can be sourced from there. Otherwise, the lower half will
820 // have to come off the stack. This can happen when an i64 is preceded
821 // by 28 bytes of arguments.
822 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000823 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000824 MF.addLiveIn(GPR[GPR_idx+1], VReg);
825 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000826 } else {
827 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
828 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
829 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
830 DAG.getSrcValue(NULL));
831 }
832 // Build the outgoing arg thingy
833 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
834 newroot = argLo;
835 } else {
836 needsLoad = true;
837 }
838 break;
839 case MVT::f32:
840 case MVT::f64:
841 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000842 if (!ArgLive) {
843 if (FPR_remaining > 0) {
844 --FPR_remaining;
845 ++FPR_idx;
846 }
847 break;
848 }
Chris Lattner915fb302005-08-30 00:19:00 +0000849 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000850 unsigned VReg;
851 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000852 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000853 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000854 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000855 MF.addLiveIn(FPR[FPR_idx], VReg);
856 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000857 --FPR_remaining;
858 ++FPR_idx;
859 } else {
860 needsLoad = true;
861 }
862 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000863 }
864
865 // We need to load the argument to a virtual register if we determined above
866 // that we ran out of physical registers of the appropriate type
867 if (needsLoad) {
868 unsigned SubregOffset = 0;
869 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
870 if (ObjectVT == MVT::i16) SubregOffset = 2;
871 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
872 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
873 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
874 DAG.getConstant(SubregOffset, MVT::i32));
875 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
876 DAG.getSrcValue(NULL));
877 }
878
879 // Every 4 bytes of argument space consumes one of the GPRs available for
880 // argument passing.
881 if (GPR_remaining > 0) {
882 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
883 GPR_remaining -= delta;
884 GPR_idx += delta;
885 }
886 ArgOffset += ObjSize;
887 if (newroot.Val)
888 DAG.setRoot(newroot.getValue(1));
889
890 ArgValues.push_back(argt);
891 }
892
893 // If the function takes variable number of arguments, make a frame index for
894 // the start of the first vararg value... for expansion of llvm.va_start.
895 if (F.isVarArg()) {
896 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
897 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
898 // If this function is vararg, store any remaining integer argument regs
899 // to their spots on the stack so that they may be loaded by deferencing the
900 // result of va_next.
901 std::vector<SDOperand> MemOps;
902 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000903 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000904 MF.addLiveIn(GPR[GPR_idx], VReg);
905 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000906 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
907 Val, FIN, DAG.getSrcValue(NULL));
908 MemOps.push_back(Store);
909 // Increment the address by four for the next argument to store
910 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
911 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
912 }
Chris Lattner80720a92005-11-30 20:40:54 +0000913 if (!MemOps.empty()) {
914 MemOps.push_back(DAG.getRoot());
915 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
916 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000917 }
918
919 // Finally, inform the code generator which regs we return values in.
920 switch (getValueType(F.getReturnType())) {
921 default: assert(0 && "Unknown type!");
922 case MVT::isVoid: break;
923 case MVT::i1:
924 case MVT::i8:
925 case MVT::i16:
926 case MVT::i32:
927 MF.addLiveOut(PPC::R3);
928 break;
929 case MVT::i64:
930 MF.addLiveOut(PPC::R3);
931 MF.addLiveOut(PPC::R4);
932 break;
933 case MVT::f32:
934 case MVT::f64:
935 MF.addLiveOut(PPC::F1);
936 break;
937 }
938
939 return ArgValues;
940}
941
942std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000943PPCTargetLowering::LowerCallTo(SDOperand Chain,
944 const Type *RetTy, bool isVarArg,
945 unsigned CallingConv, bool isTailCall,
946 SDOperand Callee, ArgListTy &Args,
947 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000948 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000949 // SelectExpr to use to put the arguments in the appropriate registers.
950 std::vector<SDOperand> args_to_use;
951
952 // Count how many bytes are to be pushed on the stack, including the linkage
953 // area, and parameter passing area.
954 unsigned NumBytes = 24;
955
956 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000957 Chain = DAG.getCALLSEQ_START(Chain,
958 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000959 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000960 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000961 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000962 default: assert(0 && "Unknown value type!");
963 case MVT::i1:
964 case MVT::i8:
965 case MVT::i16:
966 case MVT::i32:
967 case MVT::f32:
968 NumBytes += 4;
969 break;
970 case MVT::i64:
971 case MVT::f64:
972 NumBytes += 8;
973 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000974 }
Chris Lattner915fb302005-08-30 00:19:00 +0000975 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000976
Chris Lattner915fb302005-08-30 00:19:00 +0000977 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
978 // plus 32 bytes of argument space in case any called code gets funky on us.
979 // (Required by ABI to support var arg)
980 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000981
982 // Adjust the stack pointer for the new arguments...
983 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000984 Chain = DAG.getCALLSEQ_START(Chain,
985 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000986
987 // Set up a copy of the stack pointer for use loading and storing any
988 // arguments that may not fit in the registers available for argument
989 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000990 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000991
992 // Figure out which arguments are going to go in registers, and which in
993 // memory. Also, if this is a vararg function, floating point operations
994 // must be stored to our stack, and loaded into integer regs as well, if
995 // any integer regs are available for argument passing.
996 unsigned ArgOffset = 24;
997 unsigned GPR_remaining = 8;
998 unsigned FPR_remaining = 13;
999
1000 std::vector<SDOperand> MemOps;
1001 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1002 // PtrOff will be used to store the current argument to the stack if a
1003 // register cannot be found for it.
1004 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1005 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1006 MVT::ValueType ArgVT = getValueType(Args[i].second);
1007
1008 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001009 default: assert(0 && "Unexpected ValueType for argument!");
1010 case MVT::i1:
1011 case MVT::i8:
1012 case MVT::i16:
1013 // Promote the integer to 32 bits. If the input type is signed use a
1014 // sign extend, otherwise use a zero extend.
1015 if (Args[i].second->isSigned())
1016 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1017 else
1018 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1019 // FALL THROUGH
1020 case MVT::i32:
1021 if (GPR_remaining > 0) {
1022 args_to_use.push_back(Args[i].first);
1023 --GPR_remaining;
1024 } else {
1025 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1026 Args[i].first, PtrOff,
1027 DAG.getSrcValue(NULL)));
1028 }
1029 ArgOffset += 4;
1030 break;
1031 case MVT::i64:
1032 // If we have one free GPR left, we can place the upper half of the i64
1033 // in it, and store the other half to the stack. If we have two or more
1034 // free GPRs, then we can pass both halves of the i64 in registers.
1035 if (GPR_remaining > 0) {
1036 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1037 Args[i].first, DAG.getConstant(1, MVT::i32));
1038 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1039 Args[i].first, DAG.getConstant(0, MVT::i32));
1040 args_to_use.push_back(Hi);
1041 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001042 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001043 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001044 --GPR_remaining;
1045 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001046 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1047 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001048 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001049 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001050 }
Chris Lattner915fb302005-08-30 00:19:00 +00001051 } else {
1052 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1053 Args[i].first, PtrOff,
1054 DAG.getSrcValue(NULL)));
1055 }
1056 ArgOffset += 8;
1057 break;
1058 case MVT::f32:
1059 case MVT::f64:
1060 if (FPR_remaining > 0) {
1061 args_to_use.push_back(Args[i].first);
1062 --FPR_remaining;
1063 if (isVarArg) {
1064 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1065 Args[i].first, PtrOff,
1066 DAG.getSrcValue(NULL));
1067 MemOps.push_back(Store);
1068 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001069 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001070 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1071 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001072 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001073 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001074 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001075 }
1076 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001077 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1078 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001079 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1080 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001081 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001082 args_to_use.push_back(Load);
1083 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001084 }
1085 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001086 // If we have any FPRs remaining, we may also have GPRs remaining.
1087 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1088 // GPRs.
1089 if (GPR_remaining > 0) {
1090 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1091 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001092 }
Chris Lattner915fb302005-08-30 00:19:00 +00001093 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1094 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1095 --GPR_remaining;
1096 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001097 }
Chris Lattner915fb302005-08-30 00:19:00 +00001098 } else {
1099 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1100 Args[i].first, PtrOff,
1101 DAG.getSrcValue(NULL)));
1102 }
1103 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1104 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001105 }
1106 }
1107 if (!MemOps.empty())
1108 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1109 }
1110
1111 std::vector<MVT::ValueType> RetVals;
1112 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001113 MVT::ValueType ActualRetTyVT = RetTyVT;
1114 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1115 ActualRetTyVT = MVT::i32; // Promote result to i32.
1116
Chris Lattnere00ebf02006-01-28 07:33:03 +00001117 if (RetTyVT == MVT::i64) {
1118 RetVals.push_back(MVT::i32);
1119 RetVals.push_back(MVT::i32);
1120 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001121 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001122 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001123 RetVals.push_back(MVT::Other);
1124
Chris Lattner2823b3e2005-11-17 05:56:14 +00001125 // If the callee is a GlobalAddress node (quite common, every direct call is)
1126 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1127 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1128 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1129
Chris Lattner281b55e2006-01-27 23:34:02 +00001130 std::vector<SDOperand> Ops;
1131 Ops.push_back(Chain);
1132 Ops.push_back(Callee);
1133 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1134 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001135 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001136 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1137 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001138 SDOperand RetVal = TheCall;
1139
1140 // If the result is a small value, add a note so that we keep track of the
1141 // information about whether it is sign or zero extended.
1142 if (RetTyVT != ActualRetTyVT) {
1143 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1144 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1145 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001146 } else if (RetTyVT == MVT::i64) {
1147 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001148 }
1149
1150 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001151}
1152
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001153MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001154PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1155 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001156 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001157 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1158 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001159 "Unexpected instr type to insert");
1160
1161 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1162 // control-flow pattern. The incoming instruction knows the destination vreg
1163 // to set, the condition code register to branch on, the true/false values to
1164 // select between, and a branch opcode to use.
1165 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1166 ilist<MachineBasicBlock>::iterator It = BB;
1167 ++It;
1168
1169 // thisMBB:
1170 // ...
1171 // TrueVal = ...
1172 // cmpTY ccX, r1, r2
1173 // bCC copy1MBB
1174 // fallthrough --> copy0MBB
1175 MachineBasicBlock *thisMBB = BB;
1176 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1177 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1178 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1179 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1180 MachineFunction *F = BB->getParent();
1181 F->getBasicBlockList().insert(It, copy0MBB);
1182 F->getBasicBlockList().insert(It, sinkMBB);
1183 // Update machine-CFG edges
1184 BB->addSuccessor(copy0MBB);
1185 BB->addSuccessor(sinkMBB);
1186
1187 // copy0MBB:
1188 // %FalseValue = ...
1189 // # fallthrough to sinkMBB
1190 BB = copy0MBB;
1191
1192 // Update machine-CFG edges
1193 BB->addSuccessor(sinkMBB);
1194
1195 // sinkMBB:
1196 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1197 // ...
1198 BB = sinkMBB;
1199 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1200 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1201 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1202
1203 delete MI; // The pseudo instruction is gone now.
1204 return BB;
1205}
1206
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001207SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1208 DAGCombinerInfo &DCI) const {
1209 TargetMachine &TM = getTargetMachine();
1210 SelectionDAG &DAG = DCI.DAG;
1211 switch (N->getOpcode()) {
1212 default: break;
1213 case ISD::SINT_TO_FP:
1214 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001215 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1216 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1217 // We allow the src/dst to be either f32/f64, but the intermediate
1218 // type must be i64.
1219 if (N->getOperand(0).getValueType() == MVT::i64) {
1220 SDOperand Val = N->getOperand(0).getOperand(0);
1221 if (Val.getValueType() == MVT::f32) {
1222 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1223 DCI.AddToWorklist(Val.Val);
1224 }
1225
1226 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001227 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001228 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001229 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001230 if (N->getValueType(0) == MVT::f32) {
1231 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1232 DCI.AddToWorklist(Val.Val);
1233 }
1234 return Val;
1235 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1236 // If the intermediate type is i32, we can avoid the load/store here
1237 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001238 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001239 }
1240 }
1241 break;
Chris Lattner51269842006-03-01 05:50:56 +00001242 case ISD::STORE:
1243 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1244 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1245 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1246 N->getOperand(1).getValueType() == MVT::i32) {
1247 SDOperand Val = N->getOperand(1).getOperand(0);
1248 if (Val.getValueType() == MVT::f32) {
1249 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1250 DCI.AddToWorklist(Val.Val);
1251 }
1252 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1253 DCI.AddToWorklist(Val.Val);
1254
1255 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1256 N->getOperand(2), N->getOperand(3));
1257 DCI.AddToWorklist(Val.Val);
1258 return Val;
1259 }
1260 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001261 }
1262
1263 return SDOperand();
1264}
1265
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001266/// getConstraintType - Given a constraint letter, return the type of
1267/// constraint it is for this target.
1268PPCTargetLowering::ConstraintType
1269PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1270 switch (ConstraintLetter) {
1271 default: break;
1272 case 'b':
1273 case 'r':
1274 case 'f':
1275 case 'v':
1276 case 'y':
1277 return C_RegisterClass;
1278 }
1279 return TargetLowering::getConstraintType(ConstraintLetter);
1280}
1281
1282
Chris Lattnerddc787d2006-01-31 19:20:21 +00001283std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001284getRegClassForInlineAsmConstraint(const std::string &Constraint,
1285 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001286 if (Constraint.size() == 1) {
1287 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1288 default: break; // Unknown constriant letter
1289 case 'b':
1290 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1291 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1292 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1293 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1294 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1295 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1296 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1297 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1298 0);
1299 case 'r':
1300 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1301 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1302 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1303 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1304 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1305 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1306 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1307 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1308 0);
1309 case 'f':
1310 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1311 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1312 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1313 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1314 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1315 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1316 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1317 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1318 0);
1319 case 'v':
1320 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1321 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1322 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1323 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1324 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1325 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1326 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1327 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1328 0);
1329 case 'y':
1330 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1331 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1332 0);
1333 }
1334 }
1335
Chris Lattner1efa40f2006-02-22 00:56:39 +00001336 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001337}
Chris Lattner763317d2006-02-07 00:47:13 +00001338
1339// isOperandValidForConstraint
1340bool PPCTargetLowering::
1341isOperandValidForConstraint(SDOperand Op, char Letter) {
1342 switch (Letter) {
1343 default: break;
1344 case 'I':
1345 case 'J':
1346 case 'K':
1347 case 'L':
1348 case 'M':
1349 case 'N':
1350 case 'O':
1351 case 'P': {
1352 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1353 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1354 switch (Letter) {
1355 default: assert(0 && "Unknown constraint letter!");
1356 case 'I': // "I" is a signed 16-bit constant.
1357 return (short)Value == (int)Value;
1358 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1359 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1360 return (short)Value == 0;
1361 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1362 return (Value >> 16) == 0;
1363 case 'M': // "M" is a constant that is greater than 31.
1364 return Value > 31;
1365 case 'N': // "N" is a positive constant that is an exact power of two.
1366 return (int)Value > 0 && isPowerOf2_32(Value);
1367 case 'O': // "O" is the constant zero.
1368 return Value == 0;
1369 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1370 return (short)-Value == (int)-Value;
1371 }
1372 break;
1373 }
1374 }
1375
1376 // Handle standard constraint letters.
1377 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1378}
Evan Chengc4c62572006-03-13 23:20:37 +00001379
1380/// isLegalAddressImmediate - Return true if the integer value can be used
1381/// as the offset of the target addressing mode.
1382bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1383 // PPC allows a sign-extended 16-bit immediate field.
1384 return (V > -(1 << 16) && V < (1 << 16)-1);
1385}