blob: 9dace121cd60c7146c81edbe85a3b0dfb519805f [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.
185 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
186
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);
195 }
196
Nate Begeman425a9692005-11-29 08:17:20 +0000197 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000198 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000199 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
200 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000201
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000202 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000203
Chris Lattnerb2177b92006-03-19 06:55:52 +0000204 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
205 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000206
207 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
208 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000209 }
210
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000211 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000212 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000213
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000214 // We have target-specific dag combine patterns for the following nodes:
215 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000216 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000217
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000218 computeRegisterProperties();
219}
220
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000221const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
222 switch (Opcode) {
223 default: return 0;
224 case PPCISD::FSEL: return "PPCISD::FSEL";
225 case PPCISD::FCFID: return "PPCISD::FCFID";
226 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
227 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000228 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000229 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
230 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000231 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000232 case PPCISD::Hi: return "PPCISD::Hi";
233 case PPCISD::Lo: return "PPCISD::Lo";
234 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
235 case PPCISD::SRL: return "PPCISD::SRL";
236 case PPCISD::SRA: return "PPCISD::SRA";
237 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000238 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
239 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000240 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000241 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000242 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000243 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000244 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000245 }
246}
247
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000248/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
249static bool isFloatingPointZero(SDOperand Op) {
250 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
251 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
252 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
253 // Maybe this has already been legalized into the constant pool?
254 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
255 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
256 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
257 }
258 return false;
259}
260
Chris Lattneref819f82006-03-20 06:33:01 +0000261
262/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
263/// specifies a splat of a single element that is suitable for input to
264/// VSPLTB/VSPLTH/VSPLTW.
265bool PPC::isSplatShuffleMask(SDNode *N) {
266 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000267
268 // We can only splat 8-bit, 16-bit, and 32-bit quantities.
269 if (N->getNumOperands() != 4 && N->getNumOperands() != 8 &&
270 N->getNumOperands() != 16)
271 return false;
272
Chris Lattner88a99ef2006-03-20 06:37:44 +0000273 // This is a splat operation if each element of the permute is the same, and
274 // if the value doesn't reference the second vector.
275 SDOperand Elt = N->getOperand(0);
276 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
277 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
278 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
279 "Invalid VECTOR_SHUFFLE mask!");
280 if (N->getOperand(i) != Elt) return false;
281 }
282
283 // Make sure it is a splat of the first vector operand.
284 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
Chris Lattneref819f82006-03-20 06:33:01 +0000285}
286
287/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
288/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
289unsigned PPC::getVSPLTImmediate(SDNode *N) {
290 assert(isSplatShuffleMask(N));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000291 return cast<ConstantSDNode>(N->getOperand(0))->getValue();
Chris Lattneref819f82006-03-20 06:33:01 +0000292}
293
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000294/// isVecSplatImm - Return true if this is a build_vector of constants which
295/// can be formed by using a vspltis[bhw] instruction. The ByteSize field
296/// indicates the number of bytes of each element [124] -> [bhw].
297bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) {
298 SDOperand OpVal(0, 0);
299 // Check to see if this buildvec has a single non-undef value in its elements.
300 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
301 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
302 if (OpVal.Val == 0)
303 OpVal = N->getOperand(i);
304 else if (OpVal != N->getOperand(i))
305 return false;
306 }
307
308 if (OpVal.Val == 0) return false; // All UNDEF: use implicit def.
309
Nate Begeman98e70cc2006-03-28 04:15:58 +0000310 unsigned ValSizeInBytes = 0;
311 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000312 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
313 Value = CN->getValue();
314 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
315 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
316 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
317 Value = FloatToBits(CN->getValue());
318 ValSizeInBytes = 4;
319 }
320
321 // If the splat value is larger than the element value, then we can never do
322 // this splat. The only case that we could fit the replicated bits into our
323 // immediate field for would be zero, and we prefer to use vxor for it.
324 if (ValSizeInBytes < ByteSize) return false;
325
326 // If the element value is larger than the splat value, cut it in half and
327 // check to see if the two halves are equal. Continue doing this until we
328 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
329 while (ValSizeInBytes > ByteSize) {
330 ValSizeInBytes >>= 1;
331
332 // If the top half equals the bottom half, we're still ok.
333 if (((Value >> (ValSizeInBytes*8)) & ((8 << ValSizeInBytes)-1)) !=
334 (Value & ((8 << ValSizeInBytes)-1)))
335 return false;
336 }
337
338 // Properly sign extend the value.
339 int ShAmt = (4-ByteSize)*8;
340 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
341
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000342 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000343 if (MaskVal == 0) return false;
344
345 if (Val) *Val = MaskVal;
346
347 // Finally, if this value fits in a 5 bit sext field, return true.
348 return ((MaskVal << (32-5)) >> (32-5)) == MaskVal;
349}
350
Chris Lattneref819f82006-03-20 06:33:01 +0000351
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000352/// LowerOperation - Provide custom lowering hooks for some operations.
353///
Nate Begeman21e463b2005-10-16 05:39:50 +0000354SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000355 switch (Op.getOpcode()) {
356 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000357 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000358 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000359 SDOperand Src = Op.getOperand(0);
360 if (Src.getValueType() == MVT::f32)
361 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
362
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000363 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000364 switch (Op.getValueType()) {
365 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
366 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000367 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000368 break;
369 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000370 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000371 break;
372 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000373
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000374 // Convert the FP value to an int value through memory.
375 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
376 if (Op.getValueType() == MVT::i32)
377 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
378 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000379 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000380 case ISD::SINT_TO_FP:
381 if (Op.getOperand(0).getValueType() == MVT::i64) {
382 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
383 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
384 if (Op.getValueType() == MVT::f32)
385 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
386 return FP;
387 } else {
388 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
389 "Unhandled SINT_TO_FP type in custom expander!");
390 // Since we only generate this in 64-bit mode, we can take advantage of
391 // 64-bit registers. In particular, sign extend the input value into the
392 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
393 // then lfd it and fcfid it.
394 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
395 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
396 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
397
398 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
399 Op.getOperand(0));
400
401 // STD the extended value into the stack slot.
402 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
403 DAG.getEntryNode(), Ext64, FIdx,
404 DAG.getSrcValue(NULL));
405 // Load the value as a double.
406 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
407
408 // FCFID it and return it.
409 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
410 if (Op.getValueType() == MVT::f32)
411 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
412 return FP;
413 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000414 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000415
Chris Lattnerf7605322005-08-31 21:09:52 +0000416 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000417 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000418 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
419 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
420 break;
421
422 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
423
424 // Cannot handle SETEQ/SETNE.
425 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
426
427 MVT::ValueType ResVT = Op.getValueType();
428 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
429 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
430 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000431
Chris Lattnerf7605322005-08-31 21:09:52 +0000432 // If the RHS of the comparison is a 0.0, we don't need to do the
433 // subtraction at all.
434 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000435 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000436 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000437 case ISD::SETULT:
438 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000439 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000440 case ISD::SETUGE:
441 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000442 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
443 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000444 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000445 case ISD::SETUGT:
446 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000447 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000448 case ISD::SETULE:
449 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000450 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
451 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000452 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000453 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000454 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000455
Chris Lattnereb255f22005-10-25 20:54:57 +0000456 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000457 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000458 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000459 case ISD::SETULT:
460 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000461 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
462 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
463 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
464 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000465 case ISD::SETUGE:
466 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000467 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
468 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
469 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
470 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000471 case ISD::SETUGT:
472 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000473 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
474 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
475 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
476 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000477 case ISD::SETULE:
478 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000479 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
480 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
481 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
482 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000483 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000484 break;
485 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000486 case ISD::SHL: {
487 assert(Op.getValueType() == MVT::i64 &&
488 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
489 // The generic code does a fine job expanding shift by a constant.
490 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
491
492 // Otherwise, expand into a bunch of logical ops. Note that these ops
493 // depend on the PPC behavior for oversized shift amounts.
494 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
495 DAG.getConstant(0, MVT::i32));
496 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
497 DAG.getConstant(1, MVT::i32));
498 SDOperand Amt = Op.getOperand(1);
499
500 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
501 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000502 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
503 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000504 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
505 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
506 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000507 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000508 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000509 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000510 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
511 }
512 case ISD::SRL: {
513 assert(Op.getValueType() == MVT::i64 &&
514 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
515 // The generic code does a fine job expanding shift by a constant.
516 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
517
518 // Otherwise, expand into a bunch of logical ops. Note that these ops
519 // depend on the PPC behavior for oversized shift amounts.
520 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
521 DAG.getConstant(0, MVT::i32));
522 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
523 DAG.getConstant(1, MVT::i32));
524 SDOperand Amt = Op.getOperand(1);
525
526 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
527 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000528 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
529 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000530 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
531 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
532 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000533 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000534 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000535 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000536 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
537 }
538 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000539 assert(Op.getValueType() == MVT::i64 &&
540 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
541 // The generic code does a fine job expanding shift by a constant.
542 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
543
544 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
545 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
546 DAG.getConstant(0, MVT::i32));
547 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
548 DAG.getConstant(1, MVT::i32));
549 SDOperand Amt = Op.getOperand(1);
550
551 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
552 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000553 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
554 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000555 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
556 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
557 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000558 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
559 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000560 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
561 Tmp4, Tmp6, ISD::SETLE);
562 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000563 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000564 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000565 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
566 Constant *C = CP->get();
567 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000568 SDOperand Zero = DAG.getConstant(0, MVT::i32);
569
Evan Cheng4c1aa862006-02-22 20:19:42 +0000570 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000571 // Generate non-pic code that has direct accesses to the constant pool.
572 // The address of the global is just (hi(&g)+lo(&g)).
573 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
574 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
575 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
576 }
577
578 // Only lower ConstantPool on Darwin.
579 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
580 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000581 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000582 // With PIC, the first instruction is actually "GR+hi(&G)".
583 Hi = DAG.getNode(ISD::ADD, MVT::i32,
584 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
585 }
586
587 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
588 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
589 return Lo;
590 }
Chris Lattner860e8862005-11-17 07:30:41 +0000591 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000592 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
593 GlobalValue *GV = GSDN->getGlobal();
594 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000595 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000596
Evan Cheng4c1aa862006-02-22 20:19:42 +0000597 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000598 // Generate non-pic code that has direct accesses to globals.
599 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000600 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
601 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
602 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
603 }
Chris Lattner860e8862005-11-17 07:30:41 +0000604
Chris Lattner1d05cb42005-11-17 18:55:48 +0000605 // Only lower GlobalAddress on Darwin.
606 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000607
Chris Lattner860e8862005-11-17 07:30:41 +0000608 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000609 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000610 // With PIC, the first instruction is actually "GR+hi(&G)".
611 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000612 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000613 }
614
615 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
616 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
617
Chris Lattner37dd6f12006-01-29 20:49:17 +0000618 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
619 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000620 return Lo;
621
622 // If the global is weak or external, we have to go through the lazy
623 // resolution stub.
624 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
625 }
Nate Begeman44775902006-01-31 08:17:29 +0000626 case ISD::SETCC: {
627 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000628
629 // If we're comparing for equality to zero, expose the fact that this is
630 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
631 // fold the new nodes.
632 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
633 if (C->isNullValue() && CC == ISD::SETEQ) {
634 MVT::ValueType VT = Op.getOperand(0).getValueType();
635 SDOperand Zext = Op.getOperand(0);
636 if (VT < MVT::i32) {
637 VT = MVT::i32;
638 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
639 }
640 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
641 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
642 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
643 DAG.getConstant(Log2b, getShiftAmountTy()));
644 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
645 }
646 // Leave comparisons against 0 and -1 alone for now, since they're usually
647 // optimized. FIXME: revisit this when we can custom lower all setcc
648 // optimizations.
649 if (C->isAllOnesValue() || C->isNullValue())
650 break;
651 }
652
653 // If we have an integer seteq/setne, turn it into a compare against zero
654 // by subtracting the rhs from the lhs, which is faster than setting a
655 // condition register, reading it back out, and masking the correct bit.
656 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
657 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
658 MVT::ValueType VT = Op.getValueType();
659 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
660 Op.getOperand(1));
661 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
662 }
Nate Begeman44775902006-01-31 08:17:29 +0000663 break;
664 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000665 case ISD::VASTART: {
666 // vastart just stores the address of the VarArgsFrameIndex slot into the
667 // memory location argument.
668 // FIXME: Replace MVT::i32 with PointerTy
669 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
670 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
671 Op.getOperand(1), Op.getOperand(2));
672 }
Nate Begemanee625572006-01-27 21:09:22 +0000673 case ISD::RET: {
674 SDOperand Copy;
675
676 switch(Op.getNumOperands()) {
677 default:
678 assert(0 && "Do not know how to return this many arguments!");
679 abort();
680 case 1:
681 return SDOperand(); // ret void is legal
682 case 2: {
683 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
684 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
685 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
686 SDOperand());
687 break;
688 }
689 case 3:
690 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
691 SDOperand());
692 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
693 break;
694 }
695 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
696 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000697 case ISD::SCALAR_TO_VECTOR: {
698 // Create a stack slot that is 16-byte aligned.
699 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
700 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
701 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
702
703 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000704 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
705 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner7f20b132006-03-28 01:43:22 +0000706 // Load it out.
707 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
Chris Lattnerb2177b92006-03-19 06:55:52 +0000708 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000709 case ISD::BUILD_VECTOR:
710 // If this is a case we can't handle, return null and let the default
711 // expansion code take care of it. If we CAN select this case, return Op.
712
713 // See if this is all zeros.
714 // FIXME: We should handle splat(-0.0), and other cases here.
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000715 if (ISD::isBuildVectorAllZeros(Op.Val))
Chris Lattner64b3a082006-03-24 07:48:08 +0000716 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000717
718 if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb
719 PPC::isVecSplatImm(Op.Val, 2) || // vspltish
720 PPC::isVecSplatImm(Op.Val, 4)) // vspltisw
721 return Op;
722
Chris Lattner64b3a082006-03-24 07:48:08 +0000723 return SDOperand();
724
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000725 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000726 SDOperand V1 = Op.getOperand(0);
727 SDOperand V2 = Op.getOperand(1);
728 SDOperand PermMask = Op.getOperand(2);
729
730 // Cases that are handled by instructions that take permute immediates
731 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
732 // selected by the instruction selector.
733 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
734 break;
735
736 // TODO: Handle more cases, and also handle cases that are cheaper to do as
737 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000738
739 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
740 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000741 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000742
743 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
744 // that it is in input element units, not in bytes. Convert now.
745 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
746 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
747
748 std::vector<SDOperand> ResultMask;
749 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
750 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
751
752 for (unsigned j = 0; j != BytesPerElement; ++j)
753 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
754 MVT::i8));
755 }
756
757 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
758 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
759 }
Chris Lattner48b61a72006-03-28 00:40:33 +0000760 case ISD::INTRINSIC_WO_CHAIN: {
Chris Lattnera17b1552006-03-31 05:13:27 +0000761 unsigned IntNo=cast<ConstantSDNode>(Op.getOperand(0))->getValue();
Chris Lattner6d92cad2006-03-26 10:06:40 +0000762
763 // If this is a lowered altivec predicate compare, CompareOpc is set to the
764 // opcode number of the comparison.
765 int CompareOpc = -1;
Chris Lattnera17b1552006-03-31 05:13:27 +0000766 bool isDot = false;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000767 switch (IntNo) {
768 default: return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattnera17b1552006-03-31 05:13:27 +0000769 // Comparison predicates.
770 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
771 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
772 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
773 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
774 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
775 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
776 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
777 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
778 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
779 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
780 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
781 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
782 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
783
784 // Normal Comparisons.
785 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
786 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
787 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
788 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
789 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
790 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
791 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
792 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
793 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
794 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
795 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
796 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
797 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000798 }
799
800 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
801
Chris Lattnera17b1552006-03-31 05:13:27 +0000802 // If this is a non-dot comparison, make the VCMP node.
803 if (!isDot)
804 return DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
805 Op.getOperand(1), Op.getOperand(2),
806 DAG.getConstant(CompareOpc, MVT::i32));
807
Chris Lattner6d92cad2006-03-26 10:06:40 +0000808 // Create the PPCISD altivec 'dot' comparison node.
809 std::vector<SDOperand> Ops;
810 std::vector<MVT::ValueType> VTs;
811 Ops.push_back(Op.getOperand(2)); // LHS
812 Ops.push_back(Op.getOperand(3)); // RHS
813 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
814 VTs.push_back(Op.getOperand(2).getValueType());
815 VTs.push_back(MVT::Flag);
816 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
817
818 // Now that we have the comparison, emit a copy from the CR to a GPR.
819 // This is flagged to the above dot comparison.
820 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
821 DAG.getRegister(PPC::CR6, MVT::i32),
822 CompNode.getValue(1));
823
824 // Unpack the result based on how the target uses it.
825 unsigned BitNo; // Bit # of CR6.
826 bool InvertBit; // Invert result?
827 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
828 default: // Can't happen, don't crash on invalid number though.
829 case 0: // Return the value of the EQ bit of CR6.
830 BitNo = 0; InvertBit = false;
831 break;
832 case 1: // Return the inverted value of the EQ bit of CR6.
833 BitNo = 0; InvertBit = true;
834 break;
835 case 2: // Return the value of the LT bit of CR6.
836 BitNo = 2; InvertBit = false;
837 break;
838 case 3: // Return the inverted value of the LT bit of CR6.
839 BitNo = 2; InvertBit = true;
840 break;
841 }
842
843 // Shift the bit into the low position.
844 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
845 DAG.getConstant(8-(3-BitNo), MVT::i32));
846 // Isolate the bit.
847 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
848 DAG.getConstant(1, MVT::i32));
849
850 // If we are supposed to, toggle the bit.
851 if (InvertBit)
852 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
853 DAG.getConstant(1, MVT::i32));
854 return Flags;
855 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000856 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000857 return SDOperand();
858}
859
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000860std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000861PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000862 //
863 // add beautiful description of PPC stack frame format, or at least some docs
864 //
865 MachineFunction &MF = DAG.getMachineFunction();
866 MachineFrameInfo *MFI = MF.getFrameInfo();
867 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000868 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000869 std::vector<SDOperand> ArgValues;
870
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000871 unsigned ArgOffset = 24;
872 unsigned GPR_remaining = 8;
873 unsigned FPR_remaining = 13;
874 unsigned GPR_idx = 0, FPR_idx = 0;
875 static const unsigned GPR[] = {
876 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
877 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
878 };
879 static const unsigned FPR[] = {
880 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
881 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
882 };
883
884 // Add DAG nodes to load the arguments... On entry to a function on PPC,
885 // the arguments start at offset 24, although they are likely to be passed
886 // in registers.
887 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
888 SDOperand newroot, argt;
889 unsigned ObjSize;
890 bool needsLoad = false;
891 bool ArgLive = !I->use_empty();
892 MVT::ValueType ObjectVT = getValueType(I->getType());
893
894 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000895 default: assert(0 && "Unhandled argument type!");
896 case MVT::i1:
897 case MVT::i8:
898 case MVT::i16:
899 case MVT::i32:
900 ObjSize = 4;
901 if (!ArgLive) break;
902 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000903 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000904 MF.addLiveIn(GPR[GPR_idx], VReg);
905 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000906 if (ObjectVT != MVT::i32) {
907 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
908 : ISD::AssertZext;
909 argt = DAG.getNode(AssertOp, MVT::i32, argt,
910 DAG.getValueType(ObjectVT));
911 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
912 }
Chris Lattner915fb302005-08-30 00:19:00 +0000913 } else {
914 needsLoad = true;
915 }
916 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000917 case MVT::i64:
918 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000919 if (!ArgLive) break;
920 if (GPR_remaining > 0) {
921 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000922 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000923 MF.addLiveIn(GPR[GPR_idx], VReg);
924 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000925 // If we have two or more remaining argument registers, then both halves
926 // of the i64 can be sourced from there. Otherwise, the lower half will
927 // have to come off the stack. This can happen when an i64 is preceded
928 // by 28 bytes of arguments.
929 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000930 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000931 MF.addLiveIn(GPR[GPR_idx+1], VReg);
932 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000933 } else {
934 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
935 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
936 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
937 DAG.getSrcValue(NULL));
938 }
939 // Build the outgoing arg thingy
940 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
941 newroot = argLo;
942 } else {
943 needsLoad = true;
944 }
945 break;
946 case MVT::f32:
947 case MVT::f64:
948 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000949 if (!ArgLive) {
950 if (FPR_remaining > 0) {
951 --FPR_remaining;
952 ++FPR_idx;
953 }
954 break;
955 }
Chris Lattner915fb302005-08-30 00:19:00 +0000956 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000957 unsigned VReg;
958 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000959 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000960 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000961 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000962 MF.addLiveIn(FPR[FPR_idx], VReg);
963 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000964 --FPR_remaining;
965 ++FPR_idx;
966 } else {
967 needsLoad = true;
968 }
969 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000970 }
971
972 // We need to load the argument to a virtual register if we determined above
973 // that we ran out of physical registers of the appropriate type
974 if (needsLoad) {
975 unsigned SubregOffset = 0;
976 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
977 if (ObjectVT == MVT::i16) SubregOffset = 2;
978 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
979 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
980 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
981 DAG.getConstant(SubregOffset, MVT::i32));
982 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
983 DAG.getSrcValue(NULL));
984 }
985
986 // Every 4 bytes of argument space consumes one of the GPRs available for
987 // argument passing.
988 if (GPR_remaining > 0) {
989 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
990 GPR_remaining -= delta;
991 GPR_idx += delta;
992 }
993 ArgOffset += ObjSize;
994 if (newroot.Val)
995 DAG.setRoot(newroot.getValue(1));
996
997 ArgValues.push_back(argt);
998 }
999
1000 // If the function takes variable number of arguments, make a frame index for
1001 // the start of the first vararg value... for expansion of llvm.va_start.
1002 if (F.isVarArg()) {
1003 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1004 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1005 // If this function is vararg, store any remaining integer argument regs
1006 // to their spots on the stack so that they may be loaded by deferencing the
1007 // result of va_next.
1008 std::vector<SDOperand> MemOps;
1009 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001010 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001011 MF.addLiveIn(GPR[GPR_idx], VReg);
1012 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001013 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1014 Val, FIN, DAG.getSrcValue(NULL));
1015 MemOps.push_back(Store);
1016 // Increment the address by four for the next argument to store
1017 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1018 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1019 }
Chris Lattner80720a92005-11-30 20:40:54 +00001020 if (!MemOps.empty()) {
1021 MemOps.push_back(DAG.getRoot());
1022 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1023 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001024 }
1025
1026 // Finally, inform the code generator which regs we return values in.
1027 switch (getValueType(F.getReturnType())) {
1028 default: assert(0 && "Unknown type!");
1029 case MVT::isVoid: break;
1030 case MVT::i1:
1031 case MVT::i8:
1032 case MVT::i16:
1033 case MVT::i32:
1034 MF.addLiveOut(PPC::R3);
1035 break;
1036 case MVT::i64:
1037 MF.addLiveOut(PPC::R3);
1038 MF.addLiveOut(PPC::R4);
1039 break;
1040 case MVT::f32:
1041 case MVT::f64:
1042 MF.addLiveOut(PPC::F1);
1043 break;
1044 }
1045
1046 return ArgValues;
1047}
1048
1049std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001050PPCTargetLowering::LowerCallTo(SDOperand Chain,
1051 const Type *RetTy, bool isVarArg,
1052 unsigned CallingConv, bool isTailCall,
1053 SDOperand Callee, ArgListTy &Args,
1054 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001055 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001056 // SelectExpr to use to put the arguments in the appropriate registers.
1057 std::vector<SDOperand> args_to_use;
1058
1059 // Count how many bytes are to be pushed on the stack, including the linkage
1060 // area, and parameter passing area.
1061 unsigned NumBytes = 24;
1062
1063 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001064 Chain = DAG.getCALLSEQ_START(Chain,
1065 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001066 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001067 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001068 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001069 default: assert(0 && "Unknown value type!");
1070 case MVT::i1:
1071 case MVT::i8:
1072 case MVT::i16:
1073 case MVT::i32:
1074 case MVT::f32:
1075 NumBytes += 4;
1076 break;
1077 case MVT::i64:
1078 case MVT::f64:
1079 NumBytes += 8;
1080 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001081 }
Chris Lattner915fb302005-08-30 00:19:00 +00001082 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001083
Chris Lattner915fb302005-08-30 00:19:00 +00001084 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1085 // plus 32 bytes of argument space in case any called code gets funky on us.
1086 // (Required by ABI to support var arg)
1087 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001088
1089 // Adjust the stack pointer for the new arguments...
1090 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001091 Chain = DAG.getCALLSEQ_START(Chain,
1092 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001093
1094 // Set up a copy of the stack pointer for use loading and storing any
1095 // arguments that may not fit in the registers available for argument
1096 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001097 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001098
1099 // Figure out which arguments are going to go in registers, and which in
1100 // memory. Also, if this is a vararg function, floating point operations
1101 // must be stored to our stack, and loaded into integer regs as well, if
1102 // any integer regs are available for argument passing.
1103 unsigned ArgOffset = 24;
1104 unsigned GPR_remaining = 8;
1105 unsigned FPR_remaining = 13;
1106
1107 std::vector<SDOperand> MemOps;
1108 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1109 // PtrOff will be used to store the current argument to the stack if a
1110 // register cannot be found for it.
1111 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1112 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1113 MVT::ValueType ArgVT = getValueType(Args[i].second);
1114
1115 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001116 default: assert(0 && "Unexpected ValueType for argument!");
1117 case MVT::i1:
1118 case MVT::i8:
1119 case MVT::i16:
1120 // Promote the integer to 32 bits. If the input type is signed use a
1121 // sign extend, otherwise use a zero extend.
1122 if (Args[i].second->isSigned())
1123 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1124 else
1125 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1126 // FALL THROUGH
1127 case MVT::i32:
1128 if (GPR_remaining > 0) {
1129 args_to_use.push_back(Args[i].first);
1130 --GPR_remaining;
1131 } else {
1132 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1133 Args[i].first, PtrOff,
1134 DAG.getSrcValue(NULL)));
1135 }
1136 ArgOffset += 4;
1137 break;
1138 case MVT::i64:
1139 // If we have one free GPR left, we can place the upper half of the i64
1140 // in it, and store the other half to the stack. If we have two or more
1141 // free GPRs, then we can pass both halves of the i64 in registers.
1142 if (GPR_remaining > 0) {
1143 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1144 Args[i].first, DAG.getConstant(1, MVT::i32));
1145 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1146 Args[i].first, DAG.getConstant(0, MVT::i32));
1147 args_to_use.push_back(Hi);
1148 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001149 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001150 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001151 --GPR_remaining;
1152 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001153 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1154 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001155 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001156 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001157 }
Chris Lattner915fb302005-08-30 00:19:00 +00001158 } else {
1159 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1160 Args[i].first, PtrOff,
1161 DAG.getSrcValue(NULL)));
1162 }
1163 ArgOffset += 8;
1164 break;
1165 case MVT::f32:
1166 case MVT::f64:
1167 if (FPR_remaining > 0) {
1168 args_to_use.push_back(Args[i].first);
1169 --FPR_remaining;
1170 if (isVarArg) {
1171 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1172 Args[i].first, PtrOff,
1173 DAG.getSrcValue(NULL));
1174 MemOps.push_back(Store);
1175 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001176 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001177 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1178 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001179 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001180 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001181 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001182 }
1183 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001184 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1185 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001186 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1187 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001188 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001189 args_to_use.push_back(Load);
1190 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001191 }
1192 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001193 // If we have any FPRs remaining, we may also have GPRs remaining.
1194 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1195 // GPRs.
1196 if (GPR_remaining > 0) {
1197 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1198 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001199 }
Chris Lattner915fb302005-08-30 00:19:00 +00001200 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1201 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1202 --GPR_remaining;
1203 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001204 }
Chris Lattner915fb302005-08-30 00:19:00 +00001205 } else {
1206 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1207 Args[i].first, PtrOff,
1208 DAG.getSrcValue(NULL)));
1209 }
1210 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1211 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001212 }
1213 }
1214 if (!MemOps.empty())
1215 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1216 }
1217
1218 std::vector<MVT::ValueType> RetVals;
1219 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001220 MVT::ValueType ActualRetTyVT = RetTyVT;
1221 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1222 ActualRetTyVT = MVT::i32; // Promote result to i32.
1223
Chris Lattnere00ebf02006-01-28 07:33:03 +00001224 if (RetTyVT == MVT::i64) {
1225 RetVals.push_back(MVT::i32);
1226 RetVals.push_back(MVT::i32);
1227 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001228 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001229 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001230 RetVals.push_back(MVT::Other);
1231
Chris Lattner2823b3e2005-11-17 05:56:14 +00001232 // If the callee is a GlobalAddress node (quite common, every direct call is)
1233 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1234 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1235 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1236
Chris Lattner281b55e2006-01-27 23:34:02 +00001237 std::vector<SDOperand> Ops;
1238 Ops.push_back(Chain);
1239 Ops.push_back(Callee);
1240 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1241 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001242 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001243 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1244 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001245 SDOperand RetVal = TheCall;
1246
1247 // If the result is a small value, add a note so that we keep track of the
1248 // information about whether it is sign or zero extended.
1249 if (RetTyVT != ActualRetTyVT) {
1250 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1251 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1252 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001253 } else if (RetTyVT == MVT::i64) {
1254 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001255 }
1256
1257 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001258}
1259
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001260MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001261PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1262 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001263 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001264 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1265 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001266 "Unexpected instr type to insert");
1267
1268 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1269 // control-flow pattern. The incoming instruction knows the destination vreg
1270 // to set, the condition code register to branch on, the true/false values to
1271 // select between, and a branch opcode to use.
1272 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1273 ilist<MachineBasicBlock>::iterator It = BB;
1274 ++It;
1275
1276 // thisMBB:
1277 // ...
1278 // TrueVal = ...
1279 // cmpTY ccX, r1, r2
1280 // bCC copy1MBB
1281 // fallthrough --> copy0MBB
1282 MachineBasicBlock *thisMBB = BB;
1283 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1284 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1285 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1286 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1287 MachineFunction *F = BB->getParent();
1288 F->getBasicBlockList().insert(It, copy0MBB);
1289 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001290 // Update machine-CFG edges by first adding all successors of the current
1291 // block to the new block which will contain the Phi node for the select.
1292 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1293 e = BB->succ_end(); i != e; ++i)
1294 sinkMBB->addSuccessor(*i);
1295 // Next, remove all successors of the current block, and add the true
1296 // and fallthrough blocks as its successors.
1297 while(!BB->succ_empty())
1298 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001299 BB->addSuccessor(copy0MBB);
1300 BB->addSuccessor(sinkMBB);
1301
1302 // copy0MBB:
1303 // %FalseValue = ...
1304 // # fallthrough to sinkMBB
1305 BB = copy0MBB;
1306
1307 // Update machine-CFG edges
1308 BB->addSuccessor(sinkMBB);
1309
1310 // sinkMBB:
1311 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1312 // ...
1313 BB = sinkMBB;
1314 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1315 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1316 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1317
1318 delete MI; // The pseudo instruction is gone now.
1319 return BB;
1320}
1321
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001322SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1323 DAGCombinerInfo &DCI) const {
1324 TargetMachine &TM = getTargetMachine();
1325 SelectionDAG &DAG = DCI.DAG;
1326 switch (N->getOpcode()) {
1327 default: break;
1328 case ISD::SINT_TO_FP:
1329 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001330 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1331 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1332 // We allow the src/dst to be either f32/f64, but the intermediate
1333 // type must be i64.
1334 if (N->getOperand(0).getValueType() == MVT::i64) {
1335 SDOperand Val = N->getOperand(0).getOperand(0);
1336 if (Val.getValueType() == MVT::f32) {
1337 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1338 DCI.AddToWorklist(Val.Val);
1339 }
1340
1341 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001342 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001343 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001344 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001345 if (N->getValueType(0) == MVT::f32) {
1346 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1347 DCI.AddToWorklist(Val.Val);
1348 }
1349 return Val;
1350 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1351 // If the intermediate type is i32, we can avoid the load/store here
1352 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001353 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001354 }
1355 }
1356 break;
Chris Lattner51269842006-03-01 05:50:56 +00001357 case ISD::STORE:
1358 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1359 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1360 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1361 N->getOperand(1).getValueType() == MVT::i32) {
1362 SDOperand Val = N->getOperand(1).getOperand(0);
1363 if (Val.getValueType() == MVT::f32) {
1364 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1365 DCI.AddToWorklist(Val.Val);
1366 }
1367 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1368 DCI.AddToWorklist(Val.Val);
1369
1370 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1371 N->getOperand(2), N->getOperand(3));
1372 DCI.AddToWorklist(Val.Val);
1373 return Val;
1374 }
1375 break;
Chris Lattner4468c222006-03-31 06:02:07 +00001376 case PPCISD::VCMP: {
1377 // If a VCMPo node already exists with exactly the same operands as this
1378 // node, use its result instead of this node (VCMPo computes both a CR6 and
1379 // a normal output).
1380 //
1381 if (!N->getOperand(0).hasOneUse() &&
1382 !N->getOperand(1).hasOneUse() &&
1383 !N->getOperand(2).hasOneUse()) {
1384
1385 // Scan all of the users of the LHS, looking for VCMPo's that match.
1386 SDNode *VCMPoNode = 0;
1387
1388 SDNode *LHSN = N->getOperand(0).Val;
1389 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
1390 UI != E; ++UI)
1391 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
1392 (*UI)->getOperand(1) == N->getOperand(1) &&
1393 (*UI)->getOperand(2) == N->getOperand(2) &&
1394 (*UI)->getOperand(0) == N->getOperand(0)) {
1395 VCMPoNode = *UI;
1396 break;
1397 }
1398
1399 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00001400 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00001401 return SDOperand(VCMPoNode, 0);
1402 }
1403 break;
1404 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001405 }
1406
1407 return SDOperand();
1408}
1409
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001410/// getConstraintType - Given a constraint letter, return the type of
1411/// constraint it is for this target.
1412PPCTargetLowering::ConstraintType
1413PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1414 switch (ConstraintLetter) {
1415 default: break;
1416 case 'b':
1417 case 'r':
1418 case 'f':
1419 case 'v':
1420 case 'y':
1421 return C_RegisterClass;
1422 }
1423 return TargetLowering::getConstraintType(ConstraintLetter);
1424}
1425
1426
Chris Lattnerddc787d2006-01-31 19:20:21 +00001427std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001428getRegClassForInlineAsmConstraint(const std::string &Constraint,
1429 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001430 if (Constraint.size() == 1) {
1431 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1432 default: break; // Unknown constriant letter
1433 case 'b':
1434 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1435 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1436 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1437 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1438 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1439 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1440 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1441 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1442 0);
1443 case 'r':
1444 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1445 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1446 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1447 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1448 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1449 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1450 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1451 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1452 0);
1453 case 'f':
1454 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1455 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1456 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1457 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1458 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1459 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1460 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1461 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1462 0);
1463 case 'v':
1464 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1465 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1466 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1467 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1468 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1469 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1470 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1471 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1472 0);
1473 case 'y':
1474 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1475 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1476 0);
1477 }
1478 }
1479
Chris Lattner1efa40f2006-02-22 00:56:39 +00001480 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001481}
Chris Lattner763317d2006-02-07 00:47:13 +00001482
1483// isOperandValidForConstraint
1484bool PPCTargetLowering::
1485isOperandValidForConstraint(SDOperand Op, char Letter) {
1486 switch (Letter) {
1487 default: break;
1488 case 'I':
1489 case 'J':
1490 case 'K':
1491 case 'L':
1492 case 'M':
1493 case 'N':
1494 case 'O':
1495 case 'P': {
1496 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1497 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1498 switch (Letter) {
1499 default: assert(0 && "Unknown constraint letter!");
1500 case 'I': // "I" is a signed 16-bit constant.
1501 return (short)Value == (int)Value;
1502 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1503 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1504 return (short)Value == 0;
1505 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1506 return (Value >> 16) == 0;
1507 case 'M': // "M" is a constant that is greater than 31.
1508 return Value > 31;
1509 case 'N': // "N" is a positive constant that is an exact power of two.
1510 return (int)Value > 0 && isPowerOf2_32(Value);
1511 case 'O': // "O" is the constant zero.
1512 return Value == 0;
1513 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1514 return (short)-Value == (int)-Value;
1515 }
1516 break;
1517 }
1518 }
1519
1520 // Handle standard constraint letters.
1521 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1522}
Evan Chengc4c62572006-03-13 23:20:37 +00001523
1524/// isLegalAddressImmediate - Return true if the integer value can be used
1525/// as the offset of the target addressing mode.
1526bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1527 // PPC allows a sign-extended 16-bit immediate field.
1528 return (V > -(1 << 16) && V < (1 << 16)-1);
1529}