blob: c94b9f1bf4140b60bba0297fb9f353714da86096 [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);
Chris Lattner51269842006-03-01 05:50:56 +0000190 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000191
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000192 computeRegisterProperties();
193}
194
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000195const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
196 switch (Opcode) {
197 default: return 0;
198 case PPCISD::FSEL: return "PPCISD::FSEL";
199 case PPCISD::FCFID: return "PPCISD::FCFID";
200 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
201 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000202 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000203 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
204 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
205 case PPCISD::Hi: return "PPCISD::Hi";
206 case PPCISD::Lo: return "PPCISD::Lo";
207 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
208 case PPCISD::SRL: return "PPCISD::SRL";
209 case PPCISD::SRA: return "PPCISD::SRA";
210 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000211 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000212 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
213 }
214}
215
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000216/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
217static bool isFloatingPointZero(SDOperand Op) {
218 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
219 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
220 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
221 // Maybe this has already been legalized into the constant pool?
222 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
223 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
224 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
225 }
226 return false;
227}
228
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000229/// LowerOperation - Provide custom lowering hooks for some operations.
230///
Nate Begeman21e463b2005-10-16 05:39:50 +0000231SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000232 switch (Op.getOpcode()) {
233 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000234 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000235 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000236 SDOperand Src = Op.getOperand(0);
237 if (Src.getValueType() == MVT::f32)
238 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
239
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000240 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000241 switch (Op.getValueType()) {
242 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
243 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000244 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000245 break;
246 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000247 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000248 break;
249 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000250
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000251 // Convert the FP value to an int value through memory.
252 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
253 if (Op.getValueType() == MVT::i32)
254 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
255 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000256 }
257 case ISD::SINT_TO_FP: {
258 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
259 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000260 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
261 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000262 if (MVT::f32 == Op.getValueType())
263 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
264 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000265 }
266 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000267 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000268 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
269 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
270 break;
271
272 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
273
274 // Cannot handle SETEQ/SETNE.
275 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
276
277 MVT::ValueType ResVT = Op.getValueType();
278 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
279 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
280 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000281
Chris Lattnerf7605322005-08-31 21:09:52 +0000282 // If the RHS of the comparison is a 0.0, we don't need to do the
283 // subtraction at all.
284 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000285 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000286 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000287 case ISD::SETULT:
288 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000289 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000290 case ISD::SETUGE:
291 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000292 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
293 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000294 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000295 case ISD::SETUGT:
296 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000297 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000298 case ISD::SETULE:
299 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000300 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
301 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000302 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000303 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000304 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000305
Chris Lattnereb255f22005-10-25 20:54:57 +0000306 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000307 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000308 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000309 case ISD::SETULT:
310 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000311 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
312 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
313 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
314 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000315 case ISD::SETUGE:
316 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000317 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
318 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
319 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
320 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000321 case ISD::SETUGT:
322 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000323 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
324 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
325 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
326 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000327 case ISD::SETULE:
328 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000329 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
330 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
331 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
332 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000333 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000334 break;
335 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000336 case ISD::SHL: {
337 assert(Op.getValueType() == MVT::i64 &&
338 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
339 // The generic code does a fine job expanding shift by a constant.
340 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
341
342 // Otherwise, expand into a bunch of logical ops. Note that these ops
343 // depend on the PPC behavior for oversized shift amounts.
344 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
345 DAG.getConstant(0, MVT::i32));
346 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
347 DAG.getConstant(1, MVT::i32));
348 SDOperand Amt = Op.getOperand(1);
349
350 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
351 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000352 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
353 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000354 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
355 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
356 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000357 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000358 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000359 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000360 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
361 }
362 case ISD::SRL: {
363 assert(Op.getValueType() == MVT::i64 &&
364 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
365 // The generic code does a fine job expanding shift by a constant.
366 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
367
368 // Otherwise, expand into a bunch of logical ops. Note that these ops
369 // depend on the PPC behavior for oversized shift amounts.
370 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
371 DAG.getConstant(0, MVT::i32));
372 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
373 DAG.getConstant(1, MVT::i32));
374 SDOperand Amt = Op.getOperand(1);
375
376 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
377 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000378 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
379 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000380 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
381 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
382 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000383 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000384 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000385 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000386 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
387 }
388 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000389 assert(Op.getValueType() == MVT::i64 &&
390 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
391 // The generic code does a fine job expanding shift by a constant.
392 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
393
394 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
395 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
396 DAG.getConstant(0, MVT::i32));
397 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
398 DAG.getConstant(1, MVT::i32));
399 SDOperand Amt = Op.getOperand(1);
400
401 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
402 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000403 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
404 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000405 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
406 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
407 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000408 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
409 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000410 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
411 Tmp4, Tmp6, ISD::SETLE);
412 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000413 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000414 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000415 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
416 Constant *C = CP->get();
417 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000418 SDOperand Zero = DAG.getConstant(0, MVT::i32);
419
Evan Cheng4c1aa862006-02-22 20:19:42 +0000420 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000421 // Generate non-pic code that has direct accesses to the constant pool.
422 // The address of the global is just (hi(&g)+lo(&g)).
423 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
424 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
425 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
426 }
427
428 // Only lower ConstantPool on Darwin.
429 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
430 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000431 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000432 // With PIC, the first instruction is actually "GR+hi(&G)".
433 Hi = DAG.getNode(ISD::ADD, MVT::i32,
434 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
435 }
436
437 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
438 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
439 return Lo;
440 }
Chris Lattner860e8862005-11-17 07:30:41 +0000441 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000442 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
443 GlobalValue *GV = GSDN->getGlobal();
444 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000445 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000446
Evan Cheng4c1aa862006-02-22 20:19:42 +0000447 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000448 // Generate non-pic code that has direct accesses to globals.
449 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000450 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
451 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
452 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
453 }
Chris Lattner860e8862005-11-17 07:30:41 +0000454
Chris Lattner1d05cb42005-11-17 18:55:48 +0000455 // Only lower GlobalAddress on Darwin.
456 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000457
Chris Lattner860e8862005-11-17 07:30:41 +0000458 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000459 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000460 // With PIC, the first instruction is actually "GR+hi(&G)".
461 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000462 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000463 }
464
465 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
466 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
467
Chris Lattner37dd6f12006-01-29 20:49:17 +0000468 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
469 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000470 return Lo;
471
472 // If the global is weak or external, we have to go through the lazy
473 // resolution stub.
474 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
475 }
Nate Begeman44775902006-01-31 08:17:29 +0000476 case ISD::SETCC: {
477 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000478
479 // If we're comparing for equality to zero, expose the fact that this is
480 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
481 // fold the new nodes.
482 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
483 if (C->isNullValue() && CC == ISD::SETEQ) {
484 MVT::ValueType VT = Op.getOperand(0).getValueType();
485 SDOperand Zext = Op.getOperand(0);
486 if (VT < MVT::i32) {
487 VT = MVT::i32;
488 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
489 }
490 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
491 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
492 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
493 DAG.getConstant(Log2b, getShiftAmountTy()));
494 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
495 }
496 // Leave comparisons against 0 and -1 alone for now, since they're usually
497 // optimized. FIXME: revisit this when we can custom lower all setcc
498 // optimizations.
499 if (C->isAllOnesValue() || C->isNullValue())
500 break;
501 }
502
503 // If we have an integer seteq/setne, turn it into a compare against zero
504 // by subtracting the rhs from the lhs, which is faster than setting a
505 // condition register, reading it back out, and masking the correct bit.
506 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
507 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
508 MVT::ValueType VT = Op.getValueType();
509 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
510 Op.getOperand(1));
511 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
512 }
Nate Begeman44775902006-01-31 08:17:29 +0000513 break;
514 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000515 case ISD::VASTART: {
516 // vastart just stores the address of the VarArgsFrameIndex slot into the
517 // memory location argument.
518 // FIXME: Replace MVT::i32 with PointerTy
519 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
520 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
521 Op.getOperand(1), Op.getOperand(2));
522 }
Nate Begemanee625572006-01-27 21:09:22 +0000523 case ISD::RET: {
524 SDOperand Copy;
525
526 switch(Op.getNumOperands()) {
527 default:
528 assert(0 && "Do not know how to return this many arguments!");
529 abort();
530 case 1:
531 return SDOperand(); // ret void is legal
532 case 2: {
533 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
534 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
535 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
536 SDOperand());
537 break;
538 }
539 case 3:
540 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
541 SDOperand());
542 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
543 break;
544 }
545 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
546 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000547 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000548 return SDOperand();
549}
550
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000551std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000552PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000553 //
554 // add beautiful description of PPC stack frame format, or at least some docs
555 //
556 MachineFunction &MF = DAG.getMachineFunction();
557 MachineFrameInfo *MFI = MF.getFrameInfo();
558 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000559 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000560 std::vector<SDOperand> ArgValues;
561
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000562 unsigned ArgOffset = 24;
563 unsigned GPR_remaining = 8;
564 unsigned FPR_remaining = 13;
565 unsigned GPR_idx = 0, FPR_idx = 0;
566 static const unsigned GPR[] = {
567 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
568 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
569 };
570 static const unsigned FPR[] = {
571 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
572 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
573 };
574
575 // Add DAG nodes to load the arguments... On entry to a function on PPC,
576 // the arguments start at offset 24, although they are likely to be passed
577 // in registers.
578 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
579 SDOperand newroot, argt;
580 unsigned ObjSize;
581 bool needsLoad = false;
582 bool ArgLive = !I->use_empty();
583 MVT::ValueType ObjectVT = getValueType(I->getType());
584
585 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000586 default: assert(0 && "Unhandled argument type!");
587 case MVT::i1:
588 case MVT::i8:
589 case MVT::i16:
590 case MVT::i32:
591 ObjSize = 4;
592 if (!ArgLive) break;
593 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000594 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000595 MF.addLiveIn(GPR[GPR_idx], VReg);
596 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000597 if (ObjectVT != MVT::i32) {
598 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
599 : ISD::AssertZext;
600 argt = DAG.getNode(AssertOp, MVT::i32, argt,
601 DAG.getValueType(ObjectVT));
602 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
603 }
Chris Lattner915fb302005-08-30 00:19:00 +0000604 } else {
605 needsLoad = true;
606 }
607 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000608 case MVT::i64:
609 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000610 if (!ArgLive) break;
611 if (GPR_remaining > 0) {
612 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000613 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000614 MF.addLiveIn(GPR[GPR_idx], VReg);
615 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000616 // If we have two or more remaining argument registers, then both halves
617 // of the i64 can be sourced from there. Otherwise, the lower half will
618 // have to come off the stack. This can happen when an i64 is preceded
619 // by 28 bytes of arguments.
620 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000621 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000622 MF.addLiveIn(GPR[GPR_idx+1], VReg);
623 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000624 } else {
625 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
626 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
627 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
628 DAG.getSrcValue(NULL));
629 }
630 // Build the outgoing arg thingy
631 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
632 newroot = argLo;
633 } else {
634 needsLoad = true;
635 }
636 break;
637 case MVT::f32:
638 case MVT::f64:
639 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000640 if (!ArgLive) {
641 if (FPR_remaining > 0) {
642 --FPR_remaining;
643 ++FPR_idx;
644 }
645 break;
646 }
Chris Lattner915fb302005-08-30 00:19:00 +0000647 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000648 unsigned VReg;
649 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000650 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000651 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000652 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000653 MF.addLiveIn(FPR[FPR_idx], VReg);
654 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000655 --FPR_remaining;
656 ++FPR_idx;
657 } else {
658 needsLoad = true;
659 }
660 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000661 }
662
663 // We need to load the argument to a virtual register if we determined above
664 // that we ran out of physical registers of the appropriate type
665 if (needsLoad) {
666 unsigned SubregOffset = 0;
667 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
668 if (ObjectVT == MVT::i16) SubregOffset = 2;
669 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
670 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
671 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
672 DAG.getConstant(SubregOffset, MVT::i32));
673 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
674 DAG.getSrcValue(NULL));
675 }
676
677 // Every 4 bytes of argument space consumes one of the GPRs available for
678 // argument passing.
679 if (GPR_remaining > 0) {
680 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
681 GPR_remaining -= delta;
682 GPR_idx += delta;
683 }
684 ArgOffset += ObjSize;
685 if (newroot.Val)
686 DAG.setRoot(newroot.getValue(1));
687
688 ArgValues.push_back(argt);
689 }
690
691 // If the function takes variable number of arguments, make a frame index for
692 // the start of the first vararg value... for expansion of llvm.va_start.
693 if (F.isVarArg()) {
694 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
695 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
696 // If this function is vararg, store any remaining integer argument regs
697 // to their spots on the stack so that they may be loaded by deferencing the
698 // result of va_next.
699 std::vector<SDOperand> MemOps;
700 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000701 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000702 MF.addLiveIn(GPR[GPR_idx], VReg);
703 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000704 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
705 Val, FIN, DAG.getSrcValue(NULL));
706 MemOps.push_back(Store);
707 // Increment the address by four for the next argument to store
708 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
709 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
710 }
Chris Lattner80720a92005-11-30 20:40:54 +0000711 if (!MemOps.empty()) {
712 MemOps.push_back(DAG.getRoot());
713 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
714 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000715 }
716
717 // Finally, inform the code generator which regs we return values in.
718 switch (getValueType(F.getReturnType())) {
719 default: assert(0 && "Unknown type!");
720 case MVT::isVoid: break;
721 case MVT::i1:
722 case MVT::i8:
723 case MVT::i16:
724 case MVT::i32:
725 MF.addLiveOut(PPC::R3);
726 break;
727 case MVT::i64:
728 MF.addLiveOut(PPC::R3);
729 MF.addLiveOut(PPC::R4);
730 break;
731 case MVT::f32:
732 case MVT::f64:
733 MF.addLiveOut(PPC::F1);
734 break;
735 }
736
737 return ArgValues;
738}
739
740std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000741PPCTargetLowering::LowerCallTo(SDOperand Chain,
742 const Type *RetTy, bool isVarArg,
743 unsigned CallingConv, bool isTailCall,
744 SDOperand Callee, ArgListTy &Args,
745 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000746 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000747 // SelectExpr to use to put the arguments in the appropriate registers.
748 std::vector<SDOperand> args_to_use;
749
750 // Count how many bytes are to be pushed on the stack, including the linkage
751 // area, and parameter passing area.
752 unsigned NumBytes = 24;
753
754 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000755 Chain = DAG.getCALLSEQ_START(Chain,
756 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000757 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000758 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000759 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000760 default: assert(0 && "Unknown value type!");
761 case MVT::i1:
762 case MVT::i8:
763 case MVT::i16:
764 case MVT::i32:
765 case MVT::f32:
766 NumBytes += 4;
767 break;
768 case MVT::i64:
769 case MVT::f64:
770 NumBytes += 8;
771 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000772 }
Chris Lattner915fb302005-08-30 00:19:00 +0000773 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000774
Chris Lattner915fb302005-08-30 00:19:00 +0000775 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
776 // plus 32 bytes of argument space in case any called code gets funky on us.
777 // (Required by ABI to support var arg)
778 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000779
780 // Adjust the stack pointer for the new arguments...
781 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000782 Chain = DAG.getCALLSEQ_START(Chain,
783 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000784
785 // Set up a copy of the stack pointer for use loading and storing any
786 // arguments that may not fit in the registers available for argument
787 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000788 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000789
790 // Figure out which arguments are going to go in registers, and which in
791 // memory. Also, if this is a vararg function, floating point operations
792 // must be stored to our stack, and loaded into integer regs as well, if
793 // any integer regs are available for argument passing.
794 unsigned ArgOffset = 24;
795 unsigned GPR_remaining = 8;
796 unsigned FPR_remaining = 13;
797
798 std::vector<SDOperand> MemOps;
799 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
800 // PtrOff will be used to store the current argument to the stack if a
801 // register cannot be found for it.
802 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
803 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
804 MVT::ValueType ArgVT = getValueType(Args[i].second);
805
806 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000807 default: assert(0 && "Unexpected ValueType for argument!");
808 case MVT::i1:
809 case MVT::i8:
810 case MVT::i16:
811 // Promote the integer to 32 bits. If the input type is signed use a
812 // sign extend, otherwise use a zero extend.
813 if (Args[i].second->isSigned())
814 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
815 else
816 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
817 // FALL THROUGH
818 case MVT::i32:
819 if (GPR_remaining > 0) {
820 args_to_use.push_back(Args[i].first);
821 --GPR_remaining;
822 } else {
823 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
824 Args[i].first, PtrOff,
825 DAG.getSrcValue(NULL)));
826 }
827 ArgOffset += 4;
828 break;
829 case MVT::i64:
830 // If we have one free GPR left, we can place the upper half of the i64
831 // in it, and store the other half to the stack. If we have two or more
832 // free GPRs, then we can pass both halves of the i64 in registers.
833 if (GPR_remaining > 0) {
834 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
835 Args[i].first, DAG.getConstant(1, MVT::i32));
836 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
837 Args[i].first, DAG.getConstant(0, MVT::i32));
838 args_to_use.push_back(Hi);
839 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000840 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000841 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000842 --GPR_remaining;
843 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000844 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
845 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000846 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000847 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000848 }
Chris Lattner915fb302005-08-30 00:19:00 +0000849 } else {
850 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
851 Args[i].first, PtrOff,
852 DAG.getSrcValue(NULL)));
853 }
854 ArgOffset += 8;
855 break;
856 case MVT::f32:
857 case MVT::f64:
858 if (FPR_remaining > 0) {
859 args_to_use.push_back(Args[i].first);
860 --FPR_remaining;
861 if (isVarArg) {
862 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
863 Args[i].first, PtrOff,
864 DAG.getSrcValue(NULL));
865 MemOps.push_back(Store);
866 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000867 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000868 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
869 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000870 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000871 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000872 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000873 }
874 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000875 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
876 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000877 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
878 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000879 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000880 args_to_use.push_back(Load);
881 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000882 }
883 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000884 // If we have any FPRs remaining, we may also have GPRs remaining.
885 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
886 // GPRs.
887 if (GPR_remaining > 0) {
888 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
889 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000890 }
Chris Lattner915fb302005-08-30 00:19:00 +0000891 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
892 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
893 --GPR_remaining;
894 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000895 }
Chris Lattner915fb302005-08-30 00:19:00 +0000896 } else {
897 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
898 Args[i].first, PtrOff,
899 DAG.getSrcValue(NULL)));
900 }
901 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
902 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000903 }
904 }
905 if (!MemOps.empty())
906 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
907 }
908
909 std::vector<MVT::ValueType> RetVals;
910 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000911 MVT::ValueType ActualRetTyVT = RetTyVT;
912 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
913 ActualRetTyVT = MVT::i32; // Promote result to i32.
914
Chris Lattnere00ebf02006-01-28 07:33:03 +0000915 if (RetTyVT == MVT::i64) {
916 RetVals.push_back(MVT::i32);
917 RetVals.push_back(MVT::i32);
918 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000919 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000920 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000921 RetVals.push_back(MVT::Other);
922
Chris Lattner2823b3e2005-11-17 05:56:14 +0000923 // If the callee is a GlobalAddress node (quite common, every direct call is)
924 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
925 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
926 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
927
Chris Lattner281b55e2006-01-27 23:34:02 +0000928 std::vector<SDOperand> Ops;
929 Ops.push_back(Chain);
930 Ops.push_back(Callee);
931 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
932 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000933 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000934 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
935 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000936 SDOperand RetVal = TheCall;
937
938 // If the result is a small value, add a note so that we keep track of the
939 // information about whether it is sign or zero extended.
940 if (RetTyVT != ActualRetTyVT) {
941 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
942 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
943 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000944 } else if (RetTyVT == MVT::i64) {
945 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000946 }
947
948 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000949}
950
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000951MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000952PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
953 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000954 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000955 MI->getOpcode() == PPC::SELECT_CC_F4 ||
956 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000957 "Unexpected instr type to insert");
958
959 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
960 // control-flow pattern. The incoming instruction knows the destination vreg
961 // to set, the condition code register to branch on, the true/false values to
962 // select between, and a branch opcode to use.
963 const BasicBlock *LLVM_BB = BB->getBasicBlock();
964 ilist<MachineBasicBlock>::iterator It = BB;
965 ++It;
966
967 // thisMBB:
968 // ...
969 // TrueVal = ...
970 // cmpTY ccX, r1, r2
971 // bCC copy1MBB
972 // fallthrough --> copy0MBB
973 MachineBasicBlock *thisMBB = BB;
974 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
975 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
976 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
977 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
978 MachineFunction *F = BB->getParent();
979 F->getBasicBlockList().insert(It, copy0MBB);
980 F->getBasicBlockList().insert(It, sinkMBB);
981 // Update machine-CFG edges
982 BB->addSuccessor(copy0MBB);
983 BB->addSuccessor(sinkMBB);
984
985 // copy0MBB:
986 // %FalseValue = ...
987 // # fallthrough to sinkMBB
988 BB = copy0MBB;
989
990 // Update machine-CFG edges
991 BB->addSuccessor(sinkMBB);
992
993 // sinkMBB:
994 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
995 // ...
996 BB = sinkMBB;
997 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
998 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
999 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1000
1001 delete MI; // The pseudo instruction is gone now.
1002 return BB;
1003}
1004
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001005SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1006 DAGCombinerInfo &DCI) const {
1007 TargetMachine &TM = getTargetMachine();
1008 SelectionDAG &DAG = DCI.DAG;
1009 switch (N->getOpcode()) {
1010 default: break;
1011 case ISD::SINT_TO_FP:
1012 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
1013 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1014 // We allow the src/dst to be either f32/f64, but force the intermediate
1015 // type to be i64.
1016 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT &&
1017 N->getOperand(0).getValueType() == MVT::i64) {
1018
1019 SDOperand Val = N->getOperand(0).getOperand(0);
1020 if (Val.getValueType() == MVT::f32) {
1021 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1022 DCI.AddToWorklist(Val.Val);
1023 }
1024
1025 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
1026 DCI.AddToWorklist(Val.Val);
1027 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
1028 DCI.AddToWorklist(Val.Val);
1029 if (N->getValueType(0) == MVT::f32) {
1030 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1031 DCI.AddToWorklist(Val.Val);
1032 }
1033 return Val;
1034 }
1035 }
1036 break;
Chris Lattner51269842006-03-01 05:50:56 +00001037 case ISD::STORE:
1038 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1039 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1040 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1041 N->getOperand(1).getValueType() == MVT::i32) {
1042 SDOperand Val = N->getOperand(1).getOperand(0);
1043 if (Val.getValueType() == MVT::f32) {
1044 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1045 DCI.AddToWorklist(Val.Val);
1046 }
1047 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1048 DCI.AddToWorklist(Val.Val);
1049
1050 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1051 N->getOperand(2), N->getOperand(3));
1052 DCI.AddToWorklist(Val.Val);
1053 return Val;
1054 }
1055 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001056 }
1057
1058 return SDOperand();
1059}
1060
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001061/// getConstraintType - Given a constraint letter, return the type of
1062/// constraint it is for this target.
1063PPCTargetLowering::ConstraintType
1064PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1065 switch (ConstraintLetter) {
1066 default: break;
1067 case 'b':
1068 case 'r':
1069 case 'f':
1070 case 'v':
1071 case 'y':
1072 return C_RegisterClass;
1073 }
1074 return TargetLowering::getConstraintType(ConstraintLetter);
1075}
1076
1077
Chris Lattnerddc787d2006-01-31 19:20:21 +00001078std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001079getRegClassForInlineAsmConstraint(const std::string &Constraint,
1080 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001081 if (Constraint.size() == 1) {
1082 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1083 default: break; // Unknown constriant letter
1084 case 'b':
1085 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1086 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1087 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1088 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1089 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1090 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1091 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1092 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1093 0);
1094 case 'r':
1095 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1096 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1097 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1098 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1099 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1100 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1101 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1102 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1103 0);
1104 case 'f':
1105 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1106 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1107 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1108 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1109 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1110 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1111 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1112 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1113 0);
1114 case 'v':
1115 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1116 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1117 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1118 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1119 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1120 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1121 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1122 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1123 0);
1124 case 'y':
1125 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1126 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1127 0);
1128 }
1129 }
1130
Chris Lattner1efa40f2006-02-22 00:56:39 +00001131 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001132}
Chris Lattner763317d2006-02-07 00:47:13 +00001133
1134// isOperandValidForConstraint
1135bool PPCTargetLowering::
1136isOperandValidForConstraint(SDOperand Op, char Letter) {
1137 switch (Letter) {
1138 default: break;
1139 case 'I':
1140 case 'J':
1141 case 'K':
1142 case 'L':
1143 case 'M':
1144 case 'N':
1145 case 'O':
1146 case 'P': {
1147 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1148 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1149 switch (Letter) {
1150 default: assert(0 && "Unknown constraint letter!");
1151 case 'I': // "I" is a signed 16-bit constant.
1152 return (short)Value == (int)Value;
1153 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1154 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1155 return (short)Value == 0;
1156 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1157 return (Value >> 16) == 0;
1158 case 'M': // "M" is a constant that is greater than 31.
1159 return Value > 31;
1160 case 'N': // "N" is a positive constant that is an exact power of two.
1161 return (int)Value > 0 && isPowerOf2_32(Value);
1162 case 'O': // "O" is the constant zero.
1163 return Value == 0;
1164 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1165 return (short)-Value == (int)-Value;
1166 }
1167 break;
1168 }
1169 }
1170
1171 // Handle standard constraint letters.
1172 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1173}