blob: 5722594c61d654f804076090195b94d3b9fcaed9 [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
Chris Lattner7ff7e672006-04-04 17:25:31 +0000184 // We promote all shuffles to v16i8.
185 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Promote);
186 AddPromotedToType(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, MVT::v16i8);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000187
188 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
189 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
190 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
191 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
192 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
193 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
194 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
195 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattner01cae072006-04-03 23:55:43 +0000196
197 setOperationAction(ISD::SCALAR_TO_VECTOR, (MVT::ValueType)VT, Expand);
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000198 }
199
Chris Lattner7ff7e672006-04-04 17:25:31 +0000200 // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
201 // with merges, splats, etc.
202 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
203
Nate Begeman425a9692005-11-29 08:17:20 +0000204 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000205 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000206 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
207 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000208
Chris Lattnere3fea5a2006-03-31 19:52:36 +0000209 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000210
Chris Lattnerb2177b92006-03-19 06:55:52 +0000211 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
212 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000213
Chris Lattner541f91b2006-04-02 00:43:36 +0000214 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
215 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000216 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
217 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000218 }
219
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000220 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000221 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000222
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000223 // We have target-specific dag combine patterns for the following nodes:
224 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000225 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000226
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000227 computeRegisterProperties();
228}
229
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000230const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
231 switch (Opcode) {
232 default: return 0;
233 case PPCISD::FSEL: return "PPCISD::FSEL";
234 case PPCISD::FCFID: return "PPCISD::FCFID";
235 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
236 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000237 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000238 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
239 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000240 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000241 case PPCISD::Hi: return "PPCISD::Hi";
242 case PPCISD::Lo: return "PPCISD::Lo";
243 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
244 case PPCISD::SRL: return "PPCISD::SRL";
245 case PPCISD::SRA: return "PPCISD::SRA";
246 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000247 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
248 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000249 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000250 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000251 case PPCISD::MFCR: return "PPCISD::MFCR";
Chris Lattnera17b1552006-03-31 05:13:27 +0000252 case PPCISD::VCMP: return "PPCISD::VCMP";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000253 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000254 }
255}
256
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000257/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
258static bool isFloatingPointZero(SDOperand Op) {
259 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
260 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
261 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
262 // Maybe this has already been legalized into the constant pool?
263 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
264 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
265 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
266 }
267 return false;
268}
269
Chris Lattnerddb739e2006-04-06 17:23:16 +0000270/// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return
271/// true if Op is undef or if it matches the specified value.
272static bool isConstantOrUndef(SDOperand Op, unsigned Val) {
273 return Op.getOpcode() == ISD::UNDEF ||
274 cast<ConstantSDNode>(Op)->getValue() == Val;
275}
276
277/// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
278/// VPKUHUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000279bool PPC::isVPKUHUMShuffleMask(SDNode *N, bool isUnary) {
280 if (!isUnary) {
281 for (unsigned i = 0; i != 16; ++i)
282 if (!isConstantOrUndef(N->getOperand(i), i*2+1))
283 return false;
284 } else {
285 for (unsigned i = 0; i != 8; ++i)
286 if (!isConstantOrUndef(N->getOperand(i), i*2+1) ||
287 !isConstantOrUndef(N->getOperand(i+8), i*2+1))
288 return false;
289 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000290 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000291}
292
293/// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
294/// VPKUWUM instruction.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000295bool PPC::isVPKUWUMShuffleMask(SDNode *N, bool isUnary) {
296 if (!isUnary) {
297 for (unsigned i = 0; i != 16; i += 2)
298 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
299 !isConstantOrUndef(N->getOperand(i+1), i*2+3))
300 return false;
301 } else {
302 for (unsigned i = 0; i != 8; i += 2)
303 if (!isConstantOrUndef(N->getOperand(i ), i*2+2) ||
304 !isConstantOrUndef(N->getOperand(i+1), i*2+3) ||
305 !isConstantOrUndef(N->getOperand(i+8), i*2+2) ||
306 !isConstantOrUndef(N->getOperand(i+9), i*2+3))
307 return false;
308 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000309 return true;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000310}
311
Chris Lattnercaad1632006-04-06 22:02:42 +0000312/// isVMerge - Common function, used to match vmrg* shuffles.
313///
314static bool isVMerge(SDNode *N, unsigned UnitSize,
315 unsigned LHSStart, unsigned RHSStart) {
Chris Lattner116cc482006-04-06 21:11:54 +0000316 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
317 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
318 assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
319 "Unsupported merge size!");
320
321 for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units
322 for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit
323 if (!isConstantOrUndef(N->getOperand(i*UnitSize*2+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000324 LHSStart+j+i*UnitSize) ||
Chris Lattner116cc482006-04-06 21:11:54 +0000325 !isConstantOrUndef(N->getOperand(i*UnitSize*2+UnitSize+j),
Chris Lattnercaad1632006-04-06 22:02:42 +0000326 RHSStart+j+i*UnitSize))
Chris Lattner116cc482006-04-06 21:11:54 +0000327 return false;
328 }
Chris Lattnercaad1632006-04-06 22:02:42 +0000329 return true;
330}
331
332/// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
333/// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
334bool PPC::isVMRGLShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
335 if (!isUnary)
336 return isVMerge(N, UnitSize, 8, 24);
337 return isVMerge(N, UnitSize, 8, 8);
Chris Lattner116cc482006-04-06 21:11:54 +0000338}
339
340/// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
341/// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
Chris Lattnercaad1632006-04-06 22:02:42 +0000342bool PPC::isVMRGHShuffleMask(SDNode *N, unsigned UnitSize, bool isUnary) {
343 if (!isUnary)
344 return isVMerge(N, UnitSize, 0, 16);
345 return isVMerge(N, UnitSize, 0, 0);
Chris Lattner116cc482006-04-06 21:11:54 +0000346}
347
348
Chris Lattnerd0608e12006-04-06 18:26:28 +0000349/// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
350/// amount, otherwise return -1.
Chris Lattnerf24380e2006-04-06 22:28:36 +0000351int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
Chris Lattner116cc482006-04-06 21:11:54 +0000352 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
353 N->getNumOperands() == 16 && "PPC only supports shuffles by bytes!");
Chris Lattnerd0608e12006-04-06 18:26:28 +0000354 // Find the first non-undef value in the shuffle mask.
355 unsigned i;
356 for (i = 0; i != 16 && N->getOperand(i).getOpcode() == ISD::UNDEF; ++i)
357 /*search*/;
358
359 if (i == 16) return -1; // all undef.
360
361 // Otherwise, check to see if the rest of the elements are consequtively
362 // numbered from this value.
363 unsigned ShiftAmt = cast<ConstantSDNode>(N->getOperand(i))->getValue();
364 if (ShiftAmt < i) return -1;
365 ShiftAmt -= i;
Chris Lattnerddb739e2006-04-06 17:23:16 +0000366
Chris Lattnerf24380e2006-04-06 22:28:36 +0000367 if (!isUnary) {
368 // Check the rest of the elements to see if they are consequtive.
369 for (++i; i != 16; ++i)
370 if (!isConstantOrUndef(N->getOperand(i), ShiftAmt+i))
371 return -1;
372 } else {
373 // Check the rest of the elements to see if they are consequtive.
374 for (++i; i != 16; ++i)
375 if (!isConstantOrUndef(N->getOperand(i), (ShiftAmt+i) & 15))
376 return -1;
377 }
Chris Lattnerd0608e12006-04-06 18:26:28 +0000378
379 return ShiftAmt;
380}
Chris Lattneref819f82006-03-20 06:33:01 +0000381
382/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
383/// specifies a splat of a single element that is suitable for input to
384/// VSPLTB/VSPLTH/VSPLTW.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000385bool PPC::isSplatShuffleMask(SDNode *N, unsigned EltSize) {
386 assert(N->getOpcode() == ISD::BUILD_VECTOR &&
387 N->getNumOperands() == 16 &&
388 (EltSize == 1 || EltSize == 2 || EltSize == 4));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000389
Chris Lattner88a99ef2006-03-20 06:37:44 +0000390 // This is a splat operation if each element of the permute is the same, and
391 // if the value doesn't reference the second vector.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000392 unsigned ElementBase = 0;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000393 SDOperand Elt = N->getOperand(0);
Chris Lattner7ff7e672006-04-04 17:25:31 +0000394 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt))
395 ElementBase = EltV->getValue();
396 else
397 return false; // FIXME: Handle UNDEF elements too!
398
399 if (cast<ConstantSDNode>(Elt)->getValue() >= 16)
400 return false;
401
402 // Check that they are consequtive.
403 for (unsigned i = 1; i != EltSize; ++i) {
404 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
405 cast<ConstantSDNode>(N->getOperand(i))->getValue() != i+ElementBase)
406 return false;
407 }
408
Chris Lattner88a99ef2006-03-20 06:37:44 +0000409 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000410 for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
Chris Lattner88a99ef2006-03-20 06:37:44 +0000411 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
412 "Invalid VECTOR_SHUFFLE mask!");
Chris Lattner7ff7e672006-04-04 17:25:31 +0000413 for (unsigned j = 0; j != EltSize; ++j)
414 if (N->getOperand(i+j) != N->getOperand(j))
415 return false;
Chris Lattner88a99ef2006-03-20 06:37:44 +0000416 }
417
Chris Lattner7ff7e672006-04-04 17:25:31 +0000418 return true;
Chris Lattneref819f82006-03-20 06:33:01 +0000419}
420
421/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
422/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
Chris Lattner7ff7e672006-04-04 17:25:31 +0000423unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
424 assert(isSplatShuffleMask(N, EltSize));
425 return cast<ConstantSDNode>(N->getOperand(0))->getValue() / EltSize;
Chris Lattneref819f82006-03-20 06:33:01 +0000426}
427
Chris Lattner140a58f2006-04-08 06:46:53 +0000428/// get_VSPLI_elt - If this is a build_vector of constants which can be formed
429/// by using a vspltis[bhw] instruction of the specified element size, return
430/// the constant being splatted. The ByteSize field indicates the number of
431/// bytes of each element [124] -> [bhw].
432SDOperand PPC::get_VSPLI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000433 SDOperand OpVal(0, 0);
Chris Lattner79d9a882006-04-08 07:14:26 +0000434
435 // If ByteSize of the splat is bigger than the element size of the
436 // build_vector, then we have a case where we are checking for a splat where
437 // multiple elements of the buildvector are folded together into a single
438 // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
439 unsigned EltSize = 16/N->getNumOperands();
440 if (EltSize < ByteSize) {
441 unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval.
442 SDOperand UniquedVals[4];
443 assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
444
445 // See if all of the elements in the buildvector agree across.
446 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
447 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
448 // If the element isn't a constant, bail fully out.
449 if (!isa<ConstantSDNode>(N->getOperand(i))) return SDOperand();
450
451
452 if (UniquedVals[i&(Multiple-1)].Val == 0)
453 UniquedVals[i&(Multiple-1)] = N->getOperand(i);
454 else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
455 return SDOperand(); // no match.
456 }
457
458 // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
459 // either constant or undef values that are identical for each chunk. See
460 // if these chunks can form into a larger vspltis*.
461
462 // Check to see if all of the leading entries are either 0 or -1. If
463 // neither, then this won't fit into the immediate field.
464 bool LeadingZero = true;
465 bool LeadingOnes = true;
466 for (unsigned i = 0; i != Multiple-1; ++i) {
467 if (UniquedVals[i].Val == 0) continue; // Must have been undefs.
468
469 LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
470 LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
471 }
472 // Finally, check the least significant entry.
473 if (LeadingZero) {
474 if (UniquedVals[Multiple-1].Val == 0)
475 return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef
476 int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getValue();
477 if (Val < 16)
478 return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4)
479 }
480 if (LeadingOnes) {
481 if (UniquedVals[Multiple-1].Val == 0)
482 return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef
483 int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSignExtended();
484 if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2)
485 return DAG.getTargetConstant(Val, MVT::i32);
486 }
487
488 return SDOperand();
489 }
490
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000491 // Check to see if this buildvec has a single non-undef value in its elements.
492 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
493 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
494 if (OpVal.Val == 0)
495 OpVal = N->getOperand(i);
496 else if (OpVal != N->getOperand(i))
Chris Lattner140a58f2006-04-08 06:46:53 +0000497 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000498 }
499
Chris Lattner140a58f2006-04-08 06:46:53 +0000500 if (OpVal.Val == 0) return SDOperand(); // All UNDEF: use implicit def.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000501
Nate Begeman98e70cc2006-03-28 04:15:58 +0000502 unsigned ValSizeInBytes = 0;
503 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000504 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
505 Value = CN->getValue();
506 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
507 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
508 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
509 Value = FloatToBits(CN->getValue());
510 ValSizeInBytes = 4;
511 }
512
513 // If the splat value is larger than the element value, then we can never do
514 // this splat. The only case that we could fit the replicated bits into our
515 // immediate field for would be zero, and we prefer to use vxor for it.
Chris Lattner140a58f2006-04-08 06:46:53 +0000516 if (ValSizeInBytes < ByteSize) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000517
518 // If the element value is larger than the splat value, cut it in half and
519 // check to see if the two halves are equal. Continue doing this until we
520 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
521 while (ValSizeInBytes > ByteSize) {
522 ValSizeInBytes >>= 1;
523
524 // If the top half equals the bottom half, we're still ok.
Chris Lattner9b42bdd2006-04-05 17:39:25 +0000525 if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
526 (Value & ((1 << (8*ValSizeInBytes))-1)))
Chris Lattner140a58f2006-04-08 06:46:53 +0000527 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000528 }
529
530 // Properly sign extend the value.
531 int ShAmt = (4-ByteSize)*8;
532 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
533
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000534 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner140a58f2006-04-08 06:46:53 +0000535 if (MaskVal == 0) return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000536
Chris Lattner140a58f2006-04-08 06:46:53 +0000537 // Finally, if this value fits in a 5 bit sext field, return it
538 if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
539 return DAG.getTargetConstant(MaskVal, MVT::i32);
540 return SDOperand();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000541}
542
Chris Lattneref819f82006-03-20 06:33:01 +0000543
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000544/// LowerOperation - Provide custom lowering hooks for some operations.
545///
Nate Begeman21e463b2005-10-16 05:39:50 +0000546SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000547 switch (Op.getOpcode()) {
548 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000549 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000550 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000551 SDOperand Src = Op.getOperand(0);
552 if (Src.getValueType() == MVT::f32)
553 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
554
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000555 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000556 switch (Op.getValueType()) {
557 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
558 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000559 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000560 break;
561 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000562 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000563 break;
564 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000565
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000566 // Convert the FP value to an int value through memory.
567 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
568 if (Op.getValueType() == MVT::i32)
569 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
570 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000571 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000572 case ISD::SINT_TO_FP:
573 if (Op.getOperand(0).getValueType() == MVT::i64) {
574 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
575 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
576 if (Op.getValueType() == MVT::f32)
577 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
578 return FP;
579 } else {
580 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
581 "Unhandled SINT_TO_FP type in custom expander!");
582 // Since we only generate this in 64-bit mode, we can take advantage of
583 // 64-bit registers. In particular, sign extend the input value into the
584 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
585 // then lfd it and fcfid it.
586 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
587 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
588 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
589
590 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
591 Op.getOperand(0));
592
593 // STD the extended value into the stack slot.
594 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
595 DAG.getEntryNode(), Ext64, FIdx,
596 DAG.getSrcValue(NULL));
597 // Load the value as a double.
598 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
599
600 // FCFID it and return it.
601 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
602 if (Op.getValueType() == MVT::f32)
603 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
604 return FP;
605 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000606 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000607
Chris Lattnerf7605322005-08-31 21:09:52 +0000608 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000609 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000610 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
611 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
612 break;
613
614 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
615
616 // Cannot handle SETEQ/SETNE.
617 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
618
619 MVT::ValueType ResVT = Op.getValueType();
620 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
621 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
622 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000623
Chris Lattnerf7605322005-08-31 21:09:52 +0000624 // If the RHS of the comparison is a 0.0, we don't need to do the
625 // subtraction at all.
626 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000627 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000628 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000629 case ISD::SETULT:
630 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000631 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000632 case ISD::SETUGE:
633 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000634 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
635 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000636 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000637 case ISD::SETUGT:
638 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000639 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000640 case ISD::SETULE:
641 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000642 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
643 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000644 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000645 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000646 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000647
Chris Lattnereb255f22005-10-25 20:54:57 +0000648 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000649 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000650 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000651 case ISD::SETULT:
652 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000653 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
654 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
655 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
656 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000657 case ISD::SETUGE:
658 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000659 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
660 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
661 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
662 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000663 case ISD::SETUGT:
664 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000665 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
666 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
667 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
668 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000669 case ISD::SETULE:
670 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000671 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
672 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
673 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
674 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000675 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000676 break;
677 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000678 case ISD::SHL: {
679 assert(Op.getValueType() == MVT::i64 &&
680 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
681 // The generic code does a fine job expanding shift by a constant.
682 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
683
684 // Otherwise, expand into a bunch of logical ops. Note that these ops
685 // depend on the PPC behavior for oversized shift amounts.
686 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
687 DAG.getConstant(0, MVT::i32));
688 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
689 DAG.getConstant(1, MVT::i32));
690 SDOperand Amt = Op.getOperand(1);
691
692 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
693 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000694 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
695 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000696 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
697 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
698 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000699 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000700 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000701 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000702 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
703 }
704 case ISD::SRL: {
705 assert(Op.getValueType() == MVT::i64 &&
706 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
707 // The generic code does a fine job expanding shift by a constant.
708 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
709
710 // Otherwise, expand into a bunch of logical ops. Note that these ops
711 // depend on the PPC behavior for oversized shift amounts.
712 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
713 DAG.getConstant(0, MVT::i32));
714 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
715 DAG.getConstant(1, MVT::i32));
716 SDOperand Amt = Op.getOperand(1);
717
718 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
719 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000720 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
721 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000722 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
723 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
724 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000725 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000726 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000727 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000728 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
729 }
730 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000731 assert(Op.getValueType() == MVT::i64 &&
732 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
733 // The generic code does a fine job expanding shift by a constant.
734 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
735
736 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
737 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
738 DAG.getConstant(0, MVT::i32));
739 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
740 DAG.getConstant(1, MVT::i32));
741 SDOperand Amt = Op.getOperand(1);
742
743 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
744 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000745 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
746 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000747 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
748 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
749 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000750 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
751 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000752 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
753 Tmp4, Tmp6, ISD::SETLE);
754 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000755 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000756 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000757 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
758 Constant *C = CP->get();
759 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000760 SDOperand Zero = DAG.getConstant(0, MVT::i32);
761
Evan Cheng4c1aa862006-02-22 20:19:42 +0000762 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000763 // Generate non-pic code that has direct accesses to the constant pool.
764 // The address of the global is just (hi(&g)+lo(&g)).
765 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
766 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
767 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
768 }
769
770 // Only lower ConstantPool on Darwin.
771 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
772 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000773 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000774 // With PIC, the first instruction is actually "GR+hi(&G)".
775 Hi = DAG.getNode(ISD::ADD, MVT::i32,
776 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
777 }
778
779 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
780 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
781 return Lo;
782 }
Chris Lattner860e8862005-11-17 07:30:41 +0000783 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000784 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
785 GlobalValue *GV = GSDN->getGlobal();
786 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000787 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000788
Evan Cheng4c1aa862006-02-22 20:19:42 +0000789 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000790 // Generate non-pic code that has direct accesses to globals.
791 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000792 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
793 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
794 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
795 }
Chris Lattner860e8862005-11-17 07:30:41 +0000796
Chris Lattner1d05cb42005-11-17 18:55:48 +0000797 // Only lower GlobalAddress on Darwin.
798 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000799
Chris Lattner860e8862005-11-17 07:30:41 +0000800 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000801 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000802 // With PIC, the first instruction is actually "GR+hi(&G)".
803 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000804 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000805 }
806
807 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
808 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
809
Chris Lattner37dd6f12006-01-29 20:49:17 +0000810 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
811 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000812 return Lo;
813
814 // If the global is weak or external, we have to go through the lazy
815 // resolution stub.
816 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
817 }
Nate Begeman44775902006-01-31 08:17:29 +0000818 case ISD::SETCC: {
819 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000820
821 // If we're comparing for equality to zero, expose the fact that this is
822 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
823 // fold the new nodes.
824 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
825 if (C->isNullValue() && CC == ISD::SETEQ) {
826 MVT::ValueType VT = Op.getOperand(0).getValueType();
827 SDOperand Zext = Op.getOperand(0);
828 if (VT < MVT::i32) {
829 VT = MVT::i32;
830 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
831 }
832 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
833 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
834 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
835 DAG.getConstant(Log2b, getShiftAmountTy()));
836 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
837 }
838 // Leave comparisons against 0 and -1 alone for now, since they're usually
839 // optimized. FIXME: revisit this when we can custom lower all setcc
840 // optimizations.
841 if (C->isAllOnesValue() || C->isNullValue())
842 break;
843 }
844
845 // If we have an integer seteq/setne, turn it into a compare against zero
846 // by subtracting the rhs from the lhs, which is faster than setting a
847 // condition register, reading it back out, and masking the correct bit.
848 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
849 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
850 MVT::ValueType VT = Op.getValueType();
851 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
852 Op.getOperand(1));
853 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
854 }
Nate Begeman44775902006-01-31 08:17:29 +0000855 break;
856 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000857 case ISD::VASTART: {
858 // vastart just stores the address of the VarArgsFrameIndex slot into the
859 // memory location argument.
860 // FIXME: Replace MVT::i32 with PointerTy
861 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
862 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
863 Op.getOperand(1), Op.getOperand(2));
864 }
Nate Begemanee625572006-01-27 21:09:22 +0000865 case ISD::RET: {
866 SDOperand Copy;
867
868 switch(Op.getNumOperands()) {
869 default:
870 assert(0 && "Do not know how to return this many arguments!");
871 abort();
872 case 1:
873 return SDOperand(); // ret void is legal
874 case 2: {
875 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
876 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
877 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
878 SDOperand());
879 break;
880 }
881 case 3:
882 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
883 SDOperand());
884 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
885 break;
886 }
887 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
888 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000889 case ISD::SCALAR_TO_VECTOR: {
890 // Create a stack slot that is 16-byte aligned.
891 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
892 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
893 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
894
895 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000896 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
897 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner7f20b132006-03-28 01:43:22 +0000898 // Load it out.
899 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
Chris Lattnerb2177b92006-03-19 06:55:52 +0000900 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000901 case ISD::BUILD_VECTOR:
902 // If this is a case we can't handle, return null and let the default
903 // expansion code take care of it. If we CAN select this case, return Op.
904
905 // See if this is all zeros.
906 // FIXME: We should handle splat(-0.0), and other cases here.
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000907 if (ISD::isBuildVectorAllZeros(Op.Val))
Chris Lattner64b3a082006-03-24 07:48:08 +0000908 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000909
Chris Lattner140a58f2006-04-08 06:46:53 +0000910 if (PPC::get_VSPLI_elt(Op.Val, 1, DAG).Val || // vspltisb
911 PPC::get_VSPLI_elt(Op.Val, 2, DAG).Val || // vspltish
912 PPC::get_VSPLI_elt(Op.Val, 4, DAG).Val) // vspltisw
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000913 return Op;
914
Chris Lattner64b3a082006-03-24 07:48:08 +0000915 return SDOperand();
916
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000917 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000918 SDOperand V1 = Op.getOperand(0);
919 SDOperand V2 = Op.getOperand(1);
920 SDOperand PermMask = Op.getOperand(2);
921
922 // Cases that are handled by instructions that take permute immediates
923 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
924 // selected by the instruction selector.
Chris Lattnercaad1632006-04-06 22:02:42 +0000925 if (V2.getOpcode() == ISD::UNDEF) {
926 if (PPC::isSplatShuffleMask(PermMask.Val, 1) ||
927 PPC::isSplatShuffleMask(PermMask.Val, 2) ||
928 PPC::isSplatShuffleMask(PermMask.Val, 4) ||
Chris Lattnerf24380e2006-04-06 22:28:36 +0000929 PPC::isVPKUWUMShuffleMask(PermMask.Val, true) ||
930 PPC::isVPKUHUMShuffleMask(PermMask.Val, true) ||
931 PPC::isVSLDOIShuffleMask(PermMask.Val, true) != -1 ||
Chris Lattnercaad1632006-04-06 22:02:42 +0000932 PPC::isVMRGLShuffleMask(PermMask.Val, 1, true) ||
933 PPC::isVMRGLShuffleMask(PermMask.Val, 2, true) ||
934 PPC::isVMRGLShuffleMask(PermMask.Val, 4, true) ||
935 PPC::isVMRGHShuffleMask(PermMask.Val, 1, true) ||
936 PPC::isVMRGHShuffleMask(PermMask.Val, 2, true) ||
937 PPC::isVMRGHShuffleMask(PermMask.Val, 4, true)) {
938 return Op;
939 }
940 }
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000941
Chris Lattnerf24380e2006-04-06 22:28:36 +0000942 // Altivec has a variety of "shuffle immediates" that take two vector inputs
943 // and produce a fixed permutation. If any of these match, do not lower to
944 // VPERM.
945 if (PPC::isVPKUWUMShuffleMask(PermMask.Val, false) ||
946 PPC::isVPKUHUMShuffleMask(PermMask.Val, false) ||
947 PPC::isVSLDOIShuffleMask(PermMask.Val, false) != -1 ||
Chris Lattnercaad1632006-04-06 22:02:42 +0000948 PPC::isVMRGLShuffleMask(PermMask.Val, 1, false) ||
949 PPC::isVMRGLShuffleMask(PermMask.Val, 2, false) ||
950 PPC::isVMRGLShuffleMask(PermMask.Val, 4, false) ||
951 PPC::isVMRGHShuffleMask(PermMask.Val, 1, false) ||
952 PPC::isVMRGHShuffleMask(PermMask.Val, 2, false) ||
953 PPC::isVMRGHShuffleMask(PermMask.Val, 4, false))
Chris Lattnerddb739e2006-04-06 17:23:16 +0000954 return Op;
955
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000956 // TODO: Handle more cases, and also handle cases that are cheaper to do as
957 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000958
959 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
960 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000961 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000962
963 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
964 // that it is in input element units, not in bytes. Convert now.
965 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
966 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
967
968 std::vector<SDOperand> ResultMask;
969 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
970 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
971
972 for (unsigned j = 0; j != BytesPerElement; ++j)
973 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
974 MVT::i8));
975 }
976
977 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
978 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
979 }
Chris Lattner48b61a72006-03-28 00:40:33 +0000980 case ISD::INTRINSIC_WO_CHAIN: {
Chris Lattnerbbe77de2006-04-02 06:26:07 +0000981 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
Chris Lattner6d92cad2006-03-26 10:06:40 +0000982
983 // If this is a lowered altivec predicate compare, CompareOpc is set to the
984 // opcode number of the comparison.
985 int CompareOpc = -1;
Chris Lattnera17b1552006-03-31 05:13:27 +0000986 bool isDot = false;
Chris Lattner6d92cad2006-03-26 10:06:40 +0000987 switch (IntNo) {
988 default: return SDOperand(); // Don't custom lower most intrinsics.
Chris Lattnera17b1552006-03-31 05:13:27 +0000989 // Comparison predicates.
990 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break;
991 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
992 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break;
993 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break;
994 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
995 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
996 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
997 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
998 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
999 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
1000 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
1001 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
1002 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
1003
1004 // Normal Comparisons.
1005 case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break;
1006 case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break;
1007 case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break;
1008 case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break;
1009 case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break;
1010 case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break;
1011 case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break;
1012 case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break;
1013 case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break;
1014 case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break;
1015 case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break;
1016 case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break;
1017 case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break;
Chris Lattner6d92cad2006-03-26 10:06:40 +00001018 }
1019
1020 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
1021
Chris Lattnera17b1552006-03-31 05:13:27 +00001022 // If this is a non-dot comparison, make the VCMP node.
Chris Lattner90217992006-04-06 23:12:19 +00001023 if (!isDot) {
1024 SDOperand Tmp = DAG.getNode(PPCISD::VCMP, Op.getOperand(2).getValueType(),
1025 Op.getOperand(1), Op.getOperand(2),
1026 DAG.getConstant(CompareOpc, MVT::i32));
1027 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Tmp);
1028 }
Chris Lattnera17b1552006-03-31 05:13:27 +00001029
Chris Lattner6d92cad2006-03-26 10:06:40 +00001030 // Create the PPCISD altivec 'dot' comparison node.
1031 std::vector<SDOperand> Ops;
1032 std::vector<MVT::ValueType> VTs;
1033 Ops.push_back(Op.getOperand(2)); // LHS
1034 Ops.push_back(Op.getOperand(3)); // RHS
1035 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
1036 VTs.push_back(Op.getOperand(2).getValueType());
1037 VTs.push_back(MVT::Flag);
1038 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
1039
1040 // Now that we have the comparison, emit a copy from the CR to a GPR.
1041 // This is flagged to the above dot comparison.
1042 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
1043 DAG.getRegister(PPC::CR6, MVT::i32),
1044 CompNode.getValue(1));
1045
1046 // Unpack the result based on how the target uses it.
1047 unsigned BitNo; // Bit # of CR6.
1048 bool InvertBit; // Invert result?
1049 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1050 default: // Can't happen, don't crash on invalid number though.
1051 case 0: // Return the value of the EQ bit of CR6.
1052 BitNo = 0; InvertBit = false;
1053 break;
1054 case 1: // Return the inverted value of the EQ bit of CR6.
1055 BitNo = 0; InvertBit = true;
1056 break;
1057 case 2: // Return the value of the LT bit of CR6.
1058 BitNo = 2; InvertBit = false;
1059 break;
1060 case 3: // Return the inverted value of the LT bit of CR6.
1061 BitNo = 2; InvertBit = true;
1062 break;
1063 }
1064
1065 // Shift the bit into the low position.
1066 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
1067 DAG.getConstant(8-(3-BitNo), MVT::i32));
1068 // Isolate the bit.
1069 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
1070 DAG.getConstant(1, MVT::i32));
1071
1072 // If we are supposed to, toggle the bit.
1073 if (InvertBit)
1074 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
1075 DAG.getConstant(1, MVT::i32));
1076 return Flags;
1077 }
Chris Lattnerbc11c342005-08-31 20:23:54 +00001078 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +00001079 return SDOperand();
1080}
1081
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001082std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001083PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001084 //
1085 // add beautiful description of PPC stack frame format, or at least some docs
1086 //
1087 MachineFunction &MF = DAG.getMachineFunction();
1088 MachineFrameInfo *MFI = MF.getFrameInfo();
1089 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +00001090 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001091 std::vector<SDOperand> ArgValues;
1092
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001093 unsigned ArgOffset = 24;
1094 unsigned GPR_remaining = 8;
1095 unsigned FPR_remaining = 13;
1096 unsigned GPR_idx = 0, FPR_idx = 0;
1097 static const unsigned GPR[] = {
1098 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1099 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1100 };
1101 static const unsigned FPR[] = {
1102 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
1103 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
1104 };
1105
1106 // Add DAG nodes to load the arguments... On entry to a function on PPC,
1107 // the arguments start at offset 24, although they are likely to be passed
1108 // in registers.
1109 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
1110 SDOperand newroot, argt;
1111 unsigned ObjSize;
1112 bool needsLoad = false;
1113 bool ArgLive = !I->use_empty();
1114 MVT::ValueType ObjectVT = getValueType(I->getType());
1115
1116 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001117 default: assert(0 && "Unhandled argument type!");
1118 case MVT::i1:
1119 case MVT::i8:
1120 case MVT::i16:
1121 case MVT::i32:
1122 ObjSize = 4;
1123 if (!ArgLive) break;
1124 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001125 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001126 MF.addLiveIn(GPR[GPR_idx], VReg);
1127 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +00001128 if (ObjectVT != MVT::i32) {
1129 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
1130 : ISD::AssertZext;
1131 argt = DAG.getNode(AssertOp, MVT::i32, argt,
1132 DAG.getValueType(ObjectVT));
1133 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
1134 }
Chris Lattner915fb302005-08-30 00:19:00 +00001135 } else {
1136 needsLoad = true;
1137 }
1138 break;
Chris Lattner80720a92005-11-30 20:40:54 +00001139 case MVT::i64:
1140 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +00001141 if (!ArgLive) break;
1142 if (GPR_remaining > 0) {
1143 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +00001144 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001145 MF.addLiveIn(GPR[GPR_idx], VReg);
1146 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001147 // If we have two or more remaining argument registers, then both halves
1148 // of the i64 can be sourced from there. Otherwise, the lower half will
1149 // have to come off the stack. This can happen when an i64 is preceded
1150 // by 28 bytes of arguments.
1151 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001152 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001153 MF.addLiveIn(GPR[GPR_idx+1], VReg);
1154 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +00001155 } else {
1156 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
1157 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1158 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
1159 DAG.getSrcValue(NULL));
1160 }
1161 // Build the outgoing arg thingy
1162 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
1163 newroot = argLo;
1164 } else {
1165 needsLoad = true;
1166 }
1167 break;
1168 case MVT::f32:
1169 case MVT::f64:
1170 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +00001171 if (!ArgLive) {
1172 if (FPR_remaining > 0) {
1173 --FPR_remaining;
1174 ++FPR_idx;
1175 }
1176 break;
1177 }
Chris Lattner915fb302005-08-30 00:19:00 +00001178 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +00001179 unsigned VReg;
1180 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +00001181 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +00001182 else
Nate Begeman1d9d7422005-10-18 00:28:58 +00001183 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001184 MF.addLiveIn(FPR[FPR_idx], VReg);
1185 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +00001186 --FPR_remaining;
1187 ++FPR_idx;
1188 } else {
1189 needsLoad = true;
1190 }
1191 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001192 }
1193
1194 // We need to load the argument to a virtual register if we determined above
1195 // that we ran out of physical registers of the appropriate type
1196 if (needsLoad) {
1197 unsigned SubregOffset = 0;
1198 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
1199 if (ObjectVT == MVT::i16) SubregOffset = 2;
1200 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1201 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
1202 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
1203 DAG.getConstant(SubregOffset, MVT::i32));
1204 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
1205 DAG.getSrcValue(NULL));
1206 }
1207
1208 // Every 4 bytes of argument space consumes one of the GPRs available for
1209 // argument passing.
1210 if (GPR_remaining > 0) {
1211 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
1212 GPR_remaining -= delta;
1213 GPR_idx += delta;
1214 }
1215 ArgOffset += ObjSize;
1216 if (newroot.Val)
1217 DAG.setRoot(newroot.getValue(1));
1218
1219 ArgValues.push_back(argt);
1220 }
1221
1222 // If the function takes variable number of arguments, make a frame index for
1223 // the start of the first vararg value... for expansion of llvm.va_start.
1224 if (F.isVarArg()) {
1225 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
1226 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1227 // If this function is vararg, store any remaining integer argument regs
1228 // to their spots on the stack so that they may be loaded by deferencing the
1229 // result of va_next.
1230 std::vector<SDOperand> MemOps;
1231 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +00001232 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +00001233 MF.addLiveIn(GPR[GPR_idx], VReg);
1234 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001235 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
1236 Val, FIN, DAG.getSrcValue(NULL));
1237 MemOps.push_back(Store);
1238 // Increment the address by four for the next argument to store
1239 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
1240 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
1241 }
Chris Lattner80720a92005-11-30 20:40:54 +00001242 if (!MemOps.empty()) {
1243 MemOps.push_back(DAG.getRoot());
1244 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
1245 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001246 }
1247
1248 // Finally, inform the code generator which regs we return values in.
1249 switch (getValueType(F.getReturnType())) {
1250 default: assert(0 && "Unknown type!");
1251 case MVT::isVoid: break;
1252 case MVT::i1:
1253 case MVT::i8:
1254 case MVT::i16:
1255 case MVT::i32:
1256 MF.addLiveOut(PPC::R3);
1257 break;
1258 case MVT::i64:
1259 MF.addLiveOut(PPC::R3);
1260 MF.addLiveOut(PPC::R4);
1261 break;
1262 case MVT::f32:
1263 case MVT::f64:
1264 MF.addLiveOut(PPC::F1);
1265 break;
1266 }
1267
1268 return ArgValues;
1269}
1270
1271std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001272PPCTargetLowering::LowerCallTo(SDOperand Chain,
1273 const Type *RetTy, bool isVarArg,
1274 unsigned CallingConv, bool isTailCall,
1275 SDOperand Callee, ArgListTy &Args,
1276 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001277 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001278 // SelectExpr to use to put the arguments in the appropriate registers.
1279 std::vector<SDOperand> args_to_use;
1280
1281 // Count how many bytes are to be pushed on the stack, including the linkage
1282 // area, and parameter passing area.
1283 unsigned NumBytes = 24;
1284
1285 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001286 Chain = DAG.getCALLSEQ_START(Chain,
1287 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001288 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001289 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001290 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001291 default: assert(0 && "Unknown value type!");
1292 case MVT::i1:
1293 case MVT::i8:
1294 case MVT::i16:
1295 case MVT::i32:
1296 case MVT::f32:
1297 NumBytes += 4;
1298 break;
1299 case MVT::i64:
1300 case MVT::f64:
1301 NumBytes += 8;
1302 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001303 }
Chris Lattner915fb302005-08-30 00:19:00 +00001304 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001305
Chris Lattner915fb302005-08-30 00:19:00 +00001306 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1307 // plus 32 bytes of argument space in case any called code gets funky on us.
1308 // (Required by ABI to support var arg)
1309 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001310
1311 // Adjust the stack pointer for the new arguments...
1312 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001313 Chain = DAG.getCALLSEQ_START(Chain,
1314 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001315
1316 // Set up a copy of the stack pointer for use loading and storing any
1317 // arguments that may not fit in the registers available for argument
1318 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001319 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001320
1321 // Figure out which arguments are going to go in registers, and which in
1322 // memory. Also, if this is a vararg function, floating point operations
1323 // must be stored to our stack, and loaded into integer regs as well, if
1324 // any integer regs are available for argument passing.
1325 unsigned ArgOffset = 24;
1326 unsigned GPR_remaining = 8;
1327 unsigned FPR_remaining = 13;
1328
1329 std::vector<SDOperand> MemOps;
1330 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1331 // PtrOff will be used to store the current argument to the stack if a
1332 // register cannot be found for it.
1333 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1334 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1335 MVT::ValueType ArgVT = getValueType(Args[i].second);
1336
1337 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001338 default: assert(0 && "Unexpected ValueType for argument!");
1339 case MVT::i1:
1340 case MVT::i8:
1341 case MVT::i16:
1342 // Promote the integer to 32 bits. If the input type is signed use a
1343 // sign extend, otherwise use a zero extend.
1344 if (Args[i].second->isSigned())
1345 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1346 else
1347 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1348 // FALL THROUGH
1349 case MVT::i32:
1350 if (GPR_remaining > 0) {
1351 args_to_use.push_back(Args[i].first);
1352 --GPR_remaining;
1353 } else {
1354 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1355 Args[i].first, PtrOff,
1356 DAG.getSrcValue(NULL)));
1357 }
1358 ArgOffset += 4;
1359 break;
1360 case MVT::i64:
1361 // If we have one free GPR left, we can place the upper half of the i64
1362 // in it, and store the other half to the stack. If we have two or more
1363 // free GPRs, then we can pass both halves of the i64 in registers.
1364 if (GPR_remaining > 0) {
1365 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1366 Args[i].first, DAG.getConstant(1, MVT::i32));
1367 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1368 Args[i].first, DAG.getConstant(0, MVT::i32));
1369 args_to_use.push_back(Hi);
1370 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001371 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001372 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001373 --GPR_remaining;
1374 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001375 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1376 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001377 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001378 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001379 }
Chris Lattner915fb302005-08-30 00:19:00 +00001380 } else {
1381 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1382 Args[i].first, PtrOff,
1383 DAG.getSrcValue(NULL)));
1384 }
1385 ArgOffset += 8;
1386 break;
1387 case MVT::f32:
1388 case MVT::f64:
1389 if (FPR_remaining > 0) {
1390 args_to_use.push_back(Args[i].first);
1391 --FPR_remaining;
1392 if (isVarArg) {
1393 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1394 Args[i].first, PtrOff,
1395 DAG.getSrcValue(NULL));
1396 MemOps.push_back(Store);
1397 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001398 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001399 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1400 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001401 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001402 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001403 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001404 }
1405 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001406 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1407 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001408 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1409 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001410 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001411 args_to_use.push_back(Load);
1412 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001413 }
1414 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001415 // If we have any FPRs remaining, we may also have GPRs remaining.
1416 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1417 // GPRs.
1418 if (GPR_remaining > 0) {
1419 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1420 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001421 }
Chris Lattner915fb302005-08-30 00:19:00 +00001422 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1423 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1424 --GPR_remaining;
1425 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001426 }
Chris Lattner915fb302005-08-30 00:19:00 +00001427 } else {
1428 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1429 Args[i].first, PtrOff,
1430 DAG.getSrcValue(NULL)));
1431 }
1432 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1433 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001434 }
1435 }
1436 if (!MemOps.empty())
1437 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1438 }
1439
1440 std::vector<MVT::ValueType> RetVals;
1441 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001442 MVT::ValueType ActualRetTyVT = RetTyVT;
1443 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1444 ActualRetTyVT = MVT::i32; // Promote result to i32.
1445
Chris Lattnere00ebf02006-01-28 07:33:03 +00001446 if (RetTyVT == MVT::i64) {
1447 RetVals.push_back(MVT::i32);
1448 RetVals.push_back(MVT::i32);
1449 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001450 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001451 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001452 RetVals.push_back(MVT::Other);
1453
Chris Lattner2823b3e2005-11-17 05:56:14 +00001454 // If the callee is a GlobalAddress node (quite common, every direct call is)
1455 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1456 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1457 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1458
Chris Lattner281b55e2006-01-27 23:34:02 +00001459 std::vector<SDOperand> Ops;
1460 Ops.push_back(Chain);
1461 Ops.push_back(Callee);
1462 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1463 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001464 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001465 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1466 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001467 SDOperand RetVal = TheCall;
1468
1469 // If the result is a small value, add a note so that we keep track of the
1470 // information about whether it is sign or zero extended.
1471 if (RetTyVT != ActualRetTyVT) {
1472 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1473 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1474 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001475 } else if (RetTyVT == MVT::i64) {
1476 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001477 }
1478
1479 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001480}
1481
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001482MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001483PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1484 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001485 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001486 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1487 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001488 "Unexpected instr type to insert");
1489
1490 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1491 // control-flow pattern. The incoming instruction knows the destination vreg
1492 // to set, the condition code register to branch on, the true/false values to
1493 // select between, and a branch opcode to use.
1494 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1495 ilist<MachineBasicBlock>::iterator It = BB;
1496 ++It;
1497
1498 // thisMBB:
1499 // ...
1500 // TrueVal = ...
1501 // cmpTY ccX, r1, r2
1502 // bCC copy1MBB
1503 // fallthrough --> copy0MBB
1504 MachineBasicBlock *thisMBB = BB;
1505 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1506 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1507 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1508 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1509 MachineFunction *F = BB->getParent();
1510 F->getBasicBlockList().insert(It, copy0MBB);
1511 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001512 // Update machine-CFG edges by first adding all successors of the current
1513 // block to the new block which will contain the Phi node for the select.
1514 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1515 e = BB->succ_end(); i != e; ++i)
1516 sinkMBB->addSuccessor(*i);
1517 // Next, remove all successors of the current block, and add the true
1518 // and fallthrough blocks as its successors.
1519 while(!BB->succ_empty())
1520 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001521 BB->addSuccessor(copy0MBB);
1522 BB->addSuccessor(sinkMBB);
1523
1524 // copy0MBB:
1525 // %FalseValue = ...
1526 // # fallthrough to sinkMBB
1527 BB = copy0MBB;
1528
1529 // Update machine-CFG edges
1530 BB->addSuccessor(sinkMBB);
1531
1532 // sinkMBB:
1533 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1534 // ...
1535 BB = sinkMBB;
1536 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1537 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1538 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1539
1540 delete MI; // The pseudo instruction is gone now.
1541 return BB;
1542}
1543
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001544SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1545 DAGCombinerInfo &DCI) const {
1546 TargetMachine &TM = getTargetMachine();
1547 SelectionDAG &DAG = DCI.DAG;
1548 switch (N->getOpcode()) {
1549 default: break;
1550 case ISD::SINT_TO_FP:
1551 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001552 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1553 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1554 // We allow the src/dst to be either f32/f64, but the intermediate
1555 // type must be i64.
1556 if (N->getOperand(0).getValueType() == MVT::i64) {
1557 SDOperand Val = N->getOperand(0).getOperand(0);
1558 if (Val.getValueType() == MVT::f32) {
1559 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1560 DCI.AddToWorklist(Val.Val);
1561 }
1562
1563 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001564 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001565 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001566 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001567 if (N->getValueType(0) == MVT::f32) {
1568 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1569 DCI.AddToWorklist(Val.Val);
1570 }
1571 return Val;
1572 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1573 // If the intermediate type is i32, we can avoid the load/store here
1574 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001575 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001576 }
1577 }
1578 break;
Chris Lattner51269842006-03-01 05:50:56 +00001579 case ISD::STORE:
1580 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1581 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1582 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1583 N->getOperand(1).getValueType() == MVT::i32) {
1584 SDOperand Val = N->getOperand(1).getOperand(0);
1585 if (Val.getValueType() == MVT::f32) {
1586 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1587 DCI.AddToWorklist(Val.Val);
1588 }
1589 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1590 DCI.AddToWorklist(Val.Val);
1591
1592 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1593 N->getOperand(2), N->getOperand(3));
1594 DCI.AddToWorklist(Val.Val);
1595 return Val;
1596 }
1597 break;
Chris Lattner4468c222006-03-31 06:02:07 +00001598 case PPCISD::VCMP: {
1599 // If a VCMPo node already exists with exactly the same operands as this
1600 // node, use its result instead of this node (VCMPo computes both a CR6 and
1601 // a normal output).
1602 //
1603 if (!N->getOperand(0).hasOneUse() &&
1604 !N->getOperand(1).hasOneUse() &&
1605 !N->getOperand(2).hasOneUse()) {
1606
1607 // Scan all of the users of the LHS, looking for VCMPo's that match.
1608 SDNode *VCMPoNode = 0;
1609
1610 SDNode *LHSN = N->getOperand(0).Val;
1611 for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
1612 UI != E; ++UI)
1613 if ((*UI)->getOpcode() == PPCISD::VCMPo &&
1614 (*UI)->getOperand(1) == N->getOperand(1) &&
1615 (*UI)->getOperand(2) == N->getOperand(2) &&
1616 (*UI)->getOperand(0) == N->getOperand(0)) {
1617 VCMPoNode = *UI;
1618 break;
1619 }
1620
1621 // If there are non-zero uses of the flag value, use the VCMPo node!
Chris Lattner33497cc2006-03-31 06:04:53 +00001622 if (VCMPoNode && !VCMPoNode->hasNUsesOfValue(0, 1))
Chris Lattner4468c222006-03-31 06:02:07 +00001623 return SDOperand(VCMPoNode, 0);
1624 }
1625 break;
1626 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001627 }
1628
1629 return SDOperand();
1630}
1631
Chris Lattnerbbe77de2006-04-02 06:26:07 +00001632void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
1633 uint64_t Mask,
1634 uint64_t &KnownZero,
1635 uint64_t &KnownOne,
1636 unsigned Depth) const {
1637 KnownZero = 0;
1638 KnownOne = 0;
1639 switch (Op.getOpcode()) {
1640 default: break;
1641 case ISD::INTRINSIC_WO_CHAIN: {
1642 switch (cast<ConstantSDNode>(Op.getOperand(0))->getValue()) {
1643 default: break;
1644 case Intrinsic::ppc_altivec_vcmpbfp_p:
1645 case Intrinsic::ppc_altivec_vcmpeqfp_p:
1646 case Intrinsic::ppc_altivec_vcmpequb_p:
1647 case Intrinsic::ppc_altivec_vcmpequh_p:
1648 case Intrinsic::ppc_altivec_vcmpequw_p:
1649 case Intrinsic::ppc_altivec_vcmpgefp_p:
1650 case Intrinsic::ppc_altivec_vcmpgtfp_p:
1651 case Intrinsic::ppc_altivec_vcmpgtsb_p:
1652 case Intrinsic::ppc_altivec_vcmpgtsh_p:
1653 case Intrinsic::ppc_altivec_vcmpgtsw_p:
1654 case Intrinsic::ppc_altivec_vcmpgtub_p:
1655 case Intrinsic::ppc_altivec_vcmpgtuh_p:
1656 case Intrinsic::ppc_altivec_vcmpgtuw_p:
1657 KnownZero = ~1U; // All bits but the low one are known to be zero.
1658 break;
1659 }
1660 }
1661 }
1662}
1663
1664
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001665/// getConstraintType - Given a constraint letter, return the type of
1666/// constraint it is for this target.
1667PPCTargetLowering::ConstraintType
1668PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1669 switch (ConstraintLetter) {
1670 default: break;
1671 case 'b':
1672 case 'r':
1673 case 'f':
1674 case 'v':
1675 case 'y':
1676 return C_RegisterClass;
1677 }
1678 return TargetLowering::getConstraintType(ConstraintLetter);
1679}
1680
1681
Chris Lattnerddc787d2006-01-31 19:20:21 +00001682std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001683getRegClassForInlineAsmConstraint(const std::string &Constraint,
1684 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001685 if (Constraint.size() == 1) {
1686 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1687 default: break; // Unknown constriant letter
1688 case 'b':
1689 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1690 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1691 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1692 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1693 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1694 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1695 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1696 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1697 0);
1698 case 'r':
1699 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1700 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1701 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1702 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1703 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1704 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1705 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1706 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1707 0);
1708 case 'f':
1709 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1710 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1711 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1712 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1713 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1714 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1715 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1716 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1717 0);
1718 case 'v':
1719 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1720 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1721 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1722 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1723 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1724 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1725 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1726 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1727 0);
1728 case 'y':
1729 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1730 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1731 0);
1732 }
1733 }
1734
Chris Lattner1efa40f2006-02-22 00:56:39 +00001735 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001736}
Chris Lattner763317d2006-02-07 00:47:13 +00001737
1738// isOperandValidForConstraint
1739bool PPCTargetLowering::
1740isOperandValidForConstraint(SDOperand Op, char Letter) {
1741 switch (Letter) {
1742 default: break;
1743 case 'I':
1744 case 'J':
1745 case 'K':
1746 case 'L':
1747 case 'M':
1748 case 'N':
1749 case 'O':
1750 case 'P': {
1751 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1752 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1753 switch (Letter) {
1754 default: assert(0 && "Unknown constraint letter!");
1755 case 'I': // "I" is a signed 16-bit constant.
1756 return (short)Value == (int)Value;
1757 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1758 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1759 return (short)Value == 0;
1760 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1761 return (Value >> 16) == 0;
1762 case 'M': // "M" is a constant that is greater than 31.
1763 return Value > 31;
1764 case 'N': // "N" is a positive constant that is an exact power of two.
1765 return (int)Value > 0 && isPowerOf2_32(Value);
1766 case 'O': // "O" is the constant zero.
1767 return Value == 0;
1768 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1769 return (short)-Value == (int)-Value;
1770 }
1771 break;
1772 }
1773 }
1774
1775 // Handle standard constraint letters.
1776 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1777}
Evan Chengc4c62572006-03-13 23:20:37 +00001778
1779/// isLegalAddressImmediate - Return true if the integer value can be used
1780/// as the offset of the target addressing mode.
1781bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1782 // PPC allows a sign-extended 16-bit immediate field.
1783 return (V > -(1 << 16) && V < (1 << 16)-1);
1784}