blob: 40095223b53a8f67383c69dd2063bb3e71099f95 [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"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000021#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000022#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000023#include "llvm/Function.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000024#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026using namespace llvm;
27
Nate Begeman21e463b2005-10-16 05:39:50 +000028PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000029 : TargetLowering(TM) {
30
31 // Fold away setcc operations if possible.
32 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000033 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000034
Chris Lattnerd145a612005-09-27 22:18:25 +000035 // Use _setjmp/_longjmp instead of setjmp/longjmp.
36 setUseUnderscoreSetJmpLongJmp(true);
37
Chris Lattner7c5a3d32005-08-16 17:14:42 +000038 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000039 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
40 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
41 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000042
Chris Lattnera54aa942006-01-29 06:26:08 +000043 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
44 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
45
Chris Lattner7c5a3d32005-08-16 17:14:42 +000046 // PowerPC has no intrinsics for these particular operations
47 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
48 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
49 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
50
51 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
52 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
53 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
54
55 // PowerPC has no SREM/UREM instructions
56 setOperationAction(ISD::SREM, MVT::i32, Expand);
57 setOperationAction(ISD::UREM, MVT::i32, Expand);
58
59 // We don't support sin/cos/sqrt/fmod
60 setOperationAction(ISD::FSIN , MVT::f64, Expand);
61 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000062 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000063 setOperationAction(ISD::FSIN , MVT::f32, Expand);
64 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000065 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000066
67 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000068 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000069 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
70 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
71 }
72
Nate Begemand88fc032006-01-14 03:14:10 +000073 // PowerPC does not have BSWAP, CTPOP or CTTZ
74 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000075 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
76 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
77
Nate Begeman35ef9132006-01-11 21:21:00 +000078 // PowerPC does not have ROTR
79 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
80
Chris Lattner7c5a3d32005-08-16 17:14:42 +000081 // PowerPC does not have Select
82 setOperationAction(ISD::SELECT, MVT::i32, Expand);
83 setOperationAction(ISD::SELECT, MVT::f32, Expand);
84 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000085
Chris Lattner0b1e4e52005-08-26 17:36:52 +000086 // PowerPC wants to turn select_cc of FP into fsel when possible.
87 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
88 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000089
Nate Begeman750ac1b2006-02-01 07:19:44 +000090 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000091 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000092
Nate Begeman7cbd5252005-08-16 19:49:35 +000093 // PowerPC does not have BRCOND* which requires SetCC
94 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
95 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000096
Chris Lattnerf7605322005-08-31 21:09:52 +000097 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
98 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000099
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000100 // PowerPC does not have [U|S]INT_TO_FP
101 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
102 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
103
Chris Lattner53e88452005-12-23 05:13:35 +0000104 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
105 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
106
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000107 // PowerPC does not have truncstore for i1.
108 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000109
Jim Laskeyabf6d172006-01-05 01:25:28 +0000110 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000111 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000112 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000113 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000114 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000115 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000116
Nate Begeman28a6b022005-12-10 02:36:00 +0000117 // We want to legalize GlobalAddress and ConstantPool nodes into the
118 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000119 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000120 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000121
Nate Begemanee625572006-01-27 21:09:22 +0000122 // RET must be custom lowered, to meet ABI requirements
123 setOperationAction(ISD::RET , MVT::Other, Custom);
124
Nate Begemanacc398c2006-01-25 18:21:52 +0000125 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
126 setOperationAction(ISD::VASTART , MVT::Other, Custom);
127
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000128 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000129 setOperationAction(ISD::VAARG , MVT::Other, Expand);
130 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
131 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000132 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
133 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
134 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000135
Nate Begemanc09eeec2005-09-06 22:03:27 +0000136 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000137 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000138 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
139 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000140 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
141 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
142 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000143 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000144 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000145 }
146
147 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
148 // 64 bit PowerPC implementations can support i64 types directly
149 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000150 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
151 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000152 } else {
153 // 32 bit PowerPC wants to expand i64 shifts itself.
154 setOperationAction(ISD::SHL, MVT::i64, Custom);
155 setOperationAction(ISD::SRL, MVT::i64, Custom);
156 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000157 }
158
Evan Chengd30bf012006-03-01 01:11:20 +0000159 // First set operation action for all vector types to expand. Then we
160 // will selectively turn on ones that can be effectively codegen'd.
161 for (unsigned VT = (unsigned)MVT::Vector + 1;
162 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
163 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
164 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
165 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
166 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
167 }
168
Nate Begeman425a9692005-11-29 08:17:20 +0000169 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000170 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000171 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000172
Evan Chengd30bf012006-03-01 01:11:20 +0000173 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
174 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
175 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
176 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
177 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
178 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000179 // FIXME: We don't support any ConstantVec's yet. We should custom expand
180 // the ones we do!
Chris Lattnerd9b55dd2006-01-29 08:41:37 +0000181 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
182 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000183 }
184
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000185 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000186 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000187
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000188 // We have target-specific dag combine patterns for the following nodes:
189 setTargetDAGCombine(ISD::SINT_TO_FP);
190
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000191 computeRegisterProperties();
192}
193
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000194const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
195 switch (Opcode) {
196 default: return 0;
197 case PPCISD::FSEL: return "PPCISD::FSEL";
198 case PPCISD::FCFID: return "PPCISD::FCFID";
199 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
200 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
201 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
202 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
203 case PPCISD::Hi: return "PPCISD::Hi";
204 case PPCISD::Lo: return "PPCISD::Lo";
205 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
206 case PPCISD::SRL: return "PPCISD::SRL";
207 case PPCISD::SRA: return "PPCISD::SRA";
208 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000209 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000210 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
211 }
212}
213
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000214/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
215static bool isFloatingPointZero(SDOperand Op) {
216 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
217 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
218 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
219 // Maybe this has already been legalized into the constant pool?
220 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
221 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
222 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
223 }
224 return false;
225}
226
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000227/// LowerOperation - Provide custom lowering hooks for some operations.
228///
Nate Begeman21e463b2005-10-16 05:39:50 +0000229SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000230 switch (Op.getOpcode()) {
231 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000232 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000233 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000234 SDOperand Src = Op.getOperand(0);
235 if (Src.getValueType() == MVT::f32)
236 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
237
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000238 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000239 switch (Op.getValueType()) {
240 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
241 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000242 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000243 break;
244 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000245 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000246 break;
247 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000248
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000249 // Convert the FP value to an int value through memory.
250 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
251 if (Op.getValueType() == MVT::i32)
252 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
253 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000254 }
255 case ISD::SINT_TO_FP: {
256 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
257 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000258 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
259 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000260 if (MVT::f32 == Op.getValueType())
261 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
262 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000263 }
264 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000265 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000266 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
267 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
268 break;
269
270 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
271
272 // Cannot handle SETEQ/SETNE.
273 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
274
275 MVT::ValueType ResVT = Op.getValueType();
276 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
277 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
278 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000279
Chris Lattnerf7605322005-08-31 21:09:52 +0000280 // If the RHS of the comparison is a 0.0, we don't need to do the
281 // subtraction at all.
282 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000283 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000284 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000285 case ISD::SETULT:
286 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000287 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000288 case ISD::SETUGE:
289 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000290 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
291 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000292 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000293 case ISD::SETUGT:
294 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000295 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000296 case ISD::SETULE:
297 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000298 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
299 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000300 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000301 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000302 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000303
Chris Lattnereb255f22005-10-25 20:54:57 +0000304 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000305 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000306 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000307 case ISD::SETULT:
308 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000309 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
310 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
311 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
312 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000313 case ISD::SETUGE:
314 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000315 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
316 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
317 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
318 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000319 case ISD::SETUGT:
320 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000321 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
322 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
323 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
324 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000325 case ISD::SETULE:
326 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000327 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
328 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
329 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
330 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000331 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000332 break;
333 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000334 case ISD::SHL: {
335 assert(Op.getValueType() == MVT::i64 &&
336 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
337 // The generic code does a fine job expanding shift by a constant.
338 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
339
340 // Otherwise, expand into a bunch of logical ops. Note that these ops
341 // depend on the PPC behavior for oversized shift amounts.
342 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
343 DAG.getConstant(0, MVT::i32));
344 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
345 DAG.getConstant(1, MVT::i32));
346 SDOperand Amt = Op.getOperand(1);
347
348 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
349 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000350 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
351 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000352 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
353 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
354 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000355 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000356 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000357 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000358 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
359 }
360 case ISD::SRL: {
361 assert(Op.getValueType() == MVT::i64 &&
362 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
363 // The generic code does a fine job expanding shift by a constant.
364 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
365
366 // Otherwise, expand into a bunch of logical ops. Note that these ops
367 // depend on the PPC behavior for oversized shift amounts.
368 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
369 DAG.getConstant(0, MVT::i32));
370 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
371 DAG.getConstant(1, MVT::i32));
372 SDOperand Amt = Op.getOperand(1);
373
374 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
375 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000376 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
377 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000378 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
379 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
380 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000381 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000382 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000383 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000384 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
385 }
386 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000387 assert(Op.getValueType() == MVT::i64 &&
388 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
389 // The generic code does a fine job expanding shift by a constant.
390 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
391
392 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
393 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
394 DAG.getConstant(0, MVT::i32));
395 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
396 DAG.getConstant(1, MVT::i32));
397 SDOperand Amt = Op.getOperand(1);
398
399 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
400 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000401 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
402 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000403 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
404 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
405 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000406 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
407 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000408 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
409 Tmp4, Tmp6, ISD::SETLE);
410 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000411 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000412 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000413 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
414 Constant *C = CP->get();
415 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000416 SDOperand Zero = DAG.getConstant(0, MVT::i32);
417
Evan Cheng4c1aa862006-02-22 20:19:42 +0000418 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000419 // Generate non-pic code that has direct accesses to the constant pool.
420 // The address of the global is just (hi(&g)+lo(&g)).
421 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
422 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
423 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
424 }
425
426 // Only lower ConstantPool on Darwin.
427 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
428 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000429 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000430 // With PIC, the first instruction is actually "GR+hi(&G)".
431 Hi = DAG.getNode(ISD::ADD, MVT::i32,
432 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
433 }
434
435 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
436 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
437 return Lo;
438 }
Chris Lattner860e8862005-11-17 07:30:41 +0000439 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000440 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
441 GlobalValue *GV = GSDN->getGlobal();
442 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000443 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000444
Evan Cheng4c1aa862006-02-22 20:19:42 +0000445 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000446 // Generate non-pic code that has direct accesses to globals.
447 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000448 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
449 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
450 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
451 }
Chris Lattner860e8862005-11-17 07:30:41 +0000452
Chris Lattner1d05cb42005-11-17 18:55:48 +0000453 // Only lower GlobalAddress on Darwin.
454 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000455
Chris Lattner860e8862005-11-17 07:30:41 +0000456 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000457 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000458 // With PIC, the first instruction is actually "GR+hi(&G)".
459 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000460 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000461 }
462
463 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
464 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
465
Chris Lattner37dd6f12006-01-29 20:49:17 +0000466 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
467 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000468 return Lo;
469
470 // If the global is weak or external, we have to go through the lazy
471 // resolution stub.
472 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
473 }
Nate Begeman44775902006-01-31 08:17:29 +0000474 case ISD::SETCC: {
475 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000476
477 // If we're comparing for equality to zero, expose the fact that this is
478 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
479 // fold the new nodes.
480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
481 if (C->isNullValue() && CC == ISD::SETEQ) {
482 MVT::ValueType VT = Op.getOperand(0).getValueType();
483 SDOperand Zext = Op.getOperand(0);
484 if (VT < MVT::i32) {
485 VT = MVT::i32;
486 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
487 }
488 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
489 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
490 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
491 DAG.getConstant(Log2b, getShiftAmountTy()));
492 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
493 }
494 // Leave comparisons against 0 and -1 alone for now, since they're usually
495 // optimized. FIXME: revisit this when we can custom lower all setcc
496 // optimizations.
497 if (C->isAllOnesValue() || C->isNullValue())
498 break;
499 }
500
501 // If we have an integer seteq/setne, turn it into a compare against zero
502 // by subtracting the rhs from the lhs, which is faster than setting a
503 // condition register, reading it back out, and masking the correct bit.
504 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
505 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
506 MVT::ValueType VT = Op.getValueType();
507 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
508 Op.getOperand(1));
509 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
510 }
Nate Begeman44775902006-01-31 08:17:29 +0000511 break;
512 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000513 case ISD::VASTART: {
514 // vastart just stores the address of the VarArgsFrameIndex slot into the
515 // memory location argument.
516 // FIXME: Replace MVT::i32 with PointerTy
517 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
518 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
519 Op.getOperand(1), Op.getOperand(2));
520 }
Nate Begemanee625572006-01-27 21:09:22 +0000521 case ISD::RET: {
522 SDOperand Copy;
523
524 switch(Op.getNumOperands()) {
525 default:
526 assert(0 && "Do not know how to return this many arguments!");
527 abort();
528 case 1:
529 return SDOperand(); // ret void is legal
530 case 2: {
531 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
532 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
533 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
534 SDOperand());
535 break;
536 }
537 case 3:
538 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
539 SDOperand());
540 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
541 break;
542 }
543 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
544 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000545 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000546 return SDOperand();
547}
548
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000549std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000550PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000551 //
552 // add beautiful description of PPC stack frame format, or at least some docs
553 //
554 MachineFunction &MF = DAG.getMachineFunction();
555 MachineFrameInfo *MFI = MF.getFrameInfo();
556 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000557 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000558 std::vector<SDOperand> ArgValues;
559
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000560 unsigned ArgOffset = 24;
561 unsigned GPR_remaining = 8;
562 unsigned FPR_remaining = 13;
563 unsigned GPR_idx = 0, FPR_idx = 0;
564 static const unsigned GPR[] = {
565 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
566 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
567 };
568 static const unsigned FPR[] = {
569 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
570 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
571 };
572
573 // Add DAG nodes to load the arguments... On entry to a function on PPC,
574 // the arguments start at offset 24, although they are likely to be passed
575 // in registers.
576 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
577 SDOperand newroot, argt;
578 unsigned ObjSize;
579 bool needsLoad = false;
580 bool ArgLive = !I->use_empty();
581 MVT::ValueType ObjectVT = getValueType(I->getType());
582
583 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000584 default: assert(0 && "Unhandled argument type!");
585 case MVT::i1:
586 case MVT::i8:
587 case MVT::i16:
588 case MVT::i32:
589 ObjSize = 4;
590 if (!ArgLive) break;
591 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000592 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000593 MF.addLiveIn(GPR[GPR_idx], VReg);
594 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000595 if (ObjectVT != MVT::i32) {
596 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
597 : ISD::AssertZext;
598 argt = DAG.getNode(AssertOp, MVT::i32, argt,
599 DAG.getValueType(ObjectVT));
600 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
601 }
Chris Lattner915fb302005-08-30 00:19:00 +0000602 } else {
603 needsLoad = true;
604 }
605 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000606 case MVT::i64:
607 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000608 if (!ArgLive) break;
609 if (GPR_remaining > 0) {
610 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000611 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000612 MF.addLiveIn(GPR[GPR_idx], VReg);
613 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000614 // If we have two or more remaining argument registers, then both halves
615 // of the i64 can be sourced from there. Otherwise, the lower half will
616 // have to come off the stack. This can happen when an i64 is preceded
617 // by 28 bytes of arguments.
618 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000619 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000620 MF.addLiveIn(GPR[GPR_idx+1], VReg);
621 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000622 } else {
623 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
624 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
625 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
626 DAG.getSrcValue(NULL));
627 }
628 // Build the outgoing arg thingy
629 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
630 newroot = argLo;
631 } else {
632 needsLoad = true;
633 }
634 break;
635 case MVT::f32:
636 case MVT::f64:
637 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000638 if (!ArgLive) {
639 if (FPR_remaining > 0) {
640 --FPR_remaining;
641 ++FPR_idx;
642 }
643 break;
644 }
Chris Lattner915fb302005-08-30 00:19:00 +0000645 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000646 unsigned VReg;
647 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000648 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000649 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000650 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000651 MF.addLiveIn(FPR[FPR_idx], VReg);
652 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000653 --FPR_remaining;
654 ++FPR_idx;
655 } else {
656 needsLoad = true;
657 }
658 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000659 }
660
661 // We need to load the argument to a virtual register if we determined above
662 // that we ran out of physical registers of the appropriate type
663 if (needsLoad) {
664 unsigned SubregOffset = 0;
665 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
666 if (ObjectVT == MVT::i16) SubregOffset = 2;
667 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
668 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
669 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
670 DAG.getConstant(SubregOffset, MVT::i32));
671 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
672 DAG.getSrcValue(NULL));
673 }
674
675 // Every 4 bytes of argument space consumes one of the GPRs available for
676 // argument passing.
677 if (GPR_remaining > 0) {
678 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
679 GPR_remaining -= delta;
680 GPR_idx += delta;
681 }
682 ArgOffset += ObjSize;
683 if (newroot.Val)
684 DAG.setRoot(newroot.getValue(1));
685
686 ArgValues.push_back(argt);
687 }
688
689 // If the function takes variable number of arguments, make a frame index for
690 // the start of the first vararg value... for expansion of llvm.va_start.
691 if (F.isVarArg()) {
692 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
693 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
694 // If this function is vararg, store any remaining integer argument regs
695 // to their spots on the stack so that they may be loaded by deferencing the
696 // result of va_next.
697 std::vector<SDOperand> MemOps;
698 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000699 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000700 MF.addLiveIn(GPR[GPR_idx], VReg);
701 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000702 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
703 Val, FIN, DAG.getSrcValue(NULL));
704 MemOps.push_back(Store);
705 // Increment the address by four for the next argument to store
706 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
707 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
708 }
Chris Lattner80720a92005-11-30 20:40:54 +0000709 if (!MemOps.empty()) {
710 MemOps.push_back(DAG.getRoot());
711 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
712 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000713 }
714
715 // Finally, inform the code generator which regs we return values in.
716 switch (getValueType(F.getReturnType())) {
717 default: assert(0 && "Unknown type!");
718 case MVT::isVoid: break;
719 case MVT::i1:
720 case MVT::i8:
721 case MVT::i16:
722 case MVT::i32:
723 MF.addLiveOut(PPC::R3);
724 break;
725 case MVT::i64:
726 MF.addLiveOut(PPC::R3);
727 MF.addLiveOut(PPC::R4);
728 break;
729 case MVT::f32:
730 case MVT::f64:
731 MF.addLiveOut(PPC::F1);
732 break;
733 }
734
735 return ArgValues;
736}
737
738std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000739PPCTargetLowering::LowerCallTo(SDOperand Chain,
740 const Type *RetTy, bool isVarArg,
741 unsigned CallingConv, bool isTailCall,
742 SDOperand Callee, ArgListTy &Args,
743 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000744 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000745 // SelectExpr to use to put the arguments in the appropriate registers.
746 std::vector<SDOperand> args_to_use;
747
748 // Count how many bytes are to be pushed on the stack, including the linkage
749 // area, and parameter passing area.
750 unsigned NumBytes = 24;
751
752 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000753 Chain = DAG.getCALLSEQ_START(Chain,
754 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000755 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000756 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000757 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000758 default: assert(0 && "Unknown value type!");
759 case MVT::i1:
760 case MVT::i8:
761 case MVT::i16:
762 case MVT::i32:
763 case MVT::f32:
764 NumBytes += 4;
765 break;
766 case MVT::i64:
767 case MVT::f64:
768 NumBytes += 8;
769 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000770 }
Chris Lattner915fb302005-08-30 00:19:00 +0000771 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000772
Chris Lattner915fb302005-08-30 00:19:00 +0000773 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
774 // plus 32 bytes of argument space in case any called code gets funky on us.
775 // (Required by ABI to support var arg)
776 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000777
778 // Adjust the stack pointer for the new arguments...
779 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000780 Chain = DAG.getCALLSEQ_START(Chain,
781 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000782
783 // Set up a copy of the stack pointer for use loading and storing any
784 // arguments that may not fit in the registers available for argument
785 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000786 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000787
788 // Figure out which arguments are going to go in registers, and which in
789 // memory. Also, if this is a vararg function, floating point operations
790 // must be stored to our stack, and loaded into integer regs as well, if
791 // any integer regs are available for argument passing.
792 unsigned ArgOffset = 24;
793 unsigned GPR_remaining = 8;
794 unsigned FPR_remaining = 13;
795
796 std::vector<SDOperand> MemOps;
797 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
798 // PtrOff will be used to store the current argument to the stack if a
799 // register cannot be found for it.
800 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
801 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
802 MVT::ValueType ArgVT = getValueType(Args[i].second);
803
804 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000805 default: assert(0 && "Unexpected ValueType for argument!");
806 case MVT::i1:
807 case MVT::i8:
808 case MVT::i16:
809 // Promote the integer to 32 bits. If the input type is signed use a
810 // sign extend, otherwise use a zero extend.
811 if (Args[i].second->isSigned())
812 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
813 else
814 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
815 // FALL THROUGH
816 case MVT::i32:
817 if (GPR_remaining > 0) {
818 args_to_use.push_back(Args[i].first);
819 --GPR_remaining;
820 } else {
821 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
822 Args[i].first, PtrOff,
823 DAG.getSrcValue(NULL)));
824 }
825 ArgOffset += 4;
826 break;
827 case MVT::i64:
828 // If we have one free GPR left, we can place the upper half of the i64
829 // in it, and store the other half to the stack. If we have two or more
830 // free GPRs, then we can pass both halves of the i64 in registers.
831 if (GPR_remaining > 0) {
832 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
833 Args[i].first, DAG.getConstant(1, MVT::i32));
834 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
835 Args[i].first, DAG.getConstant(0, MVT::i32));
836 args_to_use.push_back(Hi);
837 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000838 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000839 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000840 --GPR_remaining;
841 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000842 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
843 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000844 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000845 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000846 }
Chris Lattner915fb302005-08-30 00:19:00 +0000847 } else {
848 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
849 Args[i].first, PtrOff,
850 DAG.getSrcValue(NULL)));
851 }
852 ArgOffset += 8;
853 break;
854 case MVT::f32:
855 case MVT::f64:
856 if (FPR_remaining > 0) {
857 args_to_use.push_back(Args[i].first);
858 --FPR_remaining;
859 if (isVarArg) {
860 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
861 Args[i].first, PtrOff,
862 DAG.getSrcValue(NULL));
863 MemOps.push_back(Store);
864 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000865 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000866 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
867 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000868 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000869 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000870 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000871 }
872 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000873 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
874 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000875 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
876 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000877 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000878 args_to_use.push_back(Load);
879 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000880 }
881 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000882 // If we have any FPRs remaining, we may also have GPRs remaining.
883 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
884 // GPRs.
885 if (GPR_remaining > 0) {
886 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
887 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000888 }
Chris Lattner915fb302005-08-30 00:19:00 +0000889 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
890 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
891 --GPR_remaining;
892 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000893 }
Chris Lattner915fb302005-08-30 00:19:00 +0000894 } else {
895 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
896 Args[i].first, PtrOff,
897 DAG.getSrcValue(NULL)));
898 }
899 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
900 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000901 }
902 }
903 if (!MemOps.empty())
904 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
905 }
906
907 std::vector<MVT::ValueType> RetVals;
908 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000909 MVT::ValueType ActualRetTyVT = RetTyVT;
910 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
911 ActualRetTyVT = MVT::i32; // Promote result to i32.
912
Chris Lattnere00ebf02006-01-28 07:33:03 +0000913 if (RetTyVT == MVT::i64) {
914 RetVals.push_back(MVT::i32);
915 RetVals.push_back(MVT::i32);
916 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000917 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000918 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000919 RetVals.push_back(MVT::Other);
920
Chris Lattner2823b3e2005-11-17 05:56:14 +0000921 // If the callee is a GlobalAddress node (quite common, every direct call is)
922 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
923 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
924 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
925
Chris Lattner281b55e2006-01-27 23:34:02 +0000926 std::vector<SDOperand> Ops;
927 Ops.push_back(Chain);
928 Ops.push_back(Callee);
929 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
930 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000931 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000932 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
933 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000934 SDOperand RetVal = TheCall;
935
936 // If the result is a small value, add a note so that we keep track of the
937 // information about whether it is sign or zero extended.
938 if (RetTyVT != ActualRetTyVT) {
939 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
940 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
941 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000942 } else if (RetTyVT == MVT::i64) {
943 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000944 }
945
946 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000947}
948
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000949MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000950PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
951 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000952 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000953 MI->getOpcode() == PPC::SELECT_CC_F4 ||
954 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000955 "Unexpected instr type to insert");
956
957 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
958 // control-flow pattern. The incoming instruction knows the destination vreg
959 // to set, the condition code register to branch on, the true/false values to
960 // select between, and a branch opcode to use.
961 const BasicBlock *LLVM_BB = BB->getBasicBlock();
962 ilist<MachineBasicBlock>::iterator It = BB;
963 ++It;
964
965 // thisMBB:
966 // ...
967 // TrueVal = ...
968 // cmpTY ccX, r1, r2
969 // bCC copy1MBB
970 // fallthrough --> copy0MBB
971 MachineBasicBlock *thisMBB = BB;
972 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
973 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
974 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
975 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
976 MachineFunction *F = BB->getParent();
977 F->getBasicBlockList().insert(It, copy0MBB);
978 F->getBasicBlockList().insert(It, sinkMBB);
979 // Update machine-CFG edges
980 BB->addSuccessor(copy0MBB);
981 BB->addSuccessor(sinkMBB);
982
983 // copy0MBB:
984 // %FalseValue = ...
985 // # fallthrough to sinkMBB
986 BB = copy0MBB;
987
988 // Update machine-CFG edges
989 BB->addSuccessor(sinkMBB);
990
991 // sinkMBB:
992 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
993 // ...
994 BB = sinkMBB;
995 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
996 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
997 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
998
999 delete MI; // The pseudo instruction is gone now.
1000 return BB;
1001}
1002
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001003SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1004 DAGCombinerInfo &DCI) const {
1005 TargetMachine &TM = getTargetMachine();
1006 SelectionDAG &DAG = DCI.DAG;
1007 switch (N->getOpcode()) {
1008 default: break;
1009 case ISD::SINT_TO_FP:
1010 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
1011 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1012 // We allow the src/dst to be either f32/f64, but force the intermediate
1013 // type to be i64.
1014 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT &&
1015 N->getOperand(0).getValueType() == MVT::i64) {
1016
1017 SDOperand Val = N->getOperand(0).getOperand(0);
1018 if (Val.getValueType() == MVT::f32) {
1019 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1020 DCI.AddToWorklist(Val.Val);
1021 }
1022
1023 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
1024 DCI.AddToWorklist(Val.Val);
1025 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
1026 DCI.AddToWorklist(Val.Val);
1027 if (N->getValueType(0) == MVT::f32) {
1028 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1029 DCI.AddToWorklist(Val.Val);
1030 }
1031 return Val;
1032 }
1033 }
1034 break;
1035 }
1036
1037 return SDOperand();
1038}
1039
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001040/// getConstraintType - Given a constraint letter, return the type of
1041/// constraint it is for this target.
1042PPCTargetLowering::ConstraintType
1043PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1044 switch (ConstraintLetter) {
1045 default: break;
1046 case 'b':
1047 case 'r':
1048 case 'f':
1049 case 'v':
1050 case 'y':
1051 return C_RegisterClass;
1052 }
1053 return TargetLowering::getConstraintType(ConstraintLetter);
1054}
1055
1056
Chris Lattnerddc787d2006-01-31 19:20:21 +00001057std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001058getRegClassForInlineAsmConstraint(const std::string &Constraint,
1059 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001060 if (Constraint.size() == 1) {
1061 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1062 default: break; // Unknown constriant letter
1063 case 'b':
1064 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1065 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1066 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1067 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1068 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1069 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1070 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1071 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1072 0);
1073 case 'r':
1074 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1075 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1076 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1077 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1078 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1079 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1080 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1081 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1082 0);
1083 case 'f':
1084 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1085 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1086 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1087 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1088 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1089 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1090 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1091 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1092 0);
1093 case 'v':
1094 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1095 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1096 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1097 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1098 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1099 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1100 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1101 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1102 0);
1103 case 'y':
1104 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1105 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1106 0);
1107 }
1108 }
1109
Chris Lattner1efa40f2006-02-22 00:56:39 +00001110 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001111}
Chris Lattner763317d2006-02-07 00:47:13 +00001112
1113// isOperandValidForConstraint
1114bool PPCTargetLowering::
1115isOperandValidForConstraint(SDOperand Op, char Letter) {
1116 switch (Letter) {
1117 default: break;
1118 case 'I':
1119 case 'J':
1120 case 'K':
1121 case 'L':
1122 case 'M':
1123 case 'N':
1124 case 'O':
1125 case 'P': {
1126 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1127 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1128 switch (Letter) {
1129 default: assert(0 && "Unknown constraint letter!");
1130 case 'I': // "I" is a signed 16-bit constant.
1131 return (short)Value == (int)Value;
1132 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1133 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1134 return (short)Value == 0;
1135 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1136 return (Value >> 16) == 0;
1137 case 'M': // "M" is a constant that is greater than 31.
1138 return Value > 31;
1139 case 'N': // "N" is a positive constant that is an exact power of two.
1140 return (int)Value > 0 && isPowerOf2_32(Value);
1141 case 'O': // "O" is the constant zero.
1142 return Value == 0;
1143 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1144 return (short)-Value == (int)-Value;
1145 }
1146 break;
1147 }
1148 }
1149
1150 // Handle standard constraint letters.
1151 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1152}