blob: d29ec20b2d25c6fd7d08a1f7cb377a983e8d4426 [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 Lattnerecfe55e2006-03-22 05:30:33 +0000143 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000144 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
145 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
146 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000147 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000148 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000149 }
150
151 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
152 // 64 bit PowerPC implementations can support i64 types directly
153 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000154 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
155 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000156 } else {
157 // 32 bit PowerPC wants to expand i64 shifts itself.
158 setOperationAction(ISD::SHL, MVT::i64, Custom);
159 setOperationAction(ISD::SRL, MVT::i64, Custom);
160 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000161 }
162
Evan Chengd30bf012006-03-01 01:11:20 +0000163 // First set operation action for all vector types to expand. Then we
164 // will selectively turn on ones that can be effectively codegen'd.
165 for (unsigned VT = (unsigned)MVT::Vector + 1;
166 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
167 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
168 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
169 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
170 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000171 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000172 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000173
174 // FIXME: We don't support any BUILD_VECTOR's yet. We should custom expand
175 // the ones we do, like splat(0.0) and splat(-0.0).
176 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000177 }
178
Nate Begeman425a9692005-11-29 08:17:20 +0000179 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000180 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000181 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000182
Evan Chengd30bf012006-03-01 01:11:20 +0000183 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
184 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
185 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
186 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
187 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
188 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000189 setOperationAction(ISD::LOAD , MVT::v16i8, Legal);
190
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);
Nate Begeman425a9692005-11-29 08:17:20 +0000196 }
197
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000198 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000199 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000200
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000201 // We have target-specific dag combine patterns for the following nodes:
202 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000203 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000204
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000205 computeRegisterProperties();
206}
207
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000208const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
209 switch (Opcode) {
210 default: return 0;
211 case PPCISD::FSEL: return "PPCISD::FSEL";
212 case PPCISD::FCFID: return "PPCISD::FCFID";
213 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
214 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000215 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000216 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
217 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerb2177b92006-03-19 06:55:52 +0000218 case PPCISD::LVE_X: return "PPCISD::LVE_X";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000219 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000220 case PPCISD::Hi: return "PPCISD::Hi";
221 case PPCISD::Lo: return "PPCISD::Lo";
222 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
223 case PPCISD::SRL: return "PPCISD::SRL";
224 case PPCISD::SRA: return "PPCISD::SRA";
225 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000226 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
227 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000228 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000229 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
230 }
231}
232
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000233/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
234static bool isFloatingPointZero(SDOperand Op) {
235 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
236 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
237 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
238 // Maybe this has already been legalized into the constant pool?
239 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
240 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
241 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
242 }
243 return false;
244}
245
Chris Lattneref819f82006-03-20 06:33:01 +0000246
247/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
248/// specifies a splat of a single element that is suitable for input to
249/// VSPLTB/VSPLTH/VSPLTW.
250bool PPC::isSplatShuffleMask(SDNode *N) {
251 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000252
253 // We can only splat 8-bit, 16-bit, and 32-bit quantities.
254 if (N->getNumOperands() != 4 && N->getNumOperands() != 8 &&
255 N->getNumOperands() != 16)
256 return false;
257
Chris Lattner88a99ef2006-03-20 06:37:44 +0000258 // This is a splat operation if each element of the permute is the same, and
259 // if the value doesn't reference the second vector.
260 SDOperand Elt = N->getOperand(0);
261 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
262 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
263 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
264 "Invalid VECTOR_SHUFFLE mask!");
265 if (N->getOperand(i) != Elt) return false;
266 }
267
268 // Make sure it is a splat of the first vector operand.
269 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
Chris Lattneref819f82006-03-20 06:33:01 +0000270}
271
272/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
273/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
274unsigned PPC::getVSPLTImmediate(SDNode *N) {
275 assert(isSplatShuffleMask(N));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000276 return cast<ConstantSDNode>(N->getOperand(0))->getValue();
Chris Lattneref819f82006-03-20 06:33:01 +0000277}
278
279
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000280/// LowerOperation - Provide custom lowering hooks for some operations.
281///
Nate Begeman21e463b2005-10-16 05:39:50 +0000282SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000283 switch (Op.getOpcode()) {
284 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000285 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000286 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000287 SDOperand Src = Op.getOperand(0);
288 if (Src.getValueType() == MVT::f32)
289 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
290
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000291 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000292 switch (Op.getValueType()) {
293 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
294 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000295 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000296 break;
297 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000298 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000299 break;
300 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000301
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000302 // Convert the FP value to an int value through memory.
303 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
304 if (Op.getValueType() == MVT::i32)
305 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
306 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000307 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000308 case ISD::SINT_TO_FP:
309 if (Op.getOperand(0).getValueType() == MVT::i64) {
310 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
311 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
312 if (Op.getValueType() == MVT::f32)
313 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
314 return FP;
315 } else {
316 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
317 "Unhandled SINT_TO_FP type in custom expander!");
318 // Since we only generate this in 64-bit mode, we can take advantage of
319 // 64-bit registers. In particular, sign extend the input value into the
320 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
321 // then lfd it and fcfid it.
322 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
323 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
324 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
325
326 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
327 Op.getOperand(0));
328
329 // STD the extended value into the stack slot.
330 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
331 DAG.getEntryNode(), Ext64, FIdx,
332 DAG.getSrcValue(NULL));
333 // Load the value as a double.
334 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
335
336 // FCFID it and return it.
337 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
338 if (Op.getValueType() == MVT::f32)
339 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
340 return FP;
341 }
342
Chris Lattnerf7605322005-08-31 21:09:52 +0000343 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000344 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000345 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
346 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
347 break;
348
349 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
350
351 // Cannot handle SETEQ/SETNE.
352 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
353
354 MVT::ValueType ResVT = Op.getValueType();
355 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
356 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
357 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000358
Chris Lattnerf7605322005-08-31 21:09:52 +0000359 // If the RHS of the comparison is a 0.0, we don't need to do the
360 // subtraction at all.
361 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000362 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000363 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000364 case ISD::SETULT:
365 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000366 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000367 case ISD::SETUGE:
368 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000369 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
370 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000371 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000372 case ISD::SETUGT:
373 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000374 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000375 case ISD::SETULE:
376 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000377 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
378 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000379 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000380 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000381 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000382
Chris Lattnereb255f22005-10-25 20:54:57 +0000383 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000384 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000385 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000386 case ISD::SETULT:
387 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000388 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
389 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
390 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
391 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000392 case ISD::SETUGE:
393 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000394 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
395 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
396 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
397 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000398 case ISD::SETUGT:
399 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000400 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
401 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
402 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
403 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000404 case ISD::SETULE:
405 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000406 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
407 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
408 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
409 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000410 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000411 break;
412 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000413 case ISD::SHL: {
414 assert(Op.getValueType() == MVT::i64 &&
415 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
416 // The generic code does a fine job expanding shift by a constant.
417 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
418
419 // Otherwise, expand into a bunch of logical ops. Note that these ops
420 // depend on the PPC behavior for oversized shift amounts.
421 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
422 DAG.getConstant(0, MVT::i32));
423 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
424 DAG.getConstant(1, MVT::i32));
425 SDOperand Amt = Op.getOperand(1);
426
427 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
428 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000429 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
430 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000431 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
432 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
433 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000434 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000435 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000436 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000437 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
438 }
439 case ISD::SRL: {
440 assert(Op.getValueType() == MVT::i64 &&
441 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
442 // The generic code does a fine job expanding shift by a constant.
443 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
444
445 // Otherwise, expand into a bunch of logical ops. Note that these ops
446 // depend on the PPC behavior for oversized shift amounts.
447 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
448 DAG.getConstant(0, MVT::i32));
449 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
450 DAG.getConstant(1, MVT::i32));
451 SDOperand Amt = Op.getOperand(1);
452
453 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
454 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000455 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
456 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000457 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
458 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
459 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000460 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000461 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000462 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000463 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
464 }
465 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000466 assert(Op.getValueType() == MVT::i64 &&
467 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
468 // The generic code does a fine job expanding shift by a constant.
469 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
470
471 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
472 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
473 DAG.getConstant(0, MVT::i32));
474 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
475 DAG.getConstant(1, MVT::i32));
476 SDOperand Amt = Op.getOperand(1);
477
478 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
479 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000480 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
481 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000482 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
483 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
484 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000485 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
486 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000487 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
488 Tmp4, Tmp6, ISD::SETLE);
489 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000490 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000491 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000492 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
493 Constant *C = CP->get();
494 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000495 SDOperand Zero = DAG.getConstant(0, MVT::i32);
496
Evan Cheng4c1aa862006-02-22 20:19:42 +0000497 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000498 // Generate non-pic code that has direct accesses to the constant pool.
499 // The address of the global is just (hi(&g)+lo(&g)).
500 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
501 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
502 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
503 }
504
505 // Only lower ConstantPool on Darwin.
506 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
507 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000508 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000509 // With PIC, the first instruction is actually "GR+hi(&G)".
510 Hi = DAG.getNode(ISD::ADD, MVT::i32,
511 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
512 }
513
514 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
515 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
516 return Lo;
517 }
Chris Lattner860e8862005-11-17 07:30:41 +0000518 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000519 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
520 GlobalValue *GV = GSDN->getGlobal();
521 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000522 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000523
Evan Cheng4c1aa862006-02-22 20:19:42 +0000524 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000525 // Generate non-pic code that has direct accesses to globals.
526 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000527 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
528 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
529 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
530 }
Chris Lattner860e8862005-11-17 07:30:41 +0000531
Chris Lattner1d05cb42005-11-17 18:55:48 +0000532 // Only lower GlobalAddress on Darwin.
533 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000534
Chris Lattner860e8862005-11-17 07:30:41 +0000535 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000536 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000537 // With PIC, the first instruction is actually "GR+hi(&G)".
538 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000539 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000540 }
541
542 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
543 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
544
Chris Lattner37dd6f12006-01-29 20:49:17 +0000545 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
546 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000547 return Lo;
548
549 // If the global is weak or external, we have to go through the lazy
550 // resolution stub.
551 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
552 }
Nate Begeman44775902006-01-31 08:17:29 +0000553 case ISD::SETCC: {
554 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000555
556 // If we're comparing for equality to zero, expose the fact that this is
557 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
558 // fold the new nodes.
559 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
560 if (C->isNullValue() && CC == ISD::SETEQ) {
561 MVT::ValueType VT = Op.getOperand(0).getValueType();
562 SDOperand Zext = Op.getOperand(0);
563 if (VT < MVT::i32) {
564 VT = MVT::i32;
565 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
566 }
567 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
568 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
569 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
570 DAG.getConstant(Log2b, getShiftAmountTy()));
571 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
572 }
573 // Leave comparisons against 0 and -1 alone for now, since they're usually
574 // optimized. FIXME: revisit this when we can custom lower all setcc
575 // optimizations.
576 if (C->isAllOnesValue() || C->isNullValue())
577 break;
578 }
579
580 // If we have an integer seteq/setne, turn it into a compare against zero
581 // by subtracting the rhs from the lhs, which is faster than setting a
582 // condition register, reading it back out, and masking the correct bit.
583 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
584 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
585 MVT::ValueType VT = Op.getValueType();
586 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
587 Op.getOperand(1));
588 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
589 }
Nate Begeman44775902006-01-31 08:17:29 +0000590 break;
591 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000592 case ISD::VASTART: {
593 // vastart just stores the address of the VarArgsFrameIndex slot into the
594 // memory location argument.
595 // FIXME: Replace MVT::i32 with PointerTy
596 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
597 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
598 Op.getOperand(1), Op.getOperand(2));
599 }
Nate Begemanee625572006-01-27 21:09:22 +0000600 case ISD::RET: {
601 SDOperand Copy;
602
603 switch(Op.getNumOperands()) {
604 default:
605 assert(0 && "Do not know how to return this many arguments!");
606 abort();
607 case 1:
608 return SDOperand(); // ret void is legal
609 case 2: {
610 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
611 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
612 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
613 SDOperand());
614 break;
615 }
616 case 3:
617 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
618 SDOperand());
619 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
620 break;
621 }
622 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
623 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000624 case ISD::SCALAR_TO_VECTOR: {
625 // Create a stack slot that is 16-byte aligned.
626 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
627 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
628 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
629
630 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000631 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
632 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner23baa1b2006-03-20 22:37:23 +0000633 // LVE_X it out.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000634 return DAG.getNode(PPCISD::LVE_X, Op.getValueType(), Store, FIdx,
635 DAG.getSrcValue(NULL));
636 }
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000637 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000638 SDOperand V1 = Op.getOperand(0);
639 SDOperand V2 = Op.getOperand(1);
640 SDOperand PermMask = Op.getOperand(2);
641
642 // Cases that are handled by instructions that take permute immediates
643 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
644 // selected by the instruction selector.
645 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
646 break;
647
648 // TODO: Handle more cases, and also handle cases that are cheaper to do as
649 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000650
651 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
652 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000653 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000654
655 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
656 // that it is in input element units, not in bytes. Convert now.
657 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
658 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
659
660 std::vector<SDOperand> ResultMask;
661 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
662 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
663
664 for (unsigned j = 0; j != BytesPerElement; ++j)
665 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
666 MVT::i8));
667 }
668
669 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
670 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
671 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000672 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000673 return SDOperand();
674}
675
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000676std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000677PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000678 //
679 // add beautiful description of PPC stack frame format, or at least some docs
680 //
681 MachineFunction &MF = DAG.getMachineFunction();
682 MachineFrameInfo *MFI = MF.getFrameInfo();
683 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000684 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000685 std::vector<SDOperand> ArgValues;
686
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000687 unsigned ArgOffset = 24;
688 unsigned GPR_remaining = 8;
689 unsigned FPR_remaining = 13;
690 unsigned GPR_idx = 0, FPR_idx = 0;
691 static const unsigned GPR[] = {
692 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
693 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
694 };
695 static const unsigned FPR[] = {
696 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
697 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
698 };
699
700 // Add DAG nodes to load the arguments... On entry to a function on PPC,
701 // the arguments start at offset 24, although they are likely to be passed
702 // in registers.
703 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
704 SDOperand newroot, argt;
705 unsigned ObjSize;
706 bool needsLoad = false;
707 bool ArgLive = !I->use_empty();
708 MVT::ValueType ObjectVT = getValueType(I->getType());
709
710 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000711 default: assert(0 && "Unhandled argument type!");
712 case MVT::i1:
713 case MVT::i8:
714 case MVT::i16:
715 case MVT::i32:
716 ObjSize = 4;
717 if (!ArgLive) break;
718 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000719 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000720 MF.addLiveIn(GPR[GPR_idx], VReg);
721 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000722 if (ObjectVT != MVT::i32) {
723 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
724 : ISD::AssertZext;
725 argt = DAG.getNode(AssertOp, MVT::i32, argt,
726 DAG.getValueType(ObjectVT));
727 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
728 }
Chris Lattner915fb302005-08-30 00:19:00 +0000729 } else {
730 needsLoad = true;
731 }
732 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000733 case MVT::i64:
734 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000735 if (!ArgLive) break;
736 if (GPR_remaining > 0) {
737 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000738 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000739 MF.addLiveIn(GPR[GPR_idx], VReg);
740 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000741 // If we have two or more remaining argument registers, then both halves
742 // of the i64 can be sourced from there. Otherwise, the lower half will
743 // have to come off the stack. This can happen when an i64 is preceded
744 // by 28 bytes of arguments.
745 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000746 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000747 MF.addLiveIn(GPR[GPR_idx+1], VReg);
748 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000749 } else {
750 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
751 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
752 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
753 DAG.getSrcValue(NULL));
754 }
755 // Build the outgoing arg thingy
756 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
757 newroot = argLo;
758 } else {
759 needsLoad = true;
760 }
761 break;
762 case MVT::f32:
763 case MVT::f64:
764 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000765 if (!ArgLive) {
766 if (FPR_remaining > 0) {
767 --FPR_remaining;
768 ++FPR_idx;
769 }
770 break;
771 }
Chris Lattner915fb302005-08-30 00:19:00 +0000772 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000773 unsigned VReg;
774 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000775 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000776 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000777 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000778 MF.addLiveIn(FPR[FPR_idx], VReg);
779 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000780 --FPR_remaining;
781 ++FPR_idx;
782 } else {
783 needsLoad = true;
784 }
785 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000786 }
787
788 // We need to load the argument to a virtual register if we determined above
789 // that we ran out of physical registers of the appropriate type
790 if (needsLoad) {
791 unsigned SubregOffset = 0;
792 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
793 if (ObjectVT == MVT::i16) SubregOffset = 2;
794 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
795 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
796 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
797 DAG.getConstant(SubregOffset, MVT::i32));
798 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
799 DAG.getSrcValue(NULL));
800 }
801
802 // Every 4 bytes of argument space consumes one of the GPRs available for
803 // argument passing.
804 if (GPR_remaining > 0) {
805 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
806 GPR_remaining -= delta;
807 GPR_idx += delta;
808 }
809 ArgOffset += ObjSize;
810 if (newroot.Val)
811 DAG.setRoot(newroot.getValue(1));
812
813 ArgValues.push_back(argt);
814 }
815
816 // If the function takes variable number of arguments, make a frame index for
817 // the start of the first vararg value... for expansion of llvm.va_start.
818 if (F.isVarArg()) {
819 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
820 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
821 // If this function is vararg, store any remaining integer argument regs
822 // to their spots on the stack so that they may be loaded by deferencing the
823 // result of va_next.
824 std::vector<SDOperand> MemOps;
825 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000826 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000827 MF.addLiveIn(GPR[GPR_idx], VReg);
828 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000829 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
830 Val, FIN, DAG.getSrcValue(NULL));
831 MemOps.push_back(Store);
832 // Increment the address by four for the next argument to store
833 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
834 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
835 }
Chris Lattner80720a92005-11-30 20:40:54 +0000836 if (!MemOps.empty()) {
837 MemOps.push_back(DAG.getRoot());
838 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
839 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000840 }
841
842 // Finally, inform the code generator which regs we return values in.
843 switch (getValueType(F.getReturnType())) {
844 default: assert(0 && "Unknown type!");
845 case MVT::isVoid: break;
846 case MVT::i1:
847 case MVT::i8:
848 case MVT::i16:
849 case MVT::i32:
850 MF.addLiveOut(PPC::R3);
851 break;
852 case MVT::i64:
853 MF.addLiveOut(PPC::R3);
854 MF.addLiveOut(PPC::R4);
855 break;
856 case MVT::f32:
857 case MVT::f64:
858 MF.addLiveOut(PPC::F1);
859 break;
860 }
861
862 return ArgValues;
863}
864
865std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000866PPCTargetLowering::LowerCallTo(SDOperand Chain,
867 const Type *RetTy, bool isVarArg,
868 unsigned CallingConv, bool isTailCall,
869 SDOperand Callee, ArgListTy &Args,
870 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000871 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000872 // SelectExpr to use to put the arguments in the appropriate registers.
873 std::vector<SDOperand> args_to_use;
874
875 // Count how many bytes are to be pushed on the stack, including the linkage
876 // area, and parameter passing area.
877 unsigned NumBytes = 24;
878
879 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000880 Chain = DAG.getCALLSEQ_START(Chain,
881 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000882 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000883 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000884 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000885 default: assert(0 && "Unknown value type!");
886 case MVT::i1:
887 case MVT::i8:
888 case MVT::i16:
889 case MVT::i32:
890 case MVT::f32:
891 NumBytes += 4;
892 break;
893 case MVT::i64:
894 case MVT::f64:
895 NumBytes += 8;
896 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000897 }
Chris Lattner915fb302005-08-30 00:19:00 +0000898 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000899
Chris Lattner915fb302005-08-30 00:19:00 +0000900 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
901 // plus 32 bytes of argument space in case any called code gets funky on us.
902 // (Required by ABI to support var arg)
903 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000904
905 // Adjust the stack pointer for the new arguments...
906 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000907 Chain = DAG.getCALLSEQ_START(Chain,
908 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000909
910 // Set up a copy of the stack pointer for use loading and storing any
911 // arguments that may not fit in the registers available for argument
912 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000913 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000914
915 // Figure out which arguments are going to go in registers, and which in
916 // memory. Also, if this is a vararg function, floating point operations
917 // must be stored to our stack, and loaded into integer regs as well, if
918 // any integer regs are available for argument passing.
919 unsigned ArgOffset = 24;
920 unsigned GPR_remaining = 8;
921 unsigned FPR_remaining = 13;
922
923 std::vector<SDOperand> MemOps;
924 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
925 // PtrOff will be used to store the current argument to the stack if a
926 // register cannot be found for it.
927 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
928 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
929 MVT::ValueType ArgVT = getValueType(Args[i].second);
930
931 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000932 default: assert(0 && "Unexpected ValueType for argument!");
933 case MVT::i1:
934 case MVT::i8:
935 case MVT::i16:
936 // Promote the integer to 32 bits. If the input type is signed use a
937 // sign extend, otherwise use a zero extend.
938 if (Args[i].second->isSigned())
939 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
940 else
941 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
942 // FALL THROUGH
943 case MVT::i32:
944 if (GPR_remaining > 0) {
945 args_to_use.push_back(Args[i].first);
946 --GPR_remaining;
947 } else {
948 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
949 Args[i].first, PtrOff,
950 DAG.getSrcValue(NULL)));
951 }
952 ArgOffset += 4;
953 break;
954 case MVT::i64:
955 // If we have one free GPR left, we can place the upper half of the i64
956 // in it, and store the other half to the stack. If we have two or more
957 // free GPRs, then we can pass both halves of the i64 in registers.
958 if (GPR_remaining > 0) {
959 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
960 Args[i].first, DAG.getConstant(1, MVT::i32));
961 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
962 Args[i].first, DAG.getConstant(0, MVT::i32));
963 args_to_use.push_back(Hi);
964 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000965 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000966 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000967 --GPR_remaining;
968 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000969 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
970 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000971 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000972 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000973 }
Chris Lattner915fb302005-08-30 00:19:00 +0000974 } else {
975 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
976 Args[i].first, PtrOff,
977 DAG.getSrcValue(NULL)));
978 }
979 ArgOffset += 8;
980 break;
981 case MVT::f32:
982 case MVT::f64:
983 if (FPR_remaining > 0) {
984 args_to_use.push_back(Args[i].first);
985 --FPR_remaining;
986 if (isVarArg) {
987 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
988 Args[i].first, PtrOff,
989 DAG.getSrcValue(NULL));
990 MemOps.push_back(Store);
991 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000992 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000993 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
994 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000995 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000996 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000997 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000998 }
999 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001000 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1001 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001002 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1003 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001004 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001005 args_to_use.push_back(Load);
1006 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001007 }
1008 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001009 // If we have any FPRs remaining, we may also have GPRs remaining.
1010 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1011 // GPRs.
1012 if (GPR_remaining > 0) {
1013 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1014 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001015 }
Chris Lattner915fb302005-08-30 00:19:00 +00001016 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1017 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1018 --GPR_remaining;
1019 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001020 }
Chris Lattner915fb302005-08-30 00:19:00 +00001021 } else {
1022 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1023 Args[i].first, PtrOff,
1024 DAG.getSrcValue(NULL)));
1025 }
1026 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1027 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001028 }
1029 }
1030 if (!MemOps.empty())
1031 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1032 }
1033
1034 std::vector<MVT::ValueType> RetVals;
1035 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001036 MVT::ValueType ActualRetTyVT = RetTyVT;
1037 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1038 ActualRetTyVT = MVT::i32; // Promote result to i32.
1039
Chris Lattnere00ebf02006-01-28 07:33:03 +00001040 if (RetTyVT == MVT::i64) {
1041 RetVals.push_back(MVT::i32);
1042 RetVals.push_back(MVT::i32);
1043 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001044 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001045 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001046 RetVals.push_back(MVT::Other);
1047
Chris Lattner2823b3e2005-11-17 05:56:14 +00001048 // If the callee is a GlobalAddress node (quite common, every direct call is)
1049 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1050 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1051 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1052
Chris Lattner281b55e2006-01-27 23:34:02 +00001053 std::vector<SDOperand> Ops;
1054 Ops.push_back(Chain);
1055 Ops.push_back(Callee);
1056 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1057 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001058 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001059 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1060 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001061 SDOperand RetVal = TheCall;
1062
1063 // If the result is a small value, add a note so that we keep track of the
1064 // information about whether it is sign or zero extended.
1065 if (RetTyVT != ActualRetTyVT) {
1066 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1067 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1068 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001069 } else if (RetTyVT == MVT::i64) {
1070 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001071 }
1072
1073 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001074}
1075
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001076MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001077PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1078 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001079 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001080 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1081 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001082 "Unexpected instr type to insert");
1083
1084 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1085 // control-flow pattern. The incoming instruction knows the destination vreg
1086 // to set, the condition code register to branch on, the true/false values to
1087 // select between, and a branch opcode to use.
1088 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1089 ilist<MachineBasicBlock>::iterator It = BB;
1090 ++It;
1091
1092 // thisMBB:
1093 // ...
1094 // TrueVal = ...
1095 // cmpTY ccX, r1, r2
1096 // bCC copy1MBB
1097 // fallthrough --> copy0MBB
1098 MachineBasicBlock *thisMBB = BB;
1099 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1100 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1101 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1102 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1103 MachineFunction *F = BB->getParent();
1104 F->getBasicBlockList().insert(It, copy0MBB);
1105 F->getBasicBlockList().insert(It, sinkMBB);
1106 // Update machine-CFG edges
1107 BB->addSuccessor(copy0MBB);
1108 BB->addSuccessor(sinkMBB);
1109
1110 // copy0MBB:
1111 // %FalseValue = ...
1112 // # fallthrough to sinkMBB
1113 BB = copy0MBB;
1114
1115 // Update machine-CFG edges
1116 BB->addSuccessor(sinkMBB);
1117
1118 // sinkMBB:
1119 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1120 // ...
1121 BB = sinkMBB;
1122 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1123 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1124 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1125
1126 delete MI; // The pseudo instruction is gone now.
1127 return BB;
1128}
1129
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001130SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1131 DAGCombinerInfo &DCI) const {
1132 TargetMachine &TM = getTargetMachine();
1133 SelectionDAG &DAG = DCI.DAG;
1134 switch (N->getOpcode()) {
1135 default: break;
1136 case ISD::SINT_TO_FP:
1137 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001138 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1139 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1140 // We allow the src/dst to be either f32/f64, but the intermediate
1141 // type must be i64.
1142 if (N->getOperand(0).getValueType() == MVT::i64) {
1143 SDOperand Val = N->getOperand(0).getOperand(0);
1144 if (Val.getValueType() == MVT::f32) {
1145 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1146 DCI.AddToWorklist(Val.Val);
1147 }
1148
1149 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001150 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001151 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001152 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001153 if (N->getValueType(0) == MVT::f32) {
1154 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1155 DCI.AddToWorklist(Val.Val);
1156 }
1157 return Val;
1158 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1159 // If the intermediate type is i32, we can avoid the load/store here
1160 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001161 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001162 }
1163 }
1164 break;
Chris Lattner51269842006-03-01 05:50:56 +00001165 case ISD::STORE:
1166 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1167 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1168 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1169 N->getOperand(1).getValueType() == MVT::i32) {
1170 SDOperand Val = N->getOperand(1).getOperand(0);
1171 if (Val.getValueType() == MVT::f32) {
1172 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1173 DCI.AddToWorklist(Val.Val);
1174 }
1175 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1176 DCI.AddToWorklist(Val.Val);
1177
1178 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1179 N->getOperand(2), N->getOperand(3));
1180 DCI.AddToWorklist(Val.Val);
1181 return Val;
1182 }
1183 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001184 }
1185
1186 return SDOperand();
1187}
1188
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001189/// getConstraintType - Given a constraint letter, return the type of
1190/// constraint it is for this target.
1191PPCTargetLowering::ConstraintType
1192PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1193 switch (ConstraintLetter) {
1194 default: break;
1195 case 'b':
1196 case 'r':
1197 case 'f':
1198 case 'v':
1199 case 'y':
1200 return C_RegisterClass;
1201 }
1202 return TargetLowering::getConstraintType(ConstraintLetter);
1203}
1204
1205
Chris Lattnerddc787d2006-01-31 19:20:21 +00001206std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001207getRegClassForInlineAsmConstraint(const std::string &Constraint,
1208 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001209 if (Constraint.size() == 1) {
1210 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1211 default: break; // Unknown constriant letter
1212 case 'b':
1213 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1214 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1215 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1216 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1217 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1218 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1219 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1220 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1221 0);
1222 case 'r':
1223 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1224 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1225 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1226 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1227 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1228 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1229 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1230 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1231 0);
1232 case 'f':
1233 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1234 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1235 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1236 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1237 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1238 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1239 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1240 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1241 0);
1242 case 'v':
1243 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1244 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1245 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1246 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1247 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1248 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1249 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1250 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1251 0);
1252 case 'y':
1253 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1254 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1255 0);
1256 }
1257 }
1258
Chris Lattner1efa40f2006-02-22 00:56:39 +00001259 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001260}
Chris Lattner763317d2006-02-07 00:47:13 +00001261
1262// isOperandValidForConstraint
1263bool PPCTargetLowering::
1264isOperandValidForConstraint(SDOperand Op, char Letter) {
1265 switch (Letter) {
1266 default: break;
1267 case 'I':
1268 case 'J':
1269 case 'K':
1270 case 'L':
1271 case 'M':
1272 case 'N':
1273 case 'O':
1274 case 'P': {
1275 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1276 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1277 switch (Letter) {
1278 default: assert(0 && "Unknown constraint letter!");
1279 case 'I': // "I" is a signed 16-bit constant.
1280 return (short)Value == (int)Value;
1281 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1282 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1283 return (short)Value == 0;
1284 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1285 return (Value >> 16) == 0;
1286 case 'M': // "M" is a constant that is greater than 31.
1287 return Value > 31;
1288 case 'N': // "N" is a positive constant that is an exact power of two.
1289 return (int)Value > 0 && isPowerOf2_32(Value);
1290 case 'O': // "O" is the constant zero.
1291 return Value == 0;
1292 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1293 return (short)-Value == (int)-Value;
1294 }
1295 break;
1296 }
1297 }
1298
1299 // Handle standard constraint letters.
1300 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1301}
Evan Chengc4c62572006-03-13 23:20:37 +00001302
1303/// isLegalAddressImmediate - Return true if the integer value can be used
1304/// as the offset of the target addressing mode.
1305bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1306 // PPC allows a sign-extended 16-bit immediate field.
1307 return (V > -(1 << 16) && V < (1 << 16)-1);
1308}