blob: 1a930dcd6425c48ec77fd9b9662f2044c46f2c35 [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 Lattner01cae072006-04-03 23:55:43 +0000185 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
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
Chris Lattner01cae072006-04-03 23:55:43 +0000199 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
200
Nate Begeman425a9692005-11-29 08:17:20 +0000201 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000202 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000203 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
204 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000205
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000206 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000207
Chris Lattnerb2177b92006-03-19 06:55:52 +0000208 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
209 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000210
Chris Lattner541f91b2006-04-02 00:43:36 +0000211 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
212 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000213 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
214 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000215 }
216
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000217 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000218 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000219
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000220 // We have target-specific dag combine patterns for the following nodes:
221 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000222 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000223
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000224 computeRegisterProperties();
225}
226
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000227const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
228 switch (Opcode) {
229 default: return 0;
230 case PPCISD::FSEL: return "PPCISD::FSEL";
231 case PPCISD::FCFID: return "PPCISD::FCFID";
232 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
233 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000234 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000235 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
236 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000237 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000238 case PPCISD::Hi: return "PPCISD::Hi";
239 case PPCISD::Lo: return "PPCISD::Lo";
240 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
241 case PPCISD::SRL: return "PPCISD::SRL";
242 case PPCISD::SRA: return "PPCISD::SRA";
243 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000244 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
245 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000246 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000247 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000248 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000249 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000250 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000251 }
252}
253
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000254/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
255static bool isFloatingPointZero(SDOperand Op) {
256 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
257 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
258 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
259 // Maybe this has already been legalized into the constant pool?
260 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
261 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
262 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
263 }
264 return false;
265}
266
Chris Lattneref819f82006-03-20 06:33:01 +0000267
268/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
269/// specifies a splat of a single element that is suitable for input to
270/// VSPLTB/VSPLTH/VSPLTW.
271bool PPC::isSplatShuffleMask(SDNode *N) {
272 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000273
274 // We can only splat 8-bit, 16-bit, and 32-bit quantities.
275 if (N->getNumOperands() != 4 && N->getNumOperands() != 8 &&
276 N->getNumOperands() != 16)
277 return false;
278
Chris Lattner88a99ef2006-03-20 06:37:44 +0000279 // This is a splat operation if each element of the permute is the same, and
280 // if the value doesn't reference the second vector.
281 SDOperand Elt = N->getOperand(0);
282 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
283 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
284 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
285 "Invalid VECTOR_SHUFFLE mask!");
286 if (N->getOperand(i) != Elt) return false;
287 }
288
289 // Make sure it is a splat of the first vector operand.
290 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
Chris Lattneref819f82006-03-20 06:33:01 +0000291}
292
293/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
294/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
295unsigned PPC::getVSPLTImmediate(SDNode *N) {
296 assert(isSplatShuffleMask(N));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000297 return cast<ConstantSDNode>(N->getOperand(0))->getValue();
Chris Lattneref819f82006-03-20 06:33:01 +0000298}
299
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000300/// isVecSplatImm - Return true if this is a build_vector of constants which
301/// can be formed by using a vspltis[bhw] instruction. The ByteSize field
302/// indicates the number of bytes of each element [124] -> [bhw].
303bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) {
304 SDOperand OpVal(0, 0);
305 // Check to see if this buildvec has a single non-undef value in its elements.
306 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
307 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
308 if (OpVal.Val == 0)
309 OpVal = N->getOperand(i);
310 else if (OpVal != N->getOperand(i))
311 return false;
312 }
313
314 if (OpVal.Val == 0) return false; // All UNDEF: use implicit def.
315
Nate Begeman98e70cc2006-03-28 04:15:58 +0000316 unsigned ValSizeInBytes = 0;
317 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000318 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
319 Value = CN->getValue();
320 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
321 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
322 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
323 Value = FloatToBits(CN->getValue());
324 ValSizeInBytes = 4;
325 }
326
327 // If the splat value is larger than the element value, then we can never do
328 // this splat. The only case that we could fit the replicated bits into our
329 // immediate field for would be zero, and we prefer to use vxor for it.
330 if (ValSizeInBytes < ByteSize) return false;
331
332 // If the element value is larger than the splat value, cut it in half and
333 // check to see if the two halves are equal. Continue doing this until we
334 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
335 while (ValSizeInBytes > ByteSize) {
336 ValSizeInBytes >>= 1;
337
338 // If the top half equals the bottom half, we're still ok.
339 if (((Value >> (ValSizeInBytes*8)) & ((8 << ValSizeInBytes)-1)) !=
340 (Value & ((8 << ValSizeInBytes)-1)))
341 return false;
342 }
343
344 // Properly sign extend the value.
345 int ShAmt = (4-ByteSize)*8;
346 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
347
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000348 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000349 if (MaskVal == 0) return false;
350
351 if (Val) *Val = MaskVal;
352
353 // Finally, if this value fits in a 5 bit sext field, return true.
354 return ((MaskVal << (32-5)) >> (32-5)) == MaskVal;
355}
356
Chris Lattneref819f82006-03-20 06:33:01 +0000357
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000358/// LowerOperation - Provide custom lowering hooks for some operations.
359///
Nate Begeman21e463b2005-10-16 05:39:50 +0000360SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000361 switch (Op.getOpcode()) {
362 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000363 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000364 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000365 SDOperand Src = Op.getOperand(0);
366 if (Src.getValueType() == MVT::f32)
367 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
368
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000369 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000370 switch (Op.getValueType()) {
371 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
372 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000373 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000374 break;
375 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000376 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000377 break;
378 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000379
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000380 // Convert the FP value to an int value through memory.
381 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
382 if (Op.getValueType() == MVT::i32)
383 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
384 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000385 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000386 case ISD::SINT_TO_FP:
387 if (Op.getOperand(0).getValueType() == MVT::i64) {
388 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
389 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
390 if (Op.getValueType() == MVT::f32)
391 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
392 return FP;
393 } else {
394 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
395 "Unhandled SINT_TO_FP type in custom expander!");
396 // Since we only generate this in 64-bit mode, we can take advantage of
397 // 64-bit registers. In particular, sign extend the input value into the
398 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
399 // then lfd it and fcfid it.
400 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
401 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
402 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
403
404 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
405 Op.getOperand(0));
406
407 // STD the extended value into the stack slot.
408 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
409 DAG.getEntryNode(), Ext64, FIdx,
410 DAG.getSrcValue(NULL));
411 // Load the value as a double.
412 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
413
414 // FCFID it and return it.
415 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
416 if (Op.getValueType() == MVT::f32)
417 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
418 return FP;
419 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000420 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000421
Chris Lattnerf7605322005-08-31 21:09:52 +0000422 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000423 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000424 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
425 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
426 break;
427
428 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
429
430 // Cannot handle SETEQ/SETNE.
431 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
432
433 MVT::ValueType ResVT = Op.getValueType();
434 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
435 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
436 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000437
Chris Lattnerf7605322005-08-31 21:09:52 +0000438 // If the RHS of the comparison is a 0.0, we don't need to do the
439 // subtraction at all.
440 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000441 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000442 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000443 case ISD::SETULT:
444 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000445 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000446 case ISD::SETUGE:
447 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000448 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
449 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000450 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000451 case ISD::SETUGT:
452 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000453 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000454 case ISD::SETULE:
455 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000456 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
457 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000458 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000459 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000460 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000461
Chris Lattnereb255f22005-10-25 20:54:57 +0000462 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000463 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000464 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000465 case ISD::SETULT:
466 case ISD::SETLT:
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000471 case ISD::SETUGE:
472 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000473 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
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, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000477 case ISD::SETUGT:
478 case ISD::SETGT:
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000483 case ISD::SETULE:
484 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000485 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
486 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
487 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
488 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000489 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000490 break;
491 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000492 case ISD::SHL: {
493 assert(Op.getValueType() == MVT::i64 &&
494 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
495 // The generic code does a fine job expanding shift by a constant.
496 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
497
498 // Otherwise, expand into a bunch of logical ops. Note that these ops
499 // depend on the PPC behavior for oversized shift amounts.
500 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
501 DAG.getConstant(0, MVT::i32));
502 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
503 DAG.getConstant(1, MVT::i32));
504 SDOperand Amt = Op.getOperand(1);
505
506 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
507 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000508 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
509 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000510 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
511 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
512 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000513 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000514 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000515 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000516 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
517 }
518 case ISD::SRL: {
519 assert(Op.getValueType() == MVT::i64 &&
520 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
521 // The generic code does a fine job expanding shift by a constant.
522 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
523
524 // Otherwise, expand into a bunch of logical ops. Note that these ops
525 // depend on the PPC behavior for oversized shift amounts.
526 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
527 DAG.getConstant(0, MVT::i32));
528 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
529 DAG.getConstant(1, MVT::i32));
530 SDOperand Amt = Op.getOperand(1);
531
532 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
533 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000534 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
535 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000536 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
537 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
538 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000539 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000540 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000541 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000542 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
543 }
544 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000545 assert(Op.getValueType() == MVT::i64 &&
546 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
547 // The generic code does a fine job expanding shift by a constant.
548 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
549
550 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
551 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
552 DAG.getConstant(0, MVT::i32));
553 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
554 DAG.getConstant(1, MVT::i32));
555 SDOperand Amt = Op.getOperand(1);
556
557 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
558 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000559 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
560 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000561 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
562 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
563 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000564 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
565 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000566 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
567 Tmp4, Tmp6, ISD::SETLE);
568 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000569 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000570 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000571 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
572 Constant *C = CP->get();
573 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000574 SDOperand Zero = DAG.getConstant(0, MVT::i32);
575
Evan Cheng4c1aa862006-02-22 20:19:42 +0000576 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000577 // Generate non-pic code that has direct accesses to the constant pool.
578 // The address of the global is just (hi(&g)+lo(&g)).
579 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
580 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
581 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
582 }
583
584 // Only lower ConstantPool on Darwin.
585 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
586 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000587 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000588 // With PIC, the first instruction is actually "GR+hi(&G)".
589 Hi = DAG.getNode(ISD::ADD, MVT::i32,
590 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
591 }
592
593 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
594 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
595 return Lo;
596 }
Chris Lattner860e8862005-11-17 07:30:41 +0000597 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000598 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
599 GlobalValue *GV = GSDN->getGlobal();
600 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000601 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000602
Evan Cheng4c1aa862006-02-22 20:19:42 +0000603 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000604 // Generate non-pic code that has direct accesses to globals.
605 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000606 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
607 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
608 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
609 }
Chris Lattner860e8862005-11-17 07:30:41 +0000610
Chris Lattner1d05cb42005-11-17 18:55:48 +0000611 // Only lower GlobalAddress on Darwin.
612 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000613
Chris Lattner860e8862005-11-17 07:30:41 +0000614 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000615 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000616 // With PIC, the first instruction is actually "GR+hi(&G)".
617 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000618 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000619 }
620
621 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
622 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
623
Chris Lattner37dd6f12006-01-29 20:49:17 +0000624 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
625 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000626 return Lo;
627
628 // If the global is weak or external, we have to go through the lazy
629 // resolution stub.
630 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
631 }
Nate Begeman44775902006-01-31 08:17:29 +0000632 case ISD::SETCC: {
633 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000634
635 // If we're comparing for equality to zero, expose the fact that this is
636 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
637 // fold the new nodes.
638 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
639 if (C->isNullValue() && CC == ISD::SETEQ) {
640 MVT::ValueType VT = Op.getOperand(0).getValueType();
641 SDOperand Zext = Op.getOperand(0);
642 if (VT < MVT::i32) {
643 VT = MVT::i32;
644 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
645 }
646 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
647 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
648 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
649 DAG.getConstant(Log2b, getShiftAmountTy()));
650 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
651 }
652 // Leave comparisons against 0 and -1 alone for now, since they're usually
653 // optimized. FIXME: revisit this when we can custom lower all setcc
654 // optimizations.
655 if (C->isAllOnesValue() || C->isNullValue())
656 break;
657 }
658
659 // If we have an integer seteq/setne, turn it into a compare against zero
660 // by subtracting the rhs from the lhs, which is faster than setting a
661 // condition register, reading it back out, and masking the correct bit.
662 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
663 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
664 MVT::ValueType VT = Op.getValueType();
665 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
666 Op.getOperand(1));
667 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
668 }
Nate Begeman44775902006-01-31 08:17:29 +0000669 break;
670 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000671 case ISD::VASTART: {
672 // vastart just stores the address of the VarArgsFrameIndex slot into the
673 // memory location argument.
674 // FIXME: Replace MVT::i32 with PointerTy
675 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
676 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
677 Op.getOperand(1), Op.getOperand(2));
678 }
Nate Begemanee625572006-01-27 21:09:22 +0000679 case ISD::RET: {
680 SDOperand Copy;
681
682 switch(Op.getNumOperands()) {
683 default:
684 assert(0 && "Do not know how to return this many arguments!");
685 abort();
686 case 1:
687 return SDOperand(); // ret void is legal
688 case 2: {
689 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
690 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
691 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
692 SDOperand());
693 break;
694 }
695 case 3:
696 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
697 SDOperand());
698 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
699 break;
700 }
701 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
702 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000703 case ISD::SCALAR_TO_VECTOR: {
704 // Create a stack slot that is 16-byte aligned.
705 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
706 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
707 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
708
709 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000710 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
711 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner7f20b132006-03-28 01:43:22 +0000712 // Load it out.
713 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
Chris Lattnerb2177b92006-03-19 06:55:52 +0000714 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000715 case ISD::BUILD_VECTOR:
716 // If this is a case we can't handle, return null and let the default
717 // expansion code take care of it. If we CAN select this case, return Op.
718
719 // See if this is all zeros.
720 // FIXME: We should handle splat(-0.0), and other cases here.
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000721 if (ISD::isBuildVectorAllZeros(Op.Val))
Chris Lattner64b3a082006-03-24 07:48:08 +0000722 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000723
724 if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb
725 PPC::isVecSplatImm(Op.Val, 2) || // vspltish
726 PPC::isVecSplatImm(Op.Val, 4)) // vspltisw
727 return Op;
728
Chris Lattner64b3a082006-03-24 07:48:08 +0000729 return SDOperand();
730
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000731 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000732 SDOperand V1 = Op.getOperand(0);
733 SDOperand V2 = Op.getOperand(1);
734 SDOperand PermMask = Op.getOperand(2);
735
736 // Cases that are handled by instructions that take permute immediates
737 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
738 // selected by the instruction selector.
739 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
740 break;
741
742 // TODO: Handle more cases, and also handle cases that are cheaper to do as
743 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000744
745 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
746 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000747 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000748
749 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
750 // that it is in input element units, not in bytes. Convert now.
751 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
752 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
753
754 std::vector<SDOperand> ResultMask;
755 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
756 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
757
758 for (unsigned j = 0; j != BytesPerElement; ++j)
759 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
760 MVT::i8));
761 }
762
763 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
764 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
765 }
Chris Lattner48b61a72006-03-28 00:40:33 +0000766 case ISD::INTRINSIC_WO_CHAIN: {
Chris Lattnerbbe77de2006-04-02 06:26:07 +0000767 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
Chris Lattner6d92cad2006-03-26 10:06:40 +0000768
769 // If this is a lowered altivec predicate compare, CompareOpc is set to the
770 // opcode number of the comparison.
771 int CompareOpc = -1;
Chris Lattnera17b1552006-03-31 05:13:27 +0000772 bool isDot = false;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000773 switch (IntNo) {
774 default: return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattnera17b1552006-03-31 05:13:27 +0000775 // Comparison predicates.
776 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
777 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
778 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
779 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
780 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
781 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
782 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
783 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
784 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
785 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
786 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
787 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
788 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
789
790 // Normal Comparisons.
791 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
792 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
793 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
794 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
795 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
796 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
797 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
798 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
799 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
800 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
801 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
802 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
803 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000804 }
805
806 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
807
Chris Lattnera17b1552006-03-31 05:13:27 +0000808 // If this is a non-dot comparison, make the VCMP node.
809 if (!isDot)
810 return DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
811 Op.getOperand(1), Op.getOperand(2),
812 DAG.getConstant(CompareOpc, MVT::i32));
813
Chris Lattner6d92cad2006-03-26 10:06:40 +0000814 // Create the PPCISD altivec 'dot' comparison node.
815 std::vector<SDOperand> Ops;
816 std::vector<MVT::ValueType> VTs;
817 Ops.push_back(Op.getOperand(2)); // LHS
818 Ops.push_back(Op.getOperand(3)); // RHS
819 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
820 VTs.push_back(Op.getOperand(2).getValueType());
821 VTs.push_back(MVT::Flag);
822 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
823
824 // Now that we have the comparison, emit a copy from the CR to a GPR.
825 // This is flagged to the above dot comparison.
826 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
827 DAG.getRegister(PPC::CR6, MVT::i32),
828 CompNode.getValue(1));
829
830 // Unpack the result based on how the target uses it.
831 unsigned BitNo; // Bit # of CR6.
832 bool InvertBit; // Invert result?
833 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
834 default: // Can't happen, don't crash on invalid number though.
835 case 0: // Return the value of the EQ bit of CR6.
836 BitNo = 0; InvertBit = false;
837 break;
838 case 1: // Return the inverted value of the EQ bit of CR6.
839 BitNo = 0; InvertBit = true;
840 break;
841 case 2: // Return the value of the LT bit of CR6.
842 BitNo = 2; InvertBit = false;
843 break;
844 case 3: // Return the inverted value of the LT bit of CR6.
845 BitNo = 2; InvertBit = true;
846 break;
847 }
848
849 // Shift the bit into the low position.
850 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
851 DAG.getConstant(8-(3-BitNo), MVT::i32));
852 // Isolate the bit.
853 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
854 DAG.getConstant(1, MVT::i32));
855
856 // If we are supposed to, toggle the bit.
857 if (InvertBit)
858 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
859 DAG.getConstant(1, MVT::i32));
860 return Flags;
861 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000862 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000863 return SDOperand();
864}
865
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000866std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000867PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000868 //
869 // add beautiful description of PPC stack frame format, or at least some docs
870 //
871 MachineFunction &MF = DAG.getMachineFunction();
872 MachineFrameInfo *MFI = MF.getFrameInfo();
873 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000874 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000875 std::vector<SDOperand> ArgValues;
876
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000877 unsigned ArgOffset = 24;
878 unsigned GPR_remaining = 8;
879 unsigned FPR_remaining = 13;
880 unsigned GPR_idx = 0, FPR_idx = 0;
881 static const unsigned GPR[] = {
882 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
883 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
884 };
885 static const unsigned FPR[] = {
886 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
887 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
888 };
889
890 // Add DAG nodes to load the arguments... On entry to a function on PPC,
891 // the arguments start at offset 24, although they are likely to be passed
892 // in registers.
893 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
894 SDOperand newroot, argt;
895 unsigned ObjSize;
896 bool needsLoad = false;
897 bool ArgLive = !I->use_empty();
898 MVT::ValueType ObjectVT = getValueType(I->getType());
899
900 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000901 default: assert(0 && "Unhandled argument type!");
902 case MVT::i1:
903 case MVT::i8:
904 case MVT::i16:
905 case MVT::i32:
906 ObjSize = 4;
907 if (!ArgLive) break;
908 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000909 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000910 MF.addLiveIn(GPR[GPR_idx], VReg);
911 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000912 if (ObjectVT != MVT::i32) {
913 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
914 : ISD::AssertZext;
915 argt = DAG.getNode(AssertOp, MVT::i32, argt,
916 DAG.getValueType(ObjectVT));
917 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
918 }
Chris Lattner915fb302005-08-30 00:19:00 +0000919 } else {
920 needsLoad = true;
921 }
922 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000923 case MVT::i64:
924 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000925 if (!ArgLive) break;
926 if (GPR_remaining > 0) {
927 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000928 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000929 MF.addLiveIn(GPR[GPR_idx], VReg);
930 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000931 // If we have two or more remaining argument registers, then both halves
932 // of the i64 can be sourced from there. Otherwise, the lower half will
933 // have to come off the stack. This can happen when an i64 is preceded
934 // by 28 bytes of arguments.
935 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000936 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000937 MF.addLiveIn(GPR[GPR_idx+1], VReg);
938 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000939 } else {
940 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
941 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
942 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
943 DAG.getSrcValue(NULL));
944 }
945 // Build the outgoing arg thingy
946 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
947 newroot = argLo;
948 } else {
949 needsLoad = true;
950 }
951 break;
952 case MVT::f32:
953 case MVT::f64:
954 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000955 if (!ArgLive) {
956 if (FPR_remaining > 0) {
957 --FPR_remaining;
958 ++FPR_idx;
959 }
960 break;
961 }
Chris Lattner915fb302005-08-30 00:19:00 +0000962 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000963 unsigned VReg;
964 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000965 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000966 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000967 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000968 MF.addLiveIn(FPR[FPR_idx], VReg);
969 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000970 --FPR_remaining;
971 ++FPR_idx;
972 } else {
973 needsLoad = true;
974 }
975 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000976 }
977
978 // We need to load the argument to a virtual register if we determined above
979 // that we ran out of physical registers of the appropriate type
980 if (needsLoad) {
981 unsigned SubregOffset = 0;
982 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
983 if (ObjectVT == MVT::i16) SubregOffset = 2;
984 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
985 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
986 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
987 DAG.getConstant(SubregOffset, MVT::i32));
988 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
989 DAG.getSrcValue(NULL));
990 }
991
992 // Every 4 bytes of argument space consumes one of the GPRs available for
993 // argument passing.
994 if (GPR_remaining > 0) {
995 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
996 GPR_remaining -= delta;
997 GPR_idx += delta;
998 }
999 ArgOffset += ObjSize;
1000 if (newroot.Val)
1001 DAG.setRoot(newroot.getValue(1));
1002
1003 ArgValues.push_back(argt);
1004 }
1005
1006 // If the function takes variable number of arguments, make a frame index for
1007 // the start of the first vararg value... for expansion of llvm.va_start.
1008 if (F.isVarArg()) {
1009 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1010 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1011 // If this function is vararg, store any remaining integer argument regs
1012 // to their spots on the stack so that they may be loaded by deferencing the
1013 // result of va_next.
1014 std::vector<SDOperand> MemOps;
1015 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001016 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001017 MF.addLiveIn(GPR[GPR_idx], VReg);
1018 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001019 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1020 Val, FIN, DAG.getSrcValue(NULL));
1021 MemOps.push_back(Store);
1022 // Increment the address by four for the next argument to store
1023 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1024 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1025 }
Chris Lattner80720a92005-11-30 20:40:54 +00001026 if (!MemOps.empty()) {
1027 MemOps.push_back(DAG.getRoot());
1028 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1029 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001030 }
1031
1032 // Finally, inform the code generator which regs we return values in.
1033 switch (getValueType(F.getReturnType())) {
1034 default: assert(0 && "Unknown type!");
1035 case MVT::isVoid: break;
1036 case MVT::i1:
1037 case MVT::i8:
1038 case MVT::i16:
1039 case MVT::i32:
1040 MF.addLiveOut(PPC::R3);
1041 break;
1042 case MVT::i64:
1043 MF.addLiveOut(PPC::R3);
1044 MF.addLiveOut(PPC::R4);
1045 break;
1046 case MVT::f32:
1047 case MVT::f64:
1048 MF.addLiveOut(PPC::F1);
1049 break;
1050 }
1051
1052 return ArgValues;
1053}
1054
1055std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001056PPCTargetLowering::LowerCallTo(SDOperand Chain,
1057 const Type *RetTy, bool isVarArg,
1058 unsigned CallingConv, bool isTailCall,
1059 SDOperand Callee, ArgListTy &Args,
1060 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001061 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001062 // SelectExpr to use to put the arguments in the appropriate registers.
1063 std::vector<SDOperand> args_to_use;
1064
1065 // Count how many bytes are to be pushed on the stack, including the linkage
1066 // area, and parameter passing area.
1067 unsigned NumBytes = 24;
1068
1069 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001070 Chain = DAG.getCALLSEQ_START(Chain,
1071 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001072 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001073 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001074 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001075 default: assert(0 && "Unknown value type!");
1076 case MVT::i1:
1077 case MVT::i8:
1078 case MVT::i16:
1079 case MVT::i32:
1080 case MVT::f32:
1081 NumBytes += 4;
1082 break;
1083 case MVT::i64:
1084 case MVT::f64:
1085 NumBytes += 8;
1086 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001087 }
Chris Lattner915fb302005-08-30 00:19:00 +00001088 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001089
Chris Lattner915fb302005-08-30 00:19:00 +00001090 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1091 // plus 32 bytes of argument space in case any called code gets funky on us.
1092 // (Required by ABI to support var arg)
1093 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001094
1095 // Adjust the stack pointer for the new arguments...
1096 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001097 Chain = DAG.getCALLSEQ_START(Chain,
1098 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001099
1100 // Set up a copy of the stack pointer for use loading and storing any
1101 // arguments that may not fit in the registers available for argument
1102 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001103 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001104
1105 // Figure out which arguments are going to go in registers, and which in
1106 // memory. Also, if this is a vararg function, floating point operations
1107 // must be stored to our stack, and loaded into integer regs as well, if
1108 // any integer regs are available for argument passing.
1109 unsigned ArgOffset = 24;
1110 unsigned GPR_remaining = 8;
1111 unsigned FPR_remaining = 13;
1112
1113 std::vector<SDOperand> MemOps;
1114 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1115 // PtrOff will be used to store the current argument to the stack if a
1116 // register cannot be found for it.
1117 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1118 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1119 MVT::ValueType ArgVT = getValueType(Args[i].second);
1120
1121 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001122 default: assert(0 && "Unexpected ValueType for argument!");
1123 case MVT::i1:
1124 case MVT::i8:
1125 case MVT::i16:
1126 // Promote the integer to 32 bits. If the input type is signed use a
1127 // sign extend, otherwise use a zero extend.
1128 if (Args[i].second->isSigned())
1129 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1130 else
1131 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1132 // FALL THROUGH
1133 case MVT::i32:
1134 if (GPR_remaining > 0) {
1135 args_to_use.push_back(Args[i].first);
1136 --GPR_remaining;
1137 } else {
1138 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1139 Args[i].first, PtrOff,
1140 DAG.getSrcValue(NULL)));
1141 }
1142 ArgOffset += 4;
1143 break;
1144 case MVT::i64:
1145 // If we have one free GPR left, we can place the upper half of the i64
1146 // in it, and store the other half to the stack. If we have two or more
1147 // free GPRs, then we can pass both halves of the i64 in registers.
1148 if (GPR_remaining > 0) {
1149 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1150 Args[i].first, DAG.getConstant(1, MVT::i32));
1151 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1152 Args[i].first, DAG.getConstant(0, MVT::i32));
1153 args_to_use.push_back(Hi);
1154 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001155 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001156 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001157 --GPR_remaining;
1158 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001159 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1160 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001161 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001162 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001163 }
Chris Lattner915fb302005-08-30 00:19:00 +00001164 } else {
1165 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1166 Args[i].first, PtrOff,
1167 DAG.getSrcValue(NULL)));
1168 }
1169 ArgOffset += 8;
1170 break;
1171 case MVT::f32:
1172 case MVT::f64:
1173 if (FPR_remaining > 0) {
1174 args_to_use.push_back(Args[i].first);
1175 --FPR_remaining;
1176 if (isVarArg) {
1177 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1178 Args[i].first, PtrOff,
1179 DAG.getSrcValue(NULL));
1180 MemOps.push_back(Store);
1181 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001182 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001183 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1184 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001185 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001186 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001187 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001188 }
1189 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001190 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1191 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001192 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1193 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001194 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001195 args_to_use.push_back(Load);
1196 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001197 }
1198 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001199 // If we have any FPRs remaining, we may also have GPRs remaining.
1200 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1201 // GPRs.
1202 if (GPR_remaining > 0) {
1203 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1204 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001205 }
Chris Lattner915fb302005-08-30 00:19:00 +00001206 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1207 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1208 --GPR_remaining;
1209 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001210 }
Chris Lattner915fb302005-08-30 00:19:00 +00001211 } else {
1212 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1213 Args[i].first, PtrOff,
1214 DAG.getSrcValue(NULL)));
1215 }
1216 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1217 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001218 }
1219 }
1220 if (!MemOps.empty())
1221 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1222 }
1223
1224 std::vector<MVT::ValueType> RetVals;
1225 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001226 MVT::ValueType ActualRetTyVT = RetTyVT;
1227 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1228 ActualRetTyVT = MVT::i32; // Promote result to i32.
1229
Chris Lattnere00ebf02006-01-28 07:33:03 +00001230 if (RetTyVT == MVT::i64) {
1231 RetVals.push_back(MVT::i32);
1232 RetVals.push_back(MVT::i32);
1233 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001234 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001235 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001236 RetVals.push_back(MVT::Other);
1237
Chris Lattner2823b3e2005-11-17 05:56:14 +00001238 // If the callee is a GlobalAddress node (quite common, every direct call is)
1239 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1240 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1241 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1242
Chris Lattner281b55e2006-01-27 23:34:02 +00001243 std::vector<SDOperand> Ops;
1244 Ops.push_back(Chain);
1245 Ops.push_back(Callee);
1246 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1247 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001248 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001249 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1250 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001251 SDOperand RetVal = TheCall;
1252
1253 // If the result is a small value, add a note so that we keep track of the
1254 // information about whether it is sign or zero extended.
1255 if (RetTyVT != ActualRetTyVT) {
1256 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1257 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1258 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001259 } else if (RetTyVT == MVT::i64) {
1260 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001261 }
1262
1263 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001264}
1265
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001266MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001267PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1268 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001269 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001270 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1271 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001272 "Unexpected instr type to insert");
1273
1274 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1275 // control-flow pattern. The incoming instruction knows the destination vreg
1276 // to set, the condition code register to branch on, the true/false values to
1277 // select between, and a branch opcode to use.
1278 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1279 ilist<MachineBasicBlock>::iterator It = BB;
1280 ++It;
1281
1282 // thisMBB:
1283 // ...
1284 // TrueVal = ...
1285 // cmpTY ccX, r1, r2
1286 // bCC copy1MBB
1287 // fallthrough --> copy0MBB
1288 MachineBasicBlock *thisMBB = BB;
1289 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1290 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1291 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1292 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1293 MachineFunction *F = BB->getParent();
1294 F->getBasicBlockList().insert(It, copy0MBB);
1295 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001296 // Update machine-CFG edges by first adding all successors of the current
1297 // block to the new block which will contain the Phi node for the select.
1298 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1299 e = BB->succ_end(); i != e; ++i)
1300 sinkMBB->addSuccessor(*i);
1301 // Next, remove all successors of the current block, and add the true
1302 // and fallthrough blocks as its successors.
1303 while(!BB->succ_empty())
1304 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001305 BB->addSuccessor(copy0MBB);
1306 BB->addSuccessor(sinkMBB);
1307
1308 // copy0MBB:
1309 // %FalseValue = ...
1310 // # fallthrough to sinkMBB
1311 BB = copy0MBB;
1312
1313 // Update machine-CFG edges
1314 BB->addSuccessor(sinkMBB);
1315
1316 // sinkMBB:
1317 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1318 // ...
1319 BB = sinkMBB;
1320 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1321 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1322 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1323
1324 delete MI; // The pseudo instruction is gone now.
1325 return BB;
1326}
1327
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001328SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1329 DAGCombinerInfo &DCI) const {
1330 TargetMachine &TM = getTargetMachine();
1331 SelectionDAG &DAG = DCI.DAG;
1332 switch (N->getOpcode()) {
1333 default: break;
1334 case ISD::SINT_TO_FP:
1335 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001336 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1337 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1338 // We allow the src/dst to be either f32/f64, but the intermediate
1339 // type must be i64.
1340 if (N->getOperand(0).getValueType() == MVT::i64) {
1341 SDOperand Val = N->getOperand(0).getOperand(0);
1342 if (Val.getValueType() == MVT::f32) {
1343 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1344 DCI.AddToWorklist(Val.Val);
1345 }
1346
1347 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001348 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001349 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001350 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001351 if (N->getValueType(0) == MVT::f32) {
1352 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1353 DCI.AddToWorklist(Val.Val);
1354 }
1355 return Val;
1356 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1357 // If the intermediate type is i32, we can avoid the load/store here
1358 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001359 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001360 }
1361 }
1362 break;
Chris Lattner51269842006-03-01 05:50:56 +00001363 case ISD::STORE:
1364 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1365 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1366 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1367 N->getOperand(1).getValueType() == MVT::i32) {
1368 SDOperand Val = N->getOperand(1).getOperand(0);
1369 if (Val.getValueType() == MVT::f32) {
1370 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1371 DCI.AddToWorklist(Val.Val);
1372 }
1373 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1374 DCI.AddToWorklist(Val.Val);
1375
1376 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1377 N->getOperand(2), N->getOperand(3));
1378 DCI.AddToWorklist(Val.Val);
1379 return Val;
1380 }
1381 break;
Chris Lattner4468c222006-03-31 06:02:07 +00001382 case PPCISD::VCMP: {
1383 // If a VCMPo node already exists with exactly the same operands as this
1384 // node, use its result instead of this node (VCMPo computes both a CR6 and
1385 // a normal output).
1386 //
1387 if (!N->getOperand(0).hasOneUse() &&
1388 !N->getOperand(1).hasOneUse() &&
1389 !N->getOperand(2).hasOneUse()) {
1390
1391 // Scan all of the users of the LHS, looking for VCMPo's that match.
1392 SDNode *VCMPoNode = 0;
1393
1394 SDNode *LHSN = N->getOperand(0).Val;
1395 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
1396 UI != E; ++UI)
1397 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
1398 (*UI)->getOperand(1) == N->getOperand(1) &&
1399 (*UI)->getOperand(2) == N->getOperand(2) &&
1400 (*UI)->getOperand(0) == N->getOperand(0)) {
1401 VCMPoNode = *UI;
1402 break;
1403 }
1404
1405 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00001406 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00001407 return SDOperand(VCMPoNode, 0);
1408 }
1409 break;
1410 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001411 }
1412
1413 return SDOperand();
1414}
1415
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001416void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1417 uint64_t Mask,
1418 uint64_t &KnownZero,
1419 uint64_t &KnownOne,
1420 unsigned Depth) const {
1421 KnownZero = 0;
1422 KnownOne = 0;
1423 switch (Op.getOpcode()) {
1424 default: break;
1425 case ISD::INTRINSIC_WO_CHAIN: {
1426 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
1427 default: break;
1428 case Intrinsic::ppc_altivec_vcmpbfp_p:
1429 case Intrinsic::ppc_altivec_vcmpeqfp_p:
1430 case Intrinsic::ppc_altivec_vcmpequb_p:
1431 case Intrinsic::ppc_altivec_vcmpequh_p:
1432 case Intrinsic::ppc_altivec_vcmpequw_p:
1433 case Intrinsic::ppc_altivec_vcmpgefp_p:
1434 case Intrinsic::ppc_altivec_vcmpgtfp_p:
1435 case Intrinsic::ppc_altivec_vcmpgtsb_p:
1436 case Intrinsic::ppc_altivec_vcmpgtsh_p:
1437 case Intrinsic::ppc_altivec_vcmpgtsw_p:
1438 case Intrinsic::ppc_altivec_vcmpgtub_p:
1439 case Intrinsic::ppc_altivec_vcmpgtuh_p:
1440 case Intrinsic::ppc_altivec_vcmpgtuw_p:
1441 KnownZero = ~1U; // All bits but the low one are known to be zero.
1442 break;
1443 }
1444 }
1445 }
1446}
1447
1448
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001449/// getConstraintType - Given a constraint letter, return the type of
1450/// constraint it is for this target.
1451PPCTargetLowering::ConstraintType
1452PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1453 switch (ConstraintLetter) {
1454 default: break;
1455 case 'b':
1456 case 'r':
1457 case 'f':
1458 case 'v':
1459 case 'y':
1460 return C_RegisterClass;
1461 }
1462 return TargetLowering::getConstraintType(ConstraintLetter);
1463}
1464
1465
Chris Lattnerddc787d2006-01-31 19:20:21 +00001466std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001467getRegClassForInlineAsmConstraint(const std::string &Constraint,
1468 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001469 if (Constraint.size() == 1) {
1470 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1471 default: break; // Unknown constriant letter
1472 case 'b':
1473 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1474 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1475 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1476 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1477 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1478 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1479 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1480 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1481 0);
1482 case 'r':
1483 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1484 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1485 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1486 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1487 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1488 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1489 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1490 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1491 0);
1492 case 'f':
1493 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1494 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1495 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1496 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1497 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1498 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1499 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1500 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1501 0);
1502 case 'v':
1503 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1504 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1505 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1506 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1507 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1508 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1509 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1510 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1511 0);
1512 case 'y':
1513 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1514 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1515 0);
1516 }
1517 }
1518
Chris Lattner1efa40f2006-02-22 00:56:39 +00001519 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001520}
Chris Lattner763317d2006-02-07 00:47:13 +00001521
1522// isOperandValidForConstraint
1523bool PPCTargetLowering::
1524isOperandValidForConstraint(SDOperand Op, char Letter) {
1525 switch (Letter) {
1526 default: break;
1527 case 'I':
1528 case 'J':
1529 case 'K':
1530 case 'L':
1531 case 'M':
1532 case 'N':
1533 case 'O':
1534 case 'P': {
1535 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1536 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1537 switch (Letter) {
1538 default: assert(0 && "Unknown constraint letter!");
1539 case 'I': // "I" is a signed 16-bit constant.
1540 return (short)Value == (int)Value;
1541 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1542 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1543 return (short)Value == 0;
1544 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1545 return (Value >> 16) == 0;
1546 case 'M': // "M" is a constant that is greater than 31.
1547 return Value > 31;
1548 case 'N': // "N" is a positive constant that is an exact power of two.
1549 return (int)Value > 0 && isPowerOf2_32(Value);
1550 case 'O': // "O" is the constant zero.
1551 return Value == 0;
1552 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1553 return (short)-Value == (int)-Value;
1554 }
1555 break;
1556 }
1557 }
1558
1559 // Handle standard constraint letters.
1560 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1561}
Evan Chengc4c62572006-03-13 23:20:37 +00001562
1563/// isLegalAddressImmediate - Return true if the integer value can be used
1564/// as the offset of the target addressing mode.
1565bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1566 // PPC allows a sign-extended 16-bit immediate field.
1567 return (V > -(1 << 16) && V < (1 << 16)-1);
1568}