blob: 0c50c59580f5bc023b68e439b02257a602e00159 [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 }
171
Evan Chengd30bf012006-03-01 01:11:20 +0000172 // First set operation action for all vector types to expand. Then we
173 // will selectively turn on ones that can be effectively codegen'd.
174 for (unsigned VT = (unsigned)MVT::Vector + 1;
175 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
176 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
177 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
178 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000179 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000180 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000181 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000182 }
183
Nate Begeman425a9692005-11-29 08:17:20 +0000184 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000185 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000186 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattner8d052bc2006-03-25 07:39:07 +0000187 addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
188 addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000189
Evan Chengd30bf012006-03-01 01:11:20 +0000190 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
191 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
192 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
Evan Chengd30bf012006-03-01 01:11:20 +0000193 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000194
195 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
196 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
197
Chris Lattnerb2177b92006-03-19 06:55:52 +0000198 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
199 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
Chris Lattner64b3a082006-03-24 07:48:08 +0000200
201 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
202 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Nate Begeman425a9692005-11-29 08:17:20 +0000203 }
204
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000205 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000206 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000207
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000208 // We have target-specific dag combine patterns for the following nodes:
209 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000210 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000211
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000212 computeRegisterProperties();
213}
214
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000215const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
216 switch (Opcode) {
217 default: return 0;
218 case PPCISD::FSEL: return "PPCISD::FSEL";
219 case PPCISD::FCFID: return "PPCISD::FCFID";
220 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
221 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000222 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000223 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
224 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000225 case PPCISD::VPERM: return "PPCISD::VPERM";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000226 case PPCISD::Hi: return "PPCISD::Hi";
227 case PPCISD::Lo: return "PPCISD::Lo";
228 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
229 case PPCISD::SRL: return "PPCISD::SRL";
230 case PPCISD::SRA: return "PPCISD::SRA";
231 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000232 case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32";
233 case PPCISD::STD_32: return "PPCISD::STD_32";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000234 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000235 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
Chris Lattner6d92cad2006-03-26 10:06:40 +0000236 case PPCISD::MFCR: return "PPCISD::MFCR";
237 case PPCISD::VCMPo: return "PPCISD::VCMPo";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000238 }
239}
240
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000241/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
242static bool isFloatingPointZero(SDOperand Op) {
243 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
244 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
245 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
246 // Maybe this has already been legalized into the constant pool?
247 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
248 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
249 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
250 }
251 return false;
252}
253
Chris Lattneref819f82006-03-20 06:33:01 +0000254
255/// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
256/// specifies a splat of a single element that is suitable for input to
257/// VSPLTB/VSPLTH/VSPLTW.
258bool PPC::isSplatShuffleMask(SDNode *N) {
259 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000260
261 // We can only splat 8-bit, 16-bit, and 32-bit quantities.
262 if (N->getNumOperands() != 4 && N->getNumOperands() != 8 &&
263 N->getNumOperands() != 16)
264 return false;
265
Chris Lattner88a99ef2006-03-20 06:37:44 +0000266 // This is a splat operation if each element of the permute is the same, and
267 // if the value doesn't reference the second vector.
268 SDOperand Elt = N->getOperand(0);
269 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
270 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
271 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
272 "Invalid VECTOR_SHUFFLE mask!");
273 if (N->getOperand(i) != Elt) return false;
274 }
275
276 // Make sure it is a splat of the first vector operand.
277 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
Chris Lattneref819f82006-03-20 06:33:01 +0000278}
279
280/// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
281/// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
282unsigned PPC::getVSPLTImmediate(SDNode *N) {
283 assert(isSplatShuffleMask(N));
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000284 return cast<ConstantSDNode>(N->getOperand(0))->getValue();
Chris Lattneref819f82006-03-20 06:33:01 +0000285}
286
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000287/// isVecSplatImm - Return true if this is a build_vector of constants which
288/// can be formed by using a vspltis[bhw] instruction. The ByteSize field
289/// indicates the number of bytes of each element [124] -> [bhw].
290bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) {
291 SDOperand OpVal(0, 0);
292 // Check to see if this buildvec has a single non-undef value in its elements.
293 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
294 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
295 if (OpVal.Val == 0)
296 OpVal = N->getOperand(i);
297 else if (OpVal != N->getOperand(i))
298 return false;
299 }
300
301 if (OpVal.Val == 0) return false; // All UNDEF: use implicit def.
302
Nate Begeman98e70cc2006-03-28 04:15:58 +0000303 unsigned ValSizeInBytes = 0;
304 uint64_t Value = 0;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000305 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
306 Value = CN->getValue();
307 ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8;
308 } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
309 assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
310 Value = FloatToBits(CN->getValue());
311 ValSizeInBytes = 4;
312 }
313
314 // If the splat value is larger than the element value, then we can never do
315 // this splat. The only case that we could fit the replicated bits into our
316 // immediate field for would be zero, and we prefer to use vxor for it.
317 if (ValSizeInBytes < ByteSize) return false;
318
319 // If the element value is larger than the splat value, cut it in half and
320 // check to see if the two halves are equal. Continue doing this until we
321 // get to ByteSize. This allows us to handle 0x01010101 as 0x01.
322 while (ValSizeInBytes > ByteSize) {
323 ValSizeInBytes >>= 1;
324
325 // If the top half equals the bottom half, we're still ok.
326 if (((Value >> (ValSizeInBytes*8)) & ((8 << ValSizeInBytes)-1)) !=
327 (Value & ((8 << ValSizeInBytes)-1)))
328 return false;
329 }
330
331 // Properly sign extend the value.
332 int ShAmt = (4-ByteSize)*8;
333 int MaskVal = ((int)Value << ShAmt) >> ShAmt;
334
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000335 // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000336 if (MaskVal == 0) return false;
337
338 if (Val) *Val = MaskVal;
339
340 // Finally, if this value fits in a 5 bit sext field, return true.
341 return ((MaskVal << (32-5)) >> (32-5)) == MaskVal;
342}
343
Chris Lattneref819f82006-03-20 06:33:01 +0000344
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000345/// LowerOperation - Provide custom lowering hooks for some operations.
346///
Nate Begeman21e463b2005-10-16 05:39:50 +0000347SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000348 switch (Op.getOpcode()) {
349 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000350 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000351 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000352 SDOperand Src = Op.getOperand(0);
353 if (Src.getValueType() == MVT::f32)
354 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
355
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000356 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000357 switch (Op.getValueType()) {
358 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
359 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000360 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000361 break;
362 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000363 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000364 break;
365 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000366
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000367 // Convert the FP value to an int value through memory.
368 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
369 if (Op.getValueType() == MVT::i32)
370 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
371 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000372 }
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000373 case ISD::SINT_TO_FP:
374 if (Op.getOperand(0).getValueType() == MVT::i64) {
375 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
376 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
377 if (Op.getValueType() == MVT::f32)
378 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
379 return FP;
380 } else {
381 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
382 "Unhandled SINT_TO_FP type in custom expander!");
383 // Since we only generate this in 64-bit mode, we can take advantage of
384 // 64-bit registers. In particular, sign extend the input value into the
385 // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
386 // then lfd it and fcfid it.
387 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
388 int FrameIdx = FrameInfo->CreateStackObject(8, 8);
389 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
390
391 SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32,
392 Op.getOperand(0));
393
394 // STD the extended value into the stack slot.
395 SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other,
396 DAG.getEntryNode(), Ext64, FIdx,
397 DAG.getSrcValue(NULL));
398 // Load the value as a double.
399 SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL));
400
401 // FCFID it and return it.
402 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld);
403 if (Op.getValueType() == MVT::f32)
404 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
405 return FP;
406 }
Chris Lattner7fbcef72006-03-24 07:53:47 +0000407 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000408
Chris Lattnerf7605322005-08-31 21:09:52 +0000409 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000410 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000411 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
412 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
413 break;
414
415 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
416
417 // Cannot handle SETEQ/SETNE.
418 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
419
420 MVT::ValueType ResVT = Op.getValueType();
421 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
422 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
423 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000424
Chris Lattnerf7605322005-08-31 21:09:52 +0000425 // If the RHS of the comparison is a 0.0, we don't need to do the
426 // subtraction at all.
427 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000428 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000429 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000430 case ISD::SETULT:
431 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000432 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000433 case ISD::SETUGE:
434 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000435 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
436 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000437 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000438 case ISD::SETUGT:
439 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000440 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000441 case ISD::SETULE:
442 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000443 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
444 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000445 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000446 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000447 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000448
Chris Lattnereb255f22005-10-25 20:54:57 +0000449 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000450 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000451 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000452 case ISD::SETULT:
453 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000454 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
455 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
456 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
457 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000458 case ISD::SETUGE:
459 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000460 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
461 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
462 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
463 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000464 case ISD::SETUGT:
465 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000466 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
467 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
468 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
469 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000470 case ISD::SETULE:
471 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000472 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
473 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
474 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
475 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000476 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000477 break;
478 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000479 case ISD::SHL: {
480 assert(Op.getValueType() == MVT::i64 &&
481 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
482 // The generic code does a fine job expanding shift by a constant.
483 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
484
485 // Otherwise, expand into a bunch of logical ops. Note that these ops
486 // depend on the PPC behavior for oversized shift amounts.
487 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
488 DAG.getConstant(0, MVT::i32));
489 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
490 DAG.getConstant(1, MVT::i32));
491 SDOperand Amt = Op.getOperand(1);
492
493 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
494 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000495 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
496 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000497 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
498 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
499 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000500 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000501 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000502 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000503 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
504 }
505 case ISD::SRL: {
506 assert(Op.getValueType() == MVT::i64 &&
507 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
508 // The generic code does a fine job expanding shift by a constant.
509 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
510
511 // Otherwise, expand into a bunch of logical ops. Note that these ops
512 // depend on the PPC behavior for oversized shift amounts.
513 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
514 DAG.getConstant(0, MVT::i32));
515 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
516 DAG.getConstant(1, MVT::i32));
517 SDOperand Amt = Op.getOperand(1);
518
519 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
520 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000521 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
522 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000523 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
524 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
525 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000526 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000527 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000528 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000529 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
530 }
531 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000532 assert(Op.getValueType() == MVT::i64 &&
533 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
534 // The generic code does a fine job expanding shift by a constant.
535 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
536
537 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
538 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
539 DAG.getConstant(0, MVT::i32));
540 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
541 DAG.getConstant(1, MVT::i32));
542 SDOperand Amt = Op.getOperand(1);
543
544 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
545 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000546 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
547 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000548 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
549 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
550 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000551 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
552 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000553 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
554 Tmp4, Tmp6, ISD::SETLE);
555 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000556 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000557 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000558 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
559 Constant *C = CP->get();
560 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000561 SDOperand Zero = DAG.getConstant(0, MVT::i32);
562
Evan Cheng4c1aa862006-02-22 20:19:42 +0000563 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000564 // Generate non-pic code that has direct accesses to the constant pool.
565 // The address of the global is just (hi(&g)+lo(&g)).
566 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
567 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
568 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
569 }
570
571 // Only lower ConstantPool on Darwin.
572 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
573 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000574 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000575 // With PIC, the first instruction is actually "GR+hi(&G)".
576 Hi = DAG.getNode(ISD::ADD, MVT::i32,
577 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
578 }
579
580 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
581 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
582 return Lo;
583 }
Chris Lattner860e8862005-11-17 07:30:41 +0000584 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000585 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
586 GlobalValue *GV = GSDN->getGlobal();
587 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000588 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000589
Evan Cheng4c1aa862006-02-22 20:19:42 +0000590 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000591 // Generate non-pic code that has direct accesses to globals.
592 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000593 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
594 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
595 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
596 }
Chris Lattner860e8862005-11-17 07:30:41 +0000597
Chris Lattner1d05cb42005-11-17 18:55:48 +0000598 // Only lower GlobalAddress on Darwin.
599 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000600
Chris Lattner860e8862005-11-17 07:30:41 +0000601 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000602 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000603 // With PIC, the first instruction is actually "GR+hi(&G)".
604 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000605 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000606 }
607
608 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
609 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
610
Chris Lattner37dd6f12006-01-29 20:49:17 +0000611 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
612 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000613 return Lo;
614
615 // If the global is weak or external, we have to go through the lazy
616 // resolution stub.
617 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
618 }
Nate Begeman44775902006-01-31 08:17:29 +0000619 case ISD::SETCC: {
620 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000621
622 // If we're comparing for equality to zero, expose the fact that this is
623 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
624 // fold the new nodes.
625 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
626 if (C->isNullValue() && CC == ISD::SETEQ) {
627 MVT::ValueType VT = Op.getOperand(0).getValueType();
628 SDOperand Zext = Op.getOperand(0);
629 if (VT < MVT::i32) {
630 VT = MVT::i32;
631 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
632 }
633 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
634 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
635 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
636 DAG.getConstant(Log2b, getShiftAmountTy()));
637 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
638 }
639 // Leave comparisons against 0 and -1 alone for now, since they're usually
640 // optimized. FIXME: revisit this when we can custom lower all setcc
641 // optimizations.
642 if (C->isAllOnesValue() || C->isNullValue())
643 break;
644 }
645
646 // If we have an integer seteq/setne, turn it into a compare against zero
647 // by subtracting the rhs from the lhs, which is faster than setting a
648 // condition register, reading it back out, and masking the correct bit.
649 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
650 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
651 MVT::ValueType VT = Op.getValueType();
652 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
653 Op.getOperand(1));
654 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
655 }
Nate Begeman44775902006-01-31 08:17:29 +0000656 break;
657 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000658 case ISD::VASTART: {
659 // vastart just stores the address of the VarArgsFrameIndex slot into the
660 // memory location argument.
661 // FIXME: Replace MVT::i32 with PointerTy
662 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
663 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
664 Op.getOperand(1), Op.getOperand(2));
665 }
Nate Begemanee625572006-01-27 21:09:22 +0000666 case ISD::RET: {
667 SDOperand Copy;
668
669 switch(Op.getNumOperands()) {
670 default:
671 assert(0 && "Do not know how to return this many arguments!");
672 abort();
673 case 1:
674 return SDOperand(); // ret void is legal
675 case 2: {
676 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
677 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
678 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
679 SDOperand());
680 break;
681 }
682 case 3:
683 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
684 SDOperand());
685 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
686 break;
687 }
688 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
689 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000690 case ISD::SCALAR_TO_VECTOR: {
691 // Create a stack slot that is 16-byte aligned.
692 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
693 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
694 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
695
696 // Store the input value into Value#0 of the stack slot.
Chris Lattnerb2177b92006-03-19 06:55:52 +0000697 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
698 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
Chris Lattner7f20b132006-03-28 01:43:22 +0000699 // Load it out.
700 return DAG.getLoad(Op.getValueType(), Store, FIdx, DAG.getSrcValue(NULL));
Chris Lattnerb2177b92006-03-19 06:55:52 +0000701 }
Chris Lattner64b3a082006-03-24 07:48:08 +0000702 case ISD::BUILD_VECTOR:
703 // If this is a case we can't handle, return null and let the default
704 // expansion code take care of it. If we CAN select this case, return Op.
705
706 // See if this is all zeros.
707 // FIXME: We should handle splat(-0.0), and other cases here.
Evan Cheng5b6a01b2006-03-26 09:52:32 +0000708 if (ISD::isBuildVectorAllZeros(Op.Val))
Chris Lattner64b3a082006-03-24 07:48:08 +0000709 return Op;
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000710
711 if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb
712 PPC::isVecSplatImm(Op.Val, 2) || // vspltish
713 PPC::isVecSplatImm(Op.Val, 4)) // vspltisw
714 return Op;
715
Chris Lattner64b3a082006-03-24 07:48:08 +0000716 return SDOperand();
717
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000718 case ISD::VECTOR_SHUFFLE: {
Chris Lattnerdd4d2d02006-03-20 06:51:10 +0000719 SDOperand V1 = Op.getOperand(0);
720 SDOperand V2 = Op.getOperand(1);
721 SDOperand PermMask = Op.getOperand(2);
722
723 // Cases that are handled by instructions that take permute immediates
724 // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
725 // selected by the instruction selector.
726 if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF)
727 break;
728
729 // TODO: Handle more cases, and also handle cases that are cheaper to do as
730 // multiple such instructions than as a constant pool load/vperm pair.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000731
732 // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
733 // vector that will get spilled to the constant pool.
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000734 if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
Chris Lattnerf1d0b2b2006-03-20 01:53:53 +0000735
736 // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
737 // that it is in input element units, not in bytes. Convert now.
738 MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType());
739 unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8;
740
741 std::vector<SDOperand> ResultMask;
742 for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) {
743 unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue();
744
745 for (unsigned j = 0; j != BytesPerElement; ++j)
746 ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
747 MVT::i8));
748 }
749
750 SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask);
751 return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask);
752 }
Chris Lattner48b61a72006-03-28 00:40:33 +0000753 case ISD::INTRINSIC_WO_CHAIN: {
Chris Lattner6d92cad2006-03-26 10:06:40 +0000754 bool HasChain = Op.getOperand(0).getValueType() == MVT::Other;
755 unsigned IntNo=cast<ConstantSDNode>(Op.getOperand(HasChain))->getValue();
756
757 // If this is a lowered altivec predicate compare, CompareOpc is set to the
758 // opcode number of the comparison.
759 int CompareOpc = -1;
760 switch (IntNo) {
761 default: return SDOperand(); // Don't custom lower most intrinsics.
762 case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; break;
763 case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; break;
764 case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; break;
765 case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; break;
766 case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; break;
767 case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; break;
768 case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; break;
769 case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; break;
770 case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; break;
771 case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; break;
772 case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; break;
773 case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; break;
774 case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; break;
775 }
776
777 assert(CompareOpc>0 && "We only lower altivec predicate compares so far!");
778
779 // Create the PPCISD altivec 'dot' comparison node.
780 std::vector<SDOperand> Ops;
781 std::vector<MVT::ValueType> VTs;
782 Ops.push_back(Op.getOperand(2)); // LHS
783 Ops.push_back(Op.getOperand(3)); // RHS
784 Ops.push_back(DAG.getConstant(CompareOpc, MVT::i32));
785 VTs.push_back(Op.getOperand(2).getValueType());
786 VTs.push_back(MVT::Flag);
787 SDOperand CompNode = DAG.getNode(PPCISD::VCMPo, VTs, Ops);
788
789 // Now that we have the comparison, emit a copy from the CR to a GPR.
790 // This is flagged to the above dot comparison.
791 SDOperand Flags = DAG.getNode(PPCISD::MFCR, MVT::i32,
792 DAG.getRegister(PPC::CR6, MVT::i32),
793 CompNode.getValue(1));
794
795 // Unpack the result based on how the target uses it.
796 unsigned BitNo; // Bit # of CR6.
797 bool InvertBit; // Invert result?
798 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
799 default: // Can't happen, don't crash on invalid number though.
800 case 0: // Return the value of the EQ bit of CR6.
801 BitNo = 0; InvertBit = false;
802 break;
803 case 1: // Return the inverted value of the EQ bit of CR6.
804 BitNo = 0; InvertBit = true;
805 break;
806 case 2: // Return the value of the LT bit of CR6.
807 BitNo = 2; InvertBit = false;
808 break;
809 case 3: // Return the inverted value of the LT bit of CR6.
810 BitNo = 2; InvertBit = true;
811 break;
812 }
813
814 // Shift the bit into the low position.
815 Flags = DAG.getNode(ISD::SRL, MVT::i32, Flags,
816 DAG.getConstant(8-(3-BitNo), MVT::i32));
817 // Isolate the bit.
818 Flags = DAG.getNode(ISD::AND, MVT::i32, Flags,
819 DAG.getConstant(1, MVT::i32));
820
821 // If we are supposed to, toggle the bit.
822 if (InvertBit)
823 Flags = DAG.getNode(ISD::XOR, MVT::i32, Flags,
824 DAG.getConstant(1, MVT::i32));
825 return Flags;
826 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000827 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000828 return SDOperand();
829}
830
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000831std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000832PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000833 //
834 // add beautiful description of PPC stack frame format, or at least some docs
835 //
836 MachineFunction &MF = DAG.getMachineFunction();
837 MachineFrameInfo *MFI = MF.getFrameInfo();
838 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000839 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000840 std::vector<SDOperand> ArgValues;
841
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000842 unsigned ArgOffset = 24;
843 unsigned GPR_remaining = 8;
844 unsigned FPR_remaining = 13;
845 unsigned GPR_idx = 0, FPR_idx = 0;
846 static const unsigned GPR[] = {
847 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
848 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
849 };
850 static const unsigned FPR[] = {
851 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
852 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
853 };
854
855 // Add DAG nodes to load the arguments... On entry to a function on PPC,
856 // the arguments start at offset 24, although they are likely to be passed
857 // in registers.
858 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
859 SDOperand newroot, argt;
860 unsigned ObjSize;
861 bool needsLoad = false;
862 bool ArgLive = !I->use_empty();
863 MVT::ValueType ObjectVT = getValueType(I->getType());
864
865 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000866 default: assert(0 && "Unhandled argument type!");
867 case MVT::i1:
868 case MVT::i8:
869 case MVT::i16:
870 case MVT::i32:
871 ObjSize = 4;
872 if (!ArgLive) break;
873 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000874 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000875 MF.addLiveIn(GPR[GPR_idx], VReg);
876 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000877 if (ObjectVT != MVT::i32) {
878 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
879 : ISD::AssertZext;
880 argt = DAG.getNode(AssertOp, MVT::i32, argt,
881 DAG.getValueType(ObjectVT));
882 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
883 }
Chris Lattner915fb302005-08-30 00:19:00 +0000884 } else {
885 needsLoad = true;
886 }
887 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000888 case MVT::i64:
889 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000890 if (!ArgLive) break;
891 if (GPR_remaining > 0) {
892 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000893 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000894 MF.addLiveIn(GPR[GPR_idx], VReg);
895 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000896 // If we have two or more remaining argument registers, then both halves
897 // of the i64 can be sourced from there. Otherwise, the lower half will
898 // have to come off the stack. This can happen when an i64 is preceded
899 // by 28 bytes of arguments.
900 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000901 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000902 MF.addLiveIn(GPR[GPR_idx+1], VReg);
903 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000904 } else {
905 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
906 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
907 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
908 DAG.getSrcValue(NULL));
909 }
910 // Build the outgoing arg thingy
911 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
912 newroot = argLo;
913 } else {
914 needsLoad = true;
915 }
916 break;
917 case MVT::f32:
918 case MVT::f64:
919 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000920 if (!ArgLive) {
921 if (FPR_remaining > 0) {
922 --FPR_remaining;
923 ++FPR_idx;
924 }
925 break;
926 }
Chris Lattner915fb302005-08-30 00:19:00 +0000927 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000928 unsigned VReg;
929 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000930 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000931 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000932 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000933 MF.addLiveIn(FPR[FPR_idx], VReg);
934 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000935 --FPR_remaining;
936 ++FPR_idx;
937 } else {
938 needsLoad = true;
939 }
940 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000941 }
942
943 // We need to load the argument to a virtual register if we determined above
944 // that we ran out of physical registers of the appropriate type
945 if (needsLoad) {
946 unsigned SubregOffset = 0;
947 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
948 if (ObjectVT == MVT::i16) SubregOffset = 2;
949 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
950 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
951 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
952 DAG.getConstant(SubregOffset, MVT::i32));
953 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
954 DAG.getSrcValue(NULL));
955 }
956
957 // Every 4 bytes of argument space consumes one of the GPRs available for
958 // argument passing.
959 if (GPR_remaining > 0) {
960 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
961 GPR_remaining -= delta;
962 GPR_idx += delta;
963 }
964 ArgOffset += ObjSize;
965 if (newroot.Val)
966 DAG.setRoot(newroot.getValue(1));
967
968 ArgValues.push_back(argt);
969 }
970
971 // If the function takes variable number of arguments, make a frame index for
972 // the start of the first vararg value... for expansion of llvm.va_start.
973 if (F.isVarArg()) {
974 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
975 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
976 // If this function is vararg, store any remaining integer argument regs
977 // to their spots on the stack so that they may be loaded by deferencing the
978 // result of va_next.
979 std::vector<SDOperand> MemOps;
980 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000981 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000982 MF.addLiveIn(GPR[GPR_idx], VReg);
983 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000984 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
985 Val, FIN, DAG.getSrcValue(NULL));
986 MemOps.push_back(Store);
987 // Increment the address by four for the next argument to store
988 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
989 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
990 }
Chris Lattner80720a92005-11-30 20:40:54 +0000991 if (!MemOps.empty()) {
992 MemOps.push_back(DAG.getRoot());
993 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
994 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000995 }
996
997 // Finally, inform the code generator which regs we return values in.
998 switch (getValueType(F.getReturnType())) {
999 default: assert(0 && "Unknown type!");
1000 case MVT::isVoid: break;
1001 case MVT::i1:
1002 case MVT::i8:
1003 case MVT::i16:
1004 case MVT::i32:
1005 MF.addLiveOut(PPC::R3);
1006 break;
1007 case MVT::i64:
1008 MF.addLiveOut(PPC::R3);
1009 MF.addLiveOut(PPC::R4);
1010 break;
1011 case MVT::f32:
1012 case MVT::f64:
1013 MF.addLiveOut(PPC::F1);
1014 break;
1015 }
1016
1017 return ArgValues;
1018}
1019
1020std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +00001021PPCTargetLowering::LowerCallTo(SDOperand Chain,
1022 const Type *RetTy, bool isVarArg,
1023 unsigned CallingConv, bool isTailCall,
1024 SDOperand Callee, ArgListTy &Args,
1025 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +00001026 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001027 // SelectExpr to use to put the arguments in the appropriate registers.
1028 std::vector<SDOperand> args_to_use;
1029
1030 // Count how many bytes are to be pushed on the stack, including the linkage
1031 // area, and parameter passing area.
1032 unsigned NumBytes = 24;
1033
1034 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +00001035 Chain = DAG.getCALLSEQ_START(Chain,
1036 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001037 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001038 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001039 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +00001040 default: assert(0 && "Unknown value type!");
1041 case MVT::i1:
1042 case MVT::i8:
1043 case MVT::i16:
1044 case MVT::i32:
1045 case MVT::f32:
1046 NumBytes += 4;
1047 break;
1048 case MVT::i64:
1049 case MVT::f64:
1050 NumBytes += 8;
1051 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001052 }
Chris Lattner915fb302005-08-30 00:19:00 +00001053 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001054
Chris Lattner915fb302005-08-30 00:19:00 +00001055 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
1056 // plus 32 bytes of argument space in case any called code gets funky on us.
1057 // (Required by ABI to support var arg)
1058 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001059
1060 // Adjust the stack pointer for the new arguments...
1061 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +00001062 Chain = DAG.getCALLSEQ_START(Chain,
1063 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001064
1065 // Set up a copy of the stack pointer for use loading and storing any
1066 // arguments that may not fit in the registers available for argument
1067 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +00001068 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001069
1070 // Figure out which arguments are going to go in registers, and which in
1071 // memory. Also, if this is a vararg function, floating point operations
1072 // must be stored to our stack, and loaded into integer regs as well, if
1073 // any integer regs are available for argument passing.
1074 unsigned ArgOffset = 24;
1075 unsigned GPR_remaining = 8;
1076 unsigned FPR_remaining = 13;
1077
1078 std::vector<SDOperand> MemOps;
1079 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1080 // PtrOff will be used to store the current argument to the stack if a
1081 // register cannot be found for it.
1082 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1083 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1084 MVT::ValueType ArgVT = getValueType(Args[i].second);
1085
1086 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +00001087 default: assert(0 && "Unexpected ValueType for argument!");
1088 case MVT::i1:
1089 case MVT::i8:
1090 case MVT::i16:
1091 // Promote the integer to 32 bits. If the input type is signed use a
1092 // sign extend, otherwise use a zero extend.
1093 if (Args[i].second->isSigned())
1094 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
1095 else
1096 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
1097 // FALL THROUGH
1098 case MVT::i32:
1099 if (GPR_remaining > 0) {
1100 args_to_use.push_back(Args[i].first);
1101 --GPR_remaining;
1102 } else {
1103 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1104 Args[i].first, PtrOff,
1105 DAG.getSrcValue(NULL)));
1106 }
1107 ArgOffset += 4;
1108 break;
1109 case MVT::i64:
1110 // If we have one free GPR left, we can place the upper half of the i64
1111 // in it, and store the other half to the stack. If we have two or more
1112 // free GPRs, then we can pass both halves of the i64 in registers.
1113 if (GPR_remaining > 0) {
1114 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1115 Args[i].first, DAG.getConstant(1, MVT::i32));
1116 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1117 Args[i].first, DAG.getConstant(0, MVT::i32));
1118 args_to_use.push_back(Hi);
1119 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001120 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001121 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001122 --GPR_remaining;
1123 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001124 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1125 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001126 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +00001127 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001128 }
Chris Lattner915fb302005-08-30 00:19:00 +00001129 } else {
1130 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1131 Args[i].first, PtrOff,
1132 DAG.getSrcValue(NULL)));
1133 }
1134 ArgOffset += 8;
1135 break;
1136 case MVT::f32:
1137 case MVT::f64:
1138 if (FPR_remaining > 0) {
1139 args_to_use.push_back(Args[i].first);
1140 --FPR_remaining;
1141 if (isVarArg) {
1142 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
1143 Args[i].first, PtrOff,
1144 DAG.getSrcValue(NULL));
1145 MemOps.push_back(Store);
1146 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001147 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +00001148 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1149 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001150 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001151 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001152 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +00001153 }
1154 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001155 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
1156 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +00001157 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
1158 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +00001159 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +00001160 args_to_use.push_back(Load);
1161 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001162 }
1163 } else {
Chris Lattner915fb302005-08-30 00:19:00 +00001164 // If we have any FPRs remaining, we may also have GPRs remaining.
1165 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
1166 // GPRs.
1167 if (GPR_remaining > 0) {
1168 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1169 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001170 }
Chris Lattner915fb302005-08-30 00:19:00 +00001171 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
1172 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
1173 --GPR_remaining;
1174 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001175 }
Chris Lattner915fb302005-08-30 00:19:00 +00001176 } else {
1177 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1178 Args[i].first, PtrOff,
1179 DAG.getSrcValue(NULL)));
1180 }
1181 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
1182 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001183 }
1184 }
1185 if (!MemOps.empty())
1186 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
1187 }
1188
1189 std::vector<MVT::ValueType> RetVals;
1190 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +00001191 MVT::ValueType ActualRetTyVT = RetTyVT;
1192 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
1193 ActualRetTyVT = MVT::i32; // Promote result to i32.
1194
Chris Lattnere00ebf02006-01-28 07:33:03 +00001195 if (RetTyVT == MVT::i64) {
1196 RetVals.push_back(MVT::i32);
1197 RetVals.push_back(MVT::i32);
1198 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +00001199 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001200 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001201 RetVals.push_back(MVT::Other);
1202
Chris Lattner2823b3e2005-11-17 05:56:14 +00001203 // If the callee is a GlobalAddress node (quite common, every direct call is)
1204 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1205 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1206 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
1207
Chris Lattner281b55e2006-01-27 23:34:02 +00001208 std::vector<SDOperand> Ops;
1209 Ops.push_back(Chain);
1210 Ops.push_back(Callee);
1211 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
1212 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001213 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001214 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
1215 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +00001216 SDOperand RetVal = TheCall;
1217
1218 // If the result is a small value, add a note so that we keep track of the
1219 // information about whether it is sign or zero extended.
1220 if (RetTyVT != ActualRetTyVT) {
1221 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
1222 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
1223 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +00001224 } else if (RetTyVT == MVT::i64) {
1225 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +00001226 }
1227
1228 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001229}
1230
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001231MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +00001232PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1233 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001234 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +00001235 MI->getOpcode() == PPC::SELECT_CC_F4 ||
1236 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001237 "Unexpected instr type to insert");
1238
1239 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1240 // control-flow pattern. The incoming instruction knows the destination vreg
1241 // to set, the condition code register to branch on, the true/false values to
1242 // select between, and a branch opcode to use.
1243 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1244 ilist<MachineBasicBlock>::iterator It = BB;
1245 ++It;
1246
1247 // thisMBB:
1248 // ...
1249 // TrueVal = ...
1250 // cmpTY ccX, r1, r2
1251 // bCC copy1MBB
1252 // fallthrough --> copy0MBB
1253 MachineBasicBlock *thisMBB = BB;
1254 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1255 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1256 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
1257 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1258 MachineFunction *F = BB->getParent();
1259 F->getBasicBlockList().insert(It, copy0MBB);
1260 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001261 // Update machine-CFG edges by first adding all successors of the current
1262 // block to the new block which will contain the Phi node for the select.
1263 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1264 e = BB->succ_end(); i != e; ++i)
1265 sinkMBB->addSuccessor(*i);
1266 // Next, remove all successors of the current block, and add the true
1267 // and fallthrough blocks as its successors.
1268 while(!BB->succ_empty())
1269 BB->removeSuccessor(BB->succ_begin());
Chris Lattner8a2d3ca2005-08-26 21:23:58 +00001270 BB->addSuccessor(copy0MBB);
1271 BB->addSuccessor(sinkMBB);
1272
1273 // copy0MBB:
1274 // %FalseValue = ...
1275 // # fallthrough to sinkMBB
1276 BB = copy0MBB;
1277
1278 // Update machine-CFG edges
1279 BB->addSuccessor(sinkMBB);
1280
1281 // sinkMBB:
1282 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1283 // ...
1284 BB = sinkMBB;
1285 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1286 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1287 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1288
1289 delete MI; // The pseudo instruction is gone now.
1290 return BB;
1291}
1292
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001293SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1294 DAGCombinerInfo &DCI) const {
1295 TargetMachine &TM = getTargetMachine();
1296 SelectionDAG &DAG = DCI.DAG;
1297 switch (N->getOpcode()) {
1298 default: break;
1299 case ISD::SINT_TO_FP:
1300 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001301 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
1302 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1303 // We allow the src/dst to be either f32/f64, but the intermediate
1304 // type must be i64.
1305 if (N->getOperand(0).getValueType() == MVT::i64) {
1306 SDOperand Val = N->getOperand(0).getOperand(0);
1307 if (Val.getValueType() == MVT::f32) {
1308 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1309 DCI.AddToWorklist(Val.Val);
1310 }
1311
1312 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001313 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001314 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001315 DCI.AddToWorklist(Val.Val);
Chris Lattnerecfe55e2006-03-22 05:30:33 +00001316 if (N->getValueType(0) == MVT::f32) {
1317 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1318 DCI.AddToWorklist(Val.Val);
1319 }
1320 return Val;
1321 } else if (N->getOperand(0).getValueType() == MVT::i32) {
1322 // If the intermediate type is i32, we can avoid the load/store here
1323 // too.
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001324 }
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001325 }
1326 }
1327 break;
Chris Lattner51269842006-03-01 05:50:56 +00001328 case ISD::STORE:
1329 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1330 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1331 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1332 N->getOperand(1).getValueType() == MVT::i32) {
1333 SDOperand Val = N->getOperand(1).getOperand(0);
1334 if (Val.getValueType() == MVT::f32) {
1335 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1336 DCI.AddToWorklist(Val.Val);
1337 }
1338 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1339 DCI.AddToWorklist(Val.Val);
1340
1341 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1342 N->getOperand(2), N->getOperand(3));
1343 DCI.AddToWorklist(Val.Val);
1344 return Val;
1345 }
1346 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001347 }
1348
1349 return SDOperand();
1350}
1351
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001352/// getConstraintType - Given a constraint letter, return the type of
1353/// constraint it is for this target.
1354PPCTargetLowering::ConstraintType
1355PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1356 switch (ConstraintLetter) {
1357 default: break;
1358 case 'b':
1359 case 'r':
1360 case 'f':
1361 case 'v':
1362 case 'y':
1363 return C_RegisterClass;
1364 }
1365 return TargetLowering::getConstraintType(ConstraintLetter);
1366}
1367
1368
Chris Lattnerddc787d2006-01-31 19:20:21 +00001369std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001370getRegClassForInlineAsmConstraint(const std::string &Constraint,
1371 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001372 if (Constraint.size() == 1) {
1373 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1374 default: break; // Unknown constriant letter
1375 case 'b':
1376 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1377 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1378 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1379 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1380 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1381 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1382 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1383 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1384 0);
1385 case 'r':
1386 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1387 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1388 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1389 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1390 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1391 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1392 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1393 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1394 0);
1395 case 'f':
1396 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1397 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1398 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1399 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1400 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1401 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1402 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1403 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1404 0);
1405 case 'v':
1406 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1407 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1408 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1409 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1410 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1411 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1412 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1413 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1414 0);
1415 case 'y':
1416 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1417 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1418 0);
1419 }
1420 }
1421
Chris Lattner1efa40f2006-02-22 00:56:39 +00001422 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001423}
Chris Lattner763317d2006-02-07 00:47:13 +00001424
1425// isOperandValidForConstraint
1426bool PPCTargetLowering::
1427isOperandValidForConstraint(SDOperand Op, char Letter) {
1428 switch (Letter) {
1429 default: break;
1430 case 'I':
1431 case 'J':
1432 case 'K':
1433 case 'L':
1434 case 'M':
1435 case 'N':
1436 case 'O':
1437 case 'P': {
1438 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1439 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1440 switch (Letter) {
1441 default: assert(0 && "Unknown constraint letter!");
1442 case 'I': // "I" is a signed 16-bit constant.
1443 return (short)Value == (int)Value;
1444 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1445 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1446 return (short)Value == 0;
1447 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1448 return (Value >> 16) == 0;
1449 case 'M': // "M" is a constant that is greater than 31.
1450 return Value > 31;
1451 case 'N': // "N" is a positive constant that is an exact power of two.
1452 return (int)Value > 0 && isPowerOf2_32(Value);
1453 case 'O': // "O" is the constant zero.
1454 return Value == 0;
1455 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1456 return (short)-Value == (int)-Value;
1457 }
1458 break;
1459 }
1460 }
1461
1462 // Handle standard constraint letters.
1463 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1464}
Evan Chengc4c62572006-03-13 23:20:37 +00001465
1466/// isLegalAddressImmediate - Return true if the integer value can be used
1467/// as the offset of the target addressing mode.
1468bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1469 // PPC allows a sign-extended 16-bit immediate field.
1470 return (V > -(1 << 16) && V < (1 << 16)-1);
1471}