blob: 24939dd05fc8c2e413f955dcb464ec0d7f437144 [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 Lattnerf1b47082006-04-14 05:19:18 +0000590// If this is a case we can't handle, return null and let the default
591// expansion code take care of it. If we CAN select this case, and if it
592// selects to a single instruction, return Op. Otherwise, if we can codegen
593// this case more efficiently than a constant pool load, lower it to the
594// sequence of ops that should be used.
595static SDOperand LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
596 // If this is a vector of constants or undefs, get the bits. A bit in
597 // UndefBits is set if the corresponding element of the vector is an
598 // ISD::UNDEF value. For undefs, the corresponding VectorBits values are
599 // zero.
600 uint64_t VectorBits[2];
601 uint64_t UndefBits[2];
602 if (GetConstantBuildVectorBits(Op.Val, VectorBits, UndefBits))
603 return SDOperand(); // Not a constant vector.
604
605 // See if this is all zeros.
606 if ((VectorBits[0] | VectorBits[1]) == 0) {
607 // Canonicalize all zero vectors to be v4i32.
608 if (Op.getValueType() != MVT::v4i32) {
609 SDOperand Z = DAG.getConstant(0, MVT::i32);
610 Z = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Z, Z, Z, Z);
611 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Z);
612 }
613 return Op;
614 }
615
616 // Check to see if this is something we can use VSPLTI* to form.
617 MVT::ValueType CanonicalVT = MVT::Other;
618 SDNode *CST = 0;
619
620 if ((CST = PPC::get_VSPLTI_elt(Op.Val, 4, DAG).Val)) // vspltisw
621 CanonicalVT = MVT::v4i32;
622 else if ((CST = PPC::get_VSPLTI_elt(Op.Val, 2, DAG).Val)) // vspltish
623 CanonicalVT = MVT::v8i16;
624 else if ((CST = PPC::get_VSPLTI_elt(Op.Val, 1, DAG).Val)) // vspltisb
625 CanonicalVT = MVT::v16i8;
626
627 // If this matches one of the vsplti* patterns, force it to the canonical
628 // type for the pattern.
629 if (CST) {
630 if (Op.getValueType() != CanonicalVT) {
631 // Convert the splatted element to the right element type.
632 SDOperand Elt = DAG.getNode(ISD::TRUNCATE,
633 MVT::getVectorBaseType(CanonicalVT),
634 SDOperand(CST, 0));
635 std::vector<SDOperand> Ops(MVT::getVectorNumElements(CanonicalVT), Elt);
636 SDOperand Res = DAG.getNode(ISD::BUILD_VECTOR, CanonicalVT, Ops);
637 Op = DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
638 }
639 return Op;
640 }
641
642 // If this is some other splat of 4-byte elements, see if we can handle it
643 // in another way.
644 // FIXME: Make this more undef happy and work with other widths (1,2 bytes).
645 if (VectorBits[0] == VectorBits[1] &&
646 unsigned(VectorBits[0]) == unsigned(VectorBits[0] >> 32)) {
647 unsigned Bits = unsigned(VectorBits[0]);
648
649 // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is
650 // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). These are important
651 // for fneg/fabs.
652 if (Bits == 0x80000000 || Bits == 0x7FFFFFFF) {
653 // Make -1 and vspltisw -1:
654 SDOperand OnesI = DAG.getConstant(~0U, MVT::i32);
655 SDOperand OnesV = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32,
656 OnesI, OnesI, OnesI, OnesI);
657
658 // Make the VSLW intrinsic, computing 0x8000_0000.
659 SDOperand Res
660 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, MVT::v4i32,
661 DAG.getConstant(Intrinsic::ppc_altivec_vslw, MVT::i32),
662 OnesV, OnesV);
663
664 // If this is 0x7FFF_FFFF, xor by OnesV to invert it.
665 if (Bits == 0x7FFFFFFF)
666 Res = DAG.getNode(ISD::XOR, MVT::v4i32, Res, OnesV);
667
668 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Res);
669 }
670 }
671
672 return SDOperand();
673}
674
675/// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this
676/// is a shuffle we can handle in a single instruction, return it. Otherwise,
677/// return the code it can be lowered into. Worst case, it can always be
678/// lowered into a vperm.
679static SDOperand LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
680 SDOperand V1 = Op.getOperand(0);
681 SDOperand V2 = Op.getOperand(1);
682 SDOperand PermMask = Op.getOperand(2);
683
684 // Cases that are handled by instructions that take permute immediates
685 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
686 // selected by the instruction selector.
687 if (V2.getOpcode() == ISD::UNDEF) {
688 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
689 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
690 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
691 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
692 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
693 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
694 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
695 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
696 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
697 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
698 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
699 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
700 return Op;
701 }
702 }
703
704 // Altivec has a variety of "shuffle immediates" that take two vector inputs
705 // and produce a fixed permutation. If any of these match, do not lower to
706 // VPERM.
707 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
708 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
709 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
710 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
711 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
712 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
713 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
714 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
715 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
716 return Op;
717
718 // TODO: Handle more cases, and also handle cases that are cheaper to do as
719 // multiple such instructions than as a constant pool load/vperm pair.
720
721 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
722 // vector that will get spilled to the constant pool.
723 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
724
725 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
726 // that it is in input element units, not in bytes. Convert now.
727 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
728 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
729
730 std::vector<SDOperand> ResultMask;
731 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
732 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
733
734 for (unsigned j = 0; j != BytesPerElement; ++j)
735 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
736 MVT::i8));
737 }
738
739 SDOperand VPermMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
740 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
741}
742
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000743/// LowerOperation - Provide custom lowering hooks for some operations.
744///
Nate Begeman21e463b2005-10-16 05:39:50 +0000745SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000746 switch (Op.getOpcode()) {
747 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf1b47082006-04-14 05:19:18 +0000748 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
749 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Chris Lattnerf7605322005-08-31 21:09:52 +0000750 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000751 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000752 SDOperand Src = Op.getOperand(0);
753 if (Src.getValueType() == MVT::f32)
754 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
755
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000756 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000757 switch (Op.getValueType()) {
758 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
759 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000760 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000761 break;
762 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000763 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000764 break;
765 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000766
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000767 // Convert the FP value to an int value through memory.
768 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
769 if (Op.getValueType() == MVT::i32)
770 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
771 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000772 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000773 case ISD::SINT_TO_FP:
774 if (Op.getOperand(0).getValueType() == MVT::i64) {
775 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
776 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
777 if (Op.getValueType() == MVT::f32)
778 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
779 return FP;
780 } else {
781 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
782 "Unhandled SINT_TO_FP type in custom expander!");
783 // Since we only generate this in 64-bit mode, we can take advantage of
784 // 64-bit registers. In particular, sign extend the input value into the
785 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
786 // then lfd it and fcfid it.
787 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
788 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
789 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
790
791 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
792 Op.getOperand(0));
793
794 // STD the extended value into the stack slot.
795 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
796 DAG.getEntryNode(), Ext64, FIdx,
797 DAG.getSrcValue(NULL));
798 // Load the value as a double.
799 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
800
801 // FCFID it and return it.
802 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
803 if (Op.getValueType() == MVT::f32)
804 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
805 return FP;
806 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000807 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000808
Chris Lattnerf7605322005-08-31 21:09:52 +0000809 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000810 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000811 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
812 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
813 break;
814
815 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
816
817 // Cannot handle SETEQ/SETNE.
818 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
819
820 MVT::ValueType ResVT = Op.getValueType();
821 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
822 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
823 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000824
Chris Lattnerf7605322005-08-31 21:09:52 +0000825 // If the RHS of the comparison is a 0.0, we don't need to do the
826 // subtraction at all.
827 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000828 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000829 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000830 case ISD::SETULT:
831 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000832 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000833 case ISD::SETUGE:
834 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000835 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
836 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000837 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000838 case ISD::SETUGT:
839 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000840 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000841 case ISD::SETULE:
842 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000843 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
844 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000845 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000846 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000847 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000848
Chris Lattnereb255f22005-10-25 20:54:57 +0000849 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000850 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000851 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000852 case ISD::SETULT:
853 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000854 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
855 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
856 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
857 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000858 case ISD::SETUGE:
859 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000860 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
861 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
862 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
863 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000864 case ISD::SETUGT:
865 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000866 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
867 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
868 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
869 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000870 case ISD::SETULE:
871 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000872 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
873 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
874 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
875 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000876 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000877 break;
878 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000879 case ISD::SHL: {
880 assert(Op.getValueType() == MVT::i64 &&
881 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
882 // The generic code does a fine job expanding shift by a constant.
883 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
884
885 // Otherwise, expand into a bunch of logical ops. Note that these ops
886 // depend on the PPC behavior for oversized shift amounts.
887 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
888 DAG.getConstant(0, MVT::i32));
889 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
890 DAG.getConstant(1, MVT::i32));
891 SDOperand Amt = Op.getOperand(1);
892
893 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
894 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000895 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
896 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000897 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
898 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
899 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000900 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000901 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000902 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000903 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
904 }
905 case ISD::SRL: {
906 assert(Op.getValueType() == MVT::i64 &&
907 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
908 // The generic code does a fine job expanding shift by a constant.
909 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
910
911 // Otherwise, expand into a bunch of logical ops. Note that these ops
912 // depend on the PPC behavior for oversized shift amounts.
913 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
914 DAG.getConstant(0, MVT::i32));
915 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
916 DAG.getConstant(1, MVT::i32));
917 SDOperand Amt = Op.getOperand(1);
918
919 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
920 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000921 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
922 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000923 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
924 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
925 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000926 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000927 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000928 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000929 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
930 }
931 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000932 assert(Op.getValueType() == MVT::i64 &&
933 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
934 // The generic code does a fine job expanding shift by a constant.
935 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
936
937 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
938 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
939 DAG.getConstant(0, MVT::i32));
940 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
941 DAG.getConstant(1, MVT::i32));
942 SDOperand Amt = Op.getOperand(1);
943
944 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
945 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000946 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
947 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000948 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
949 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
950 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000951 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
952 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000953 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
954 Tmp4, Tmp6, ISD::SETLE);
955 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000956 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000957 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000958 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
959 Constant *C = CP->get();
960 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000961 SDOperand Zero = DAG.getConstant(0, MVT::i32);
962
Chris Lattnera39d7982006-04-13 17:10:48 +0000963 // If this is a non-darwin platform, we don't support non-static relo models
964 // yet.
965 if (getTargetMachine().getRelocationModel() == Reloc::Static ||
966 !getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000967 // Generate non-pic code that has direct accesses to the constant pool.
968 // The address of the global is just (hi(&g)+lo(&g)).
969 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
970 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
971 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
972 }
973
Nate Begeman28a6b022005-12-10 02:36:00 +0000974 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000975 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000976 // With PIC, the first instruction is actually "GR+hi(&G)".
977 Hi = DAG.getNode(ISD::ADD, MVT::i32,
978 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
979 }
980
981 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
982 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
983 return Lo;
984 }
Chris Lattner860e8862005-11-17 07:30:41 +0000985 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000986 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
987 GlobalValue *GV = GSDN->getGlobal();
988 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000989 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000990
Chris Lattnera39d7982006-04-13 17:10:48 +0000991 // If this is a non-darwin platform, we don't support non-static relo models
992 // yet.
993 if (getTargetMachine().getRelocationModel() == Reloc::Static ||
994 !getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000995 // Generate non-pic code that has direct accesses to globals.
996 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000997 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
998 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
999 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
1000 }
Chris Lattner860e8862005-11-17 07:30:41 +00001001
1002 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +00001003 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +00001004 // With PIC, the first instruction is actually "GR+hi(&G)".
1005 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +00001006 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +00001007 }
1008
1009 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
1010 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
1011
Chris Lattner37dd6f12006-01-29 20:49:17 +00001012 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
1013 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +00001014 return Lo;
1015
1016 // If the global is weak or external, we have to go through the lazy
1017 // resolution stub.
1018 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
1019 }
Nate Begeman44775902006-01-31 08:17:29 +00001020 case ISD::SETCC: {
1021 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +00001022
1023 // If we're comparing for equality to zero, expose the fact that this is
1024 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
1025 // fold the new nodes.
1026 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1027 if (C->isNullValue() && CC == ISD::SETEQ) {
1028 MVT::ValueType VT = Op.getOperand(0).getValueType();
1029 SDOperand Zext = Op.getOperand(0);
1030 if (VT < MVT::i32) {
1031 VT = MVT::i32;
1032 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
1033 }
1034 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
1035 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
1036 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
1037 DAG.getConstant(Log2b, getShiftAmountTy()));
1038 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
1039 }
1040 // Leave comparisons against 0 and -1 alone for now, since they're usually
1041 // optimized. FIXME: revisit this when we can custom lower all setcc
1042 // optimizations.
1043 if (C->isAllOnesValue() || C->isNullValue())
1044 break;
1045 }
1046
1047 // If we have an integer seteq/setne, turn it into a compare against zero
1048 // by subtracting the rhs from the lhs, which is faster than setting a
1049 // condition register, reading it back out, and masking the correct bit.
1050 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
1051 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
1052 MVT::ValueType VT = Op.getValueType();
1053 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
1054 Op.getOperand(1));
1055 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
1056 }
Nate Begeman44775902006-01-31 08:17:29 +00001057 break;
1058 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001059 case ISD::VASTART: {
1060 // vastart just stores the address of the VarArgsFrameIndex slot into the
1061 // memory location argument.
1062 // FIXME: Replace MVT::i32 with PointerTy
1063 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1064 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1065 Op.getOperand(1), Op.getOperand(2));
1066 }
Nate Begemanee625572006-01-27 21:09:22 +00001067 case ISD::RET: {
1068 SDOperand Copy;
1069
1070 switch(Op.getNumOperands()) {
1071 default:
1072 assert(0 && "Do not know how to return this many arguments!");
1073 abort();
1074 case 1:
1075 return SDOperand(); // ret void is legal
1076 case 2: {
1077 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerbee98362006-04-11 01:38:39 +00001078 unsigned ArgReg;
1079 if (MVT::isVector(ArgVT))
1080 ArgReg = PPC::V2;
1081 else if (MVT::isInteger(ArgVT))
1082 ArgReg = PPC::R3;
1083 else {
1084 assert(MVT::isFloatingPoint(ArgVT));
1085 ArgReg = PPC::F1;
1086 }
1087
Nate Begemanee625572006-01-27 21:09:22 +00001088 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
1089 SDOperand());
Chris Lattner06c24352006-04-11 01:21:43 +00001090
1091 // If we haven't noted the R3/F1 are live out, do so now.
1092 if (DAG.getMachineFunction().liveout_empty())
1093 DAG.getMachineFunction().addLiveOut(ArgReg);
Nate Begemanee625572006-01-27 21:09:22 +00001094 break;
1095 }
1096 case 3:
1097 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
1098 SDOperand());
1099 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
Chris Lattner06c24352006-04-11 01:21:43 +00001100 // If we haven't noted the R3+R4 are live out, do so now.
1101 if (DAG.getMachineFunction().liveout_empty()) {
1102 DAG.getMachineFunction().addLiveOut(PPC::R3);
1103 DAG.getMachineFunction().addLiveOut(PPC::R4);
1104 }
Nate Begemanee625572006-01-27 21:09:22 +00001105 break;
1106 }
1107 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
1108 }
Chris Lattnerb2177b92006-03-19 06:55:52 +00001109 case ISD::SCALAR_TO_VECTOR: {
1110 // Create a stack slot that is 16-byte aligned.
1111 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
1112 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
1113 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
1114
1115 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +00001116 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1117 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner7f20b132006-03-28 01:43:22 +00001118 // Load it out.
1119 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
Chris Lattnerb2177b92006-03-19 06:55:52 +00001120 }
Chris Lattner48b61a72006-03-28 00:40:33 +00001121 case ISD::INTRINSIC_WO_CHAIN: {
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001122 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
Chris Lattner6d92cad2006-03-26 10:06:40 +00001123
1124 // If this is a lowered altivec predicate compare, CompareOpc is set to the
1125 // opcode number of the comparison.
1126 int CompareOpc = -1;
Chris Lattnera17b1552006-03-31 05:13:27 +00001127 bool isDot = false;
Chris Lattner6d92cad2006-03-26 10:06:40 +00001128 switch (IntNo) {
1129 default: return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattnera17b1552006-03-31 05:13:27 +00001130 // Comparison predicates.
1131 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
1132 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
1133 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
1134 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
1135 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
1136 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
1137 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
1138 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
1139 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
1140 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
1141 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
1142 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
1143 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
1144
1145 // Normal Comparisons.
1146 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
1147 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
1148 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
1149 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
1150 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
1151 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
1152 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
1153 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
1154 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
1155 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
1156 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
1157 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
1158 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Chris Lattner6d92cad2006-03-26 10:06:40 +00001159 }
1160
1161 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
1162
Chris Lattnera17b1552006-03-31 05:13:27 +00001163 // If this is a non-dot comparison, make the VCMP node.
Chris Lattner90217992006-04-06 23:12:19 +00001164 if (!isDot) {
1165 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
1166 Op.getOperand(1), Op.getOperand(2),
1167 DAG.getConstant(CompareOpc, MVT::i32));
1168 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
1169 }
Chris Lattnera17b1552006-03-31 05:13:27 +00001170
Chris Lattner6d92cad2006-03-26 10:06:40 +00001171 // Create the PPCISD altivec 'dot' comparison node.
1172 std::vector<SDOperand> Ops;
1173 std::vector<MVT::ValueType> VTs;
1174 Ops.push_back(Op.getOperand(2)); // LHS
1175 Ops.push_back(Op.getOperand(3)); // RHS
1176 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
1177 VTs.push_back(Op.getOperand(2).getValueType());
1178 VTs.push_back(MVT::Flag);
1179 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
1180
1181 // Now that we have the comparison, emit a copy from the CR to a GPR.
1182 // This is flagged to the above dot comparison.
1183 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
1184 DAG.getRegister(PPC::CR6, MVT::i32),
1185 CompNode.getValue(1));
1186
1187 // Unpack the result based on how the target uses it.
1188 unsigned BitNo; // Bit # of CR6.
1189 bool InvertBit; // Invert result?
1190 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1191 default: // Can't happen, don't crash on invalid number though.
1192 case 0: // Return the value of the EQ bit of CR6.
1193 BitNo = 0; InvertBit = false;
1194 break;
1195 case 1: // Return the inverted value of the EQ bit of CR6.
1196 BitNo = 0; InvertBit = true;
1197 break;
1198 case 2: // Return the value of the LT bit of CR6.
1199 BitNo = 2; InvertBit = false;
1200 break;
1201 case 3: // Return the inverted value of the LT bit of CR6.
1202 BitNo = 2; InvertBit = true;
1203 break;
1204 }
1205
1206 // Shift the bit into the low position.
1207 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
1208 DAG.getConstant(8-(3-BitNo), MVT::i32));
1209 // Isolate the bit.
1210 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
1211 DAG.getConstant(1, MVT::i32));
1212
1213 // If we are supposed to, toggle the bit.
1214 if (InvertBit)
1215 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
1216 DAG.getConstant(1, MVT::i32));
1217 return Flags;
1218 }
Chris Lattnerbc11c342005-08-31 20:23:54 +00001219 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001220 return SDOperand();
1221}
1222
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001223std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001224PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001225 //
1226 // add beautiful description of PPC stack frame format, or at least some docs
1227 //
1228 MachineFunction &MF = DAG.getMachineFunction();
1229 MachineFrameInfo *MFI = MF.getFrameInfo();
1230 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +00001231 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001232 std::vector<SDOperand> ArgValues;
1233
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001234 unsigned ArgOffset = 24;
1235 unsigned GPR_remaining = 8;
1236 unsigned FPR_remaining = 13;
1237 unsigned GPR_idx = 0, FPR_idx = 0;
1238 static const unsigned GPR[] = {
1239 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1240 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1241 };
1242 static const unsigned FPR[] = {
1243 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1244 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1245 };
1246
1247 // Add DAG nodes to load the arguments... On entry to a function on PPC,
1248 // the arguments start at offset 24, although they are likely to be passed
1249 // in registers.
1250 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
1251 SDOperand newroot, argt;
1252 unsigned ObjSize;
1253 bool needsLoad = false;
1254 bool ArgLive = !I->use_empty();
1255 MVT::ValueType ObjectVT = getValueType(I->getType());
1256
1257 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001258 default: assert(0 && "Unhandled argument type!");
1259 case MVT::i1:
1260 case MVT::i8:
1261 case MVT::i16:
1262 case MVT::i32:
1263 ObjSize = 4;
1264 if (!ArgLive) break;
1265 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001266 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001267 MF.addLiveIn(GPR[GPR_idx], VReg);
1268 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +00001269 if (ObjectVT != MVT::i32) {
1270 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
1271 : ISD::AssertZext;
1272 argt = DAG.getNode(AssertOp, MVT::i32, argt,
1273 DAG.getValueType(ObjectVT));
1274 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
1275 }
Chris Lattner915fb302005-08-30 00:19:00 +00001276 } else {
1277 needsLoad = true;
1278 }
1279 break;
Chris Lattner80720a92005-11-30 20:40:54 +00001280 case MVT::i64:
1281 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +00001282 if (!ArgLive) break;
1283 if (GPR_remaining > 0) {
1284 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +00001285 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001286 MF.addLiveIn(GPR[GPR_idx], VReg);
1287 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001288 // If we have two or more remaining argument registers, then both halves
1289 // of the i64 can be sourced from there. Otherwise, the lower half will
1290 // have to come off the stack. This can happen when an i64 is preceded
1291 // by 28 bytes of arguments.
1292 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001293 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001294 MF.addLiveIn(GPR[GPR_idx+1], VReg);
1295 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001296 } else {
1297 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
1298 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1299 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
1300 DAG.getSrcValue(NULL));
1301 }
1302 // Build the outgoing arg thingy
1303 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
1304 newroot = argLo;
1305 } else {
1306 needsLoad = true;
1307 }
1308 break;
1309 case MVT::f32:
1310 case MVT::f64:
1311 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +00001312 if (!ArgLive) {
1313 if (FPR_remaining > 0) {
1314 --FPR_remaining;
1315 ++FPR_idx;
1316 }
1317 break;
1318 }
Chris Lattner915fb302005-08-30 00:19:00 +00001319 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +00001320 unsigned VReg;
1321 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +00001322 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +00001323 else
Nate Begeman1d9d7422005-10-18 00:28:58 +00001324 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001325 MF.addLiveIn(FPR[FPR_idx], VReg);
1326 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +00001327 --FPR_remaining;
1328 ++FPR_idx;
1329 } else {
1330 needsLoad = true;
1331 }
1332 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001333 }
1334
1335 // We need to load the argument to a virtual register if we determined above
1336 // that we ran out of physical registers of the appropriate type
1337 if (needsLoad) {
1338 unsigned SubregOffset = 0;
1339 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
1340 if (ObjectVT == MVT::i16) SubregOffset = 2;
1341 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1342 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1343 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
1344 DAG.getConstant(SubregOffset, MVT::i32));
1345 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
1346 DAG.getSrcValue(NULL));
1347 }
1348
1349 // Every 4 bytes of argument space consumes one of the GPRs available for
1350 // argument passing.
1351 if (GPR_remaining > 0) {
1352 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
1353 GPR_remaining -= delta;
1354 GPR_idx += delta;
1355 }
1356 ArgOffset += ObjSize;
1357 if (newroot.Val)
1358 DAG.setRoot(newroot.getValue(1));
1359
1360 ArgValues.push_back(argt);
1361 }
1362
1363 // If the function takes variable number of arguments, make a frame index for
1364 // the start of the first vararg value... for expansion of llvm.va_start.
1365 if (F.isVarArg()) {
1366 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1367 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1368 // If this function is vararg, store any remaining integer argument regs
1369 // to their spots on the stack so that they may be loaded by deferencing the
1370 // result of va_next.
1371 std::vector<SDOperand> MemOps;
1372 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001373 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001374 MF.addLiveIn(GPR[GPR_idx], VReg);
1375 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001376 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1377 Val, FIN, DAG.getSrcValue(NULL));
1378 MemOps.push_back(Store);
1379 // Increment the address by four for the next argument to store
1380 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1381 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1382 }
Chris Lattner80720a92005-11-30 20:40:54 +00001383 if (!MemOps.empty()) {
1384 MemOps.push_back(DAG.getRoot());
1385 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1386 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001387 }
1388
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001389 return ArgValues;
1390}
1391
1392std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001393PPCTargetLowering::LowerCallTo(SDOperand Chain,
1394 const Type *RetTy, bool isVarArg,
1395 unsigned CallingConv, bool isTailCall,
1396 SDOperand Callee, ArgListTy &Args,
1397 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001398 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001399 // SelectExpr to use to put the arguments in the appropriate registers.
1400 std::vector<SDOperand> args_to_use;
1401
1402 // Count how many bytes are to be pushed on the stack, including the linkage
1403 // area, and parameter passing area.
1404 unsigned NumBytes = 24;
1405
1406 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001407 Chain = DAG.getCALLSEQ_START(Chain,
1408 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001409 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001410 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001411 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001412 default: assert(0 && "Unknown value type!");
1413 case MVT::i1:
1414 case MVT::i8:
1415 case MVT::i16:
1416 case MVT::i32:
1417 case MVT::f32:
1418 NumBytes += 4;
1419 break;
1420 case MVT::i64:
1421 case MVT::f64:
1422 NumBytes += 8;
1423 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001424 }
Chris Lattner915fb302005-08-30 00:19:00 +00001425 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001426
Chris Lattner915fb302005-08-30 00:19:00 +00001427 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1428 // plus 32 bytes of argument space in case any called code gets funky on us.
1429 // (Required by ABI to support var arg)
1430 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001431
1432 // Adjust the stack pointer for the new arguments...
1433 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001434 Chain = DAG.getCALLSEQ_START(Chain,
1435 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001436
1437 // Set up a copy of the stack pointer for use loading and storing any
1438 // arguments that may not fit in the registers available for argument
1439 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001440 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001441
1442 // Figure out which arguments are going to go in registers, and which in
1443 // memory. Also, if this is a vararg function, floating point operations
1444 // must be stored to our stack, and loaded into integer regs as well, if
1445 // any integer regs are available for argument passing.
1446 unsigned ArgOffset = 24;
1447 unsigned GPR_remaining = 8;
1448 unsigned FPR_remaining = 13;
1449
1450 std::vector<SDOperand> MemOps;
1451 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1452 // PtrOff will be used to store the current argument to the stack if a
1453 // register cannot be found for it.
1454 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1455 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1456 MVT::ValueType ArgVT = getValueType(Args[i].second);
1457
1458 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001459 default: assert(0 && "Unexpected ValueType for argument!");
1460 case MVT::i1:
1461 case MVT::i8:
1462 case MVT::i16:
1463 // Promote the integer to 32 bits. If the input type is signed use a
1464 // sign extend, otherwise use a zero extend.
1465 if (Args[i].second->isSigned())
1466 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1467 else
1468 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1469 // FALL THROUGH
1470 case MVT::i32:
1471 if (GPR_remaining > 0) {
1472 args_to_use.push_back(Args[i].first);
1473 --GPR_remaining;
1474 } else {
1475 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1476 Args[i].first, PtrOff,
1477 DAG.getSrcValue(NULL)));
1478 }
1479 ArgOffset += 4;
1480 break;
1481 case MVT::i64:
1482 // If we have one free GPR left, we can place the upper half of the i64
1483 // in it, and store the other half to the stack. If we have two or more
1484 // free GPRs, then we can pass both halves of the i64 in registers.
1485 if (GPR_remaining > 0) {
1486 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1487 Args[i].first, DAG.getConstant(1, MVT::i32));
1488 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1489 Args[i].first, DAG.getConstant(0, MVT::i32));
1490 args_to_use.push_back(Hi);
1491 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001492 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001493 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001494 --GPR_remaining;
1495 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001496 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1497 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001498 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001499 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001500 }
Chris Lattner915fb302005-08-30 00:19:00 +00001501 } else {
1502 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1503 Args[i].first, PtrOff,
1504 DAG.getSrcValue(NULL)));
1505 }
1506 ArgOffset += 8;
1507 break;
1508 case MVT::f32:
1509 case MVT::f64:
1510 if (FPR_remaining > 0) {
1511 args_to_use.push_back(Args[i].first);
1512 --FPR_remaining;
1513 if (isVarArg) {
1514 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1515 Args[i].first, PtrOff,
1516 DAG.getSrcValue(NULL));
1517 MemOps.push_back(Store);
1518 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001519 if (GPR_remaining > 0) {
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);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001524 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001525 }
1526 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001527 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1528 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001529 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1530 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001531 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001532 args_to_use.push_back(Load);
1533 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001534 }
1535 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001536 // If we have any FPRs remaining, we may also have GPRs remaining.
1537 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1538 // GPRs.
1539 if (GPR_remaining > 0) {
1540 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1541 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001542 }
Chris Lattner915fb302005-08-30 00:19:00 +00001543 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1544 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1545 --GPR_remaining;
1546 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001547 }
Chris Lattner915fb302005-08-30 00:19:00 +00001548 } else {
1549 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1550 Args[i].first, PtrOff,
1551 DAG.getSrcValue(NULL)));
1552 }
1553 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1554 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001555 }
1556 }
1557 if (!MemOps.empty())
1558 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1559 }
1560
1561 std::vector<MVT::ValueType> RetVals;
1562 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001563 MVT::ValueType ActualRetTyVT = RetTyVT;
1564 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1565 ActualRetTyVT = MVT::i32; // Promote result to i32.
1566
Chris Lattnere00ebf02006-01-28 07:33:03 +00001567 if (RetTyVT == MVT::i64) {
1568 RetVals.push_back(MVT::i32);
1569 RetVals.push_back(MVT::i32);
1570 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001571 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001572 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001573 RetVals.push_back(MVT::Other);
1574
Chris Lattner2823b3e2005-11-17 05:56:14 +00001575 // If the callee is a GlobalAddress node (quite common, every direct call is)
1576 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1577 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1578 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1579
Chris Lattner281b55e2006-01-27 23:34:02 +00001580 std::vector<SDOperand> Ops;
1581 Ops.push_back(Chain);
1582 Ops.push_back(Callee);
1583 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1584 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001585 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001586 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1587 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001588 SDOperand RetVal = TheCall;
1589
1590 // If the result is a small value, add a note so that we keep track of the
1591 // information about whether it is sign or zero extended.
1592 if (RetTyVT != ActualRetTyVT) {
1593 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1594 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1595 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001596 } else if (RetTyVT == MVT::i64) {
1597 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001598 }
1599
1600 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001601}
1602
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001603MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001604PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1605 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001606 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001607 MI->getOpcode() == PPC::SELECT_CC_F4 ||
Chris Lattner710ff322006-04-08 22:45:08 +00001608 MI->getOpcode() == PPC::SELECT_CC_F8 ||
1609 MI->getOpcode() == PPC::SELECT_CC_VRRC) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001610 "Unexpected instr type to insert");
1611
1612 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1613 // control-flow pattern. The incoming instruction knows the destination vreg
1614 // to set, the condition code register to branch on, the true/false values to
1615 // select between, and a branch opcode to use.
1616 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1617 ilist<MachineBasicBlock>::iterator It = BB;
1618 ++It;
1619
1620 // thisMBB:
1621 // ...
1622 // TrueVal = ...
1623 // cmpTY ccX, r1, r2
1624 // bCC copy1MBB
1625 // fallthrough --> copy0MBB
1626 MachineBasicBlock *thisMBB = BB;
1627 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1628 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1629 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1630 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1631 MachineFunction *F = BB->getParent();
1632 F->getBasicBlockList().insert(It, copy0MBB);
1633 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001634 // Update machine-CFG edges by first adding all successors of the current
1635 // block to the new block which will contain the Phi node for the select.
1636 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1637 e = BB->succ_end(); i != e; ++i)
1638 sinkMBB->addSuccessor(*i);
1639 // Next, remove all successors of the current block, and add the true
1640 // and fallthrough blocks as its successors.
1641 while(!BB->succ_empty())
1642 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001643 BB->addSuccessor(copy0MBB);
1644 BB->addSuccessor(sinkMBB);
1645
1646 // copy0MBB:
1647 // %FalseValue = ...
1648 // # fallthrough to sinkMBB
1649 BB = copy0MBB;
1650
1651 // Update machine-CFG edges
1652 BB->addSuccessor(sinkMBB);
1653
1654 // sinkMBB:
1655 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1656 // ...
1657 BB = sinkMBB;
1658 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1659 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1660 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1661
1662 delete MI; // The pseudo instruction is gone now.
1663 return BB;
1664}
1665
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001666SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1667 DAGCombinerInfo &DCI) const {
1668 TargetMachine &TM = getTargetMachine();
1669 SelectionDAG &DAG = DCI.DAG;
1670 switch (N->getOpcode()) {
1671 default: break;
1672 case ISD::SINT_TO_FP:
1673 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001674 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1675 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1676 // We allow the src/dst to be either f32/f64, but the intermediate
1677 // type must be i64.
1678 if (N->getOperand(0).getValueType() == MVT::i64) {
1679 SDOperand Val = N->getOperand(0).getOperand(0);
1680 if (Val.getValueType() == MVT::f32) {
1681 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1682 DCI.AddToWorklist(Val.Val);
1683 }
1684
1685 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001686 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001687 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001688 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001689 if (N->getValueType(0) == MVT::f32) {
1690 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1691 DCI.AddToWorklist(Val.Val);
1692 }
1693 return Val;
1694 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1695 // If the intermediate type is i32, we can avoid the load/store here
1696 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001697 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001698 }
1699 }
1700 break;
Chris Lattner51269842006-03-01 05:50:56 +00001701 case ISD::STORE:
1702 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1703 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1704 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1705 N->getOperand(1).getValueType() == MVT::i32) {
1706 SDOperand Val = N->getOperand(1).getOperand(0);
1707 if (Val.getValueType() == MVT::f32) {
1708 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1709 DCI.AddToWorklist(Val.Val);
1710 }
1711 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1712 DCI.AddToWorklist(Val.Val);
1713
1714 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1715 N->getOperand(2), N->getOperand(3));
1716 DCI.AddToWorklist(Val.Val);
1717 return Val;
1718 }
1719 break;
Chris Lattner4468c222006-03-31 06:02:07 +00001720 case PPCISD::VCMP: {
1721 // If a VCMPo node already exists with exactly the same operands as this
1722 // node, use its result instead of this node (VCMPo computes both a CR6 and
1723 // a normal output).
1724 //
1725 if (!N->getOperand(0).hasOneUse() &&
1726 !N->getOperand(1).hasOneUse() &&
1727 !N->getOperand(2).hasOneUse()) {
1728
1729 // Scan all of the users of the LHS, looking for VCMPo's that match.
1730 SDNode *VCMPoNode = 0;
1731
1732 SDNode *LHSN = N->getOperand(0).Val;
1733 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
1734 UI != E; ++UI)
1735 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
1736 (*UI)->getOperand(1) == N->getOperand(1) &&
1737 (*UI)->getOperand(2) == N->getOperand(2) &&
1738 (*UI)->getOperand(0) == N->getOperand(0)) {
1739 VCMPoNode = *UI;
1740 break;
1741 }
1742
1743 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00001744 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00001745 return SDOperand(VCMPoNode, 0);
1746 }
1747 break;
1748 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001749 }
1750
1751 return SDOperand();
1752}
1753
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001754void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1755 uint64_t Mask,
1756 uint64_t &KnownZero,
1757 uint64_t &KnownOne,
1758 unsigned Depth) const {
1759 KnownZero = 0;
1760 KnownOne = 0;
1761 switch (Op.getOpcode()) {
1762 default: break;
1763 case ISD::INTRINSIC_WO_CHAIN: {
1764 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
1765 default: break;
1766 case Intrinsic::ppc_altivec_vcmpbfp_p:
1767 case Intrinsic::ppc_altivec_vcmpeqfp_p:
1768 case Intrinsic::ppc_altivec_vcmpequb_p:
1769 case Intrinsic::ppc_altivec_vcmpequh_p:
1770 case Intrinsic::ppc_altivec_vcmpequw_p:
1771 case Intrinsic::ppc_altivec_vcmpgefp_p:
1772 case Intrinsic::ppc_altivec_vcmpgtfp_p:
1773 case Intrinsic::ppc_altivec_vcmpgtsb_p:
1774 case Intrinsic::ppc_altivec_vcmpgtsh_p:
1775 case Intrinsic::ppc_altivec_vcmpgtsw_p:
1776 case Intrinsic::ppc_altivec_vcmpgtub_p:
1777 case Intrinsic::ppc_altivec_vcmpgtuh_p:
1778 case Intrinsic::ppc_altivec_vcmpgtuw_p:
1779 KnownZero = ~1U; // All bits but the low one are known to be zero.
1780 break;
1781 }
1782 }
1783 }
1784}
1785
1786
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001787/// getConstraintType - Given a constraint letter, return the type of
1788/// constraint it is for this target.
1789PPCTargetLowering::ConstraintType
1790PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1791 switch (ConstraintLetter) {
1792 default: break;
1793 case 'b':
1794 case 'r':
1795 case 'f':
1796 case 'v':
1797 case 'y':
1798 return C_RegisterClass;
1799 }
1800 return TargetLowering::getConstraintType(ConstraintLetter);
1801}
1802
1803
Chris Lattnerddc787d2006-01-31 19:20:21 +00001804std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001805getRegClassForInlineAsmConstraint(const std::string &Constraint,
1806 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001807 if (Constraint.size() == 1) {
1808 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1809 default: break; // Unknown constriant letter
1810 case 'b':
1811 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1812 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1813 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1814 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1815 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1816 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1817 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1818 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1819 0);
1820 case 'r':
1821 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1822 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1823 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1824 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1825 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1826 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1827 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1828 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1829 0);
1830 case 'f':
1831 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1832 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1833 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1834 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1835 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1836 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1837 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1838 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1839 0);
1840 case 'v':
1841 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1842 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1843 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1844 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1845 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1846 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1847 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1848 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1849 0);
1850 case 'y':
1851 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1852 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1853 0);
1854 }
1855 }
1856
Chris Lattner1efa40f2006-02-22 00:56:39 +00001857 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001858}
Chris Lattner763317d2006-02-07 00:47:13 +00001859
1860// isOperandValidForConstraint
1861bool PPCTargetLowering::
1862isOperandValidForConstraint(SDOperand Op, char Letter) {
1863 switch (Letter) {
1864 default: break;
1865 case 'I':
1866 case 'J':
1867 case 'K':
1868 case 'L':
1869 case 'M':
1870 case 'N':
1871 case 'O':
1872 case 'P': {
1873 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1874 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1875 switch (Letter) {
1876 default: assert(0 && "Unknown constraint letter!");
1877 case 'I': // "I" is a signed 16-bit constant.
1878 return (short)Value == (int)Value;
1879 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1880 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1881 return (short)Value == 0;
1882 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1883 return (Value >> 16) == 0;
1884 case 'M': // "M" is a constant that is greater than 31.
1885 return Value > 31;
1886 case 'N': // "N" is a positive constant that is an exact power of two.
1887 return (int)Value > 0 && isPowerOf2_32(Value);
1888 case 'O': // "O" is the constant zero.
1889 return Value == 0;
1890 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1891 return (short)-Value == (int)-Value;
1892 }
1893 break;
1894 }
1895 }
1896
1897 // Handle standard constraint letters.
1898 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1899}
Evan Chengc4c62572006-03-13 23:20:37 +00001900
1901/// isLegalAddressImmediate - Return true if the integer value can be used
1902/// as the offset of the target addressing mode.
1903bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1904 // PPC allows a sign-extended 16-bit immediate field.
1905 return (V > -(1 << 16) && V < (1 << 16)-1);
1906}