blob: f9d3ad8459d2e0b7955a1f7a3e099a0c7d5be509 [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 Lattner64b3a082006-03-24 07:48:08 +0000282/// isZeroVector - Return true if this build_vector is an all-zero vector.
283///
284bool PPC::isZeroVector(SDNode *N) {
285 if (MVT::isInteger(N->getOperand(0).getValueType())) {
286 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
287 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
288 cast<ConstantSDNode>(N->getOperand(i))->getValue() != 0)
289 return false;
290 } else {
291 assert(MVT::isFloatingPoint(N->getOperand(0).getValueType()) &&
292 "Vector of non-int, non-float values?");
293 // See if this is all zeros.
294 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
295 if (!isa<ConstantFPSDNode>(N->getOperand(i)) ||
296 !cast<ConstantFPSDNode>(N->getOperand(i))->isExactlyValue(0.0))
297 return false;
298 }
299 return true;
300}
301
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000302/// isVecSplatImm - Return true if this is a build_vector of constants which
303/// can be formed by using a vspltis[bhw] instruction. The ByteSize field
304/// indicates the number of bytes of each element [124] -> [bhw].
305bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) {
306 SDOperand OpVal(0, 0);
307 // Check to see if this buildvec has a single non-undef value in its elements.
308 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
309 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
310 if (OpVal.Val == 0)
311 OpVal = N->getOperand(i);
312 else if (OpVal != N->getOperand(i))
313 return false;
314 }
315
316 if (OpVal.Val == 0) return false; // All UNDEF: use implicit def.
317
318 unsigned ValSizeInBytes;
319 uint64_t Value;
320 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
321 Value = CN->getValue();
322 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
323 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
324 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
325 Value = FloatToBits(CN->getValue());
326 ValSizeInBytes = 4;
327 }
328
329 // If the splat value is larger than the element value, then we can never do
330 // this splat. The only case that we could fit the replicated bits into our
331 // immediate field for would be zero, and we prefer to use vxor for it.
332 if (ValSizeInBytes < ByteSize) return false;
333
334 // If the element value is larger than the splat value, cut it in half and
335 // check to see if the two halves are equal. Continue doing this until we
336 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
337 while (ValSizeInBytes > ByteSize) {
338 ValSizeInBytes >>= 1;
339
340 // If the top half equals the bottom half, we're still ok.
341 if (((Value >> (ValSizeInBytes*8)) & ((8 << ValSizeInBytes)-1)) !=
342 (Value & ((8 << ValSizeInBytes)-1)))
343 return false;
344 }
345
346 // Properly sign extend the value.
347 int ShAmt = (4-ByteSize)*8;
348 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
349
350 // If this is zero, don't match, zero matches isZeroVector.
351 if (MaskVal == 0) return false;
352
353 if (Val) *Val = MaskVal;
354
355 // Finally, if this value fits in a 5 bit sext field, return true.
356 return ((MaskVal << (32-5)) >> (32-5)) == MaskVal;
357}
358
Chris Lattneref819f82006-03-20 06:33:01 +0000359
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000360/// LowerOperation - Provide custom lowering hooks for some operations.
361///
Nate Begeman21e463b2005-10-16 05:39:50 +0000362SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000363 switch (Op.getOpcode()) {
364 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000365 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000366 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000367 SDOperand Src = Op.getOperand(0);
368 if (Src.getValueType() == MVT::f32)
369 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
370
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000371 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000372 switch (Op.getValueType()) {
373 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
374 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000375 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000376 break;
377 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000378 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000379 break;
380 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000381
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000382 // Convert the FP value to an int value through memory.
383 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
384 if (Op.getValueType() == MVT::i32)
385 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
386 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000387 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000388 case ISD::SINT_TO_FP:
389 if (Op.getOperand(0).getValueType() == MVT::i64) {
390 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
391 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
392 if (Op.getValueType() == MVT::f32)
393 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
394 return FP;
395 } else {
396 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
397 "Unhandled SINT_TO_FP type in custom expander!");
398 // Since we only generate this in 64-bit mode, we can take advantage of
399 // 64-bit registers. In particular, sign extend the input value into the
400 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
401 // then lfd it and fcfid it.
402 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
403 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
404 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
405
406 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
407 Op.getOperand(0));
408
409 // STD the extended value into the stack slot.
410 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
411 DAG.getEntryNode(), Ext64, FIdx,
412 DAG.getSrcValue(NULL));
413 // Load the value as a double.
414 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
415
416 // FCFID it and return it.
417 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
418 if (Op.getValueType() == MVT::f32)
419 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
420 return FP;
421 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000422 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000423
Chris Lattnerf7605322005-08-31 21:09:52 +0000424 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000425 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000426 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
427 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
428 break;
429
430 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
431
432 // Cannot handle SETEQ/SETNE.
433 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
434
435 MVT::ValueType ResVT = Op.getValueType();
436 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
437 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
438 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000439
Chris Lattnerf7605322005-08-31 21:09:52 +0000440 // If the RHS of the comparison is a 0.0, we don't need to do the
441 // subtraction at all.
442 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000443 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000444 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000445 case ISD::SETULT:
446 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000447 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000448 case ISD::SETUGE:
449 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000450 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
451 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000452 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000453 case ISD::SETUGT:
454 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000455 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000456 case ISD::SETULE:
457 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000458 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
459 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000460 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000461 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000462 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000463
Chris Lattnereb255f22005-10-25 20:54:57 +0000464 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000465 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000466 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000467 case ISD::SETULT:
468 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000469 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
470 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
471 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
472 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000473 case ISD::SETUGE:
474 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000475 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
476 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
477 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
478 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000479 case ISD::SETUGT:
480 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000481 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
482 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
483 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
484 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000485 case ISD::SETULE:
486 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000487 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
488 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
489 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
490 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000491 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000492 break;
493 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000494 case ISD::SHL: {
495 assert(Op.getValueType() == MVT::i64 &&
496 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
497 // The generic code does a fine job expanding shift by a constant.
498 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
499
500 // Otherwise, expand into a bunch of logical ops. Note that these ops
501 // depend on the PPC behavior for oversized shift amounts.
502 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
503 DAG.getConstant(0, MVT::i32));
504 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
505 DAG.getConstant(1, MVT::i32));
506 SDOperand Amt = Op.getOperand(1);
507
508 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
509 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000510 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
511 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000512 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
513 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
514 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000515 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000516 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000517 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000518 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
519 }
520 case ISD::SRL: {
521 assert(Op.getValueType() == MVT::i64 &&
522 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
523 // The generic code does a fine job expanding shift by a constant.
524 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
525
526 // Otherwise, expand into a bunch of logical ops. Note that these ops
527 // depend on the PPC behavior for oversized shift amounts.
528 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
529 DAG.getConstant(0, MVT::i32));
530 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
531 DAG.getConstant(1, MVT::i32));
532 SDOperand Amt = Op.getOperand(1);
533
534 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
535 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000536 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
537 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000538 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
539 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
540 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000541 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000542 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000543 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000544 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
545 }
546 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000547 assert(Op.getValueType() == MVT::i64 &&
548 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
549 // The generic code does a fine job expanding shift by a constant.
550 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
551
552 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
553 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
554 DAG.getConstant(0, MVT::i32));
555 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
556 DAG.getConstant(1, MVT::i32));
557 SDOperand Amt = Op.getOperand(1);
558
559 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
560 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000561 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
562 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000563 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
564 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
565 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000566 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
567 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000568 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
569 Tmp4, Tmp6, ISD::SETLE);
570 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000571 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000572 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000573 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
574 Constant *C = CP->get();
575 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000576 SDOperand Zero = DAG.getConstant(0, MVT::i32);
577
Evan Cheng4c1aa862006-02-22 20:19:42 +0000578 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000579 // Generate non-pic code that has direct accesses to the constant pool.
580 // The address of the global is just (hi(&g)+lo(&g)).
581 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
582 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
583 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
584 }
585
586 // Only lower ConstantPool on Darwin.
587 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
588 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000589 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000590 // With PIC, the first instruction is actually "GR+hi(&G)".
591 Hi = DAG.getNode(ISD::ADD, MVT::i32,
592 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
593 }
594
595 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
596 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
597 return Lo;
598 }
Chris Lattner860e8862005-11-17 07:30:41 +0000599 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000600 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
601 GlobalValue *GV = GSDN->getGlobal();
602 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000603 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000604
Evan Cheng4c1aa862006-02-22 20:19:42 +0000605 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000606 // Generate non-pic code that has direct accesses to globals.
607 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000608 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
609 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
610 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
611 }
Chris Lattner860e8862005-11-17 07:30:41 +0000612
Chris Lattner1d05cb42005-11-17 18:55:48 +0000613 // Only lower GlobalAddress on Darwin.
614 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000615
Chris Lattner860e8862005-11-17 07:30:41 +0000616 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000617 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000618 // With PIC, the first instruction is actually "GR+hi(&G)".
619 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000620 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000621 }
622
623 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
624 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
625
Chris Lattner37dd6f12006-01-29 20:49:17 +0000626 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
627 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000628 return Lo;
629
630 // If the global is weak or external, we have to go through the lazy
631 // resolution stub.
632 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
633 }
Nate Begeman44775902006-01-31 08:17:29 +0000634 case ISD::SETCC: {
635 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000636
637 // If we're comparing for equality to zero, expose the fact that this is
638 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
639 // fold the new nodes.
640 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
641 if (C->isNullValue() && CC == ISD::SETEQ) {
642 MVT::ValueType VT = Op.getOperand(0).getValueType();
643 SDOperand Zext = Op.getOperand(0);
644 if (VT < MVT::i32) {
645 VT = MVT::i32;
646 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
647 }
648 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
649 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
650 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
651 DAG.getConstant(Log2b, getShiftAmountTy()));
652 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
653 }
654 // Leave comparisons against 0 and -1 alone for now, since they're usually
655 // optimized. FIXME: revisit this when we can custom lower all setcc
656 // optimizations.
657 if (C->isAllOnesValue() || C->isNullValue())
658 break;
659 }
660
661 // If we have an integer seteq/setne, turn it into a compare against zero
662 // by subtracting the rhs from the lhs, which is faster than setting a
663 // condition register, reading it back out, and masking the correct bit.
664 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
665 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
666 MVT::ValueType VT = Op.getValueType();
667 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
668 Op.getOperand(1));
669 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
670 }
Nate Begeman44775902006-01-31 08:17:29 +0000671 break;
672 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000673 case ISD::VASTART: {
674 // vastart just stores the address of the VarArgsFrameIndex slot into the
675 // memory location argument.
676 // FIXME: Replace MVT::i32 with PointerTy
677 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
678 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
679 Op.getOperand(1), Op.getOperand(2));
680 }
Nate Begemanee625572006-01-27 21:09:22 +0000681 case ISD::RET: {
682 SDOperand Copy;
683
684 switch(Op.getNumOperands()) {
685 default:
686 assert(0 && "Do not know how to return this many arguments!");
687 abort();
688 case 1:
689 return SDOperand(); // ret void is legal
690 case 2: {
691 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
692 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
693 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
694 SDOperand());
695 break;
696 }
697 case 3:
698 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
699 SDOperand());
700 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
701 break;
702 }
703 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
704 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000705 case ISD::SCALAR_TO_VECTOR: {
706 // Create a stack slot that is 16-byte aligned.
707 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
708 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
709 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
710
711 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000712 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
713 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner23baa1b2006-03-20 22:37:23 +0000714 // LVE_X it out.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000715 return DAG.getNode(PPCISD::LVE_X, Op.getValueType(), Store, FIdx,
716 DAG.getSrcValue(NULL));
717 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000718 case ISD::BUILD_VECTOR:
719 // If this is a case we can't handle, return null and let the default
720 // expansion code take care of it. If we CAN select this case, return Op.
721
722 // See if this is all zeros.
723 // FIXME: We should handle splat(-0.0), and other cases here.
724 if (PPC::isZeroVector(Op.Val))
725 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000726
727 if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb
728 PPC::isVecSplatImm(Op.Val, 2) || // vspltish
729 PPC::isVecSplatImm(Op.Val, 4)) // vspltisw
730 return Op;
731
Chris Lattner64b3a082006-03-24 07:48:08 +0000732 return SDOperand();
733
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000734 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000735 SDOperand V1 = Op.getOperand(0);
736 SDOperand V2 = Op.getOperand(1);
737 SDOperand PermMask = Op.getOperand(2);
738
739 // Cases that are handled by instructions that take permute immediates
740 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
741 // selected by the instruction selector.
742 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
743 break;
744
745 // TODO: Handle more cases, and also handle cases that are cheaper to do as
746 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000747
748 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
749 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000750 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000751
752 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
753 // that it is in input element units, not in bytes. Convert now.
754 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
755 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
756
757 std::vector<SDOperand> ResultMask;
758 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
759 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
760
761 for (unsigned j = 0; j != BytesPerElement; ++j)
762 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
763 MVT::i8));
764 }
765
766 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
767 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
768 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000769 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000770 return SDOperand();
771}
772
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000773std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000774PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000775 //
776 // add beautiful description of PPC stack frame format, or at least some docs
777 //
778 MachineFunction &MF = DAG.getMachineFunction();
779 MachineFrameInfo *MFI = MF.getFrameInfo();
780 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000781 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000782 std::vector<SDOperand> ArgValues;
783
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000784 unsigned ArgOffset = 24;
785 unsigned GPR_remaining = 8;
786 unsigned FPR_remaining = 13;
787 unsigned GPR_idx = 0, FPR_idx = 0;
788 static const unsigned GPR[] = {
789 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
790 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
791 };
792 static const unsigned FPR[] = {
793 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
794 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
795 };
796
797 // Add DAG nodes to load the arguments... On entry to a function on PPC,
798 // the arguments start at offset 24, although they are likely to be passed
799 // in registers.
800 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
801 SDOperand newroot, argt;
802 unsigned ObjSize;
803 bool needsLoad = false;
804 bool ArgLive = !I->use_empty();
805 MVT::ValueType ObjectVT = getValueType(I->getType());
806
807 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000808 default: assert(0 && "Unhandled argument type!");
809 case MVT::i1:
810 case MVT::i8:
811 case MVT::i16:
812 case MVT::i32:
813 ObjSize = 4;
814 if (!ArgLive) break;
815 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000816 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000817 MF.addLiveIn(GPR[GPR_idx], VReg);
818 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000819 if (ObjectVT != MVT::i32) {
820 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
821 : ISD::AssertZext;
822 argt = DAG.getNode(AssertOp, MVT::i32, argt,
823 DAG.getValueType(ObjectVT));
824 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
825 }
Chris Lattner915fb302005-08-30 00:19:00 +0000826 } else {
827 needsLoad = true;
828 }
829 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000830 case MVT::i64:
831 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000832 if (!ArgLive) break;
833 if (GPR_remaining > 0) {
834 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000835 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000836 MF.addLiveIn(GPR[GPR_idx], VReg);
837 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000838 // If we have two or more remaining argument registers, then both halves
839 // of the i64 can be sourced from there. Otherwise, the lower half will
840 // have to come off the stack. This can happen when an i64 is preceded
841 // by 28 bytes of arguments.
842 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000843 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000844 MF.addLiveIn(GPR[GPR_idx+1], VReg);
845 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000846 } else {
847 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
848 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
849 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
850 DAG.getSrcValue(NULL));
851 }
852 // Build the outgoing arg thingy
853 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
854 newroot = argLo;
855 } else {
856 needsLoad = true;
857 }
858 break;
859 case MVT::f32:
860 case MVT::f64:
861 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000862 if (!ArgLive) {
863 if (FPR_remaining > 0) {
864 --FPR_remaining;
865 ++FPR_idx;
866 }
867 break;
868 }
Chris Lattner915fb302005-08-30 00:19:00 +0000869 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000870 unsigned VReg;
871 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000872 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000873 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000874 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000875 MF.addLiveIn(FPR[FPR_idx], VReg);
876 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000877 --FPR_remaining;
878 ++FPR_idx;
879 } else {
880 needsLoad = true;
881 }
882 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000883 }
884
885 // We need to load the argument to a virtual register if we determined above
886 // that we ran out of physical registers of the appropriate type
887 if (needsLoad) {
888 unsigned SubregOffset = 0;
889 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
890 if (ObjectVT == MVT::i16) SubregOffset = 2;
891 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
892 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
893 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
894 DAG.getConstant(SubregOffset, MVT::i32));
895 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
896 DAG.getSrcValue(NULL));
897 }
898
899 // Every 4 bytes of argument space consumes one of the GPRs available for
900 // argument passing.
901 if (GPR_remaining > 0) {
902 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
903 GPR_remaining -= delta;
904 GPR_idx += delta;
905 }
906 ArgOffset += ObjSize;
907 if (newroot.Val)
908 DAG.setRoot(newroot.getValue(1));
909
910 ArgValues.push_back(argt);
911 }
912
913 // If the function takes variable number of arguments, make a frame index for
914 // the start of the first vararg value... for expansion of llvm.va_start.
915 if (F.isVarArg()) {
916 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
917 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
918 // If this function is vararg, store any remaining integer argument regs
919 // to their spots on the stack so that they may be loaded by deferencing the
920 // result of va_next.
921 std::vector<SDOperand> MemOps;
922 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000923 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000924 MF.addLiveIn(GPR[GPR_idx], VReg);
925 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000926 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
927 Val, FIN, DAG.getSrcValue(NULL));
928 MemOps.push_back(Store);
929 // Increment the address by four for the next argument to store
930 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
931 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
932 }
Chris Lattner80720a92005-11-30 20:40:54 +0000933 if (!MemOps.empty()) {
934 MemOps.push_back(DAG.getRoot());
935 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
936 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000937 }
938
939 // Finally, inform the code generator which regs we return values in.
940 switch (getValueType(F.getReturnType())) {
941 default: assert(0 && "Unknown type!");
942 case MVT::isVoid: break;
943 case MVT::i1:
944 case MVT::i8:
945 case MVT::i16:
946 case MVT::i32:
947 MF.addLiveOut(PPC::R3);
948 break;
949 case MVT::i64:
950 MF.addLiveOut(PPC::R3);
951 MF.addLiveOut(PPC::R4);
952 break;
953 case MVT::f32:
954 case MVT::f64:
955 MF.addLiveOut(PPC::F1);
956 break;
957 }
958
959 return ArgValues;
960}
961
962std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000963PPCTargetLowering::LowerCallTo(SDOperand Chain,
964 const Type *RetTy, bool isVarArg,
965 unsigned CallingConv, bool isTailCall,
966 SDOperand Callee, ArgListTy &Args,
967 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000968 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000969 // SelectExpr to use to put the arguments in the appropriate registers.
970 std::vector<SDOperand> args_to_use;
971
972 // Count how many bytes are to be pushed on the stack, including the linkage
973 // area, and parameter passing area.
974 unsigned NumBytes = 24;
975
976 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000977 Chain = DAG.getCALLSEQ_START(Chain,
978 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000979 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000980 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000981 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000982 default: assert(0 && "Unknown value type!");
983 case MVT::i1:
984 case MVT::i8:
985 case MVT::i16:
986 case MVT::i32:
987 case MVT::f32:
988 NumBytes += 4;
989 break;
990 case MVT::i64:
991 case MVT::f64:
992 NumBytes += 8;
993 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000994 }
Chris Lattner915fb302005-08-30 00:19:00 +0000995 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000996
Chris Lattner915fb302005-08-30 00:19:00 +0000997 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
998 // plus 32 bytes of argument space in case any called code gets funky on us.
999 // (Required by ABI to support var arg)
1000 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001001
1002 // Adjust the stack pointer for the new arguments...
1003 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001004 Chain = DAG.getCALLSEQ_START(Chain,
1005 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001006
1007 // Set up a copy of the stack pointer for use loading and storing any
1008 // arguments that may not fit in the registers available for argument
1009 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001010 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001011
1012 // Figure out which arguments are going to go in registers, and which in
1013 // memory. Also, if this is a vararg function, floating point operations
1014 // must be stored to our stack, and loaded into integer regs as well, if
1015 // any integer regs are available for argument passing.
1016 unsigned ArgOffset = 24;
1017 unsigned GPR_remaining = 8;
1018 unsigned FPR_remaining = 13;
1019
1020 std::vector<SDOperand> MemOps;
1021 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1022 // PtrOff will be used to store the current argument to the stack if a
1023 // register cannot be found for it.
1024 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1025 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1026 MVT::ValueType ArgVT = getValueType(Args[i].second);
1027
1028 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001029 default: assert(0 && "Unexpected ValueType for argument!");
1030 case MVT::i1:
1031 case MVT::i8:
1032 case MVT::i16:
1033 // Promote the integer to 32 bits. If the input type is signed use a
1034 // sign extend, otherwise use a zero extend.
1035 if (Args[i].second->isSigned())
1036 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1037 else
1038 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1039 // FALL THROUGH
1040 case MVT::i32:
1041 if (GPR_remaining > 0) {
1042 args_to_use.push_back(Args[i].first);
1043 --GPR_remaining;
1044 } else {
1045 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1046 Args[i].first, PtrOff,
1047 DAG.getSrcValue(NULL)));
1048 }
1049 ArgOffset += 4;
1050 break;
1051 case MVT::i64:
1052 // If we have one free GPR left, we can place the upper half of the i64
1053 // in it, and store the other half to the stack. If we have two or more
1054 // free GPRs, then we can pass both halves of the i64 in registers.
1055 if (GPR_remaining > 0) {
1056 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1057 Args[i].first, DAG.getConstant(1, MVT::i32));
1058 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1059 Args[i].first, DAG.getConstant(0, MVT::i32));
1060 args_to_use.push_back(Hi);
1061 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001062 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001063 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001064 --GPR_remaining;
1065 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001066 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1067 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001068 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001069 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001070 }
Chris Lattner915fb302005-08-30 00:19:00 +00001071 } else {
1072 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1073 Args[i].first, PtrOff,
1074 DAG.getSrcValue(NULL)));
1075 }
1076 ArgOffset += 8;
1077 break;
1078 case MVT::f32:
1079 case MVT::f64:
1080 if (FPR_remaining > 0) {
1081 args_to_use.push_back(Args[i].first);
1082 --FPR_remaining;
1083 if (isVarArg) {
1084 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1085 Args[i].first, PtrOff,
1086 DAG.getSrcValue(NULL));
1087 MemOps.push_back(Store);
1088 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001089 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001090 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1091 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001092 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001093 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001094 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001095 }
1096 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001097 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1098 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001099 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1100 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001101 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001102 args_to_use.push_back(Load);
1103 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001104 }
1105 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001106 // If we have any FPRs remaining, we may also have GPRs remaining.
1107 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1108 // GPRs.
1109 if (GPR_remaining > 0) {
1110 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1111 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001112 }
Chris Lattner915fb302005-08-30 00:19:00 +00001113 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1114 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1115 --GPR_remaining;
1116 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001117 }
Chris Lattner915fb302005-08-30 00:19:00 +00001118 } else {
1119 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1120 Args[i].first, PtrOff,
1121 DAG.getSrcValue(NULL)));
1122 }
1123 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1124 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001125 }
1126 }
1127 if (!MemOps.empty())
1128 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1129 }
1130
1131 std::vector<MVT::ValueType> RetVals;
1132 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001133 MVT::ValueType ActualRetTyVT = RetTyVT;
1134 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1135 ActualRetTyVT = MVT::i32; // Promote result to i32.
1136
Chris Lattnere00ebf02006-01-28 07:33:03 +00001137 if (RetTyVT == MVT::i64) {
1138 RetVals.push_back(MVT::i32);
1139 RetVals.push_back(MVT::i32);
1140 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001141 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001142 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001143 RetVals.push_back(MVT::Other);
1144
Chris Lattner2823b3e2005-11-17 05:56:14 +00001145 // If the callee is a GlobalAddress node (quite common, every direct call is)
1146 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1147 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1148 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1149
Chris Lattner281b55e2006-01-27 23:34:02 +00001150 std::vector<SDOperand> Ops;
1151 Ops.push_back(Chain);
1152 Ops.push_back(Callee);
1153 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1154 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001155 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001156 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1157 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001158 SDOperand RetVal = TheCall;
1159
1160 // If the result is a small value, add a note so that we keep track of the
1161 // information about whether it is sign or zero extended.
1162 if (RetTyVT != ActualRetTyVT) {
1163 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1164 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1165 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001166 } else if (RetTyVT == MVT::i64) {
1167 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001168 }
1169
1170 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001171}
1172
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001173MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001174PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1175 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001176 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001177 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1178 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001179 "Unexpected instr type to insert");
1180
1181 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1182 // control-flow pattern. The incoming instruction knows the destination vreg
1183 // to set, the condition code register to branch on, the true/false values to
1184 // select between, and a branch opcode to use.
1185 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1186 ilist<MachineBasicBlock>::iterator It = BB;
1187 ++It;
1188
1189 // thisMBB:
1190 // ...
1191 // TrueVal = ...
1192 // cmpTY ccX, r1, r2
1193 // bCC copy1MBB
1194 // fallthrough --> copy0MBB
1195 MachineBasicBlock *thisMBB = BB;
1196 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1197 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1198 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1199 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1200 MachineFunction *F = BB->getParent();
1201 F->getBasicBlockList().insert(It, copy0MBB);
1202 F->getBasicBlockList().insert(It, sinkMBB);
1203 // Update machine-CFG edges
1204 BB->addSuccessor(copy0MBB);
1205 BB->addSuccessor(sinkMBB);
1206
1207 // copy0MBB:
1208 // %FalseValue = ...
1209 // # fallthrough to sinkMBB
1210 BB = copy0MBB;
1211
1212 // Update machine-CFG edges
1213 BB->addSuccessor(sinkMBB);
1214
1215 // sinkMBB:
1216 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1217 // ...
1218 BB = sinkMBB;
1219 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1220 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1221 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1222
1223 delete MI; // The pseudo instruction is gone now.
1224 return BB;
1225}
1226
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001227SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1228 DAGCombinerInfo &DCI) const {
1229 TargetMachine &TM = getTargetMachine();
1230 SelectionDAG &DAG = DCI.DAG;
1231 switch (N->getOpcode()) {
1232 default: break;
1233 case ISD::SINT_TO_FP:
1234 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001235 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1236 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1237 // We allow the src/dst to be either f32/f64, but the intermediate
1238 // type must be i64.
1239 if (N->getOperand(0).getValueType() == MVT::i64) {
1240 SDOperand Val = N->getOperand(0).getOperand(0);
1241 if (Val.getValueType() == MVT::f32) {
1242 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1243 DCI.AddToWorklist(Val.Val);
1244 }
1245
1246 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001247 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001248 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001249 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001250 if (N->getValueType(0) == MVT::f32) {
1251 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1252 DCI.AddToWorklist(Val.Val);
1253 }
1254 return Val;
1255 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1256 // If the intermediate type is i32, we can avoid the load/store here
1257 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001258 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001259 }
1260 }
1261 break;
Chris Lattner51269842006-03-01 05:50:56 +00001262 case ISD::STORE:
1263 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1264 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1265 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1266 N->getOperand(1).getValueType() == MVT::i32) {
1267 SDOperand Val = N->getOperand(1).getOperand(0);
1268 if (Val.getValueType() == MVT::f32) {
1269 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1270 DCI.AddToWorklist(Val.Val);
1271 }
1272 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1273 DCI.AddToWorklist(Val.Val);
1274
1275 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1276 N->getOperand(2), N->getOperand(3));
1277 DCI.AddToWorklist(Val.Val);
1278 return Val;
1279 }
1280 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001281 }
1282
1283 return SDOperand();
1284}
1285
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001286/// getConstraintType - Given a constraint letter, return the type of
1287/// constraint it is for this target.
1288PPCTargetLowering::ConstraintType
1289PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1290 switch (ConstraintLetter) {
1291 default: break;
1292 case 'b':
1293 case 'r':
1294 case 'f':
1295 case 'v':
1296 case 'y':
1297 return C_RegisterClass;
1298 }
1299 return TargetLowering::getConstraintType(ConstraintLetter);
1300}
1301
1302
Chris Lattnerddc787d2006-01-31 19:20:21 +00001303std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001304getRegClassForInlineAsmConstraint(const std::string &Constraint,
1305 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001306 if (Constraint.size() == 1) {
1307 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1308 default: break; // Unknown constriant letter
1309 case 'b':
1310 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1311 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1312 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1313 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1314 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1315 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1316 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1317 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1318 0);
1319 case 'r':
1320 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1321 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1322 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1323 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1324 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1325 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1326 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1327 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1328 0);
1329 case 'f':
1330 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1331 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1332 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1333 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1334 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1335 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1336 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1337 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1338 0);
1339 case 'v':
1340 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1341 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1342 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1343 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1344 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1345 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1346 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1347 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1348 0);
1349 case 'y':
1350 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1351 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1352 0);
1353 }
1354 }
1355
Chris Lattner1efa40f2006-02-22 00:56:39 +00001356 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001357}
Chris Lattner763317d2006-02-07 00:47:13 +00001358
1359// isOperandValidForConstraint
1360bool PPCTargetLowering::
1361isOperandValidForConstraint(SDOperand Op, char Letter) {
1362 switch (Letter) {
1363 default: break;
1364 case 'I':
1365 case 'J':
1366 case 'K':
1367 case 'L':
1368 case 'M':
1369 case 'N':
1370 case 'O':
1371 case 'P': {
1372 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1373 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1374 switch (Letter) {
1375 default: assert(0 && "Unknown constraint letter!");
1376 case 'I': // "I" is a signed 16-bit constant.
1377 return (short)Value == (int)Value;
1378 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1379 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1380 return (short)Value == 0;
1381 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1382 return (Value >> 16) == 0;
1383 case 'M': // "M" is a constant that is greater than 31.
1384 return Value > 31;
1385 case 'N': // "N" is a positive constant that is an exact power of two.
1386 return (int)Value > 0 && isPowerOf2_32(Value);
1387 case 'O': // "O" is the constant zero.
1388 return Value == 0;
1389 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1390 return (short)-Value == (int)-Value;
1391 }
1392 break;
1393 }
1394 }
1395
1396 // Handle standard constraint letters.
1397 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1398}
Evan Chengc4c62572006-03-13 23:20:37 +00001399
1400/// isLegalAddressImmediate - Return true if the integer value can be used
1401/// as the offset of the target addressing mode.
1402bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1403 // PPC allows a sign-extended 16-bit immediate field.
1404 return (V > -(1 << 16) && V < (1 << 16)-1);
1405}