blob: ee41ed13b4640e21bcfef6d7972460093148cdf3 [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);
Nate Begemanae749a92005-10-25 23:48:36 +0000143 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
144 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
145 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000146 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000147 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000148 }
149
150 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
151 // 64 bit PowerPC implementations can support i64 types directly
152 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000153 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
154 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000155 } else {
156 // 32 bit PowerPC wants to expand i64 shifts itself.
157 setOperationAction(ISD::SHL, MVT::i64, Custom);
158 setOperationAction(ISD::SRL, MVT::i64, Custom);
159 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000160 }
161
Evan Chengd30bf012006-03-01 01:11:20 +0000162 // First set operation action for all vector types to expand. Then we
163 // will selectively turn on ones that can be effectively codegen'd.
164 for (unsigned VT = (unsigned)MVT::Vector + 1;
165 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
166 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
167 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
168 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
169 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000170 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
171
172 // FIXME: We don't support any BUILD_VECTOR's yet. We should custom expand
173 // the ones we do, like splat(0.0) and splat(-0.0).
174 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000175 }
176
Nate Begeman425a9692005-11-29 08:17:20 +0000177 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000178 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000179 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000180
Evan Chengd30bf012006-03-01 01:11:20 +0000181 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
182 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
183 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
184 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
185 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
186 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000187 setOperationAction(ISD::LOAD , MVT::v16i8, Legal);
188
189 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
190 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
191
Chris Lattnerb2177b92006-03-19 06:55:52 +0000192 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
193 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000194 }
195
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000196 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000197 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000198
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000199 // We have target-specific dag combine patterns for the following nodes:
200 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000201 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000202
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000203 computeRegisterProperties();
204}
205
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000206const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
207 switch (Opcode) {
208 default: return 0;
209 case PPCISD::FSEL: return "PPCISD::FSEL";
210 case PPCISD::FCFID: return "PPCISD::FCFID";
211 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
212 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000213 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000214 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
215 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerb2177b92006-03-19 06:55:52 +0000216 case PPCISD::LVE_X: return "PPCISD::LVE_X";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000217 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000218 case PPCISD::Hi: return "PPCISD::Hi";
219 case PPCISD::Lo: return "PPCISD::Lo";
220 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
221 case PPCISD::SRL: return "PPCISD::SRL";
222 case PPCISD::SRA: return "PPCISD::SRA";
223 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000224 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000225 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
226 }
227}
228
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000229/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
230static bool isFloatingPointZero(SDOperand Op) {
231 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
232 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
233 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
234 // Maybe this has already been legalized into the constant pool?
235 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
236 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
237 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
238 }
239 return false;
240}
241
Chris Lattneref819f82006-03-20 06:33:01 +0000242
243/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
244/// specifies a splat of a single element that is suitable for input to
245/// VSPLTB/VSPLTH/VSPLTW.
246bool PPC::isSplatShuffleMask(SDNode *N) {
247 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000248
249 // We can only splat 8-bit, 16-bit, and 32-bit quantities.
250 if (N->getNumOperands() != 4 && N->getNumOperands() != 8 &&
251 N->getNumOperands() != 16)
252 return false;
253
Chris Lattner88a99ef2006-03-20 06:37:44 +0000254 // This is a splat operation if each element of the permute is the same, and
255 // if the value doesn't reference the second vector.
256 SDOperand Elt = N->getOperand(0);
257 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
258 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
259 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
260 "Invalid VECTOR_SHUFFLE mask!");
261 if (N->getOperand(i) != Elt) return false;
262 }
263
264 // Make sure it is a splat of the first vector operand.
265 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
Chris Lattneref819f82006-03-20 06:33:01 +0000266}
267
268/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
269/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
270unsigned PPC::getVSPLTImmediate(SDNode *N) {
271 assert(isSplatShuffleMask(N));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000272 return cast<ConstantSDNode>(N->getOperand(0))->getValue();
Chris Lattneref819f82006-03-20 06:33:01 +0000273}
274
275
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000276/// LowerOperation - Provide custom lowering hooks for some operations.
277///
Nate Begeman21e463b2005-10-16 05:39:50 +0000278SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000279 switch (Op.getOpcode()) {
280 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000281 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000282 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000283 SDOperand Src = Op.getOperand(0);
284 if (Src.getValueType() == MVT::f32)
285 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
286
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000287 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000288 switch (Op.getValueType()) {
289 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
290 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000291 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000292 break;
293 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000294 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000295 break;
296 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000297
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000298 // Convert the FP value to an int value through memory.
299 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
300 if (Op.getValueType() == MVT::i32)
301 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
302 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000303 }
304 case ISD::SINT_TO_FP: {
305 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
306 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000307 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
308 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000309 if (MVT::f32 == Op.getValueType())
310 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
311 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000312 }
313 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000314 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000315 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
316 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
317 break;
318
319 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
320
321 // Cannot handle SETEQ/SETNE.
322 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
323
324 MVT::ValueType ResVT = Op.getValueType();
325 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
326 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
327 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000328
Chris Lattnerf7605322005-08-31 21:09:52 +0000329 // If the RHS of the comparison is a 0.0, we don't need to do the
330 // subtraction at all.
331 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000332 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000333 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000334 case ISD::SETULT:
335 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000336 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000337 case ISD::SETUGE:
338 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000339 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
340 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000341 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000342 case ISD::SETUGT:
343 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000344 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000345 case ISD::SETULE:
346 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000347 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
348 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000349 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000350 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000351 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000352
Chris Lattnereb255f22005-10-25 20:54:57 +0000353 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000354 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000355 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000356 case ISD::SETULT:
357 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000358 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
359 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
360 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
361 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000362 case ISD::SETUGE:
363 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000364 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
365 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
366 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
367 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000368 case ISD::SETUGT:
369 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000370 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
371 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
372 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
373 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000374 case ISD::SETULE:
375 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000376 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
377 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
378 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
379 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000380 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000381 break;
382 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000383 case ISD::SHL: {
384 assert(Op.getValueType() == MVT::i64 &&
385 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
386 // The generic code does a fine job expanding shift by a constant.
387 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
388
389 // Otherwise, expand into a bunch of logical ops. Note that these ops
390 // depend on the PPC behavior for oversized shift amounts.
391 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
392 DAG.getConstant(0, MVT::i32));
393 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
394 DAG.getConstant(1, MVT::i32));
395 SDOperand Amt = Op.getOperand(1);
396
397 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
398 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000399 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
400 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000401 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
402 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
403 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000404 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000405 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000406 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000407 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
408 }
409 case ISD::SRL: {
410 assert(Op.getValueType() == MVT::i64 &&
411 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
412 // The generic code does a fine job expanding shift by a constant.
413 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
414
415 // Otherwise, expand into a bunch of logical ops. Note that these ops
416 // depend on the PPC behavior for oversized shift amounts.
417 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
418 DAG.getConstant(0, MVT::i32));
419 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
420 DAG.getConstant(1, MVT::i32));
421 SDOperand Amt = Op.getOperand(1);
422
423 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
424 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000425 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
426 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000427 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
428 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
429 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000430 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000431 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000432 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000433 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
434 }
435 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000436 assert(Op.getValueType() == MVT::i64 &&
437 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
438 // The generic code does a fine job expanding shift by a constant.
439 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
440
441 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
442 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
443 DAG.getConstant(0, MVT::i32));
444 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
445 DAG.getConstant(1, MVT::i32));
446 SDOperand Amt = Op.getOperand(1);
447
448 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
449 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000450 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
451 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000452 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
453 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
454 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000455 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
456 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000457 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
458 Tmp4, Tmp6, ISD::SETLE);
459 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000460 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000461 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000462 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
463 Constant *C = CP->get();
464 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000465 SDOperand Zero = DAG.getConstant(0, MVT::i32);
466
Evan Cheng4c1aa862006-02-22 20:19:42 +0000467 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000468 // Generate non-pic code that has direct accesses to the constant pool.
469 // The address of the global is just (hi(&g)+lo(&g)).
470 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
471 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
472 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
473 }
474
475 // Only lower ConstantPool on Darwin.
476 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
477 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000478 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000479 // With PIC, the first instruction is actually "GR+hi(&G)".
480 Hi = DAG.getNode(ISD::ADD, MVT::i32,
481 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
482 }
483
484 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
485 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
486 return Lo;
487 }
Chris Lattner860e8862005-11-17 07:30:41 +0000488 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000489 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
490 GlobalValue *GV = GSDN->getGlobal();
491 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000492 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000493
Evan Cheng4c1aa862006-02-22 20:19:42 +0000494 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000495 // Generate non-pic code that has direct accesses to globals.
496 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000497 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
498 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
499 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
500 }
Chris Lattner860e8862005-11-17 07:30:41 +0000501
Chris Lattner1d05cb42005-11-17 18:55:48 +0000502 // Only lower GlobalAddress on Darwin.
503 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000504
Chris Lattner860e8862005-11-17 07:30:41 +0000505 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000506 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000507 // With PIC, the first instruction is actually "GR+hi(&G)".
508 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000509 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000510 }
511
512 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
513 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
514
Chris Lattner37dd6f12006-01-29 20:49:17 +0000515 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
516 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000517 return Lo;
518
519 // If the global is weak or external, we have to go through the lazy
520 // resolution stub.
521 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
522 }
Nate Begeman44775902006-01-31 08:17:29 +0000523 case ISD::SETCC: {
524 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000525
526 // If we're comparing for equality to zero, expose the fact that this is
527 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
528 // fold the new nodes.
529 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
530 if (C->isNullValue() && CC == ISD::SETEQ) {
531 MVT::ValueType VT = Op.getOperand(0).getValueType();
532 SDOperand Zext = Op.getOperand(0);
533 if (VT < MVT::i32) {
534 VT = MVT::i32;
535 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
536 }
537 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
538 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
539 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
540 DAG.getConstant(Log2b, getShiftAmountTy()));
541 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
542 }
543 // Leave comparisons against 0 and -1 alone for now, since they're usually
544 // optimized. FIXME: revisit this when we can custom lower all setcc
545 // optimizations.
546 if (C->isAllOnesValue() || C->isNullValue())
547 break;
548 }
549
550 // If we have an integer seteq/setne, turn it into a compare against zero
551 // by subtracting the rhs from the lhs, which is faster than setting a
552 // condition register, reading it back out, and masking the correct bit.
553 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
554 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
555 MVT::ValueType VT = Op.getValueType();
556 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
557 Op.getOperand(1));
558 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
559 }
Nate Begeman44775902006-01-31 08:17:29 +0000560 break;
561 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000562 case ISD::VASTART: {
563 // vastart just stores the address of the VarArgsFrameIndex slot into the
564 // memory location argument.
565 // FIXME: Replace MVT::i32 with PointerTy
566 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
567 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
568 Op.getOperand(1), Op.getOperand(2));
569 }
Nate Begemanee625572006-01-27 21:09:22 +0000570 case ISD::RET: {
571 SDOperand Copy;
572
573 switch(Op.getNumOperands()) {
574 default:
575 assert(0 && "Do not know how to return this many arguments!");
576 abort();
577 case 1:
578 return SDOperand(); // ret void is legal
579 case 2: {
580 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
581 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
582 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
583 SDOperand());
584 break;
585 }
586 case 3:
587 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
588 SDOperand());
589 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
590 break;
591 }
592 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
593 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000594 case ISD::SCALAR_TO_VECTOR: {
595 // Create a stack slot that is 16-byte aligned.
596 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
597 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
598 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
599
600 // Store the input value into Value#0 of the stack slot.
601 unsigned InSize = MVT::getSizeInBits(Op.getOperand(0).getValueType())/8;
602 FIdx = DAG.getNode(ISD::ADD, MVT::i32, FIdx,
603 DAG.getConstant(16-InSize, MVT::i32));
604 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
605 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
606 return DAG.getNode(PPCISD::LVE_X, Op.getValueType(), Store, FIdx,
607 DAG.getSrcValue(NULL));
608 }
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000609 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000610 SDOperand V1 = Op.getOperand(0);
611 SDOperand V2 = Op.getOperand(1);
612 SDOperand PermMask = Op.getOperand(2);
613
614 // Cases that are handled by instructions that take permute immediates
615 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
616 // selected by the instruction selector.
617 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
618 break;
619
620 // TODO: Handle more cases, and also handle cases that are cheaper to do as
621 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000622
623 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
624 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000625 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000626
627 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
628 // that it is in input element units, not in bytes. Convert now.
629 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
630 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
631
632 std::vector<SDOperand> ResultMask;
633 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
634 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
635
636 for (unsigned j = 0; j != BytesPerElement; ++j)
637 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
638 MVT::i8));
639 }
640
641 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
642 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
643 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000644 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000645 return SDOperand();
646}
647
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000648std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000649PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000650 //
651 // add beautiful description of PPC stack frame format, or at least some docs
652 //
653 MachineFunction &MF = DAG.getMachineFunction();
654 MachineFrameInfo *MFI = MF.getFrameInfo();
655 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000656 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000657 std::vector<SDOperand> ArgValues;
658
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000659 unsigned ArgOffset = 24;
660 unsigned GPR_remaining = 8;
661 unsigned FPR_remaining = 13;
662 unsigned GPR_idx = 0, FPR_idx = 0;
663 static const unsigned GPR[] = {
664 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
665 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
666 };
667 static const unsigned FPR[] = {
668 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
669 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
670 };
671
672 // Add DAG nodes to load the arguments... On entry to a function on PPC,
673 // the arguments start at offset 24, although they are likely to be passed
674 // in registers.
675 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
676 SDOperand newroot, argt;
677 unsigned ObjSize;
678 bool needsLoad = false;
679 bool ArgLive = !I->use_empty();
680 MVT::ValueType ObjectVT = getValueType(I->getType());
681
682 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000683 default: assert(0 && "Unhandled argument type!");
684 case MVT::i1:
685 case MVT::i8:
686 case MVT::i16:
687 case MVT::i32:
688 ObjSize = 4;
689 if (!ArgLive) break;
690 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000691 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000692 MF.addLiveIn(GPR[GPR_idx], VReg);
693 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000694 if (ObjectVT != MVT::i32) {
695 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
696 : ISD::AssertZext;
697 argt = DAG.getNode(AssertOp, MVT::i32, argt,
698 DAG.getValueType(ObjectVT));
699 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
700 }
Chris Lattner915fb302005-08-30 00:19:00 +0000701 } else {
702 needsLoad = true;
703 }
704 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000705 case MVT::i64:
706 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000707 if (!ArgLive) break;
708 if (GPR_remaining > 0) {
709 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000710 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000711 MF.addLiveIn(GPR[GPR_idx], VReg);
712 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000713 // If we have two or more remaining argument registers, then both halves
714 // of the i64 can be sourced from there. Otherwise, the lower half will
715 // have to come off the stack. This can happen when an i64 is preceded
716 // by 28 bytes of arguments.
717 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000718 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000719 MF.addLiveIn(GPR[GPR_idx+1], VReg);
720 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000721 } else {
722 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
723 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
724 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
725 DAG.getSrcValue(NULL));
726 }
727 // Build the outgoing arg thingy
728 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
729 newroot = argLo;
730 } else {
731 needsLoad = true;
732 }
733 break;
734 case MVT::f32:
735 case MVT::f64:
736 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000737 if (!ArgLive) {
738 if (FPR_remaining > 0) {
739 --FPR_remaining;
740 ++FPR_idx;
741 }
742 break;
743 }
Chris Lattner915fb302005-08-30 00:19:00 +0000744 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000745 unsigned VReg;
746 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000747 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000748 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000749 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000750 MF.addLiveIn(FPR[FPR_idx], VReg);
751 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000752 --FPR_remaining;
753 ++FPR_idx;
754 } else {
755 needsLoad = true;
756 }
757 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000758 }
759
760 // We need to load the argument to a virtual register if we determined above
761 // that we ran out of physical registers of the appropriate type
762 if (needsLoad) {
763 unsigned SubregOffset = 0;
764 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
765 if (ObjectVT == MVT::i16) SubregOffset = 2;
766 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
767 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
768 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
769 DAG.getConstant(SubregOffset, MVT::i32));
770 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
771 DAG.getSrcValue(NULL));
772 }
773
774 // Every 4 bytes of argument space consumes one of the GPRs available for
775 // argument passing.
776 if (GPR_remaining > 0) {
777 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
778 GPR_remaining -= delta;
779 GPR_idx += delta;
780 }
781 ArgOffset += ObjSize;
782 if (newroot.Val)
783 DAG.setRoot(newroot.getValue(1));
784
785 ArgValues.push_back(argt);
786 }
787
788 // If the function takes variable number of arguments, make a frame index for
789 // the start of the first vararg value... for expansion of llvm.va_start.
790 if (F.isVarArg()) {
791 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
792 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
793 // If this function is vararg, store any remaining integer argument regs
794 // to their spots on the stack so that they may be loaded by deferencing the
795 // result of va_next.
796 std::vector<SDOperand> MemOps;
797 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000798 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000799 MF.addLiveIn(GPR[GPR_idx], VReg);
800 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000801 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
802 Val, FIN, DAG.getSrcValue(NULL));
803 MemOps.push_back(Store);
804 // Increment the address by four for the next argument to store
805 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
806 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
807 }
Chris Lattner80720a92005-11-30 20:40:54 +0000808 if (!MemOps.empty()) {
809 MemOps.push_back(DAG.getRoot());
810 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
811 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000812 }
813
814 // Finally, inform the code generator which regs we return values in.
815 switch (getValueType(F.getReturnType())) {
816 default: assert(0 && "Unknown type!");
817 case MVT::isVoid: break;
818 case MVT::i1:
819 case MVT::i8:
820 case MVT::i16:
821 case MVT::i32:
822 MF.addLiveOut(PPC::R3);
823 break;
824 case MVT::i64:
825 MF.addLiveOut(PPC::R3);
826 MF.addLiveOut(PPC::R4);
827 break;
828 case MVT::f32:
829 case MVT::f64:
830 MF.addLiveOut(PPC::F1);
831 break;
832 }
833
834 return ArgValues;
835}
836
837std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000838PPCTargetLowering::LowerCallTo(SDOperand Chain,
839 const Type *RetTy, bool isVarArg,
840 unsigned CallingConv, bool isTailCall,
841 SDOperand Callee, ArgListTy &Args,
842 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000843 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000844 // SelectExpr to use to put the arguments in the appropriate registers.
845 std::vector<SDOperand> args_to_use;
846
847 // Count how many bytes are to be pushed on the stack, including the linkage
848 // area, and parameter passing area.
849 unsigned NumBytes = 24;
850
851 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000852 Chain = DAG.getCALLSEQ_START(Chain,
853 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000854 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000855 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000856 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000857 default: assert(0 && "Unknown value type!");
858 case MVT::i1:
859 case MVT::i8:
860 case MVT::i16:
861 case MVT::i32:
862 case MVT::f32:
863 NumBytes += 4;
864 break;
865 case MVT::i64:
866 case MVT::f64:
867 NumBytes += 8;
868 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000869 }
Chris Lattner915fb302005-08-30 00:19:00 +0000870 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000871
Chris Lattner915fb302005-08-30 00:19:00 +0000872 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
873 // plus 32 bytes of argument space in case any called code gets funky on us.
874 // (Required by ABI to support var arg)
875 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000876
877 // Adjust the stack pointer for the new arguments...
878 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000879 Chain = DAG.getCALLSEQ_START(Chain,
880 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000881
882 // Set up a copy of the stack pointer for use loading and storing any
883 // arguments that may not fit in the registers available for argument
884 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000885 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000886
887 // Figure out which arguments are going to go in registers, and which in
888 // memory. Also, if this is a vararg function, floating point operations
889 // must be stored to our stack, and loaded into integer regs as well, if
890 // any integer regs are available for argument passing.
891 unsigned ArgOffset = 24;
892 unsigned GPR_remaining = 8;
893 unsigned FPR_remaining = 13;
894
895 std::vector<SDOperand> MemOps;
896 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
897 // PtrOff will be used to store the current argument to the stack if a
898 // register cannot be found for it.
899 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
900 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
901 MVT::ValueType ArgVT = getValueType(Args[i].second);
902
903 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000904 default: assert(0 && "Unexpected ValueType for argument!");
905 case MVT::i1:
906 case MVT::i8:
907 case MVT::i16:
908 // Promote the integer to 32 bits. If the input type is signed use a
909 // sign extend, otherwise use a zero extend.
910 if (Args[i].second->isSigned())
911 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
912 else
913 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
914 // FALL THROUGH
915 case MVT::i32:
916 if (GPR_remaining > 0) {
917 args_to_use.push_back(Args[i].first);
918 --GPR_remaining;
919 } else {
920 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
921 Args[i].first, PtrOff,
922 DAG.getSrcValue(NULL)));
923 }
924 ArgOffset += 4;
925 break;
926 case MVT::i64:
927 // If we have one free GPR left, we can place the upper half of the i64
928 // in it, and store the other half to the stack. If we have two or more
929 // free GPRs, then we can pass both halves of the i64 in registers.
930 if (GPR_remaining > 0) {
931 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
932 Args[i].first, DAG.getConstant(1, MVT::i32));
933 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
934 Args[i].first, DAG.getConstant(0, MVT::i32));
935 args_to_use.push_back(Hi);
936 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000937 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000938 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000939 --GPR_remaining;
940 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000941 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
942 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000943 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000944 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000945 }
Chris Lattner915fb302005-08-30 00:19:00 +0000946 } else {
947 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
948 Args[i].first, PtrOff,
949 DAG.getSrcValue(NULL)));
950 }
951 ArgOffset += 8;
952 break;
953 case MVT::f32:
954 case MVT::f64:
955 if (FPR_remaining > 0) {
956 args_to_use.push_back(Args[i].first);
957 --FPR_remaining;
958 if (isVarArg) {
959 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
960 Args[i].first, PtrOff,
961 DAG.getSrcValue(NULL));
962 MemOps.push_back(Store);
963 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000964 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000965 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
966 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000967 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000968 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000969 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000970 }
971 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000972 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
973 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000974 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
975 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000976 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000977 args_to_use.push_back(Load);
978 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000979 }
980 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000981 // If we have any FPRs remaining, we may also have GPRs remaining.
982 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
983 // GPRs.
984 if (GPR_remaining > 0) {
985 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
986 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000987 }
Chris Lattner915fb302005-08-30 00:19:00 +0000988 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
989 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
990 --GPR_remaining;
991 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000992 }
Chris Lattner915fb302005-08-30 00:19:00 +0000993 } else {
994 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
995 Args[i].first, PtrOff,
996 DAG.getSrcValue(NULL)));
997 }
998 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
999 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001000 }
1001 }
1002 if (!MemOps.empty())
1003 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1004 }
1005
1006 std::vector<MVT::ValueType> RetVals;
1007 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001008 MVT::ValueType ActualRetTyVT = RetTyVT;
1009 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1010 ActualRetTyVT = MVT::i32; // Promote result to i32.
1011
Chris Lattnere00ebf02006-01-28 07:33:03 +00001012 if (RetTyVT == MVT::i64) {
1013 RetVals.push_back(MVT::i32);
1014 RetVals.push_back(MVT::i32);
1015 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001016 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001017 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001018 RetVals.push_back(MVT::Other);
1019
Chris Lattner2823b3e2005-11-17 05:56:14 +00001020 // If the callee is a GlobalAddress node (quite common, every direct call is)
1021 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1022 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1023 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1024
Chris Lattner281b55e2006-01-27 23:34:02 +00001025 std::vector<SDOperand> Ops;
1026 Ops.push_back(Chain);
1027 Ops.push_back(Callee);
1028 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1029 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001030 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001031 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1032 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001033 SDOperand RetVal = TheCall;
1034
1035 // If the result is a small value, add a note so that we keep track of the
1036 // information about whether it is sign or zero extended.
1037 if (RetTyVT != ActualRetTyVT) {
1038 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1039 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1040 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001041 } else if (RetTyVT == MVT::i64) {
1042 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001043 }
1044
1045 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001046}
1047
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001048MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001049PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1050 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001051 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001052 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1053 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001054 "Unexpected instr type to insert");
1055
1056 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1057 // control-flow pattern. The incoming instruction knows the destination vreg
1058 // to set, the condition code register to branch on, the true/false values to
1059 // select between, and a branch opcode to use.
1060 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1061 ilist<MachineBasicBlock>::iterator It = BB;
1062 ++It;
1063
1064 // thisMBB:
1065 // ...
1066 // TrueVal = ...
1067 // cmpTY ccX, r1, r2
1068 // bCC copy1MBB
1069 // fallthrough --> copy0MBB
1070 MachineBasicBlock *thisMBB = BB;
1071 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1072 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1073 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1074 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1075 MachineFunction *F = BB->getParent();
1076 F->getBasicBlockList().insert(It, copy0MBB);
1077 F->getBasicBlockList().insert(It, sinkMBB);
1078 // Update machine-CFG edges
1079 BB->addSuccessor(copy0MBB);
1080 BB->addSuccessor(sinkMBB);
1081
1082 // copy0MBB:
1083 // %FalseValue = ...
1084 // # fallthrough to sinkMBB
1085 BB = copy0MBB;
1086
1087 // Update machine-CFG edges
1088 BB->addSuccessor(sinkMBB);
1089
1090 // sinkMBB:
1091 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1092 // ...
1093 BB = sinkMBB;
1094 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1095 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1096 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1097
1098 delete MI; // The pseudo instruction is gone now.
1099 return BB;
1100}
1101
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001102SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1103 DAGCombinerInfo &DCI) const {
1104 TargetMachine &TM = getTargetMachine();
1105 SelectionDAG &DAG = DCI.DAG;
1106 switch (N->getOpcode()) {
1107 default: break;
1108 case ISD::SINT_TO_FP:
1109 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
1110 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1111 // We allow the src/dst to be either f32/f64, but force the intermediate
1112 // type to be i64.
1113 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT &&
1114 N->getOperand(0).getValueType() == MVT::i64) {
1115
1116 SDOperand Val = N->getOperand(0).getOperand(0);
1117 if (Val.getValueType() == MVT::f32) {
1118 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1119 DCI.AddToWorklist(Val.Val);
1120 }
1121
1122 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
1123 DCI.AddToWorklist(Val.Val);
1124 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
1125 DCI.AddToWorklist(Val.Val);
1126 if (N->getValueType(0) == MVT::f32) {
1127 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1128 DCI.AddToWorklist(Val.Val);
1129 }
1130 return Val;
1131 }
1132 }
1133 break;
Chris Lattner51269842006-03-01 05:50:56 +00001134 case ISD::STORE:
1135 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1136 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1137 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1138 N->getOperand(1).getValueType() == MVT::i32) {
1139 SDOperand Val = N->getOperand(1).getOperand(0);
1140 if (Val.getValueType() == MVT::f32) {
1141 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1142 DCI.AddToWorklist(Val.Val);
1143 }
1144 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1145 DCI.AddToWorklist(Val.Val);
1146
1147 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1148 N->getOperand(2), N->getOperand(3));
1149 DCI.AddToWorklist(Val.Val);
1150 return Val;
1151 }
1152 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001153 }
1154
1155 return SDOperand();
1156}
1157
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001158/// getConstraintType - Given a constraint letter, return the type of
1159/// constraint it is for this target.
1160PPCTargetLowering::ConstraintType
1161PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1162 switch (ConstraintLetter) {
1163 default: break;
1164 case 'b':
1165 case 'r':
1166 case 'f':
1167 case 'v':
1168 case 'y':
1169 return C_RegisterClass;
1170 }
1171 return TargetLowering::getConstraintType(ConstraintLetter);
1172}
1173
1174
Chris Lattnerddc787d2006-01-31 19:20:21 +00001175std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001176getRegClassForInlineAsmConstraint(const std::string &Constraint,
1177 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001178 if (Constraint.size() == 1) {
1179 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1180 default: break; // Unknown constriant letter
1181 case 'b':
1182 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1183 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1184 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1185 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1186 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1187 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1188 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1189 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1190 0);
1191 case 'r':
1192 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1193 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1194 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1195 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1196 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1197 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1198 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1199 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1200 0);
1201 case 'f':
1202 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1203 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1204 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1205 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1206 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1207 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1208 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1209 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1210 0);
1211 case 'v':
1212 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1213 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1214 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1215 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1216 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1217 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1218 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1219 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1220 0);
1221 case 'y':
1222 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1223 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1224 0);
1225 }
1226 }
1227
Chris Lattner1efa40f2006-02-22 00:56:39 +00001228 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001229}
Chris Lattner763317d2006-02-07 00:47:13 +00001230
1231// isOperandValidForConstraint
1232bool PPCTargetLowering::
1233isOperandValidForConstraint(SDOperand Op, char Letter) {
1234 switch (Letter) {
1235 default: break;
1236 case 'I':
1237 case 'J':
1238 case 'K':
1239 case 'L':
1240 case 'M':
1241 case 'N':
1242 case 'O':
1243 case 'P': {
1244 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1245 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1246 switch (Letter) {
1247 default: assert(0 && "Unknown constraint letter!");
1248 case 'I': // "I" is a signed 16-bit constant.
1249 return (short)Value == (int)Value;
1250 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1251 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1252 return (short)Value == 0;
1253 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1254 return (Value >> 16) == 0;
1255 case 'M': // "M" is a constant that is greater than 31.
1256 return Value > 31;
1257 case 'N': // "N" is a positive constant that is an exact power of two.
1258 return (int)Value > 0 && isPowerOf2_32(Value);
1259 case 'O': // "O" is the constant zero.
1260 return Value == 0;
1261 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1262 return (short)-Value == (int)-Value;
1263 }
1264 break;
1265 }
1266 }
1267
1268 // Handle standard constraint letters.
1269 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1270}
Evan Chengc4c62572006-03-13 23:20:37 +00001271
1272/// isLegalAddressImmediate - Return true if the integer value can be used
1273/// as the offset of the target addressing mode.
1274bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1275 // PPC allows a sign-extended 16-bit immediate field.
1276 return (V > -(1 << 16) && V < (1 << 16)-1);
1277}