blob: 137b3f95547d617d46c8c24aa229243f73aa5691 [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 Lattnera1d95e12006-04-08 22:59:15 +000090 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
91 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
92 setOperationAction(ISD::SELECT, MVT::v8i16, Expand);
93 setOperationAction(ISD::SELECT, MVT::v16i8, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000094
Chris Lattner0b1e4e52005-08-26 17:36:52 +000095 // PowerPC wants to turn select_cc of FP into fsel when possible.
96 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
97 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000098
Nate Begeman750ac1b2006-02-01 07:19:44 +000099 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +0000100 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000101
Nate Begeman81e80972006-03-17 01:40:33 +0000102 // PowerPC does not have BRCOND which requires SetCC
103 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000104
Chris Lattnerf7605322005-08-31 21:09:52 +0000105 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
106 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000107
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000108 // PowerPC does not have [U|S]INT_TO_FP
109 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
110 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
111
Chris Lattner53e88452005-12-23 05:13:35 +0000112 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
113 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
114
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000115 // PowerPC does not have truncstore for i1.
116 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000117
Jim Laskeyabf6d172006-01-05 01:25:28 +0000118 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000119 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000120 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000121 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000122 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000123 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000124
Nate Begeman28a6b022005-12-10 02:36:00 +0000125 // We want to legalize GlobalAddress and ConstantPool nodes into the
126 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000127 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000128 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000129
Nate Begemanee625572006-01-27 21:09:22 +0000130 // RET must be custom lowered, to meet ABI requirements
131 setOperationAction(ISD::RET , MVT::Other, Custom);
132
Nate Begemanacc398c2006-01-25 18:21:52 +0000133 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
134 setOperationAction(ISD::VASTART , MVT::Other, Custom);
135
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000136 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000137 setOperationAction(ISD::VAARG , MVT::Other, Expand);
138 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
139 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000140 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
141 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
142 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000143
Chris Lattner6d92cad2006-03-26 10:06:40 +0000144 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000145 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000146
Nate Begemanc09eeec2005-09-06 22:03:27 +0000147 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000148 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000149 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
150 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner7fbcef72006-03-24 07:53:47 +0000151
152 // FIXME: disable this lowered code. This generates 64-bit register values,
153 // and we don't model the fact that the top part is clobbered by calls. We
154 // need to flag these together so that the value isn't live across a call.
155 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
156
Nate Begemanae749a92005-10-25 23:48:36 +0000157 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
158 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
159 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000160 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000161 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000162 }
163
164 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
165 // 64 bit PowerPC implementations can support i64 types directly
166 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000167 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
168 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000169 } else {
170 // 32 bit PowerPC wants to expand i64 shifts itself.
171 setOperationAction(ISD::SHL, MVT::i64, Custom);
172 setOperationAction(ISD::SRL, MVT::i64, Custom);
173 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000174 }
Evan Chengd30bf012006-03-01 01:11:20 +0000175
Nate Begeman425a9692005-11-29 08:17:20 +0000176 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000177 // First set operation action for all vector types to expand. Then we
178 // will selectively turn on ones that can be effectively codegen'd.
179 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
180 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
181 // add/sub/and/or/xor are legal for all supported vector VT's.
182 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
183 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
184 setOperationAction(ISD::AND , (MVT::ValueType)VT, Legal);
185 setOperationAction(ISD::OR , (MVT::ValueType)VT, Legal);
186 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Legal);
187
Chris Lattner7ff7e672006-04-04 17:25:31 +0000188 // We promote all shuffles to v16i8.
189 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
190 AddPromotedToType(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000191
192 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
193 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
194 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
195 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
196 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
197 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
198 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
199 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000200
201 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000202 }
203
Chris Lattner7ff7e672006-04-04 17:25:31 +0000204 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
205 // with merges, splats, etc.
206 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
207
Nate Begeman425a9692005-11-29 08:17:20 +0000208 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000209 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000210 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
211 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000212
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000213 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000214
Chris Lattnerb2177b92006-03-19 06:55:52 +0000215 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
216 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000217
Chris Lattner541f91b2006-04-02 00:43:36 +0000218 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
219 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000220 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
221 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000222 }
223
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000224 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000225 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000226
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000227 // We have target-specific dag combine patterns for the following nodes:
228 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000229 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000230
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000231 computeRegisterProperties();
232}
233
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000234const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
235 switch (Opcode) {
236 default: return 0;
237 case PPCISD::FSEL: return "PPCISD::FSEL";
238 case PPCISD::FCFID: return "PPCISD::FCFID";
239 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
240 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000241 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000242 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
243 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000244 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000245 case PPCISD::Hi: return "PPCISD::Hi";
246 case PPCISD::Lo: return "PPCISD::Lo";
247 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
248 case PPCISD::SRL: return "PPCISD::SRL";
249 case PPCISD::SRA: return "PPCISD::SRA";
250 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000251 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
252 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000253 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000254 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000255 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000256 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000257 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000258 }
259}
260
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000261/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
262static bool isFloatingPointZero(SDOperand Op) {
263 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
264 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
265 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
266 // Maybe this has already been legalized into the constant pool?
267 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
268 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
269 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
270 }
271 return false;
272}
273
Chris Lattnerddb739e2006-04-06 17:23:16 +0000274/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
275/// true if Op is undef or if it matches the specified value.
276static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
277 return Op.getOpcode() == ISD::UNDEF ||
278 cast<ConstantSDNode>(Op)->getValue() == Val;
279}
280
281/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
282/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000283bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
284 if (!isUnary) {
285 for (unsigned i = 0; i != 16; ++i)
286 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
287 return false;
288 } else {
289 for (unsigned i = 0; i != 8; ++i)
290 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
291 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
292 return false;
293 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000294 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000295}
296
297/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
298/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000299bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
300 if (!isUnary) {
301 for (unsigned i = 0; i != 16; i += 2)
302 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
303 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
304 return false;
305 } else {
306 for (unsigned i = 0; i != 8; i += 2)
307 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
308 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
309 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
310 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
311 return false;
312 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000313 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000314}
315
Chris Lattnercaad1632006-04-06 22:02:42 +0000316/// isVMerge - Common function, used to match vmrg* shuffles.
317///
318static bool isVMerge(SDNode *N, unsigned UnitSize,
319 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000320 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
321 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
322 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
323 "Unsupported merge size!");
324
325 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
326 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
327 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000328 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000329 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000330 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000331 return false;
332 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000333 return true;
334}
335
336/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
337/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
338bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
339 if (!isUnary)
340 return isVMerge(N, UnitSize, 8, 24);
341 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000342}
343
344/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
345/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000346bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
347 if (!isUnary)
348 return isVMerge(N, UnitSize, 0, 16);
349 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000350}
351
352
Chris Lattnerd0608e12006-04-06 18:26:28 +0000353/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
354/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000355int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000356 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
357 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000358 // Find the first non-undef value in the shuffle mask.
359 unsigned i;
360 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
361 /*search*/;
362
363 if (i == 16) return -1; // all undef.
364
365 // Otherwise, check to see if the rest of the elements are consequtively
366 // numbered from this value.
367 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
368 if (ShiftAmt < i) return -1;
369 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000370
Chris Lattnerf24380e2006-04-06 22:28:36 +0000371 if (!isUnary) {
372 // Check the rest of the elements to see if they are consequtive.
373 for (++i; i != 16; ++i)
374 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
375 return -1;
376 } else {
377 // Check the rest of the elements to see if they are consequtive.
378 for (++i; i != 16; ++i)
379 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
380 return -1;
381 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000382
383 return ShiftAmt;
384}
Chris Lattneref819f82006-03-20 06:33:01 +0000385
386/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
387/// specifies a splat of a single element that is suitable for input to
388/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000389bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
390 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
391 N->getNumOperands() == 16 &&
392 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000393
Chris Lattner88a99ef2006-03-20 06:37:44 +0000394 // This is a splat operation if each element of the permute is the same, and
395 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000396 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000397 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000398 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
399 ElementBase = EltV->getValue();
400 else
401 return false; // FIXME: Handle UNDEF elements too!
402
403 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
404 return false;
405
406 // Check that they are consequtive.
407 for (unsigned i = 1; i != EltSize; ++i) {
408 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
409 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
410 return false;
411 }
412
Chris Lattner88a99ef2006-03-20 06:37:44 +0000413 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000414 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattner88a99ef2006-03-20 06:37:44 +0000415 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
416 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000417 for (unsigned j = 0; j != EltSize; ++j)
418 if (N->getOperand(i+j) != N->getOperand(j))
419 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000420 }
421
Chris Lattner7ff7e672006-04-04 17:25:31 +0000422 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000423}
424
425/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
426/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000427unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
428 assert(isSplatShuffleMask(N, EltSize));
429 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000430}
431
Chris Lattnere87192a2006-04-12 17:37:20 +0000432/// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
Chris Lattner140a58f2006-04-08 06:46:53 +0000433/// by using a vspltis[bhw] instruction of the specified element size, return
434/// the constant being splatted. The ByteSize field indicates the number of
435/// bytes of each element [124] -> [bhw].
Chris Lattnere87192a2006-04-12 17:37:20 +0000436SDOperand PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000437 SDOperand OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000438
439 // If ByteSize of the splat is bigger than the element size of the
440 // build_vector, then we have a case where we are checking for a splat where
441 // multiple elements of the buildvector are folded together into a single
442 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
443 unsigned EltSize = 16/N->getNumOperands();
444 if (EltSize < ByteSize) {
445 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
446 SDOperand UniquedVals[4];
447 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
448
449 // See if all of the elements in the buildvector agree across.
450 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
451 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
452 // If the element isn't a constant, bail fully out.
453 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDOperand();
454
455
456 if (UniquedVals[i&(Multiple-1)].Val == 0)
457 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
458 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
459 return SDOperand(); // no match.
460 }
461
462 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
463 // either constant or undef values that are identical for each chunk. See
464 // if these chunks can form into a larger vspltis*.
465
466 // Check to see if all of the leading entries are either 0 or -1. If
467 // neither, then this won't fit into the immediate field.
468 bool LeadingZero = true;
469 bool LeadingOnes = true;
470 for (unsigned i = 0; i != Multiple-1; ++i) {
471 if (UniquedVals[i].Val == 0) continue; // Must have been undefs.
472
473 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
474 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
475 }
476 // Finally, check the least significant entry.
477 if (LeadingZero) {
478 if (UniquedVals[Multiple-1].Val == 0)
479 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
480 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getValue();
481 if (Val < 16)
482 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
483 }
484 if (LeadingOnes) {
485 if (UniquedVals[Multiple-1].Val == 0)
486 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
487 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSignExtended();
488 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
489 return DAG.getTargetConstant(Val, MVT::i32);
490 }
491
492 return SDOperand();
493 }
494
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000495 // Check to see if this buildvec has a single non-undef value in its elements.
496 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
497 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
498 if (OpVal.Val == 0)
499 OpVal = N->getOperand(i);
500 else if (OpVal != N->getOperand(i))
Chris Lattner140a58f2006-04-08 06:46:53 +0000501 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000502 }
503
Chris Lattner140a58f2006-04-08 06:46:53 +0000504 if (OpVal.Val == 0) return SDOperand(); // All UNDEF: use implicit def.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000505
Nate Begeman98e70cc2006-03-28 04:15:58 +0000506 unsigned ValSizeInBytes = 0;
507 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000508 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
509 Value = CN->getValue();
510 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
511 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
512 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
513 Value = FloatToBits(CN->getValue());
514 ValSizeInBytes = 4;
515 }
516
517 // If the splat value is larger than the element value, then we can never do
518 // this splat. The only case that we could fit the replicated bits into our
519 // immediate field for would be zero, and we prefer to use vxor for it.
Chris Lattner140a58f2006-04-08 06:46:53 +0000520 if (ValSizeInBytes < ByteSize) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000521
522 // If the element value is larger than the splat value, cut it in half and
523 // check to see if the two halves are equal. Continue doing this until we
524 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
525 while (ValSizeInBytes > ByteSize) {
526 ValSizeInBytes >>= 1;
527
528 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000529 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
530 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner140a58f2006-04-08 06:46:53 +0000531 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000532 }
533
534 // Properly sign extend the value.
535 int ShAmt = (4-ByteSize)*8;
536 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
537
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000538 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner140a58f2006-04-08 06:46:53 +0000539 if (MaskVal == 0) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000540
Chris Lattner140a58f2006-04-08 06:46:53 +0000541 // Finally, if this value fits in a 5 bit sext field, return it
542 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
543 return DAG.getTargetConstant(MaskVal, MVT::i32);
544 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000545}
546
Chris Lattnerac225ca2006-04-12 19:07:14 +0000547// If this is a vector of constants or undefs, get the bits. A bit in
548// UndefBits is set if the corresponding element of the vector is an
549// ISD::UNDEF value. For undefs, the corresponding VectorBits values are
550// zero. Return true if this is not an array of constants, false if it is.
551//
552// Note that VectorBits/UndefBits are returned in 'little endian' form, so
553// elements 0,1 go in VectorBits[0] and 2,3 go in VectorBits[1] for a v4i32.
554static bool GetConstantBuildVectorBits(SDNode *BV, uint64_t VectorBits[2],
555 uint64_t UndefBits[2]) {
556 // Start with zero'd results.
557 VectorBits[0] = VectorBits[1] = UndefBits[0] = UndefBits[1] = 0;
558
559 unsigned EltBitSize = MVT::getSizeInBits(BV->getOperand(0).getValueType());
560 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
561 SDOperand OpVal = BV->getOperand(i);
562
563 unsigned PartNo = i >= e/2; // In the upper 128 bits?
564 unsigned SlotNo = i & (e/2-1); // Which subpiece of the uint64_t it is.
565
566 uint64_t EltBits = 0;
567 if (OpVal.getOpcode() == ISD::UNDEF) {
568 uint64_t EltUndefBits = ~0U >> (32-EltBitSize);
569 UndefBits[PartNo] |= EltUndefBits << (SlotNo*EltBitSize);
570 continue;
571 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
572 EltBits = CN->getValue() & (~0U >> (32-EltBitSize));
573 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
574 assert(CN->getValueType(0) == MVT::f32 &&
575 "Only one legal FP vector type!");
576 EltBits = FloatToBits(CN->getValue());
577 } else {
578 // Nonconstant element.
579 return true;
580 }
581
582 VectorBits[PartNo] |= EltBits << (SlotNo*EltBitSize);
583 }
584
585 //printf("%llx %llx %llx %llx\n",
586 // VectorBits[0], VectorBits[1], UndefBits[0], UndefBits[1]);
587 return false;
588}
Chris Lattneref819f82006-03-20 06:33:01 +0000589
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000590/// LowerOperation - Provide custom lowering hooks for some operations.
591///
Nate Begeman21e463b2005-10-16 05:39:50 +0000592SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000593 switch (Op.getOpcode()) {
594 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000595 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000596 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000597 SDOperand Src = Op.getOperand(0);
598 if (Src.getValueType() == MVT::f32)
599 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
600
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000601 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000602 switch (Op.getValueType()) {
603 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
604 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000605 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000606 break;
607 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000608 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000609 break;
610 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000611
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000612 // Convert the FP value to an int value through memory.
613 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
614 if (Op.getValueType() == MVT::i32)
615 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
616 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000617 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000618 case ISD::SINT_TO_FP:
619 if (Op.getOperand(0).getValueType() == MVT::i64) {
620 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
621 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
622 if (Op.getValueType() == MVT::f32)
623 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
624 return FP;
625 } else {
626 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
627 "Unhandled SINT_TO_FP type in custom expander!");
628 // Since we only generate this in 64-bit mode, we can take advantage of
629 // 64-bit registers. In particular, sign extend the input value into the
630 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
631 // then lfd it and fcfid it.
632 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
633 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
634 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
635
636 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
637 Op.getOperand(0));
638
639 // STD the extended value into the stack slot.
640 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
641 DAG.getEntryNode(), Ext64, FIdx,
642 DAG.getSrcValue(NULL));
643 // Load the value as a double.
644 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
645
646 // FCFID it and return it.
647 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
648 if (Op.getValueType() == MVT::f32)
649 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
650 return FP;
651 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000652 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000653
Chris Lattnerf7605322005-08-31 21:09:52 +0000654 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000655 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000656 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
657 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
658 break;
659
660 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
661
662 // Cannot handle SETEQ/SETNE.
663 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
664
665 MVT::ValueType ResVT = Op.getValueType();
666 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
667 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
668 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000669
Chris Lattnerf7605322005-08-31 21:09:52 +0000670 // If the RHS of the comparison is a 0.0, we don't need to do the
671 // subtraction at all.
672 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000673 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000674 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000675 case ISD::SETULT:
676 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000677 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000678 case ISD::SETUGE:
679 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000680 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
681 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000682 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000683 case ISD::SETUGT:
684 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000685 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000686 case ISD::SETULE:
687 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000688 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
689 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000690 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000691 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000692 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000693
Chris Lattnereb255f22005-10-25 20:54:57 +0000694 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000695 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000696 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000697 case ISD::SETULT:
698 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000699 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
700 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
701 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
702 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000703 case ISD::SETUGE:
704 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000705 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
706 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
707 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
708 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000709 case ISD::SETUGT:
710 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000711 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
712 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
713 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
714 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000715 case ISD::SETULE:
716 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000717 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
718 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
719 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
720 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000721 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000722 break;
723 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000724 case ISD::SHL: {
725 assert(Op.getValueType() == MVT::i64 &&
726 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
727 // The generic code does a fine job expanding shift by a constant.
728 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
729
730 // Otherwise, expand into a bunch of logical ops. Note that these ops
731 // depend on the PPC behavior for oversized shift amounts.
732 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
733 DAG.getConstant(0, MVT::i32));
734 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
735 DAG.getConstant(1, MVT::i32));
736 SDOperand Amt = Op.getOperand(1);
737
738 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
739 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000740 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
741 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000742 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
743 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
744 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000745 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000746 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000747 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000748 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
749 }
750 case ISD::SRL: {
751 assert(Op.getValueType() == MVT::i64 &&
752 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
753 // The generic code does a fine job expanding shift by a constant.
754 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
755
756 // Otherwise, expand into a bunch of logical ops. Note that these ops
757 // depend on the PPC behavior for oversized shift amounts.
758 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
759 DAG.getConstant(0, MVT::i32));
760 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
761 DAG.getConstant(1, MVT::i32));
762 SDOperand Amt = Op.getOperand(1);
763
764 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
765 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000766 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
767 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000768 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
769 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
770 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000771 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000772 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000773 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000774 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
775 }
776 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000777 assert(Op.getValueType() == MVT::i64 &&
778 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
779 // The generic code does a fine job expanding shift by a constant.
780 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
781
782 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
783 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
784 DAG.getConstant(0, MVT::i32));
785 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
786 DAG.getConstant(1, MVT::i32));
787 SDOperand Amt = Op.getOperand(1);
788
789 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
790 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000791 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
792 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000793 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
794 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
795 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000796 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
797 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000798 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
799 Tmp4, Tmp6, ISD::SETLE);
800 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000801 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000802 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000803 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
804 Constant *C = CP->get();
805 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000806 SDOperand Zero = DAG.getConstant(0, MVT::i32);
807
Evan Cheng4c1aa862006-02-22 20:19:42 +0000808 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000809 // Generate non-pic code that has direct accesses to the constant pool.
810 // The address of the global is just (hi(&g)+lo(&g)).
811 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
812 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
813 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
814 }
815
816 // Only lower ConstantPool on Darwin.
817 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
818 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000819 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000820 // With PIC, the first instruction is actually "GR+hi(&G)".
821 Hi = DAG.getNode(ISD::ADD, MVT::i32,
822 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
823 }
824
825 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
826 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
827 return Lo;
828 }
Chris Lattner860e8862005-11-17 07:30:41 +0000829 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000830 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
831 GlobalValue *GV = GSDN->getGlobal();
832 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000833 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000834
Evan Cheng4c1aa862006-02-22 20:19:42 +0000835 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000836 // Generate non-pic code that has direct accesses to globals.
837 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000838 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
839 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
840 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
841 }
Chris Lattner860e8862005-11-17 07:30:41 +0000842
Chris Lattner1d05cb42005-11-17 18:55:48 +0000843 // Only lower GlobalAddress on Darwin.
844 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000845
Chris Lattner860e8862005-11-17 07:30:41 +0000846 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000847 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000848 // With PIC, the first instruction is actually "GR+hi(&G)".
849 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000850 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000851 }
852
853 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
854 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
855
Chris Lattner37dd6f12006-01-29 20:49:17 +0000856 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
857 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000858 return Lo;
859
860 // If the global is weak or external, we have to go through the lazy
861 // resolution stub.
862 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
863 }
Nate Begeman44775902006-01-31 08:17:29 +0000864 case ISD::SETCC: {
865 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000866
867 // If we're comparing for equality to zero, expose the fact that this is
868 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
869 // fold the new nodes.
870 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
871 if (C->isNullValue() && CC == ISD::SETEQ) {
872 MVT::ValueType VT = Op.getOperand(0).getValueType();
873 SDOperand Zext = Op.getOperand(0);
874 if (VT < MVT::i32) {
875 VT = MVT::i32;
876 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
877 }
878 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
879 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
880 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
881 DAG.getConstant(Log2b, getShiftAmountTy()));
882 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
883 }
884 // Leave comparisons against 0 and -1 alone for now, since they're usually
885 // optimized. FIXME: revisit this when we can custom lower all setcc
886 // optimizations.
887 if (C->isAllOnesValue() || C->isNullValue())
888 break;
889 }
890
891 // If we have an integer seteq/setne, turn it into a compare against zero
892 // by subtracting the rhs from the lhs, which is faster than setting a
893 // condition register, reading it back out, and masking the correct bit.
894 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
895 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
896 MVT::ValueType VT = Op.getValueType();
897 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
898 Op.getOperand(1));
899 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
900 }
Nate Begeman44775902006-01-31 08:17:29 +0000901 break;
902 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000903 case ISD::VASTART: {
904 // vastart just stores the address of the VarArgsFrameIndex slot into the
905 // memory location argument.
906 // FIXME: Replace MVT::i32 with PointerTy
907 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
908 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
909 Op.getOperand(1), Op.getOperand(2));
910 }
Nate Begemanee625572006-01-27 21:09:22 +0000911 case ISD::RET: {
912 SDOperand Copy;
913
914 switch(Op.getNumOperands()) {
915 default:
916 assert(0 && "Do not know how to return this many arguments!");
917 abort();
918 case 1:
919 return SDOperand(); // ret void is legal
920 case 2: {
921 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerbee98362006-04-11 01:38:39 +0000922 unsigned ArgReg;
923 if (MVT::isVector(ArgVT))
924 ArgReg = PPC::V2;
925 else if (MVT::isInteger(ArgVT))
926 ArgReg = PPC::R3;
927 else {
928 assert(MVT::isFloatingPoint(ArgVT));
929 ArgReg = PPC::F1;
930 }
931
Nate Begemanee625572006-01-27 21:09:22 +0000932 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
933 SDOperand());
Chris Lattner06c24352006-04-11 01:21:43 +0000934
935 // If we haven't noted the R3/F1 are live out, do so now.
936 if (DAG.getMachineFunction().liveout_empty())
937 DAG.getMachineFunction().addLiveOut(ArgReg);
Nate Begemanee625572006-01-27 21:09:22 +0000938 break;
939 }
940 case 3:
941 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
942 SDOperand());
943 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
Chris Lattner06c24352006-04-11 01:21:43 +0000944 // If we haven't noted the R3+R4 are live out, do so now.
945 if (DAG.getMachineFunction().liveout_empty()) {
946 DAG.getMachineFunction().addLiveOut(PPC::R3);
947 DAG.getMachineFunction().addLiveOut(PPC::R4);
948 }
Nate Begemanee625572006-01-27 21:09:22 +0000949 break;
950 }
951 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
952 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000953 case ISD::SCALAR_TO_VECTOR: {
954 // Create a stack slot that is 16-byte aligned.
955 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
956 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
957 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
958
959 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000960 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
961 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner7f20b132006-03-28 01:43:22 +0000962 // Load it out.
963 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
Chris Lattnerb2177b92006-03-19 06:55:52 +0000964 }
Chris Lattnere87192a2006-04-12 17:37:20 +0000965 case ISD::BUILD_VECTOR: {
Chris Lattner64b3a082006-03-24 07:48:08 +0000966 // If this is a case we can't handle, return null and let the default
Chris Lattnerac225ca2006-04-12 19:07:14 +0000967 // expansion code take care of it. If we CAN select this case, return Op
968 // or something simpler.
969
970 // If this is a vector of constants or undefs, get the bits. A bit in
971 // UndefBits is set if the corresponding element of the vector is an
972 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
973 // zero.
974 uint64_t VectorBits[2];
975 uint64_t UndefBits[2];
976 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits))
977 return SDOperand(); // Not a constant vector.
Chris Lattner2b1c3252006-04-12 16:53:28 +0000978
979 // See if this is all zeros.
Chris Lattnerac225ca2006-04-12 19:07:14 +0000980 if ((VectorBits[0] | VectorBits[1]) == 0) {
Chris Lattner2b1c3252006-04-12 16:53:28 +0000981 // Canonicalize all zero vectors to be v4i32.
982 if (Op.getValueType() != MVT::v4i32) {
983 SDOperand Z = DAG.getConstant(0, MVT::i32);
984 Z = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Z, Z, Z, Z);
985 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Z);
986 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000987 return Op;
Chris Lattner2b1c3252006-04-12 16:53:28 +0000988 }
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000989
Chris Lattnere87192a2006-04-12 17:37:20 +0000990 // Check to see if this is something we can use VSPLTI* to form.
991 MVT::ValueType CanonicalVT = MVT::Other;
992 SDNode *CST = 0;
993
994 if ((CST = PPC::get_VSPLTI_elt(Op.Val, 4, DAG).Val)) // vspltisw
995 CanonicalVT = MVT::v4i32;
996 else if ((CST = PPC::get_VSPLTI_elt(Op.Val, 2, DAG).Val)) // vspltish
997 CanonicalVT = MVT::v8i16;
998 else if ((CST = PPC::get_VSPLTI_elt(Op.Val, 1, DAG).Val)) // vspltisb
999 CanonicalVT = MVT::v16i8;
1000
1001 // If this matches one of the vsplti* patterns, force it to the canonical
1002 // type for the pattern.
1003 if (CST) {
1004 if (Op.getValueType() != CanonicalVT) {
1005 // Convert the splatted element to the right element type.
1006 SDOperand Elt = DAG.getNode(ISD::TRUNCATE,
1007 MVT::getVectorBaseType(CanonicalVT),
1008 SDOperand(CST, 0));
1009 std::vector<SDOperand> Ops(MVT::getVectorNumElements(CanonicalVT), Elt);
1010 SDOperand Res = DAG.getNode(ISD::BUILD_VECTOR, CanonicalVT, Ops);
1011 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
1012 }
Chris Lattner9c61dcf2006-03-25 06:12:06 +00001013 return Op;
Chris Lattnere87192a2006-04-12 17:37:20 +00001014 }
Chris Lattnerac225ca2006-04-12 19:07:14 +00001015
1016 // If this is some other splat of 4-byte elements, see if we can handle it
1017 // in another way.
1018 // FIXME: Make this more undef happy and work with other widths (1,2 bytes).
1019 if (VectorBits[0] == VectorBits[1] &&
1020 unsigned(VectorBits[0]) == unsigned(VectorBits[0] >> 32)) {
1021 unsigned Bits = unsigned(VectorBits[0]);
1022
1023 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
1024 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). These are important
1025 // for fneg/fabs.
1026 if (Bits == 0x80000000 || Bits == 0x7FFFFFFF) {
1027 // Make -1 and vspltisw -1:
1028 SDOperand OnesI = DAG.getConstant(~0U, MVT::i32);
1029 SDOperand OnesV = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
1030 OnesI, OnesI, OnesI, OnesI);
1031
1032 // Make the VSLW intrinsic, computing 0x8000_0000.
1033 SDOperand Res
1034 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, MVT::v4i32,
1035 DAG.getConstant(Intrinsic::ppc_altivec_vslw, MVT::i32),
1036 OnesV, OnesV);
1037
1038 // If this is 0x7FFF_FFFF, xor by OnesV to invert it.
1039 if (Bits == 0x7FFFFFFF)
1040 Res = DAG.getNode(ISD::XOR, MVT::v4i32, Res, OnesV);
1041
1042 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
1043 }
1044 }
1045
Chris Lattner9c61dcf2006-03-25 06:12:06 +00001046
Chris Lattner64b3a082006-03-24 07:48:08 +00001047 return SDOperand();
Chris Lattnere87192a2006-04-12 17:37:20 +00001048 }
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +00001049 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +00001050 SDOperand V1 = Op.getOperand(0);
1051 SDOperand V2 = Op.getOperand(1);
1052 SDOperand PermMask = Op.getOperand(2);
1053
1054 // Cases that are handled by instructions that take permute immediates
1055 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
1056 // selected by the instruction selector.
Chris Lattnercaad1632006-04-06 22:02:42 +00001057 if (V2.getOpcode() == ISD::UNDEF) {
1058 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
1059 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
1060 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
Chris Lattnerf24380e2006-04-06 22:28:36 +00001061 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
1062 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
1063 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
Chris Lattnercaad1632006-04-06 22:02:42 +00001064 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
1065 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
1066 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
1067 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
1068 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
1069 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
1070 return Op;
1071 }
1072 }
Chris Lattnerdd4d2d02006-03-20 06:51:10 +00001073
Chris Lattnerf24380e2006-04-06 22:28:36 +00001074 // Altivec has a variety of "shuffle immediates" that take two vector inputs
1075 // and produce a fixed permutation. If any of these match, do not lower to
1076 // VPERM.
1077 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
1078 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
1079 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
Chris Lattnercaad1632006-04-06 22:02:42 +00001080 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
1081 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
1082 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
1083 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
1084 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
1085 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
Chris Lattnerddb739e2006-04-06 17:23:16 +00001086 return Op;
1087
Chris Lattnerdd4d2d02006-03-20 06:51:10 +00001088 // TODO: Handle more cases, and also handle cases that are cheaper to do as
1089 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +00001090
1091 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
1092 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +00001093 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +00001094
1095 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
1096 // that it is in input element units, not in bytes. Convert now.
1097 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
1098 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
1099
1100 std::vector<SDOperand> ResultMask;
1101 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
1102 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
1103
1104 for (unsigned j = 0; j != BytesPerElement; ++j)
1105 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
1106 MVT::i8));
1107 }
1108
1109 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
1110 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
1111 }
Chris Lattner48b61a72006-03-28 00:40:33 +00001112 case ISD::INTRINSIC_WO_CHAIN: {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001113 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
Chris Lattner6d92cad2006-03-26 10:06:40 +00001114
1115 // If this is a lowered altivec predicate compare, CompareOpc is set to the
1116 // opcode number of the comparison.
1117 int CompareOpc = -1;
Chris Lattnera17b1552006-03-31 05:13:27 +00001118 bool isDot = false;
Chris Lattner6d92cad2006-03-26 10:06:40 +00001119 switch (IntNo) {
1120 default: return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattnera17b1552006-03-31 05:13:27 +00001121 // Comparison predicates.
1122 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
1123 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
1124 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
1125 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
1126 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
1127 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
1128 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
1129 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
1130 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
1131 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
1132 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
1133 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
1134 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
1135
1136 // Normal Comparisons.
1137 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
1138 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
1139 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
1140 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
1141 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
1142 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
1143 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
1144 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
1145 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
1146 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
1147 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
1148 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
1149 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Chris Lattner6d92cad2006-03-26 10:06:40 +00001150 }
1151
1152 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
1153
Chris Lattnera17b1552006-03-31 05:13:27 +00001154 // If this is a non-dot comparison, make the VCMP node.
Chris Lattner90217992006-04-06 23:12:19 +00001155 if (!isDot) {
1156 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
1157 Op.getOperand(1), Op.getOperand(2),
1158 DAG.getConstant(CompareOpc, MVT::i32));
1159 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
1160 }
Chris Lattnera17b1552006-03-31 05:13:27 +00001161
Chris Lattner6d92cad2006-03-26 10:06:40 +00001162 // Create the PPCISD altivec 'dot' comparison node.
1163 std::vector<SDOperand> Ops;
1164 std::vector<MVT::ValueType> VTs;
1165 Ops.push_back(Op.getOperand(2)); // LHS
1166 Ops.push_back(Op.getOperand(3)); // RHS
1167 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
1168 VTs.push_back(Op.getOperand(2).getValueType());
1169 VTs.push_back(MVT::Flag);
1170 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
1171
1172 // Now that we have the comparison, emit a copy from the CR to a GPR.
1173 // This is flagged to the above dot comparison.
1174 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
1175 DAG.getRegister(PPC::CR6, MVT::i32),
1176 CompNode.getValue(1));
1177
1178 // Unpack the result based on how the target uses it.
1179 unsigned BitNo; // Bit # of CR6.
1180 bool InvertBit; // Invert result?
1181 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1182 default: // Can't happen, don't crash on invalid number though.
1183 case 0: // Return the value of the EQ bit of CR6.
1184 BitNo = 0; InvertBit = false;
1185 break;
1186 case 1: // Return the inverted value of the EQ bit of CR6.
1187 BitNo = 0; InvertBit = true;
1188 break;
1189 case 2: // Return the value of the LT bit of CR6.
1190 BitNo = 2; InvertBit = false;
1191 break;
1192 case 3: // Return the inverted value of the LT bit of CR6.
1193 BitNo = 2; InvertBit = true;
1194 break;
1195 }
1196
1197 // Shift the bit into the low position.
1198 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
1199 DAG.getConstant(8-(3-BitNo), MVT::i32));
1200 // Isolate the bit.
1201 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
1202 DAG.getConstant(1, MVT::i32));
1203
1204 // If we are supposed to, toggle the bit.
1205 if (InvertBit)
1206 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
1207 DAG.getConstant(1, MVT::i32));
1208 return Flags;
1209 }
Chris Lattnerbc11c342005-08-31 20:23:54 +00001210 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001211 return SDOperand();
1212}
1213
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001214std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001215PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001216 //
1217 // add beautiful description of PPC stack frame format, or at least some docs
1218 //
1219 MachineFunction &MF = DAG.getMachineFunction();
1220 MachineFrameInfo *MFI = MF.getFrameInfo();
1221 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +00001222 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001223 std::vector<SDOperand> ArgValues;
1224
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001225 unsigned ArgOffset = 24;
1226 unsigned GPR_remaining = 8;
1227 unsigned FPR_remaining = 13;
1228 unsigned GPR_idx = 0, FPR_idx = 0;
1229 static const unsigned GPR[] = {
1230 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1231 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1232 };
1233 static const unsigned FPR[] = {
1234 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1235 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1236 };
1237
1238 // Add DAG nodes to load the arguments... On entry to a function on PPC,
1239 // the arguments start at offset 24, although they are likely to be passed
1240 // in registers.
1241 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
1242 SDOperand newroot, argt;
1243 unsigned ObjSize;
1244 bool needsLoad = false;
1245 bool ArgLive = !I->use_empty();
1246 MVT::ValueType ObjectVT = getValueType(I->getType());
1247
1248 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001249 default: assert(0 && "Unhandled argument type!");
1250 case MVT::i1:
1251 case MVT::i8:
1252 case MVT::i16:
1253 case MVT::i32:
1254 ObjSize = 4;
1255 if (!ArgLive) break;
1256 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001257 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001258 MF.addLiveIn(GPR[GPR_idx], VReg);
1259 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +00001260 if (ObjectVT != MVT::i32) {
1261 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
1262 : ISD::AssertZext;
1263 argt = DAG.getNode(AssertOp, MVT::i32, argt,
1264 DAG.getValueType(ObjectVT));
1265 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
1266 }
Chris Lattner915fb302005-08-30 00:19:00 +00001267 } else {
1268 needsLoad = true;
1269 }
1270 break;
Chris Lattner80720a92005-11-30 20:40:54 +00001271 case MVT::i64:
1272 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +00001273 if (!ArgLive) break;
1274 if (GPR_remaining > 0) {
1275 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +00001276 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001277 MF.addLiveIn(GPR[GPR_idx], VReg);
1278 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001279 // If we have two or more remaining argument registers, then both halves
1280 // of the i64 can be sourced from there. Otherwise, the lower half will
1281 // have to come off the stack. This can happen when an i64 is preceded
1282 // by 28 bytes of arguments.
1283 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001284 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001285 MF.addLiveIn(GPR[GPR_idx+1], VReg);
1286 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001287 } else {
1288 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
1289 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1290 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
1291 DAG.getSrcValue(NULL));
1292 }
1293 // Build the outgoing arg thingy
1294 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
1295 newroot = argLo;
1296 } else {
1297 needsLoad = true;
1298 }
1299 break;
1300 case MVT::f32:
1301 case MVT::f64:
1302 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +00001303 if (!ArgLive) {
1304 if (FPR_remaining > 0) {
1305 --FPR_remaining;
1306 ++FPR_idx;
1307 }
1308 break;
1309 }
Chris Lattner915fb302005-08-30 00:19:00 +00001310 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +00001311 unsigned VReg;
1312 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +00001313 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +00001314 else
Nate Begeman1d9d7422005-10-18 00:28:58 +00001315 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001316 MF.addLiveIn(FPR[FPR_idx], VReg);
1317 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +00001318 --FPR_remaining;
1319 ++FPR_idx;
1320 } else {
1321 needsLoad = true;
1322 }
1323 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001324 }
1325
1326 // We need to load the argument to a virtual register if we determined above
1327 // that we ran out of physical registers of the appropriate type
1328 if (needsLoad) {
1329 unsigned SubregOffset = 0;
1330 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
1331 if (ObjectVT == MVT::i16) SubregOffset = 2;
1332 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1333 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1334 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
1335 DAG.getConstant(SubregOffset, MVT::i32));
1336 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
1337 DAG.getSrcValue(NULL));
1338 }
1339
1340 // Every 4 bytes of argument space consumes one of the GPRs available for
1341 // argument passing.
1342 if (GPR_remaining > 0) {
1343 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
1344 GPR_remaining -= delta;
1345 GPR_idx += delta;
1346 }
1347 ArgOffset += ObjSize;
1348 if (newroot.Val)
1349 DAG.setRoot(newroot.getValue(1));
1350
1351 ArgValues.push_back(argt);
1352 }
1353
1354 // If the function takes variable number of arguments, make a frame index for
1355 // the start of the first vararg value... for expansion of llvm.va_start.
1356 if (F.isVarArg()) {
1357 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1358 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1359 // If this function is vararg, store any remaining integer argument regs
1360 // to their spots on the stack so that they may be loaded by deferencing the
1361 // result of va_next.
1362 std::vector<SDOperand> MemOps;
1363 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001364 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001365 MF.addLiveIn(GPR[GPR_idx], VReg);
1366 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001367 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1368 Val, FIN, DAG.getSrcValue(NULL));
1369 MemOps.push_back(Store);
1370 // Increment the address by four for the next argument to store
1371 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1372 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1373 }
Chris Lattner80720a92005-11-30 20:40:54 +00001374 if (!MemOps.empty()) {
1375 MemOps.push_back(DAG.getRoot());
1376 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1377 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001378 }
1379
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001380 return ArgValues;
1381}
1382
1383std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001384PPCTargetLowering::LowerCallTo(SDOperand Chain,
1385 const Type *RetTy, bool isVarArg,
1386 unsigned CallingConv, bool isTailCall,
1387 SDOperand Callee, ArgListTy &Args,
1388 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001389 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001390 // SelectExpr to use to put the arguments in the appropriate registers.
1391 std::vector<SDOperand> args_to_use;
1392
1393 // Count how many bytes are to be pushed on the stack, including the linkage
1394 // area, and parameter passing area.
1395 unsigned NumBytes = 24;
1396
1397 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001398 Chain = DAG.getCALLSEQ_START(Chain,
1399 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001400 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001401 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001402 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001403 default: assert(0 && "Unknown value type!");
1404 case MVT::i1:
1405 case MVT::i8:
1406 case MVT::i16:
1407 case MVT::i32:
1408 case MVT::f32:
1409 NumBytes += 4;
1410 break;
1411 case MVT::i64:
1412 case MVT::f64:
1413 NumBytes += 8;
1414 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001415 }
Chris Lattner915fb302005-08-30 00:19:00 +00001416 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001417
Chris Lattner915fb302005-08-30 00:19:00 +00001418 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1419 // plus 32 bytes of argument space in case any called code gets funky on us.
1420 // (Required by ABI to support var arg)
1421 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001422
1423 // Adjust the stack pointer for the new arguments...
1424 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001425 Chain = DAG.getCALLSEQ_START(Chain,
1426 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001427
1428 // Set up a copy of the stack pointer for use loading and storing any
1429 // arguments that may not fit in the registers available for argument
1430 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001431 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001432
1433 // Figure out which arguments are going to go in registers, and which in
1434 // memory. Also, if this is a vararg function, floating point operations
1435 // must be stored to our stack, and loaded into integer regs as well, if
1436 // any integer regs are available for argument passing.
1437 unsigned ArgOffset = 24;
1438 unsigned GPR_remaining = 8;
1439 unsigned FPR_remaining = 13;
1440
1441 std::vector<SDOperand> MemOps;
1442 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1443 // PtrOff will be used to store the current argument to the stack if a
1444 // register cannot be found for it.
1445 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1446 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1447 MVT::ValueType ArgVT = getValueType(Args[i].second);
1448
1449 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001450 default: assert(0 && "Unexpected ValueType for argument!");
1451 case MVT::i1:
1452 case MVT::i8:
1453 case MVT::i16:
1454 // Promote the integer to 32 bits. If the input type is signed use a
1455 // sign extend, otherwise use a zero extend.
1456 if (Args[i].second->isSigned())
1457 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1458 else
1459 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1460 // FALL THROUGH
1461 case MVT::i32:
1462 if (GPR_remaining > 0) {
1463 args_to_use.push_back(Args[i].first);
1464 --GPR_remaining;
1465 } else {
1466 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1467 Args[i].first, PtrOff,
1468 DAG.getSrcValue(NULL)));
1469 }
1470 ArgOffset += 4;
1471 break;
1472 case MVT::i64:
1473 // If we have one free GPR left, we can place the upper half of the i64
1474 // in it, and store the other half to the stack. If we have two or more
1475 // free GPRs, then we can pass both halves of the i64 in registers.
1476 if (GPR_remaining > 0) {
1477 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1478 Args[i].first, DAG.getConstant(1, MVT::i32));
1479 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1480 Args[i].first, DAG.getConstant(0, MVT::i32));
1481 args_to_use.push_back(Hi);
1482 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001483 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001484 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001485 --GPR_remaining;
1486 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001487 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1488 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001489 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001490 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001491 }
Chris Lattner915fb302005-08-30 00:19:00 +00001492 } else {
1493 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1494 Args[i].first, PtrOff,
1495 DAG.getSrcValue(NULL)));
1496 }
1497 ArgOffset += 8;
1498 break;
1499 case MVT::f32:
1500 case MVT::f64:
1501 if (FPR_remaining > 0) {
1502 args_to_use.push_back(Args[i].first);
1503 --FPR_remaining;
1504 if (isVarArg) {
1505 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1506 Args[i].first, PtrOff,
1507 DAG.getSrcValue(NULL));
1508 MemOps.push_back(Store);
1509 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001510 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001511 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1512 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001513 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001514 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001515 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001516 }
1517 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001518 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1519 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001520 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1521 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001522 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001523 args_to_use.push_back(Load);
1524 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001525 }
1526 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001527 // If we have any FPRs remaining, we may also have GPRs remaining.
1528 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1529 // GPRs.
1530 if (GPR_remaining > 0) {
1531 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1532 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001533 }
Chris Lattner915fb302005-08-30 00:19:00 +00001534 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1535 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1536 --GPR_remaining;
1537 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001538 }
Chris Lattner915fb302005-08-30 00:19:00 +00001539 } else {
1540 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1541 Args[i].first, PtrOff,
1542 DAG.getSrcValue(NULL)));
1543 }
1544 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1545 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001546 }
1547 }
1548 if (!MemOps.empty())
1549 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1550 }
1551
1552 std::vector<MVT::ValueType> RetVals;
1553 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001554 MVT::ValueType ActualRetTyVT = RetTyVT;
1555 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1556 ActualRetTyVT = MVT::i32; // Promote result to i32.
1557
Chris Lattnere00ebf02006-01-28 07:33:03 +00001558 if (RetTyVT == MVT::i64) {
1559 RetVals.push_back(MVT::i32);
1560 RetVals.push_back(MVT::i32);
1561 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001562 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001563 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001564 RetVals.push_back(MVT::Other);
1565
Chris Lattner2823b3e2005-11-17 05:56:14 +00001566 // If the callee is a GlobalAddress node (quite common, every direct call is)
1567 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1568 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1569 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1570
Chris Lattner281b55e2006-01-27 23:34:02 +00001571 std::vector<SDOperand> Ops;
1572 Ops.push_back(Chain);
1573 Ops.push_back(Callee);
1574 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1575 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001576 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001577 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1578 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001579 SDOperand RetVal = TheCall;
1580
1581 // If the result is a small value, add a note so that we keep track of the
1582 // information about whether it is sign or zero extended.
1583 if (RetTyVT != ActualRetTyVT) {
1584 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1585 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1586 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001587 } else if (RetTyVT == MVT::i64) {
1588 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001589 }
1590
1591 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001592}
1593
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001594MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001595PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1596 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001597 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001598 MI->getOpcode() == PPC::SELECT_CC_F4 ||
Chris Lattner710ff322006-04-08 22:45:08 +00001599 MI->getOpcode() == PPC::SELECT_CC_F8 ||
1600 MI->getOpcode() == PPC::SELECT_CC_VRRC) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001601 "Unexpected instr type to insert");
1602
1603 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1604 // control-flow pattern. The incoming instruction knows the destination vreg
1605 // to set, the condition code register to branch on, the true/false values to
1606 // select between, and a branch opcode to use.
1607 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1608 ilist<MachineBasicBlock>::iterator It = BB;
1609 ++It;
1610
1611 // thisMBB:
1612 // ...
1613 // TrueVal = ...
1614 // cmpTY ccX, r1, r2
1615 // bCC copy1MBB
1616 // fallthrough --> copy0MBB
1617 MachineBasicBlock *thisMBB = BB;
1618 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1619 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1620 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1621 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1622 MachineFunction *F = BB->getParent();
1623 F->getBasicBlockList().insert(It, copy0MBB);
1624 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001625 // Update machine-CFG edges by first adding all successors of the current
1626 // block to the new block which will contain the Phi node for the select.
1627 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1628 e = BB->succ_end(); i != e; ++i)
1629 sinkMBB->addSuccessor(*i);
1630 // Next, remove all successors of the current block, and add the true
1631 // and fallthrough blocks as its successors.
1632 while(!BB->succ_empty())
1633 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001634 BB->addSuccessor(copy0MBB);
1635 BB->addSuccessor(sinkMBB);
1636
1637 // copy0MBB:
1638 // %FalseValue = ...
1639 // # fallthrough to sinkMBB
1640 BB = copy0MBB;
1641
1642 // Update machine-CFG edges
1643 BB->addSuccessor(sinkMBB);
1644
1645 // sinkMBB:
1646 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1647 // ...
1648 BB = sinkMBB;
1649 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1650 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1651 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1652
1653 delete MI; // The pseudo instruction is gone now.
1654 return BB;
1655}
1656
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001657SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1658 DAGCombinerInfo &DCI) const {
1659 TargetMachine &TM = getTargetMachine();
1660 SelectionDAG &DAG = DCI.DAG;
1661 switch (N->getOpcode()) {
1662 default: break;
1663 case ISD::SINT_TO_FP:
1664 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001665 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1666 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1667 // We allow the src/dst to be either f32/f64, but the intermediate
1668 // type must be i64.
1669 if (N->getOperand(0).getValueType() == MVT::i64) {
1670 SDOperand Val = N->getOperand(0).getOperand(0);
1671 if (Val.getValueType() == MVT::f32) {
1672 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1673 DCI.AddToWorklist(Val.Val);
1674 }
1675
1676 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001677 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001678 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001679 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001680 if (N->getValueType(0) == MVT::f32) {
1681 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1682 DCI.AddToWorklist(Val.Val);
1683 }
1684 return Val;
1685 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1686 // If the intermediate type is i32, we can avoid the load/store here
1687 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001688 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001689 }
1690 }
1691 break;
Chris Lattner51269842006-03-01 05:50:56 +00001692 case ISD::STORE:
1693 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1694 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1695 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1696 N->getOperand(1).getValueType() == MVT::i32) {
1697 SDOperand Val = N->getOperand(1).getOperand(0);
1698 if (Val.getValueType() == MVT::f32) {
1699 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1700 DCI.AddToWorklist(Val.Val);
1701 }
1702 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1703 DCI.AddToWorklist(Val.Val);
1704
1705 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1706 N->getOperand(2), N->getOperand(3));
1707 DCI.AddToWorklist(Val.Val);
1708 return Val;
1709 }
1710 break;
Chris Lattner4468c222006-03-31 06:02:07 +00001711 case PPCISD::VCMP: {
1712 // If a VCMPo node already exists with exactly the same operands as this
1713 // node, use its result instead of this node (VCMPo computes both a CR6 and
1714 // a normal output).
1715 //
1716 if (!N->getOperand(0).hasOneUse() &&
1717 !N->getOperand(1).hasOneUse() &&
1718 !N->getOperand(2).hasOneUse()) {
1719
1720 // Scan all of the users of the LHS, looking for VCMPo's that match.
1721 SDNode *VCMPoNode = 0;
1722
1723 SDNode *LHSN = N->getOperand(0).Val;
1724 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
1725 UI != E; ++UI)
1726 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
1727 (*UI)->getOperand(1) == N->getOperand(1) &&
1728 (*UI)->getOperand(2) == N->getOperand(2) &&
1729 (*UI)->getOperand(0) == N->getOperand(0)) {
1730 VCMPoNode = *UI;
1731 break;
1732 }
1733
1734 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00001735 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00001736 return SDOperand(VCMPoNode, 0);
1737 }
1738 break;
1739 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001740 }
1741
1742 return SDOperand();
1743}
1744
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001745void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1746 uint64_t Mask,
1747 uint64_t &KnownZero,
1748 uint64_t &KnownOne,
1749 unsigned Depth) const {
1750 KnownZero = 0;
1751 KnownOne = 0;
1752 switch (Op.getOpcode()) {
1753 default: break;
1754 case ISD::INTRINSIC_WO_CHAIN: {
1755 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
1756 default: break;
1757 case Intrinsic::ppc_altivec_vcmpbfp_p:
1758 case Intrinsic::ppc_altivec_vcmpeqfp_p:
1759 case Intrinsic::ppc_altivec_vcmpequb_p:
1760 case Intrinsic::ppc_altivec_vcmpequh_p:
1761 case Intrinsic::ppc_altivec_vcmpequw_p:
1762 case Intrinsic::ppc_altivec_vcmpgefp_p:
1763 case Intrinsic::ppc_altivec_vcmpgtfp_p:
1764 case Intrinsic::ppc_altivec_vcmpgtsb_p:
1765 case Intrinsic::ppc_altivec_vcmpgtsh_p:
1766 case Intrinsic::ppc_altivec_vcmpgtsw_p:
1767 case Intrinsic::ppc_altivec_vcmpgtub_p:
1768 case Intrinsic::ppc_altivec_vcmpgtuh_p:
1769 case Intrinsic::ppc_altivec_vcmpgtuw_p:
1770 KnownZero = ~1U; // All bits but the low one are known to be zero.
1771 break;
1772 }
1773 }
1774 }
1775}
1776
1777
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001778/// getConstraintType - Given a constraint letter, return the type of
1779/// constraint it is for this target.
1780PPCTargetLowering::ConstraintType
1781PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1782 switch (ConstraintLetter) {
1783 default: break;
1784 case 'b':
1785 case 'r':
1786 case 'f':
1787 case 'v':
1788 case 'y':
1789 return C_RegisterClass;
1790 }
1791 return TargetLowering::getConstraintType(ConstraintLetter);
1792}
1793
1794
Chris Lattnerddc787d2006-01-31 19:20:21 +00001795std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001796getRegClassForInlineAsmConstraint(const std::string &Constraint,
1797 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001798 if (Constraint.size() == 1) {
1799 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1800 default: break; // Unknown constriant letter
1801 case 'b':
1802 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1803 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1804 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1805 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1806 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1807 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1808 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1809 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1810 0);
1811 case 'r':
1812 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1813 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1814 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1815 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1816 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1817 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1818 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1819 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1820 0);
1821 case 'f':
1822 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1823 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1824 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1825 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1826 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1827 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1828 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1829 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1830 0);
1831 case 'v':
1832 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1833 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1834 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1835 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1836 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1837 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1838 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1839 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1840 0);
1841 case 'y':
1842 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1843 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1844 0);
1845 }
1846 }
1847
Chris Lattner1efa40f2006-02-22 00:56:39 +00001848 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001849}
Chris Lattner763317d2006-02-07 00:47:13 +00001850
1851// isOperandValidForConstraint
1852bool PPCTargetLowering::
1853isOperandValidForConstraint(SDOperand Op, char Letter) {
1854 switch (Letter) {
1855 default: break;
1856 case 'I':
1857 case 'J':
1858 case 'K':
1859 case 'L':
1860 case 'M':
1861 case 'N':
1862 case 'O':
1863 case 'P': {
1864 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1865 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1866 switch (Letter) {
1867 default: assert(0 && "Unknown constraint letter!");
1868 case 'I': // "I" is a signed 16-bit constant.
1869 return (short)Value == (int)Value;
1870 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1871 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1872 return (short)Value == 0;
1873 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1874 return (Value >> 16) == 0;
1875 case 'M': // "M" is a constant that is greater than 31.
1876 return Value > 31;
1877 case 'N': // "N" is a positive constant that is an exact power of two.
1878 return (int)Value > 0 && isPowerOf2_32(Value);
1879 case 'O': // "O" is the constant zero.
1880 return Value == 0;
1881 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1882 return (short)-Value == (int)-Value;
1883 }
1884 break;
1885 }
1886 }
1887
1888 // Handle standard constraint letters.
1889 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1890}
Evan Chengc4c62572006-03-13 23:20:37 +00001891
1892/// isLegalAddressImmediate - Return true if the integer value can be used
1893/// as the offset of the target addressing mode.
1894bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1895 // PPC allows a sign-extended 16-bit immediate field.
1896 return (V > -(1 << 16) && V < (1 << 16)-1);
1897}