blob: 686f2fa06965a37c661a0d4e5f54c323d2af3515 [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"
Chris Lattner6d92cad2006-03-26 10:06:40 +000025#include "llvm/Intrinsics.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000026#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000027#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000028using namespace llvm;
29
Nate Begeman21e463b2005-10-16 05:39:50 +000030PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000031 : TargetLowering(TM) {
32
33 // Fold away setcc operations if possible.
34 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000035 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000036
Chris Lattnerd145a612005-09-27 22:18:25 +000037 // Use _setjmp/_longjmp instead of setjmp/longjmp.
38 setUseUnderscoreSetJmpLongJmp(true);
39
Chris Lattner7c5a3d32005-08-16 17:14:42 +000040 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000041 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
42 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
43 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000044
Chris Lattnera54aa942006-01-29 06:26:08 +000045 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
46 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
47
Chris Lattner7c5a3d32005-08-16 17:14:42 +000048 // PowerPC has no intrinsics for these particular operations
49 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
50 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
51 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
52
53 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
54 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
55 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
56
57 // PowerPC has no SREM/UREM instructions
58 setOperationAction(ISD::SREM, MVT::i32, Expand);
59 setOperationAction(ISD::UREM, MVT::i32, Expand);
60
61 // We don't support sin/cos/sqrt/fmod
62 setOperationAction(ISD::FSIN , MVT::f64, Expand);
63 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000064 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000065 setOperationAction(ISD::FSIN , MVT::f32, Expand);
66 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000067 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000068
69 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000070 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000071 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
72 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
73 }
74
Chris Lattner9601a862006-03-05 05:08:37 +000075 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
76 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
77
Nate Begemand88fc032006-01-14 03:14:10 +000078 // PowerPC does not have BSWAP, CTPOP or CTTZ
79 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000080 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
81 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
82
Nate Begeman35ef9132006-01-11 21:21:00 +000083 // PowerPC does not have ROTR
84 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
85
Chris Lattner7c5a3d32005-08-16 17:14:42 +000086 // PowerPC does not have Select
87 setOperationAction(ISD::SELECT, MVT::i32, Expand);
88 setOperationAction(ISD::SELECT, MVT::f32, Expand);
89 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000090
Chris Lattner0b1e4e52005-08-26 17:36:52 +000091 // PowerPC wants to turn select_cc of FP into fsel when possible.
92 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
93 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000094
Nate Begeman750ac1b2006-02-01 07:19:44 +000095 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000096 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000097
Nate Begeman81e80972006-03-17 01:40:33 +000098 // PowerPC does not have BRCOND which requires SetCC
99 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000100
Chris Lattnerf7605322005-08-31 21:09:52 +0000101 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
102 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000103
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000104 // PowerPC does not have [U|S]INT_TO_FP
105 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
106 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
107
Chris Lattner53e88452005-12-23 05:13:35 +0000108 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
109 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
110
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000111 // PowerPC does not have truncstore for i1.
112 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000113
Jim Laskeyabf6d172006-01-05 01:25:28 +0000114 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000115 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000116 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000117 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000118 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000119 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000120
Nate Begeman28a6b022005-12-10 02:36:00 +0000121 // We want to legalize GlobalAddress and ConstantPool nodes into the
122 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000123 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000124 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000125
Nate Begemanee625572006-01-27 21:09:22 +0000126 // RET must be custom lowered, to meet ABI requirements
127 setOperationAction(ISD::RET , MVT::Other, Custom);
128
Nate Begemanacc398c2006-01-25 18:21:52 +0000129 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
130 setOperationAction(ISD::VASTART , MVT::Other, Custom);
131
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000132 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000133 setOperationAction(ISD::VAARG , MVT::Other, Expand);
134 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
135 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000136 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
137 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
138 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000139
Chris Lattner6d92cad2006-03-26 10:06:40 +0000140 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000141 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000142
Nate Begemanc09eeec2005-09-06 22:03:27 +0000143 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000144 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000145 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
146 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner7fbcef72006-03-24 07:53:47 +0000147
148 // FIXME: disable this lowered code. This generates 64-bit register values,
149 // and we don't model the fact that the top part is clobbered by calls. We
150 // need to flag these together so that the value isn't live across a call.
151 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
152
Nate Begemanae749a92005-10-25 23:48:36 +0000153 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
154 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
155 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000156 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000157 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000158 }
159
160 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
161 // 64 bit PowerPC implementations can support i64 types directly
162 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000163 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
164 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000165 } else {
166 // 32 bit PowerPC wants to expand i64 shifts itself.
167 setOperationAction(ISD::SHL, MVT::i64, Custom);
168 setOperationAction(ISD::SRL, MVT::i64, Custom);
169 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000170 }
Evan Chengd30bf012006-03-01 01:11:20 +0000171
Nate Begeman425a9692005-11-29 08:17:20 +0000172 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000173 // First set operation action for all vector types to expand. Then we
174 // will selectively turn on ones that can be effectively codegen'd.
175 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
176 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
177 // add/sub/and/or/xor are legal for all supported vector VT's.
178 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
179 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
180 setOperationAction(ISD::AND , (MVT::ValueType)VT, Legal);
181 setOperationAction(ISD::OR , (MVT::ValueType)VT, Legal);
182 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Legal);
183
Chris Lattner7ff7e672006-04-04 17:25:31 +0000184 // We promote all shuffles to v16i8.
185 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
186 AddPromotedToType(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000187
188 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
189 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
190 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
191 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
192 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
193 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
194 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
195 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000196
197 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000198 }
199
Chris Lattner7ff7e672006-04-04 17:25:31 +0000200 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
201 // with merges, splats, etc.
202 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
203
Nate Begeman425a9692005-11-29 08:17:20 +0000204 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000205 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000206 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
207 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000208
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000209 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000210
Chris Lattnerb2177b92006-03-19 06:55:52 +0000211 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
212 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000213
Chris Lattner541f91b2006-04-02 00:43:36 +0000214 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
215 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000216 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
217 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000218 }
219
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000220 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000221 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000222
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000223 // We have target-specific dag combine patterns for the following nodes:
224 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000225 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000226
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000227 computeRegisterProperties();
228}
229
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000230const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
231 switch (Opcode) {
232 default: return 0;
233 case PPCISD::FSEL: return "PPCISD::FSEL";
234 case PPCISD::FCFID: return "PPCISD::FCFID";
235 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
236 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000237 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000238 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
239 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000240 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000241 case PPCISD::Hi: return "PPCISD::Hi";
242 case PPCISD::Lo: return "PPCISD::Lo";
243 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
244 case PPCISD::SRL: return "PPCISD::SRL";
245 case PPCISD::SRA: return "PPCISD::SRA";
246 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000247 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
248 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000249 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000250 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000251 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000252 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000253 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000254 }
255}
256
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000257/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
258static bool isFloatingPointZero(SDOperand Op) {
259 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
260 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
261 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
262 // Maybe this has already been legalized into the constant pool?
263 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
264 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
265 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
266 }
267 return false;
268}
269
Chris Lattnerddb739e2006-04-06 17:23:16 +0000270/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
271/// true if Op is undef or if it matches the specified value.
272static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
273 return Op.getOpcode() == ISD::UNDEF ||
274 cast<ConstantSDNode>(Op)->getValue() == Val;
275}
276
277/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
278/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000279bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
280 if (!isUnary) {
281 for (unsigned i = 0; i != 16; ++i)
282 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
283 return false;
284 } else {
285 for (unsigned i = 0; i != 8; ++i)
286 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
287 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
288 return false;
289 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000290 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000291}
292
293/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
294/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000295bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
296 if (!isUnary) {
297 for (unsigned i = 0; i != 16; i += 2)
298 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
299 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
300 return false;
301 } else {
302 for (unsigned i = 0; i != 8; i += 2)
303 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
304 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
305 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
306 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
307 return false;
308 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000309 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000310}
311
Chris Lattnercaad1632006-04-06 22:02:42 +0000312/// isVMerge - Common function, used to match vmrg* shuffles.
313///
314static bool isVMerge(SDNode *N, unsigned UnitSize,
315 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000316 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
317 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
318 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
319 "Unsupported merge size!");
320
321 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
322 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
323 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000324 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000325 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000326 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000327 return false;
328 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000329 return true;
330}
331
332/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
333/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
334bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
335 if (!isUnary)
336 return isVMerge(N, UnitSize, 8, 24);
337 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000338}
339
340/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
341/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000342bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
343 if (!isUnary)
344 return isVMerge(N, UnitSize, 0, 16);
345 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000346}
347
348
Chris Lattnerd0608e12006-04-06 18:26:28 +0000349/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
350/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000351int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000352 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
353 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000354 // Find the first non-undef value in the shuffle mask.
355 unsigned i;
356 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
357 /*search*/;
358
359 if (i == 16) return -1; // all undef.
360
361 // Otherwise, check to see if the rest of the elements are consequtively
362 // numbered from this value.
363 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
364 if (ShiftAmt < i) return -1;
365 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000366
Chris Lattnerf24380e2006-04-06 22:28:36 +0000367 if (!isUnary) {
368 // Check the rest of the elements to see if they are consequtive.
369 for (++i; i != 16; ++i)
370 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
371 return -1;
372 } else {
373 // Check the rest of the elements to see if they are consequtive.
374 for (++i; i != 16; ++i)
375 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
376 return -1;
377 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000378
379 return ShiftAmt;
380}
Chris Lattneref819f82006-03-20 06:33:01 +0000381
382/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
383/// specifies a splat of a single element that is suitable for input to
384/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000385bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
386 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
387 N->getNumOperands() == 16 &&
388 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000389
Chris Lattner88a99ef2006-03-20 06:37:44 +0000390 // This is a splat operation if each element of the permute is the same, and
391 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000392 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000393 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000394 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
395 ElementBase = EltV->getValue();
396 else
397 return false; // FIXME: Handle UNDEF elements too!
398
399 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
400 return false;
401
402 // Check that they are consequtive.
403 for (unsigned i = 1; i != EltSize; ++i) {
404 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
405 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
406 return false;
407 }
408
Chris Lattner88a99ef2006-03-20 06:37:44 +0000409 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000410 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattner88a99ef2006-03-20 06:37:44 +0000411 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
412 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000413 for (unsigned j = 0; j != EltSize; ++j)
414 if (N->getOperand(i+j) != N->getOperand(j))
415 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000416 }
417
Chris Lattner7ff7e672006-04-04 17:25:31 +0000418 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000419}
420
421/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
422/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000423unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
424 assert(isSplatShuffleMask(N, EltSize));
425 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000426}
427
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000428/// isVecSplatImm - Return true if this is a build_vector of constants which
429/// can be formed by using a vspltis[bhw] instruction. The ByteSize field
430/// indicates the number of bytes of each element [124] -> [bhw].
431bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) {
432 SDOperand OpVal(0, 0);
433 // Check to see if this buildvec has a single non-undef value in its elements.
434 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
435 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
436 if (OpVal.Val == 0)
437 OpVal = N->getOperand(i);
438 else if (OpVal != N->getOperand(i))
439 return false;
440 }
441
442 if (OpVal.Val == 0) return false; // All UNDEF: use implicit def.
443
Nate Begeman98e70cc2006-03-28 04:15:58 +0000444 unsigned ValSizeInBytes = 0;
445 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000446 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
447 Value = CN->getValue();
448 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
449 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
450 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
451 Value = FloatToBits(CN->getValue());
452 ValSizeInBytes = 4;
453 }
454
455 // If the splat value is larger than the element value, then we can never do
456 // this splat. The only case that we could fit the replicated bits into our
457 // immediate field for would be zero, and we prefer to use vxor for it.
458 if (ValSizeInBytes < ByteSize) return false;
459
460 // If the element value is larger than the splat value, cut it in half and
461 // check to see if the two halves are equal. Continue doing this until we
462 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
463 while (ValSizeInBytes > ByteSize) {
464 ValSizeInBytes >>= 1;
465
466 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000467 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
468 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000469 return false;
470 }
471
472 // Properly sign extend the value.
473 int ShAmt = (4-ByteSize)*8;
474 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
475
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000476 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000477 if (MaskVal == 0) return false;
478
479 if (Val) *Val = MaskVal;
480
481 // Finally, if this value fits in a 5 bit sext field, return true.
482 return ((MaskVal << (32-5)) >> (32-5)) == MaskVal;
483}
484
Chris Lattneref819f82006-03-20 06:33:01 +0000485
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000486/// LowerOperation - Provide custom lowering hooks for some operations.
487///
Nate Begeman21e463b2005-10-16 05:39:50 +0000488SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000489 switch (Op.getOpcode()) {
490 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000491 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000492 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000493 SDOperand Src = Op.getOperand(0);
494 if (Src.getValueType() == MVT::f32)
495 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
496
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000497 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000498 switch (Op.getValueType()) {
499 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
500 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000501 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000502 break;
503 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000504 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000505 break;
506 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000507
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000508 // Convert the FP value to an int value through memory.
509 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
510 if (Op.getValueType() == MVT::i32)
511 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
512 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000513 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000514 case ISD::SINT_TO_FP:
515 if (Op.getOperand(0).getValueType() == MVT::i64) {
516 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
517 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
518 if (Op.getValueType() == MVT::f32)
519 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
520 return FP;
521 } else {
522 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
523 "Unhandled SINT_TO_FP type in custom expander!");
524 // Since we only generate this in 64-bit mode, we can take advantage of
525 // 64-bit registers. In particular, sign extend the input value into the
526 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
527 // then lfd it and fcfid it.
528 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
529 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
530 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
531
532 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
533 Op.getOperand(0));
534
535 // STD the extended value into the stack slot.
536 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
537 DAG.getEntryNode(), Ext64, FIdx,
538 DAG.getSrcValue(NULL));
539 // Load the value as a double.
540 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
541
542 // FCFID it and return it.
543 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
544 if (Op.getValueType() == MVT::f32)
545 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
546 return FP;
547 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000548 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000549
Chris Lattnerf7605322005-08-31 21:09:52 +0000550 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000551 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000552 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
553 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
554 break;
555
556 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
557
558 // Cannot handle SETEQ/SETNE.
559 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
560
561 MVT::ValueType ResVT = Op.getValueType();
562 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
563 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
564 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000565
Chris Lattnerf7605322005-08-31 21:09:52 +0000566 // If the RHS of the comparison is a 0.0, we don't need to do the
567 // subtraction at all.
568 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000569 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000570 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000571 case ISD::SETULT:
572 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000573 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000574 case ISD::SETUGE:
575 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000576 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
577 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000578 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000579 case ISD::SETUGT:
580 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000581 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000582 case ISD::SETULE:
583 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000584 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
585 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000586 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000587 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000588 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000589
Chris Lattnereb255f22005-10-25 20:54:57 +0000590 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000591 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000592 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000593 case ISD::SETULT:
594 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000595 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
596 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
597 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
598 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000599 case ISD::SETUGE:
600 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000601 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
602 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
603 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
604 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000605 case ISD::SETUGT:
606 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000607 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
608 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
609 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
610 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000611 case ISD::SETULE:
612 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000613 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
614 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
615 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
616 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000617 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000618 break;
619 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000620 case ISD::SHL: {
621 assert(Op.getValueType() == MVT::i64 &&
622 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
623 // The generic code does a fine job expanding shift by a constant.
624 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
625
626 // Otherwise, expand into a bunch of logical ops. Note that these ops
627 // depend on the PPC behavior for oversized shift amounts.
628 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
629 DAG.getConstant(0, MVT::i32));
630 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
631 DAG.getConstant(1, MVT::i32));
632 SDOperand Amt = Op.getOperand(1);
633
634 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
635 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000636 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
637 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000638 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
639 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
640 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000641 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000642 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000643 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000644 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
645 }
646 case ISD::SRL: {
647 assert(Op.getValueType() == MVT::i64 &&
648 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
649 // The generic code does a fine job expanding shift by a constant.
650 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
651
652 // Otherwise, expand into a bunch of logical ops. Note that these ops
653 // depend on the PPC behavior for oversized shift amounts.
654 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
655 DAG.getConstant(0, MVT::i32));
656 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
657 DAG.getConstant(1, MVT::i32));
658 SDOperand Amt = Op.getOperand(1);
659
660 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
661 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000662 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
663 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000664 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
665 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
666 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000667 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000668 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000669 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000670 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
671 }
672 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000673 assert(Op.getValueType() == MVT::i64 &&
674 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
675 // The generic code does a fine job expanding shift by a constant.
676 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
677
678 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
679 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
680 DAG.getConstant(0, MVT::i32));
681 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
682 DAG.getConstant(1, MVT::i32));
683 SDOperand Amt = Op.getOperand(1);
684
685 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
686 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000687 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
688 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000689 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
690 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
691 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000692 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
693 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000694 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
695 Tmp4, Tmp6, ISD::SETLE);
696 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000697 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000698 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000699 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
700 Constant *C = CP->get();
701 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000702 SDOperand Zero = DAG.getConstant(0, MVT::i32);
703
Evan Cheng4c1aa862006-02-22 20:19:42 +0000704 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000705 // Generate non-pic code that has direct accesses to the constant pool.
706 // The address of the global is just (hi(&g)+lo(&g)).
707 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
708 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
709 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
710 }
711
712 // Only lower ConstantPool on Darwin.
713 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
714 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000715 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000716 // With PIC, the first instruction is actually "GR+hi(&G)".
717 Hi = DAG.getNode(ISD::ADD, MVT::i32,
718 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
719 }
720
721 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
722 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
723 return Lo;
724 }
Chris Lattner860e8862005-11-17 07:30:41 +0000725 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000726 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
727 GlobalValue *GV = GSDN->getGlobal();
728 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000729 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000730
Evan Cheng4c1aa862006-02-22 20:19:42 +0000731 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000732 // Generate non-pic code that has direct accesses to globals.
733 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000734 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
735 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
736 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
737 }
Chris Lattner860e8862005-11-17 07:30:41 +0000738
Chris Lattner1d05cb42005-11-17 18:55:48 +0000739 // Only lower GlobalAddress on Darwin.
740 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000741
Chris Lattner860e8862005-11-17 07:30:41 +0000742 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000743 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000744 // With PIC, the first instruction is actually "GR+hi(&G)".
745 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000746 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000747 }
748
749 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
750 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
751
Chris Lattner37dd6f12006-01-29 20:49:17 +0000752 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
753 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000754 return Lo;
755
756 // If the global is weak or external, we have to go through the lazy
757 // resolution stub.
758 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
759 }
Nate Begeman44775902006-01-31 08:17:29 +0000760 case ISD::SETCC: {
761 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000762
763 // If we're comparing for equality to zero, expose the fact that this is
764 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
765 // fold the new nodes.
766 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
767 if (C->isNullValue() && CC == ISD::SETEQ) {
768 MVT::ValueType VT = Op.getOperand(0).getValueType();
769 SDOperand Zext = Op.getOperand(0);
770 if (VT < MVT::i32) {
771 VT = MVT::i32;
772 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
773 }
774 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
775 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
776 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
777 DAG.getConstant(Log2b, getShiftAmountTy()));
778 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
779 }
780 // Leave comparisons against 0 and -1 alone for now, since they're usually
781 // optimized. FIXME: revisit this when we can custom lower all setcc
782 // optimizations.
783 if (C->isAllOnesValue() || C->isNullValue())
784 break;
785 }
786
787 // If we have an integer seteq/setne, turn it into a compare against zero
788 // by subtracting the rhs from the lhs, which is faster than setting a
789 // condition register, reading it back out, and masking the correct bit.
790 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
791 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
792 MVT::ValueType VT = Op.getValueType();
793 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
794 Op.getOperand(1));
795 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
796 }
Nate Begeman44775902006-01-31 08:17:29 +0000797 break;
798 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000799 case ISD::VASTART: {
800 // vastart just stores the address of the VarArgsFrameIndex slot into the
801 // memory location argument.
802 // FIXME: Replace MVT::i32 with PointerTy
803 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
804 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
805 Op.getOperand(1), Op.getOperand(2));
806 }
Nate Begemanee625572006-01-27 21:09:22 +0000807 case ISD::RET: {
808 SDOperand Copy;
809
810 switch(Op.getNumOperands()) {
811 default:
812 assert(0 && "Do not know how to return this many arguments!");
813 abort();
814 case 1:
815 return SDOperand(); // ret void is legal
816 case 2: {
817 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
818 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
819 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
820 SDOperand());
821 break;
822 }
823 case 3:
824 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
825 SDOperand());
826 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
827 break;
828 }
829 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
830 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000831 case ISD::SCALAR_TO_VECTOR: {
832 // Create a stack slot that is 16-byte aligned.
833 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
834 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
835 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
836
837 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000838 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
839 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner7f20b132006-03-28 01:43:22 +0000840 // Load it out.
841 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
Chris Lattnerb2177b92006-03-19 06:55:52 +0000842 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000843 case ISD::BUILD_VECTOR:
844 // If this is a case we can't handle, return null and let the default
845 // expansion code take care of it. If we CAN select this case, return Op.
846
847 // See if this is all zeros.
848 // FIXME: We should handle splat(-0.0), and other cases here.
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000849 if (ISD::isBuildVectorAllZeros(Op.Val))
Chris Lattner64b3a082006-03-24 07:48:08 +0000850 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000851
852 if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb
853 PPC::isVecSplatImm(Op.Val, 2) || // vspltish
854 PPC::isVecSplatImm(Op.Val, 4)) // vspltisw
855 return Op;
856
Chris Lattner64b3a082006-03-24 07:48:08 +0000857 return SDOperand();
858
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000859 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000860 SDOperand V1 = Op.getOperand(0);
861 SDOperand V2 = Op.getOperand(1);
862 SDOperand PermMask = Op.getOperand(2);
863
864 // Cases that are handled by instructions that take permute immediates
865 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
866 // selected by the instruction selector.
Chris Lattnercaad1632006-04-06 22:02:42 +0000867 if (V2.getOpcode() == ISD::UNDEF) {
868 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
869 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
870 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
Chris Lattnerf24380e2006-04-06 22:28:36 +0000871 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
872 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
873 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
Chris Lattnercaad1632006-04-06 22:02:42 +0000874 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
875 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
876 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
877 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
878 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
879 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
880 return Op;
881 }
882 }
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000883
Chris Lattnerf24380e2006-04-06 22:28:36 +0000884 // Altivec has a variety of "shuffle immediates" that take two vector inputs
885 // and produce a fixed permutation. If any of these match, do not lower to
886 // VPERM.
887 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
888 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
889 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
Chris Lattnercaad1632006-04-06 22:02:42 +0000890 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
891 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
892 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
893 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
894 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
895 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
Chris Lattnerddb739e2006-04-06 17:23:16 +0000896 return Op;
897
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000898 // TODO: Handle more cases, and also handle cases that are cheaper to do as
899 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000900
901 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
902 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000903 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000904
905 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
906 // that it is in input element units, not in bytes. Convert now.
907 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
908 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
909
910 std::vector<SDOperand> ResultMask;
911 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
912 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
913
914 for (unsigned j = 0; j != BytesPerElement; ++j)
915 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
916 MVT::i8));
917 }
918
919 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
920 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
921 }
Chris Lattner48b61a72006-03-28 00:40:33 +0000922 case ISD::INTRINSIC_WO_CHAIN: {
Chris Lattnerbbe77de2006-04-02 06:26:07 +0000923 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
Chris Lattner6d92cad2006-03-26 10:06:40 +0000924
925 // If this is a lowered altivec predicate compare, CompareOpc is set to the
926 // opcode number of the comparison.
927 int CompareOpc = -1;
Chris Lattnera17b1552006-03-31 05:13:27 +0000928 bool isDot = false;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000929 switch (IntNo) {
930 default: return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattnera17b1552006-03-31 05:13:27 +0000931 // Comparison predicates.
932 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
933 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
934 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
935 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
936 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
937 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
938 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
939 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
940 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
941 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
942 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
943 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
944 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
945
946 // Normal Comparisons.
947 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
948 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
949 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
950 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
951 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
952 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
953 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
954 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
955 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
956 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
957 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
958 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
959 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000960 }
961
962 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
963
Chris Lattnera17b1552006-03-31 05:13:27 +0000964 // If this is a non-dot comparison, make the VCMP node.
965 if (!isDot)
966 return DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
967 Op.getOperand(1), Op.getOperand(2),
968 DAG.getConstant(CompareOpc, MVT::i32));
969
Chris Lattner6d92cad2006-03-26 10:06:40 +0000970 // Create the PPCISD altivec 'dot' comparison node.
971 std::vector<SDOperand> Ops;
972 std::vector<MVT::ValueType> VTs;
973 Ops.push_back(Op.getOperand(2)); // LHS
974 Ops.push_back(Op.getOperand(3)); // RHS
975 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
976 VTs.push_back(Op.getOperand(2).getValueType());
977 VTs.push_back(MVT::Flag);
978 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
979
980 // Now that we have the comparison, emit a copy from the CR to a GPR.
981 // This is flagged to the above dot comparison.
982 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
983 DAG.getRegister(PPC::CR6, MVT::i32),
984 CompNode.getValue(1));
985
986 // Unpack the result based on how the target uses it.
987 unsigned BitNo; // Bit # of CR6.
988 bool InvertBit; // Invert result?
989 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
990 default: // Can't happen, don't crash on invalid number though.
991 case 0: // Return the value of the EQ bit of CR6.
992 BitNo = 0; InvertBit = false;
993 break;
994 case 1: // Return the inverted value of the EQ bit of CR6.
995 BitNo = 0; InvertBit = true;
996 break;
997 case 2: // Return the value of the LT bit of CR6.
998 BitNo = 2; InvertBit = false;
999 break;
1000 case 3: // Return the inverted value of the LT bit of CR6.
1001 BitNo = 2; InvertBit = true;
1002 break;
1003 }
1004
1005 // Shift the bit into the low position.
1006 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
1007 DAG.getConstant(8-(3-BitNo), MVT::i32));
1008 // Isolate the bit.
1009 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
1010 DAG.getConstant(1, MVT::i32));
1011
1012 // If we are supposed to, toggle the bit.
1013 if (InvertBit)
1014 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
1015 DAG.getConstant(1, MVT::i32));
1016 return Flags;
1017 }
Chris Lattnerbc11c342005-08-31 20:23:54 +00001018 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001019 return SDOperand();
1020}
1021
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001022std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001023PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001024 //
1025 // add beautiful description of PPC stack frame format, or at least some docs
1026 //
1027 MachineFunction &MF = DAG.getMachineFunction();
1028 MachineFrameInfo *MFI = MF.getFrameInfo();
1029 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +00001030 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001031 std::vector<SDOperand> ArgValues;
1032
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001033 unsigned ArgOffset = 24;
1034 unsigned GPR_remaining = 8;
1035 unsigned FPR_remaining = 13;
1036 unsigned GPR_idx = 0, FPR_idx = 0;
1037 static const unsigned GPR[] = {
1038 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1039 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1040 };
1041 static const unsigned FPR[] = {
1042 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1043 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1044 };
1045
1046 // Add DAG nodes to load the arguments... On entry to a function on PPC,
1047 // the arguments start at offset 24, although they are likely to be passed
1048 // in registers.
1049 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
1050 SDOperand newroot, argt;
1051 unsigned ObjSize;
1052 bool needsLoad = false;
1053 bool ArgLive = !I->use_empty();
1054 MVT::ValueType ObjectVT = getValueType(I->getType());
1055
1056 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001057 default: assert(0 && "Unhandled argument type!");
1058 case MVT::i1:
1059 case MVT::i8:
1060 case MVT::i16:
1061 case MVT::i32:
1062 ObjSize = 4;
1063 if (!ArgLive) break;
1064 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001065 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001066 MF.addLiveIn(GPR[GPR_idx], VReg);
1067 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +00001068 if (ObjectVT != MVT::i32) {
1069 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
1070 : ISD::AssertZext;
1071 argt = DAG.getNode(AssertOp, MVT::i32, argt,
1072 DAG.getValueType(ObjectVT));
1073 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
1074 }
Chris Lattner915fb302005-08-30 00:19:00 +00001075 } else {
1076 needsLoad = true;
1077 }
1078 break;
Chris Lattner80720a92005-11-30 20:40:54 +00001079 case MVT::i64:
1080 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +00001081 if (!ArgLive) break;
1082 if (GPR_remaining > 0) {
1083 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +00001084 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001085 MF.addLiveIn(GPR[GPR_idx], VReg);
1086 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001087 // If we have two or more remaining argument registers, then both halves
1088 // of the i64 can be sourced from there. Otherwise, the lower half will
1089 // have to come off the stack. This can happen when an i64 is preceded
1090 // by 28 bytes of arguments.
1091 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001092 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001093 MF.addLiveIn(GPR[GPR_idx+1], VReg);
1094 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001095 } else {
1096 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
1097 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1098 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
1099 DAG.getSrcValue(NULL));
1100 }
1101 // Build the outgoing arg thingy
1102 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
1103 newroot = argLo;
1104 } else {
1105 needsLoad = true;
1106 }
1107 break;
1108 case MVT::f32:
1109 case MVT::f64:
1110 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +00001111 if (!ArgLive) {
1112 if (FPR_remaining > 0) {
1113 --FPR_remaining;
1114 ++FPR_idx;
1115 }
1116 break;
1117 }
Chris Lattner915fb302005-08-30 00:19:00 +00001118 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +00001119 unsigned VReg;
1120 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +00001121 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +00001122 else
Nate Begeman1d9d7422005-10-18 00:28:58 +00001123 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001124 MF.addLiveIn(FPR[FPR_idx], VReg);
1125 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +00001126 --FPR_remaining;
1127 ++FPR_idx;
1128 } else {
1129 needsLoad = true;
1130 }
1131 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001132 }
1133
1134 // We need to load the argument to a virtual register if we determined above
1135 // that we ran out of physical registers of the appropriate type
1136 if (needsLoad) {
1137 unsigned SubregOffset = 0;
1138 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
1139 if (ObjectVT == MVT::i16) SubregOffset = 2;
1140 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1141 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1142 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
1143 DAG.getConstant(SubregOffset, MVT::i32));
1144 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
1145 DAG.getSrcValue(NULL));
1146 }
1147
1148 // Every 4 bytes of argument space consumes one of the GPRs available for
1149 // argument passing.
1150 if (GPR_remaining > 0) {
1151 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
1152 GPR_remaining -= delta;
1153 GPR_idx += delta;
1154 }
1155 ArgOffset += ObjSize;
1156 if (newroot.Val)
1157 DAG.setRoot(newroot.getValue(1));
1158
1159 ArgValues.push_back(argt);
1160 }
1161
1162 // If the function takes variable number of arguments, make a frame index for
1163 // the start of the first vararg value... for expansion of llvm.va_start.
1164 if (F.isVarArg()) {
1165 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1166 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1167 // If this function is vararg, store any remaining integer argument regs
1168 // to their spots on the stack so that they may be loaded by deferencing the
1169 // result of va_next.
1170 std::vector<SDOperand> MemOps;
1171 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001172 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001173 MF.addLiveIn(GPR[GPR_idx], VReg);
1174 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001175 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1176 Val, FIN, DAG.getSrcValue(NULL));
1177 MemOps.push_back(Store);
1178 // Increment the address by four for the next argument to store
1179 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1180 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1181 }
Chris Lattner80720a92005-11-30 20:40:54 +00001182 if (!MemOps.empty()) {
1183 MemOps.push_back(DAG.getRoot());
1184 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1185 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001186 }
1187
1188 // Finally, inform the code generator which regs we return values in.
1189 switch (getValueType(F.getReturnType())) {
1190 default: assert(0 && "Unknown type!");
1191 case MVT::isVoid: break;
1192 case MVT::i1:
1193 case MVT::i8:
1194 case MVT::i16:
1195 case MVT::i32:
1196 MF.addLiveOut(PPC::R3);
1197 break;
1198 case MVT::i64:
1199 MF.addLiveOut(PPC::R3);
1200 MF.addLiveOut(PPC::R4);
1201 break;
1202 case MVT::f32:
1203 case MVT::f64:
1204 MF.addLiveOut(PPC::F1);
1205 break;
1206 }
1207
1208 return ArgValues;
1209}
1210
1211std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001212PPCTargetLowering::LowerCallTo(SDOperand Chain,
1213 const Type *RetTy, bool isVarArg,
1214 unsigned CallingConv, bool isTailCall,
1215 SDOperand Callee, ArgListTy &Args,
1216 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001217 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001218 // SelectExpr to use to put the arguments in the appropriate registers.
1219 std::vector<SDOperand> args_to_use;
1220
1221 // Count how many bytes are to be pushed on the stack, including the linkage
1222 // area, and parameter passing area.
1223 unsigned NumBytes = 24;
1224
1225 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001226 Chain = DAG.getCALLSEQ_START(Chain,
1227 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001228 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001229 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001230 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001231 default: assert(0 && "Unknown value type!");
1232 case MVT::i1:
1233 case MVT::i8:
1234 case MVT::i16:
1235 case MVT::i32:
1236 case MVT::f32:
1237 NumBytes += 4;
1238 break;
1239 case MVT::i64:
1240 case MVT::f64:
1241 NumBytes += 8;
1242 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001243 }
Chris Lattner915fb302005-08-30 00:19:00 +00001244 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001245
Chris Lattner915fb302005-08-30 00:19:00 +00001246 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1247 // plus 32 bytes of argument space in case any called code gets funky on us.
1248 // (Required by ABI to support var arg)
1249 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001250
1251 // Adjust the stack pointer for the new arguments...
1252 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001253 Chain = DAG.getCALLSEQ_START(Chain,
1254 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001255
1256 // Set up a copy of the stack pointer for use loading and storing any
1257 // arguments that may not fit in the registers available for argument
1258 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001259 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001260
1261 // Figure out which arguments are going to go in registers, and which in
1262 // memory. Also, if this is a vararg function, floating point operations
1263 // must be stored to our stack, and loaded into integer regs as well, if
1264 // any integer regs are available for argument passing.
1265 unsigned ArgOffset = 24;
1266 unsigned GPR_remaining = 8;
1267 unsigned FPR_remaining = 13;
1268
1269 std::vector<SDOperand> MemOps;
1270 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1271 // PtrOff will be used to store the current argument to the stack if a
1272 // register cannot be found for it.
1273 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1274 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1275 MVT::ValueType ArgVT = getValueType(Args[i].second);
1276
1277 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001278 default: assert(0 && "Unexpected ValueType for argument!");
1279 case MVT::i1:
1280 case MVT::i8:
1281 case MVT::i16:
1282 // Promote the integer to 32 bits. If the input type is signed use a
1283 // sign extend, otherwise use a zero extend.
1284 if (Args[i].second->isSigned())
1285 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1286 else
1287 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1288 // FALL THROUGH
1289 case MVT::i32:
1290 if (GPR_remaining > 0) {
1291 args_to_use.push_back(Args[i].first);
1292 --GPR_remaining;
1293 } else {
1294 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1295 Args[i].first, PtrOff,
1296 DAG.getSrcValue(NULL)));
1297 }
1298 ArgOffset += 4;
1299 break;
1300 case MVT::i64:
1301 // If we have one free GPR left, we can place the upper half of the i64
1302 // in it, and store the other half to the stack. If we have two or more
1303 // free GPRs, then we can pass both halves of the i64 in registers.
1304 if (GPR_remaining > 0) {
1305 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1306 Args[i].first, DAG.getConstant(1, MVT::i32));
1307 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1308 Args[i].first, DAG.getConstant(0, MVT::i32));
1309 args_to_use.push_back(Hi);
1310 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001311 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001312 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001313 --GPR_remaining;
1314 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001315 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1316 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001317 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001318 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001319 }
Chris Lattner915fb302005-08-30 00:19:00 +00001320 } else {
1321 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1322 Args[i].first, PtrOff,
1323 DAG.getSrcValue(NULL)));
1324 }
1325 ArgOffset += 8;
1326 break;
1327 case MVT::f32:
1328 case MVT::f64:
1329 if (FPR_remaining > 0) {
1330 args_to_use.push_back(Args[i].first);
1331 --FPR_remaining;
1332 if (isVarArg) {
1333 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1334 Args[i].first, PtrOff,
1335 DAG.getSrcValue(NULL));
1336 MemOps.push_back(Store);
1337 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001338 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001339 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1340 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001341 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001342 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001343 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001344 }
1345 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001346 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1347 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001348 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1349 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001350 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001351 args_to_use.push_back(Load);
1352 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001353 }
1354 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001355 // If we have any FPRs remaining, we may also have GPRs remaining.
1356 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1357 // GPRs.
1358 if (GPR_remaining > 0) {
1359 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1360 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001361 }
Chris Lattner915fb302005-08-30 00:19:00 +00001362 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1363 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1364 --GPR_remaining;
1365 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001366 }
Chris Lattner915fb302005-08-30 00:19:00 +00001367 } else {
1368 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1369 Args[i].first, PtrOff,
1370 DAG.getSrcValue(NULL)));
1371 }
1372 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1373 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001374 }
1375 }
1376 if (!MemOps.empty())
1377 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1378 }
1379
1380 std::vector<MVT::ValueType> RetVals;
1381 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001382 MVT::ValueType ActualRetTyVT = RetTyVT;
1383 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1384 ActualRetTyVT = MVT::i32; // Promote result to i32.
1385
Chris Lattnere00ebf02006-01-28 07:33:03 +00001386 if (RetTyVT == MVT::i64) {
1387 RetVals.push_back(MVT::i32);
1388 RetVals.push_back(MVT::i32);
1389 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001390 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001391 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001392 RetVals.push_back(MVT::Other);
1393
Chris Lattner2823b3e2005-11-17 05:56:14 +00001394 // If the callee is a GlobalAddress node (quite common, every direct call is)
1395 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1396 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1397 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1398
Chris Lattner281b55e2006-01-27 23:34:02 +00001399 std::vector<SDOperand> Ops;
1400 Ops.push_back(Chain);
1401 Ops.push_back(Callee);
1402 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1403 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001404 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001405 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1406 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001407 SDOperand RetVal = TheCall;
1408
1409 // If the result is a small value, add a note so that we keep track of the
1410 // information about whether it is sign or zero extended.
1411 if (RetTyVT != ActualRetTyVT) {
1412 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1413 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1414 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001415 } else if (RetTyVT == MVT::i64) {
1416 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001417 }
1418
1419 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001420}
1421
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001422MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001423PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1424 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001425 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001426 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1427 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001428 "Unexpected instr type to insert");
1429
1430 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1431 // control-flow pattern. The incoming instruction knows the destination vreg
1432 // to set, the condition code register to branch on, the true/false values to
1433 // select between, and a branch opcode to use.
1434 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1435 ilist<MachineBasicBlock>::iterator It = BB;
1436 ++It;
1437
1438 // thisMBB:
1439 // ...
1440 // TrueVal = ...
1441 // cmpTY ccX, r1, r2
1442 // bCC copy1MBB
1443 // fallthrough --> copy0MBB
1444 MachineBasicBlock *thisMBB = BB;
1445 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1446 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1447 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1448 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1449 MachineFunction *F = BB->getParent();
1450 F->getBasicBlockList().insert(It, copy0MBB);
1451 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001452 // Update machine-CFG edges by first adding all successors of the current
1453 // block to the new block which will contain the Phi node for the select.
1454 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1455 e = BB->succ_end(); i != e; ++i)
1456 sinkMBB->addSuccessor(*i);
1457 // Next, remove all successors of the current block, and add the true
1458 // and fallthrough blocks as its successors.
1459 while(!BB->succ_empty())
1460 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001461 BB->addSuccessor(copy0MBB);
1462 BB->addSuccessor(sinkMBB);
1463
1464 // copy0MBB:
1465 // %FalseValue = ...
1466 // # fallthrough to sinkMBB
1467 BB = copy0MBB;
1468
1469 // Update machine-CFG edges
1470 BB->addSuccessor(sinkMBB);
1471
1472 // sinkMBB:
1473 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1474 // ...
1475 BB = sinkMBB;
1476 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1477 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1478 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1479
1480 delete MI; // The pseudo instruction is gone now.
1481 return BB;
1482}
1483
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001484SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1485 DAGCombinerInfo &DCI) const {
1486 TargetMachine &TM = getTargetMachine();
1487 SelectionDAG &DAG = DCI.DAG;
1488 switch (N->getOpcode()) {
1489 default: break;
1490 case ISD::SINT_TO_FP:
1491 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001492 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1493 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1494 // We allow the src/dst to be either f32/f64, but the intermediate
1495 // type must be i64.
1496 if (N->getOperand(0).getValueType() == MVT::i64) {
1497 SDOperand Val = N->getOperand(0).getOperand(0);
1498 if (Val.getValueType() == MVT::f32) {
1499 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1500 DCI.AddToWorklist(Val.Val);
1501 }
1502
1503 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001504 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001505 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001506 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001507 if (N->getValueType(0) == MVT::f32) {
1508 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1509 DCI.AddToWorklist(Val.Val);
1510 }
1511 return Val;
1512 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1513 // If the intermediate type is i32, we can avoid the load/store here
1514 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001515 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001516 }
1517 }
1518 break;
Chris Lattner51269842006-03-01 05:50:56 +00001519 case ISD::STORE:
1520 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1521 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1522 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1523 N->getOperand(1).getValueType() == MVT::i32) {
1524 SDOperand Val = N->getOperand(1).getOperand(0);
1525 if (Val.getValueType() == MVT::f32) {
1526 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1527 DCI.AddToWorklist(Val.Val);
1528 }
1529 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1530 DCI.AddToWorklist(Val.Val);
1531
1532 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1533 N->getOperand(2), N->getOperand(3));
1534 DCI.AddToWorklist(Val.Val);
1535 return Val;
1536 }
1537 break;
Chris Lattner4468c222006-03-31 06:02:07 +00001538 case PPCISD::VCMP: {
1539 // If a VCMPo node already exists with exactly the same operands as this
1540 // node, use its result instead of this node (VCMPo computes both a CR6 and
1541 // a normal output).
1542 //
1543 if (!N->getOperand(0).hasOneUse() &&
1544 !N->getOperand(1).hasOneUse() &&
1545 !N->getOperand(2).hasOneUse()) {
1546
1547 // Scan all of the users of the LHS, looking for VCMPo's that match.
1548 SDNode *VCMPoNode = 0;
1549
1550 SDNode *LHSN = N->getOperand(0).Val;
1551 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
1552 UI != E; ++UI)
1553 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
1554 (*UI)->getOperand(1) == N->getOperand(1) &&
1555 (*UI)->getOperand(2) == N->getOperand(2) &&
1556 (*UI)->getOperand(0) == N->getOperand(0)) {
1557 VCMPoNode = *UI;
1558 break;
1559 }
1560
1561 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00001562 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00001563 return SDOperand(VCMPoNode, 0);
1564 }
1565 break;
1566 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001567 }
1568
1569 return SDOperand();
1570}
1571
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001572void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1573 uint64_t Mask,
1574 uint64_t &KnownZero,
1575 uint64_t &KnownOne,
1576 unsigned Depth) const {
1577 KnownZero = 0;
1578 KnownOne = 0;
1579 switch (Op.getOpcode()) {
1580 default: break;
1581 case ISD::INTRINSIC_WO_CHAIN: {
1582 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
1583 default: break;
1584 case Intrinsic::ppc_altivec_vcmpbfp_p:
1585 case Intrinsic::ppc_altivec_vcmpeqfp_p:
1586 case Intrinsic::ppc_altivec_vcmpequb_p:
1587 case Intrinsic::ppc_altivec_vcmpequh_p:
1588 case Intrinsic::ppc_altivec_vcmpequw_p:
1589 case Intrinsic::ppc_altivec_vcmpgefp_p:
1590 case Intrinsic::ppc_altivec_vcmpgtfp_p:
1591 case Intrinsic::ppc_altivec_vcmpgtsb_p:
1592 case Intrinsic::ppc_altivec_vcmpgtsh_p:
1593 case Intrinsic::ppc_altivec_vcmpgtsw_p:
1594 case Intrinsic::ppc_altivec_vcmpgtub_p:
1595 case Intrinsic::ppc_altivec_vcmpgtuh_p:
1596 case Intrinsic::ppc_altivec_vcmpgtuw_p:
1597 KnownZero = ~1U; // All bits but the low one are known to be zero.
1598 break;
1599 }
1600 }
1601 }
1602}
1603
1604
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001605/// getConstraintType - Given a constraint letter, return the type of
1606/// constraint it is for this target.
1607PPCTargetLowering::ConstraintType
1608PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1609 switch (ConstraintLetter) {
1610 default: break;
1611 case 'b':
1612 case 'r':
1613 case 'f':
1614 case 'v':
1615 case 'y':
1616 return C_RegisterClass;
1617 }
1618 return TargetLowering::getConstraintType(ConstraintLetter);
1619}
1620
1621
Chris Lattnerddc787d2006-01-31 19:20:21 +00001622std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001623getRegClassForInlineAsmConstraint(const std::string &Constraint,
1624 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001625 if (Constraint.size() == 1) {
1626 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1627 default: break; // Unknown constriant letter
1628 case 'b':
1629 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1630 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1631 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1632 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1633 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1634 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1635 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1636 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1637 0);
1638 case 'r':
1639 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1640 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1641 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1642 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1643 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1644 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1645 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1646 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1647 0);
1648 case 'f':
1649 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1650 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1651 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1652 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1653 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1654 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1655 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1656 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1657 0);
1658 case 'v':
1659 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1660 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1661 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1662 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1663 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1664 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1665 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1666 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1667 0);
1668 case 'y':
1669 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1670 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1671 0);
1672 }
1673 }
1674
Chris Lattner1efa40f2006-02-22 00:56:39 +00001675 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001676}
Chris Lattner763317d2006-02-07 00:47:13 +00001677
1678// isOperandValidForConstraint
1679bool PPCTargetLowering::
1680isOperandValidForConstraint(SDOperand Op, char Letter) {
1681 switch (Letter) {
1682 default: break;
1683 case 'I':
1684 case 'J':
1685 case 'K':
1686 case 'L':
1687 case 'M':
1688 case 'N':
1689 case 'O':
1690 case 'P': {
1691 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1692 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1693 switch (Letter) {
1694 default: assert(0 && "Unknown constraint letter!");
1695 case 'I': // "I" is a signed 16-bit constant.
1696 return (short)Value == (int)Value;
1697 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1698 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1699 return (short)Value == 0;
1700 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1701 return (Value >> 16) == 0;
1702 case 'M': // "M" is a constant that is greater than 31.
1703 return Value > 31;
1704 case 'N': // "N" is a positive constant that is an exact power of two.
1705 return (int)Value > 0 && isPowerOf2_32(Value);
1706 case 'O': // "O" is the constant zero.
1707 return Value == 0;
1708 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1709 return (short)-Value == (int)-Value;
1710 }
1711 break;
1712 }
1713 }
1714
1715 // Handle standard constraint letters.
1716 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1717}
Evan Chengc4c62572006-03-13 23:20:37 +00001718
1719/// isLegalAddressImmediate - Return true if the integer value can be used
1720/// as the offset of the target addressing mode.
1721bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1722 // PPC allows a sign-extended 16-bit immediate field.
1723 return (V > -(1 << 16) && V < (1 << 16)-1);
1724}