blob: 1c3f4fdcef782b966d3fe2b5adcb8e21cd3fac19 [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
15#include "PPCTargetMachine.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000016#include "llvm/ADT/VectorExtras.h"
Evan Chengc4c62572006-03-13 23:20:37 +000017#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000021#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000022#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000023#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000024#include "llvm/Function.h"
Chris Lattner6d92cad2006-03-26 10:06:40 +000025#include "llvm/Intrinsics.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000026#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000027#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000028using namespace llvm;
29
Nate Begeman21e463b2005-10-16 05:39:50 +000030PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000031 : TargetLowering(TM) {
32
33 // Fold away setcc operations if possible.
34 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000035 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000036
Chris Lattnerd145a612005-09-27 22:18:25 +000037 // Use _setjmp/_longjmp instead of setjmp/longjmp.
38 setUseUnderscoreSetJmpLongJmp(true);
39
Chris Lattner7c5a3d32005-08-16 17:14:42 +000040 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000041 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
42 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
43 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000044
Chris Lattnera54aa942006-01-29 06:26:08 +000045 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
46 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
47
Chris Lattner7c5a3d32005-08-16 17:14:42 +000048 // PowerPC has no intrinsics for these particular operations
49 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
50 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
51 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
52
53 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
54 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
55 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
56
57 // PowerPC has no SREM/UREM instructions
58 setOperationAction(ISD::SREM, MVT::i32, Expand);
59 setOperationAction(ISD::UREM, MVT::i32, Expand);
60
61 // We don't support sin/cos/sqrt/fmod
62 setOperationAction(ISD::FSIN , MVT::f64, Expand);
63 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000064 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000065 setOperationAction(ISD::FSIN , MVT::f32, Expand);
66 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000067 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000068
69 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000070 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000071 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
72 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
73 }
74
Chris Lattner9601a862006-03-05 05:08:37 +000075 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
76 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
77
Nate Begemand88fc032006-01-14 03:14:10 +000078 // PowerPC does not have BSWAP, CTPOP or CTTZ
79 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000080 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
81 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
82
Nate Begeman35ef9132006-01-11 21:21:00 +000083 // PowerPC does not have ROTR
84 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
85
Chris Lattner7c5a3d32005-08-16 17:14:42 +000086 // PowerPC does not have Select
87 setOperationAction(ISD::SELECT, MVT::i32, Expand);
88 setOperationAction(ISD::SELECT, MVT::f32, Expand);
89 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000090
Chris Lattner0b1e4e52005-08-26 17:36:52 +000091 // PowerPC wants to turn select_cc of FP into fsel when possible.
92 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
93 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000094
Nate Begeman750ac1b2006-02-01 07:19:44 +000095 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000096 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000097
Nate Begeman81e80972006-03-17 01:40:33 +000098 // PowerPC does not have BRCOND which requires SetCC
99 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000100
Chris Lattnerf7605322005-08-31 21:09:52 +0000101 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
102 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000103
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000104 // PowerPC does not have [U|S]INT_TO_FP
105 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
106 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
107
Chris Lattner53e88452005-12-23 05:13:35 +0000108 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
109 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
110
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000111 // PowerPC does not have truncstore for i1.
112 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000113
Jim Laskeyabf6d172006-01-05 01:25:28 +0000114 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000115 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000116 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000117 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000118 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000119 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000120
Nate Begeman28a6b022005-12-10 02:36:00 +0000121 // We want to legalize GlobalAddress and ConstantPool nodes into the
122 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000123 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000124 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000125
Nate Begemanee625572006-01-27 21:09:22 +0000126 // RET must be custom lowered, to meet ABI requirements
127 setOperationAction(ISD::RET , MVT::Other, Custom);
128
Nate Begemanacc398c2006-01-25 18:21:52 +0000129 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
130 setOperationAction(ISD::VASTART , MVT::Other, Custom);
131
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000132 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000133 setOperationAction(ISD::VAARG , MVT::Other, Expand);
134 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
135 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000136 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
137 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
138 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000139
Chris Lattner6d92cad2006-03-26 10:06:40 +0000140 // We want to custom lower some of our intrinsics.
Chris Lattner48b61a72006-03-28 00:40:33 +0000141 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Chris Lattner6d92cad2006-03-26 10:06:40 +0000142
Nate Begemanc09eeec2005-09-06 22:03:27 +0000143 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000144 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000145 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
146 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner7fbcef72006-03-24 07:53:47 +0000147
148 // FIXME: disable this lowered code. This generates 64-bit register values,
149 // and we don't model the fact that the top part is clobbered by calls. We
150 // need to flag these together so that the value isn't live across a call.
151 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
152
Nate Begemanae749a92005-10-25 23:48:36 +0000153 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
154 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
155 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000156 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000157 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000158 }
159
160 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
161 // 64 bit PowerPC implementations can support i64 types directly
162 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000163 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
164 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000165 } else {
166 // 32 bit PowerPC wants to expand i64 shifts itself.
167 setOperationAction(ISD::SHL, MVT::i64, Custom);
168 setOperationAction(ISD::SRL, MVT::i64, Custom);
169 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000170 }
Evan Chengd30bf012006-03-01 01:11:20 +0000171
Nate Begeman425a9692005-11-29 08:17:20 +0000172 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000173 // First set operation action for all vector types to expand. Then we
174 // will selectively turn on ones that can be effectively codegen'd.
175 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
176 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
177 // add/sub/and/or/xor are legal for all supported vector VT's.
178 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Legal);
179 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Legal);
180 setOperationAction(ISD::AND , (MVT::ValueType)VT, Legal);
181 setOperationAction(ISD::OR , (MVT::ValueType)VT, Legal);
182 setOperationAction(ISD::XOR , (MVT::ValueType)VT, Legal);
183
184 // We can custom expand all VECTOR_SHUFFLEs to VPERM.
Chris Lattnerb6831442006-04-03 23:58:04 +0000185 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000186
187 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
188 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
189 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
190 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
191 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
192 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
193 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
194 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000195
196 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000197 }
198
Nate Begeman425a9692005-11-29 08:17:20 +0000199 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000200 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000201 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
202 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000203
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000204 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000205
Chris Lattnerb2177b92006-03-19 06:55:52 +0000206 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
207 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000208
Chris Lattner541f91b2006-04-02 00:43:36 +0000209 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
210 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000211 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
212 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000213 }
214
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000215 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000216 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000217
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000218 // We have target-specific dag combine patterns for the following nodes:
219 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000220 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000221
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000222 computeRegisterProperties();
223}
224
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000225const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
226 switch (Opcode) {
227 default: return 0;
228 case PPCISD::FSEL: return "PPCISD::FSEL";
229 case PPCISD::FCFID: return "PPCISD::FCFID";
230 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
231 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000232 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000233 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
234 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000235 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000236 case PPCISD::Hi: return "PPCISD::Hi";
237 case PPCISD::Lo: return "PPCISD::Lo";
238 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
239 case PPCISD::SRL: return "PPCISD::SRL";
240 case PPCISD::SRA: return "PPCISD::SRA";
241 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000242 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
243 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000244 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000245 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000246 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000247 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000248 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000249 }
250}
251
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000252/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
253static bool isFloatingPointZero(SDOperand Op) {
254 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
255 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
256 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
257 // Maybe this has already been legalized into the constant pool?
258 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
259 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
260 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
261 }
262 return false;
263}
264
Chris Lattneref819f82006-03-20 06:33:01 +0000265
266/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
267/// specifies a splat of a single element that is suitable for input to
268/// VSPLTB/VSPLTH/VSPLTW.
269bool PPC::isSplatShuffleMask(SDNode *N) {
270 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000271
272 // We can only splat 8-bit, 16-bit, and 32-bit quantities.
273 if (N->getNumOperands() != 4 && N->getNumOperands() != 8 &&
274 N->getNumOperands() != 16)
275 return false;
276
Chris Lattner88a99ef2006-03-20 06:37:44 +0000277 // This is a splat operation if each element of the permute is the same, and
278 // if the value doesn't reference the second vector.
279 SDOperand Elt = N->getOperand(0);
280 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
281 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
282 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
283 "Invalid VECTOR_SHUFFLE mask!");
284 if (N->getOperand(i) != Elt) return false;
285 }
286
287 // Make sure it is a splat of the first vector operand.
288 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
Chris Lattneref819f82006-03-20 06:33:01 +0000289}
290
291/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
292/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
293unsigned PPC::getVSPLTImmediate(SDNode *N) {
294 assert(isSplatShuffleMask(N));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000295 return cast<ConstantSDNode>(N->getOperand(0))->getValue();
Chris Lattneref819f82006-03-20 06:33:01 +0000296}
297
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000298/// isVecSplatImm - Return true if this is a build_vector of constants which
299/// can be formed by using a vspltis[bhw] instruction. The ByteSize field
300/// indicates the number of bytes of each element [124] -> [bhw].
301bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) {
302 SDOperand OpVal(0, 0);
303 // Check to see if this buildvec has a single non-undef value in its elements.
304 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
305 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
306 if (OpVal.Val == 0)
307 OpVal = N->getOperand(i);
308 else if (OpVal != N->getOperand(i))
309 return false;
310 }
311
312 if (OpVal.Val == 0) return false; // All UNDEF: use implicit def.
313
Nate Begeman98e70cc2006-03-28 04:15:58 +0000314 unsigned ValSizeInBytes = 0;
315 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000316 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
317 Value = CN->getValue();
318 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
319 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
320 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
321 Value = FloatToBits(CN->getValue());
322 ValSizeInBytes = 4;
323 }
324
325 // If the splat value is larger than the element value, then we can never do
326 // this splat. The only case that we could fit the replicated bits into our
327 // immediate field for would be zero, and we prefer to use vxor for it.
328 if (ValSizeInBytes < ByteSize) return false;
329
330 // If the element value is larger than the splat value, cut it in half and
331 // check to see if the two halves are equal. Continue doing this until we
332 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
333 while (ValSizeInBytes > ByteSize) {
334 ValSizeInBytes >>= 1;
335
336 // If the top half equals the bottom half, we're still ok.
337 if (((Value >> (ValSizeInBytes*8)) & ((8 << ValSizeInBytes)-1)) !=
338 (Value & ((8 << ValSizeInBytes)-1)))
339 return false;
340 }
341
342 // Properly sign extend the value.
343 int ShAmt = (4-ByteSize)*8;
344 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
345
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000346 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000347 if (MaskVal == 0) return false;
348
349 if (Val) *Val = MaskVal;
350
351 // Finally, if this value fits in a 5 bit sext field, return true.
352 return ((MaskVal << (32-5)) >> (32-5)) == MaskVal;
353}
354
Chris Lattneref819f82006-03-20 06:33:01 +0000355
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000356/// LowerOperation - Provide custom lowering hooks for some operations.
357///
Nate Begeman21e463b2005-10-16 05:39:50 +0000358SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000359 switch (Op.getOpcode()) {
360 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000361 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000362 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000363 SDOperand Src = Op.getOperand(0);
364 if (Src.getValueType() == MVT::f32)
365 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
366
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000367 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000368 switch (Op.getValueType()) {
369 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
370 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000371 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000372 break;
373 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000374 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000375 break;
376 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000377
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000378 // Convert the FP value to an int value through memory.
379 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
380 if (Op.getValueType() == MVT::i32)
381 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
382 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000383 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000384 case ISD::SINT_TO_FP:
385 if (Op.getOperand(0).getValueType() == MVT::i64) {
386 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
387 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
388 if (Op.getValueType() == MVT::f32)
389 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
390 return FP;
391 } else {
392 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
393 "Unhandled SINT_TO_FP type in custom expander!");
394 // Since we only generate this in 64-bit mode, we can take advantage of
395 // 64-bit registers. In particular, sign extend the input value into the
396 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
397 // then lfd it and fcfid it.
398 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
399 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
400 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
401
402 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
403 Op.getOperand(0));
404
405 // STD the extended value into the stack slot.
406 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
407 DAG.getEntryNode(), Ext64, FIdx,
408 DAG.getSrcValue(NULL));
409 // Load the value as a double.
410 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
411
412 // FCFID it and return it.
413 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
414 if (Op.getValueType() == MVT::f32)
415 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
416 return FP;
417 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000418 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000419
Chris Lattnerf7605322005-08-31 21:09:52 +0000420 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000421 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000422 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
423 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
424 break;
425
426 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
427
428 // Cannot handle SETEQ/SETNE.
429 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
430
431 MVT::ValueType ResVT = Op.getValueType();
432 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
433 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
434 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000435
Chris Lattnerf7605322005-08-31 21:09:52 +0000436 // If the RHS of the comparison is a 0.0, we don't need to do the
437 // subtraction at all.
438 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000439 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000440 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000441 case ISD::SETULT:
442 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000443 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000444 case ISD::SETUGE:
445 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000446 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
447 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000448 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000449 case ISD::SETUGT:
450 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000451 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000452 case ISD::SETULE:
453 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000454 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
455 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000456 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000457 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000458 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000459
Chris Lattnereb255f22005-10-25 20:54:57 +0000460 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000461 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000462 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000463 case ISD::SETULT:
464 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000465 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
466 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
467 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
468 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000469 case ISD::SETUGE:
470 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000471 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
472 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
473 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
474 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000475 case ISD::SETUGT:
476 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000477 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
478 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
479 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
480 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000481 case ISD::SETULE:
482 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000483 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
484 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
485 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
486 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000487 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000488 break;
489 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000490 case ISD::SHL: {
491 assert(Op.getValueType() == MVT::i64 &&
492 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
493 // The generic code does a fine job expanding shift by a constant.
494 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
495
496 // Otherwise, expand into a bunch of logical ops. Note that these ops
497 // depend on the PPC behavior for oversized shift amounts.
498 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
499 DAG.getConstant(0, MVT::i32));
500 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
501 DAG.getConstant(1, MVT::i32));
502 SDOperand Amt = Op.getOperand(1);
503
504 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
505 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000506 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
507 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000508 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
509 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
510 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000511 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000512 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000513 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000514 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
515 }
516 case ISD::SRL: {
517 assert(Op.getValueType() == MVT::i64 &&
518 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
519 // The generic code does a fine job expanding shift by a constant.
520 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
521
522 // Otherwise, expand into a bunch of logical ops. Note that these ops
523 // depend on the PPC behavior for oversized shift amounts.
524 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
525 DAG.getConstant(0, MVT::i32));
526 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
527 DAG.getConstant(1, MVT::i32));
528 SDOperand Amt = Op.getOperand(1);
529
530 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
531 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000532 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
533 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000534 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
535 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
536 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000537 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000538 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000539 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000540 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
541 }
542 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000543 assert(Op.getValueType() == MVT::i64 &&
544 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
545 // The generic code does a fine job expanding shift by a constant.
546 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
547
548 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
549 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
550 DAG.getConstant(0, MVT::i32));
551 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
552 DAG.getConstant(1, MVT::i32));
553 SDOperand Amt = Op.getOperand(1);
554
555 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
556 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000557 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
558 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000559 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
560 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
561 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000562 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
563 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000564 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
565 Tmp4, Tmp6, ISD::SETLE);
566 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000567 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000568 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000569 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
570 Constant *C = CP->get();
571 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000572 SDOperand Zero = DAG.getConstant(0, MVT::i32);
573
Evan Cheng4c1aa862006-02-22 20:19:42 +0000574 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000575 // Generate non-pic code that has direct accesses to the constant pool.
576 // The address of the global is just (hi(&g)+lo(&g)).
577 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
578 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
579 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
580 }
581
582 // Only lower ConstantPool on Darwin.
583 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
584 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000585 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000586 // With PIC, the first instruction is actually "GR+hi(&G)".
587 Hi = DAG.getNode(ISD::ADD, MVT::i32,
588 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
589 }
590
591 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
592 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
593 return Lo;
594 }
Chris Lattner860e8862005-11-17 07:30:41 +0000595 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000596 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
597 GlobalValue *GV = GSDN->getGlobal();
598 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000599 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000600
Evan Cheng4c1aa862006-02-22 20:19:42 +0000601 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000602 // Generate non-pic code that has direct accesses to globals.
603 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000604 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
605 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
606 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
607 }
Chris Lattner860e8862005-11-17 07:30:41 +0000608
Chris Lattner1d05cb42005-11-17 18:55:48 +0000609 // Only lower GlobalAddress on Darwin.
610 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000611
Chris Lattner860e8862005-11-17 07:30:41 +0000612 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000613 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000614 // With PIC, the first instruction is actually "GR+hi(&G)".
615 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000616 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000617 }
618
619 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
620 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
621
Chris Lattner37dd6f12006-01-29 20:49:17 +0000622 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
623 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000624 return Lo;
625
626 // If the global is weak or external, we have to go through the lazy
627 // resolution stub.
628 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
629 }
Nate Begeman44775902006-01-31 08:17:29 +0000630 case ISD::SETCC: {
631 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000632
633 // If we're comparing for equality to zero, expose the fact that this is
634 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
635 // fold the new nodes.
636 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
637 if (C->isNullValue() && CC == ISD::SETEQ) {
638 MVT::ValueType VT = Op.getOperand(0).getValueType();
639 SDOperand Zext = Op.getOperand(0);
640 if (VT < MVT::i32) {
641 VT = MVT::i32;
642 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
643 }
644 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
645 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
646 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
647 DAG.getConstant(Log2b, getShiftAmountTy()));
648 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
649 }
650 // Leave comparisons against 0 and -1 alone for now, since they're usually
651 // optimized. FIXME: revisit this when we can custom lower all setcc
652 // optimizations.
653 if (C->isAllOnesValue() || C->isNullValue())
654 break;
655 }
656
657 // If we have an integer seteq/setne, turn it into a compare against zero
658 // by subtracting the rhs from the lhs, which is faster than setting a
659 // condition register, reading it back out, and masking the correct bit.
660 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
661 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
662 MVT::ValueType VT = Op.getValueType();
663 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
664 Op.getOperand(1));
665 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
666 }
Nate Begeman44775902006-01-31 08:17:29 +0000667 break;
668 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000669 case ISD::VASTART: {
670 // vastart just stores the address of the VarArgsFrameIndex slot into the
671 // memory location argument.
672 // FIXME: Replace MVT::i32 with PointerTy
673 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
674 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
675 Op.getOperand(1), Op.getOperand(2));
676 }
Nate Begemanee625572006-01-27 21:09:22 +0000677 case ISD::RET: {
678 SDOperand Copy;
679
680 switch(Op.getNumOperands()) {
681 default:
682 assert(0 && "Do not know how to return this many arguments!");
683 abort();
684 case 1:
685 return SDOperand(); // ret void is legal
686 case 2: {
687 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
688 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
689 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
690 SDOperand());
691 break;
692 }
693 case 3:
694 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
695 SDOperand());
696 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
697 break;
698 }
699 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
700 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000701 case ISD::SCALAR_TO_VECTOR: {
702 // Create a stack slot that is 16-byte aligned.
703 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
704 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
705 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
706
707 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000708 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
709 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner7f20b132006-03-28 01:43:22 +0000710 // Load it out.
711 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
Chris Lattnerb2177b92006-03-19 06:55:52 +0000712 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000713 case ISD::BUILD_VECTOR:
714 // If this is a case we can't handle, return null and let the default
715 // expansion code take care of it. If we CAN select this case, return Op.
716
717 // See if this is all zeros.
718 // FIXME: We should handle splat(-0.0), and other cases here.
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000719 if (ISD::isBuildVectorAllZeros(Op.Val))
Chris Lattner64b3a082006-03-24 07:48:08 +0000720 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000721
722 if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb
723 PPC::isVecSplatImm(Op.Val, 2) || // vspltish
724 PPC::isVecSplatImm(Op.Val, 4)) // vspltisw
725 return Op;
726
Chris Lattner64b3a082006-03-24 07:48:08 +0000727 return SDOperand();
728
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000729 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000730 SDOperand V1 = Op.getOperand(0);
731 SDOperand V2 = Op.getOperand(1);
732 SDOperand PermMask = Op.getOperand(2);
733
734 // Cases that are handled by instructions that take permute immediates
735 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
736 // selected by the instruction selector.
737 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
738 break;
739
740 // TODO: Handle more cases, and also handle cases that are cheaper to do as
741 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000742
743 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
744 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000745 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000746
747 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
748 // that it is in input element units, not in bytes. Convert now.
749 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
750 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
751
752 std::vector<SDOperand> ResultMask;
753 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
754 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
755
756 for (unsigned j = 0; j != BytesPerElement; ++j)
757 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
758 MVT::i8));
759 }
760
761 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
762 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
763 }
Chris Lattner48b61a72006-03-28 00:40:33 +0000764 case ISD::INTRINSIC_WO_CHAIN: {
Chris Lattnerbbe77de2006-04-02 06:26:07 +0000765 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
Chris Lattner6d92cad2006-03-26 10:06:40 +0000766
767 // If this is a lowered altivec predicate compare, CompareOpc is set to the
768 // opcode number of the comparison.
769 int CompareOpc = -1;
Chris Lattnera17b1552006-03-31 05:13:27 +0000770 bool isDot = false;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000771 switch (IntNo) {
772 default: return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattnera17b1552006-03-31 05:13:27 +0000773 // Comparison predicates.
774 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
775 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
776 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
777 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
778 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
779 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
780 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
781 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
782 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
783 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
784 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
785 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
786 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
787
788 // Normal Comparisons.
789 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
790 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
791 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
792 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
793 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
794 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
795 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
796 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
797 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
798 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
799 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
800 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
801 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000802 }
803
804 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
805
Chris Lattnera17b1552006-03-31 05:13:27 +0000806 // If this is a non-dot comparison, make the VCMP node.
807 if (!isDot)
808 return DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
809 Op.getOperand(1), Op.getOperand(2),
810 DAG.getConstant(CompareOpc, MVT::i32));
811
Chris Lattner6d92cad2006-03-26 10:06:40 +0000812 // Create the PPCISD altivec 'dot' comparison node.
813 std::vector<SDOperand> Ops;
814 std::vector<MVT::ValueType> VTs;
815 Ops.push_back(Op.getOperand(2)); // LHS
816 Ops.push_back(Op.getOperand(3)); // RHS
817 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
818 VTs.push_back(Op.getOperand(2).getValueType());
819 VTs.push_back(MVT::Flag);
820 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
821
822 // Now that we have the comparison, emit a copy from the CR to a GPR.
823 // This is flagged to the above dot comparison.
824 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
825 DAG.getRegister(PPC::CR6, MVT::i32),
826 CompNode.getValue(1));
827
828 // Unpack the result based on how the target uses it.
829 unsigned BitNo; // Bit # of CR6.
830 bool InvertBit; // Invert result?
831 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
832 default: // Can't happen, don't crash on invalid number though.
833 case 0: // Return the value of the EQ bit of CR6.
834 BitNo = 0; InvertBit = false;
835 break;
836 case 1: // Return the inverted value of the EQ bit of CR6.
837 BitNo = 0; InvertBit = true;
838 break;
839 case 2: // Return the value of the LT bit of CR6.
840 BitNo = 2; InvertBit = false;
841 break;
842 case 3: // Return the inverted value of the LT bit of CR6.
843 BitNo = 2; InvertBit = true;
844 break;
845 }
846
847 // Shift the bit into the low position.
848 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
849 DAG.getConstant(8-(3-BitNo), MVT::i32));
850 // Isolate the bit.
851 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
852 DAG.getConstant(1, MVT::i32));
853
854 // If we are supposed to, toggle the bit.
855 if (InvertBit)
856 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
857 DAG.getConstant(1, MVT::i32));
858 return Flags;
859 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000860 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000861 return SDOperand();
862}
863
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000864std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000865PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000866 //
867 // add beautiful description of PPC stack frame format, or at least some docs
868 //
869 MachineFunction &MF = DAG.getMachineFunction();
870 MachineFrameInfo *MFI = MF.getFrameInfo();
871 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000872 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000873 std::vector<SDOperand> ArgValues;
874
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000875 unsigned ArgOffset = 24;
876 unsigned GPR_remaining = 8;
877 unsigned FPR_remaining = 13;
878 unsigned GPR_idx = 0, FPR_idx = 0;
879 static const unsigned GPR[] = {
880 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
881 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
882 };
883 static const unsigned FPR[] = {
884 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
885 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
886 };
887
888 // Add DAG nodes to load the arguments... On entry to a function on PPC,
889 // the arguments start at offset 24, although they are likely to be passed
890 // in registers.
891 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
892 SDOperand newroot, argt;
893 unsigned ObjSize;
894 bool needsLoad = false;
895 bool ArgLive = !I->use_empty();
896 MVT::ValueType ObjectVT = getValueType(I->getType());
897
898 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000899 default: assert(0 && "Unhandled argument type!");
900 case MVT::i1:
901 case MVT::i8:
902 case MVT::i16:
903 case MVT::i32:
904 ObjSize = 4;
905 if (!ArgLive) break;
906 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000907 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000908 MF.addLiveIn(GPR[GPR_idx], VReg);
909 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000910 if (ObjectVT != MVT::i32) {
911 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
912 : ISD::AssertZext;
913 argt = DAG.getNode(AssertOp, MVT::i32, argt,
914 DAG.getValueType(ObjectVT));
915 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
916 }
Chris Lattner915fb302005-08-30 00:19:00 +0000917 } else {
918 needsLoad = true;
919 }
920 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000921 case MVT::i64:
922 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000923 if (!ArgLive) break;
924 if (GPR_remaining > 0) {
925 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000926 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000927 MF.addLiveIn(GPR[GPR_idx], VReg);
928 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000929 // If we have two or more remaining argument registers, then both halves
930 // of the i64 can be sourced from there. Otherwise, the lower half will
931 // have to come off the stack. This can happen when an i64 is preceded
932 // by 28 bytes of arguments.
933 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000934 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000935 MF.addLiveIn(GPR[GPR_idx+1], VReg);
936 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000937 } else {
938 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
939 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
940 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
941 DAG.getSrcValue(NULL));
942 }
943 // Build the outgoing arg thingy
944 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
945 newroot = argLo;
946 } else {
947 needsLoad = true;
948 }
949 break;
950 case MVT::f32:
951 case MVT::f64:
952 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000953 if (!ArgLive) {
954 if (FPR_remaining > 0) {
955 --FPR_remaining;
956 ++FPR_idx;
957 }
958 break;
959 }
Chris Lattner915fb302005-08-30 00:19:00 +0000960 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000961 unsigned VReg;
962 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000963 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000964 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000965 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000966 MF.addLiveIn(FPR[FPR_idx], VReg);
967 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000968 --FPR_remaining;
969 ++FPR_idx;
970 } else {
971 needsLoad = true;
972 }
973 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000974 }
975
976 // We need to load the argument to a virtual register if we determined above
977 // that we ran out of physical registers of the appropriate type
978 if (needsLoad) {
979 unsigned SubregOffset = 0;
980 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
981 if (ObjectVT == MVT::i16) SubregOffset = 2;
982 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
983 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
984 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
985 DAG.getConstant(SubregOffset, MVT::i32));
986 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
987 DAG.getSrcValue(NULL));
988 }
989
990 // Every 4 bytes of argument space consumes one of the GPRs available for
991 // argument passing.
992 if (GPR_remaining > 0) {
993 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
994 GPR_remaining -= delta;
995 GPR_idx += delta;
996 }
997 ArgOffset += ObjSize;
998 if (newroot.Val)
999 DAG.setRoot(newroot.getValue(1));
1000
1001 ArgValues.push_back(argt);
1002 }
1003
1004 // If the function takes variable number of arguments, make a frame index for
1005 // the start of the first vararg value... for expansion of llvm.va_start.
1006 if (F.isVarArg()) {
1007 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1008 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1009 // If this function is vararg, store any remaining integer argument regs
1010 // to their spots on the stack so that they may be loaded by deferencing the
1011 // result of va_next.
1012 std::vector<SDOperand> MemOps;
1013 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001014 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001015 MF.addLiveIn(GPR[GPR_idx], VReg);
1016 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001017 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1018 Val, FIN, DAG.getSrcValue(NULL));
1019 MemOps.push_back(Store);
1020 // Increment the address by four for the next argument to store
1021 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1022 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1023 }
Chris Lattner80720a92005-11-30 20:40:54 +00001024 if (!MemOps.empty()) {
1025 MemOps.push_back(DAG.getRoot());
1026 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1027 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001028 }
1029
1030 // Finally, inform the code generator which regs we return values in.
1031 switch (getValueType(F.getReturnType())) {
1032 default: assert(0 && "Unknown type!");
1033 case MVT::isVoid: break;
1034 case MVT::i1:
1035 case MVT::i8:
1036 case MVT::i16:
1037 case MVT::i32:
1038 MF.addLiveOut(PPC::R3);
1039 break;
1040 case MVT::i64:
1041 MF.addLiveOut(PPC::R3);
1042 MF.addLiveOut(PPC::R4);
1043 break;
1044 case MVT::f32:
1045 case MVT::f64:
1046 MF.addLiveOut(PPC::F1);
1047 break;
1048 }
1049
1050 return ArgValues;
1051}
1052
1053std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001054PPCTargetLowering::LowerCallTo(SDOperand Chain,
1055 const Type *RetTy, bool isVarArg,
1056 unsigned CallingConv, bool isTailCall,
1057 SDOperand Callee, ArgListTy &Args,
1058 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001059 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001060 // SelectExpr to use to put the arguments in the appropriate registers.
1061 std::vector<SDOperand> args_to_use;
1062
1063 // Count how many bytes are to be pushed on the stack, including the linkage
1064 // area, and parameter passing area.
1065 unsigned NumBytes = 24;
1066
1067 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001068 Chain = DAG.getCALLSEQ_START(Chain,
1069 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001070 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001071 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001072 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001073 default: assert(0 && "Unknown value type!");
1074 case MVT::i1:
1075 case MVT::i8:
1076 case MVT::i16:
1077 case MVT::i32:
1078 case MVT::f32:
1079 NumBytes += 4;
1080 break;
1081 case MVT::i64:
1082 case MVT::f64:
1083 NumBytes += 8;
1084 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001085 }
Chris Lattner915fb302005-08-30 00:19:00 +00001086 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001087
Chris Lattner915fb302005-08-30 00:19:00 +00001088 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1089 // plus 32 bytes of argument space in case any called code gets funky on us.
1090 // (Required by ABI to support var arg)
1091 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001092
1093 // Adjust the stack pointer for the new arguments...
1094 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001095 Chain = DAG.getCALLSEQ_START(Chain,
1096 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001097
1098 // Set up a copy of the stack pointer for use loading and storing any
1099 // arguments that may not fit in the registers available for argument
1100 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001101 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001102
1103 // Figure out which arguments are going to go in registers, and which in
1104 // memory. Also, if this is a vararg function, floating point operations
1105 // must be stored to our stack, and loaded into integer regs as well, if
1106 // any integer regs are available for argument passing.
1107 unsigned ArgOffset = 24;
1108 unsigned GPR_remaining = 8;
1109 unsigned FPR_remaining = 13;
1110
1111 std::vector<SDOperand> MemOps;
1112 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1113 // PtrOff will be used to store the current argument to the stack if a
1114 // register cannot be found for it.
1115 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1116 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1117 MVT::ValueType ArgVT = getValueType(Args[i].second);
1118
1119 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001120 default: assert(0 && "Unexpected ValueType for argument!");
1121 case MVT::i1:
1122 case MVT::i8:
1123 case MVT::i16:
1124 // Promote the integer to 32 bits. If the input type is signed use a
1125 // sign extend, otherwise use a zero extend.
1126 if (Args[i].second->isSigned())
1127 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1128 else
1129 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1130 // FALL THROUGH
1131 case MVT::i32:
1132 if (GPR_remaining > 0) {
1133 args_to_use.push_back(Args[i].first);
1134 --GPR_remaining;
1135 } else {
1136 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1137 Args[i].first, PtrOff,
1138 DAG.getSrcValue(NULL)));
1139 }
1140 ArgOffset += 4;
1141 break;
1142 case MVT::i64:
1143 // If we have one free GPR left, we can place the upper half of the i64
1144 // in it, and store the other half to the stack. If we have two or more
1145 // free GPRs, then we can pass both halves of the i64 in registers.
1146 if (GPR_remaining > 0) {
1147 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1148 Args[i].first, DAG.getConstant(1, MVT::i32));
1149 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1150 Args[i].first, DAG.getConstant(0, MVT::i32));
1151 args_to_use.push_back(Hi);
1152 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001153 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001154 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001155 --GPR_remaining;
1156 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001157 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1158 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001159 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001160 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001161 }
Chris Lattner915fb302005-08-30 00:19:00 +00001162 } else {
1163 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1164 Args[i].first, PtrOff,
1165 DAG.getSrcValue(NULL)));
1166 }
1167 ArgOffset += 8;
1168 break;
1169 case MVT::f32:
1170 case MVT::f64:
1171 if (FPR_remaining > 0) {
1172 args_to_use.push_back(Args[i].first);
1173 --FPR_remaining;
1174 if (isVarArg) {
1175 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1176 Args[i].first, PtrOff,
1177 DAG.getSrcValue(NULL));
1178 MemOps.push_back(Store);
1179 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001180 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001181 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1182 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001183 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001184 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001185 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001186 }
1187 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001188 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1189 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001190 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1191 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001192 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001193 args_to_use.push_back(Load);
1194 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001195 }
1196 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001197 // If we have any FPRs remaining, we may also have GPRs remaining.
1198 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1199 // GPRs.
1200 if (GPR_remaining > 0) {
1201 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1202 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001203 }
Chris Lattner915fb302005-08-30 00:19:00 +00001204 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1205 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1206 --GPR_remaining;
1207 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001208 }
Chris Lattner915fb302005-08-30 00:19:00 +00001209 } else {
1210 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1211 Args[i].first, PtrOff,
1212 DAG.getSrcValue(NULL)));
1213 }
1214 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1215 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001216 }
1217 }
1218 if (!MemOps.empty())
1219 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1220 }
1221
1222 std::vector<MVT::ValueType> RetVals;
1223 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001224 MVT::ValueType ActualRetTyVT = RetTyVT;
1225 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1226 ActualRetTyVT = MVT::i32; // Promote result to i32.
1227
Chris Lattnere00ebf02006-01-28 07:33:03 +00001228 if (RetTyVT == MVT::i64) {
1229 RetVals.push_back(MVT::i32);
1230 RetVals.push_back(MVT::i32);
1231 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001232 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001233 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001234 RetVals.push_back(MVT::Other);
1235
Chris Lattner2823b3e2005-11-17 05:56:14 +00001236 // If the callee is a GlobalAddress node (quite common, every direct call is)
1237 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1238 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1239 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1240
Chris Lattner281b55e2006-01-27 23:34:02 +00001241 std::vector<SDOperand> Ops;
1242 Ops.push_back(Chain);
1243 Ops.push_back(Callee);
1244 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1245 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001246 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001247 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1248 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001249 SDOperand RetVal = TheCall;
1250
1251 // If the result is a small value, add a note so that we keep track of the
1252 // information about whether it is sign or zero extended.
1253 if (RetTyVT != ActualRetTyVT) {
1254 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1255 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1256 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001257 } else if (RetTyVT == MVT::i64) {
1258 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001259 }
1260
1261 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001262}
1263
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001264MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001265PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1266 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001267 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001268 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1269 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001270 "Unexpected instr type to insert");
1271
1272 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1273 // control-flow pattern. The incoming instruction knows the destination vreg
1274 // to set, the condition code register to branch on, the true/false values to
1275 // select between, and a branch opcode to use.
1276 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1277 ilist<MachineBasicBlock>::iterator It = BB;
1278 ++It;
1279
1280 // thisMBB:
1281 // ...
1282 // TrueVal = ...
1283 // cmpTY ccX, r1, r2
1284 // bCC copy1MBB
1285 // fallthrough --> copy0MBB
1286 MachineBasicBlock *thisMBB = BB;
1287 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1288 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1289 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1290 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1291 MachineFunction *F = BB->getParent();
1292 F->getBasicBlockList().insert(It, copy0MBB);
1293 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001294 // Update machine-CFG edges by first adding all successors of the current
1295 // block to the new block which will contain the Phi node for the select.
1296 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1297 e = BB->succ_end(); i != e; ++i)
1298 sinkMBB->addSuccessor(*i);
1299 // Next, remove all successors of the current block, and add the true
1300 // and fallthrough blocks as its successors.
1301 while(!BB->succ_empty())
1302 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001303 BB->addSuccessor(copy0MBB);
1304 BB->addSuccessor(sinkMBB);
1305
1306 // copy0MBB:
1307 // %FalseValue = ...
1308 // # fallthrough to sinkMBB
1309 BB = copy0MBB;
1310
1311 // Update machine-CFG edges
1312 BB->addSuccessor(sinkMBB);
1313
1314 // sinkMBB:
1315 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1316 // ...
1317 BB = sinkMBB;
1318 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1319 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1320 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1321
1322 delete MI; // The pseudo instruction is gone now.
1323 return BB;
1324}
1325
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001326SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1327 DAGCombinerInfo &DCI) const {
1328 TargetMachine &TM = getTargetMachine();
1329 SelectionDAG &DAG = DCI.DAG;
1330 switch (N->getOpcode()) {
1331 default: break;
1332 case ISD::SINT_TO_FP:
1333 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001334 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1335 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1336 // We allow the src/dst to be either f32/f64, but the intermediate
1337 // type must be i64.
1338 if (N->getOperand(0).getValueType() == MVT::i64) {
1339 SDOperand Val = N->getOperand(0).getOperand(0);
1340 if (Val.getValueType() == MVT::f32) {
1341 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1342 DCI.AddToWorklist(Val.Val);
1343 }
1344
1345 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001346 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001347 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001348 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001349 if (N->getValueType(0) == MVT::f32) {
1350 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1351 DCI.AddToWorklist(Val.Val);
1352 }
1353 return Val;
1354 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1355 // If the intermediate type is i32, we can avoid the load/store here
1356 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001357 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001358 }
1359 }
1360 break;
Chris Lattner51269842006-03-01 05:50:56 +00001361 case ISD::STORE:
1362 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1363 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1364 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1365 N->getOperand(1).getValueType() == MVT::i32) {
1366 SDOperand Val = N->getOperand(1).getOperand(0);
1367 if (Val.getValueType() == MVT::f32) {
1368 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1369 DCI.AddToWorklist(Val.Val);
1370 }
1371 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1372 DCI.AddToWorklist(Val.Val);
1373
1374 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1375 N->getOperand(2), N->getOperand(3));
1376 DCI.AddToWorklist(Val.Val);
1377 return Val;
1378 }
1379 break;
Chris Lattner4468c222006-03-31 06:02:07 +00001380 case PPCISD::VCMP: {
1381 // If a VCMPo node already exists with exactly the same operands as this
1382 // node, use its result instead of this node (VCMPo computes both a CR6 and
1383 // a normal output).
1384 //
1385 if (!N->getOperand(0).hasOneUse() &&
1386 !N->getOperand(1).hasOneUse() &&
1387 !N->getOperand(2).hasOneUse()) {
1388
1389 // Scan all of the users of the LHS, looking for VCMPo's that match.
1390 SDNode *VCMPoNode = 0;
1391
1392 SDNode *LHSN = N->getOperand(0).Val;
1393 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
1394 UI != E; ++UI)
1395 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
1396 (*UI)->getOperand(1) == N->getOperand(1) &&
1397 (*UI)->getOperand(2) == N->getOperand(2) &&
1398 (*UI)->getOperand(0) == N->getOperand(0)) {
1399 VCMPoNode = *UI;
1400 break;
1401 }
1402
1403 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00001404 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00001405 return SDOperand(VCMPoNode, 0);
1406 }
1407 break;
1408 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001409 }
1410
1411 return SDOperand();
1412}
1413
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001414void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1415 uint64_t Mask,
1416 uint64_t &KnownZero,
1417 uint64_t &KnownOne,
1418 unsigned Depth) const {
1419 KnownZero = 0;
1420 KnownOne = 0;
1421 switch (Op.getOpcode()) {
1422 default: break;
1423 case ISD::INTRINSIC_WO_CHAIN: {
1424 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
1425 default: break;
1426 case Intrinsic::ppc_altivec_vcmpbfp_p:
1427 case Intrinsic::ppc_altivec_vcmpeqfp_p:
1428 case Intrinsic::ppc_altivec_vcmpequb_p:
1429 case Intrinsic::ppc_altivec_vcmpequh_p:
1430 case Intrinsic::ppc_altivec_vcmpequw_p:
1431 case Intrinsic::ppc_altivec_vcmpgefp_p:
1432 case Intrinsic::ppc_altivec_vcmpgtfp_p:
1433 case Intrinsic::ppc_altivec_vcmpgtsb_p:
1434 case Intrinsic::ppc_altivec_vcmpgtsh_p:
1435 case Intrinsic::ppc_altivec_vcmpgtsw_p:
1436 case Intrinsic::ppc_altivec_vcmpgtub_p:
1437 case Intrinsic::ppc_altivec_vcmpgtuh_p:
1438 case Intrinsic::ppc_altivec_vcmpgtuw_p:
1439 KnownZero = ~1U; // All bits but the low one are known to be zero.
1440 break;
1441 }
1442 }
1443 }
1444}
1445
1446
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001447/// getConstraintType - Given a constraint letter, return the type of
1448/// constraint it is for this target.
1449PPCTargetLowering::ConstraintType
1450PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1451 switch (ConstraintLetter) {
1452 default: break;
1453 case 'b':
1454 case 'r':
1455 case 'f':
1456 case 'v':
1457 case 'y':
1458 return C_RegisterClass;
1459 }
1460 return TargetLowering::getConstraintType(ConstraintLetter);
1461}
1462
1463
Chris Lattnerddc787d2006-01-31 19:20:21 +00001464std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001465getRegClassForInlineAsmConstraint(const std::string &Constraint,
1466 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001467 if (Constraint.size() == 1) {
1468 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1469 default: break; // Unknown constriant letter
1470 case 'b':
1471 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1472 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1473 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1474 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1475 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1476 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1477 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1478 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1479 0);
1480 case 'r':
1481 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1482 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1483 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1484 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1485 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1486 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1487 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1488 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1489 0);
1490 case 'f':
1491 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1492 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1493 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1494 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1495 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1496 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1497 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1498 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1499 0);
1500 case 'v':
1501 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1502 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1503 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1504 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1505 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1506 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1507 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1508 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1509 0);
1510 case 'y':
1511 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1512 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1513 0);
1514 }
1515 }
1516
Chris Lattner1efa40f2006-02-22 00:56:39 +00001517 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001518}
Chris Lattner763317d2006-02-07 00:47:13 +00001519
1520// isOperandValidForConstraint
1521bool PPCTargetLowering::
1522isOperandValidForConstraint(SDOperand Op, char Letter) {
1523 switch (Letter) {
1524 default: break;
1525 case 'I':
1526 case 'J':
1527 case 'K':
1528 case 'L':
1529 case 'M':
1530 case 'N':
1531 case 'O':
1532 case 'P': {
1533 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1534 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1535 switch (Letter) {
1536 default: assert(0 && "Unknown constraint letter!");
1537 case 'I': // "I" is a signed 16-bit constant.
1538 return (short)Value == (int)Value;
1539 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1540 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1541 return (short)Value == 0;
1542 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1543 return (Value >> 16) == 0;
1544 case 'M': // "M" is a constant that is greater than 31.
1545 return Value > 31;
1546 case 'N': // "N" is a positive constant that is an exact power of two.
1547 return (int)Value > 0 && isPowerOf2_32(Value);
1548 case 'O': // "O" is the constant zero.
1549 return Value == 0;
1550 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1551 return (short)-Value == (int)-Value;
1552 }
1553 break;
1554 }
1555 }
1556
1557 // Handle standard constraint letters.
1558 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1559}
Evan Chengc4c62572006-03-13 23:20:37 +00001560
1561/// isLegalAddressImmediate - Return true if the integer value can be used
1562/// as the offset of the target addressing mode.
1563bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1564 // PPC allows a sign-extended 16-bit immediate field.
1565 return (V > -(1 << 16) && V < (1 << 16)-1);
1566}