blob: 0590b9ed687b297a769a99da7c11d79399fd4957 [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
15#include "PPCTargetMachine.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000016#include "llvm/ADT/VectorExtras.h"
Evan Chengc4c62572006-03-13 23:20:37 +000017#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000021#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000022#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000023#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000024#include "llvm/Function.h"
Nate Begeman750ac1b2006-02-01 07:19:44 +000025#include "llvm/Support/MathExtras.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000027using namespace llvm;
28
Nate Begeman21e463b2005-10-16 05:39:50 +000029PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000030 : TargetLowering(TM) {
31
32 // Fold away setcc operations if possible.
33 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000034 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000035
Chris Lattnerd145a612005-09-27 22:18:25 +000036 // Use _setjmp/_longjmp instead of setjmp/longjmp.
37 setUseUnderscoreSetJmpLongJmp(true);
38
Chris Lattner7c5a3d32005-08-16 17:14:42 +000039 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000040 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
41 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
42 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000043
Chris Lattnera54aa942006-01-29 06:26:08 +000044 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
45 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
46
Chris Lattner7c5a3d32005-08-16 17:14:42 +000047 // PowerPC has no intrinsics for these particular operations
48 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
49 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
50 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
51
52 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
53 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
54 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
55
56 // PowerPC has no SREM/UREM instructions
57 setOperationAction(ISD::SREM, MVT::i32, Expand);
58 setOperationAction(ISD::UREM, MVT::i32, Expand);
59
60 // We don't support sin/cos/sqrt/fmod
61 setOperationAction(ISD::FSIN , MVT::f64, Expand);
62 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000063 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000064 setOperationAction(ISD::FSIN , MVT::f32, Expand);
65 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000066 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000067
68 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000069 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000070 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
71 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
72 }
73
Chris Lattner9601a862006-03-05 05:08:37 +000074 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
75 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
76
Nate Begemand88fc032006-01-14 03:14:10 +000077 // PowerPC does not have BSWAP, CTPOP or CTTZ
78 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000079 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
80 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
81
Nate Begeman35ef9132006-01-11 21:21:00 +000082 // PowerPC does not have ROTR
83 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
84
Chris Lattner7c5a3d32005-08-16 17:14:42 +000085 // PowerPC does not have Select
86 setOperationAction(ISD::SELECT, MVT::i32, Expand);
87 setOperationAction(ISD::SELECT, MVT::f32, Expand);
88 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000089
Chris Lattner0b1e4e52005-08-26 17:36:52 +000090 // PowerPC wants to turn select_cc of FP into fsel when possible.
91 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
92 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000093
Nate Begeman750ac1b2006-02-01 07:19:44 +000094 // PowerPC wants to optimize integer setcc a bit
Nate Begeman44775902006-01-31 08:17:29 +000095 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000096
Nate Begeman7cbd5252005-08-16 19:49:35 +000097 // PowerPC does not have BRCOND* which requires SetCC
98 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
99 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000100
Chris Lattnerf7605322005-08-31 21:09:52 +0000101 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
102 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000103
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000104 // PowerPC does not have [U|S]INT_TO_FP
105 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
106 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
107
Chris Lattner53e88452005-12-23 05:13:35 +0000108 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
109 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
110
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000111 // PowerPC does not have truncstore for i1.
112 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000113
Jim Laskeyabf6d172006-01-05 01:25:28 +0000114 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000115 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000116 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000117 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000118 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000119 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000120
Nate Begeman28a6b022005-12-10 02:36:00 +0000121 // We want to legalize GlobalAddress and ConstantPool nodes into the
122 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000123 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000124 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000125
Nate Begemanee625572006-01-27 21:09:22 +0000126 // RET must be custom lowered, to meet ABI requirements
127 setOperationAction(ISD::RET , MVT::Other, Custom);
128
Nate Begemanacc398c2006-01-25 18:21:52 +0000129 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
130 setOperationAction(ISD::VASTART , MVT::Other, Custom);
131
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000132 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000133 setOperationAction(ISD::VAARG , MVT::Other, Expand);
134 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
135 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000136 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
137 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
138 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000139
Nate Begemanc09eeec2005-09-06 22:03:27 +0000140 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000141 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000142 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
143 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000144 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
145 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
146 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000147 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000148 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000149 }
150
151 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
152 // 64 bit PowerPC implementations can support i64 types directly
153 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000154 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
155 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000156 } else {
157 // 32 bit PowerPC wants to expand i64 shifts itself.
158 setOperationAction(ISD::SHL, MVT::i64, Custom);
159 setOperationAction(ISD::SRL, MVT::i64, Custom);
160 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000161 }
162
Evan Chengd30bf012006-03-01 01:11:20 +0000163 // First set operation action for all vector types to expand. Then we
164 // will selectively turn on ones that can be effectively codegen'd.
165 for (unsigned VT = (unsigned)MVT::Vector + 1;
166 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
167 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
168 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
169 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
170 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
171 }
172
Nate Begeman425a9692005-11-29 08:17:20 +0000173 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000174 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000175 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000176
Evan Chengd30bf012006-03-01 01:11:20 +0000177 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
178 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
179 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
180 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
181 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
182 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000183 // FIXME: We don't support any ConstantVec's yet. We should custom expand
184 // the ones we do!
Chris Lattnerd9b55dd2006-01-29 08:41:37 +0000185 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
186 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000187 }
188
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000189 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000190 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000191
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000192 // We have target-specific dag combine patterns for the following nodes:
193 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000194 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000195
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000196 computeRegisterProperties();
197}
198
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000199const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
200 switch (Opcode) {
201 default: return 0;
202 case PPCISD::FSEL: return "PPCISD::FSEL";
203 case PPCISD::FCFID: return "PPCISD::FCFID";
204 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
205 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000206 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000207 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
208 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
209 case PPCISD::Hi: return "PPCISD::Hi";
210 case PPCISD::Lo: return "PPCISD::Lo";
211 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
212 case PPCISD::SRL: return "PPCISD::SRL";
213 case PPCISD::SRA: return "PPCISD::SRA";
214 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000215 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000216 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
217 }
218}
219
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000220/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
221static bool isFloatingPointZero(SDOperand Op) {
222 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
223 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
224 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
225 // Maybe this has already been legalized into the constant pool?
226 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
227 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
228 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
229 }
230 return false;
231}
232
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000233/// LowerOperation - Provide custom lowering hooks for some operations.
234///
Nate Begeman21e463b2005-10-16 05:39:50 +0000235SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000236 switch (Op.getOpcode()) {
237 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000238 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000239 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000240 SDOperand Src = Op.getOperand(0);
241 if (Src.getValueType() == MVT::f32)
242 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
243
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000244 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000245 switch (Op.getValueType()) {
246 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
247 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000248 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000249 break;
250 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000251 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000252 break;
253 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000254
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000255 // Convert the FP value to an int value through memory.
256 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
257 if (Op.getValueType() == MVT::i32)
258 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
259 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000260 }
261 case ISD::SINT_TO_FP: {
262 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
263 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000264 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
265 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000266 if (MVT::f32 == Op.getValueType())
267 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
268 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000269 }
270 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000271 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000272 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
273 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
274 break;
275
276 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
277
278 // Cannot handle SETEQ/SETNE.
279 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
280
281 MVT::ValueType ResVT = Op.getValueType();
282 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
283 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
284 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000285
Chris Lattnerf7605322005-08-31 21:09:52 +0000286 // If the RHS of the comparison is a 0.0, we don't need to do the
287 // subtraction at all.
288 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000289 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000290 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000291 case ISD::SETULT:
292 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000293 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000294 case ISD::SETUGE:
295 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000296 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
297 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000298 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000299 case ISD::SETUGT:
300 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000301 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000302 case ISD::SETULE:
303 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000304 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
305 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000306 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000307 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000308 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000309
Chris Lattnereb255f22005-10-25 20:54:57 +0000310 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000311 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000312 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000313 case ISD::SETULT:
314 case ISD::SETLT:
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000319 case ISD::SETUGE:
320 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000321 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
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, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000325 case ISD::SETUGT:
326 case ISD::SETGT:
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000331 case ISD::SETULE:
332 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000333 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
334 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
335 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
336 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000337 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000338 break;
339 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000340 case ISD::SHL: {
341 assert(Op.getValueType() == MVT::i64 &&
342 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
343 // The generic code does a fine job expanding shift by a constant.
344 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
345
346 // Otherwise, expand into a bunch of logical ops. Note that these ops
347 // depend on the PPC behavior for oversized shift amounts.
348 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
349 DAG.getConstant(0, MVT::i32));
350 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
351 DAG.getConstant(1, MVT::i32));
352 SDOperand Amt = Op.getOperand(1);
353
354 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
355 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000356 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
357 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000358 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
359 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
360 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000361 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000362 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000363 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000364 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
365 }
366 case ISD::SRL: {
367 assert(Op.getValueType() == MVT::i64 &&
368 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
369 // The generic code does a fine job expanding shift by a constant.
370 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
371
372 // Otherwise, expand into a bunch of logical ops. Note that these ops
373 // depend on the PPC behavior for oversized shift amounts.
374 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
375 DAG.getConstant(0, MVT::i32));
376 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
377 DAG.getConstant(1, MVT::i32));
378 SDOperand Amt = Op.getOperand(1);
379
380 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
381 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000382 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
383 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000384 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
385 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
386 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000387 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000388 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000389 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000390 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
391 }
392 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000393 assert(Op.getValueType() == MVT::i64 &&
394 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
395 // The generic code does a fine job expanding shift by a constant.
396 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
397
398 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
399 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
400 DAG.getConstant(0, MVT::i32));
401 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
402 DAG.getConstant(1, MVT::i32));
403 SDOperand Amt = Op.getOperand(1);
404
405 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
406 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000407 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
408 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000409 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
410 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
411 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000412 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
413 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000414 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
415 Tmp4, Tmp6, ISD::SETLE);
416 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000417 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000418 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000419 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
420 Constant *C = CP->get();
421 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000422 SDOperand Zero = DAG.getConstant(0, MVT::i32);
423
Evan Cheng4c1aa862006-02-22 20:19:42 +0000424 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000425 // Generate non-pic code that has direct accesses to the constant pool.
426 // The address of the global is just (hi(&g)+lo(&g)).
427 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
428 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
429 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
430 }
431
432 // Only lower ConstantPool on Darwin.
433 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
434 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000435 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000436 // With PIC, the first instruction is actually "GR+hi(&G)".
437 Hi = DAG.getNode(ISD::ADD, MVT::i32,
438 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
439 }
440
441 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
442 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
443 return Lo;
444 }
Chris Lattner860e8862005-11-17 07:30:41 +0000445 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000446 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
447 GlobalValue *GV = GSDN->getGlobal();
448 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000449 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000450
Evan Cheng4c1aa862006-02-22 20:19:42 +0000451 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000452 // Generate non-pic code that has direct accesses to globals.
453 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000454 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
455 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
456 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
457 }
Chris Lattner860e8862005-11-17 07:30:41 +0000458
Chris Lattner1d05cb42005-11-17 18:55:48 +0000459 // Only lower GlobalAddress on Darwin.
460 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000461
Chris Lattner860e8862005-11-17 07:30:41 +0000462 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000463 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000464 // With PIC, the first instruction is actually "GR+hi(&G)".
465 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000466 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000467 }
468
469 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
470 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
471
Chris Lattner37dd6f12006-01-29 20:49:17 +0000472 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
473 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000474 return Lo;
475
476 // If the global is weak or external, we have to go through the lazy
477 // resolution stub.
478 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
479 }
Nate Begeman44775902006-01-31 08:17:29 +0000480 case ISD::SETCC: {
481 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000482
483 // If we're comparing for equality to zero, expose the fact that this is
484 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
485 // fold the new nodes.
486 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
487 if (C->isNullValue() && CC == ISD::SETEQ) {
488 MVT::ValueType VT = Op.getOperand(0).getValueType();
489 SDOperand Zext = Op.getOperand(0);
490 if (VT < MVT::i32) {
491 VT = MVT::i32;
492 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
493 }
494 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
495 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
496 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
497 DAG.getConstant(Log2b, getShiftAmountTy()));
498 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
499 }
500 // Leave comparisons against 0 and -1 alone for now, since they're usually
501 // optimized. FIXME: revisit this when we can custom lower all setcc
502 // optimizations.
503 if (C->isAllOnesValue() || C->isNullValue())
504 break;
505 }
506
507 // If we have an integer seteq/setne, turn it into a compare against zero
508 // by subtracting the rhs from the lhs, which is faster than setting a
509 // condition register, reading it back out, and masking the correct bit.
510 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
511 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
512 MVT::ValueType VT = Op.getValueType();
513 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
514 Op.getOperand(1));
515 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
516 }
Nate Begeman44775902006-01-31 08:17:29 +0000517 break;
518 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000519 case ISD::VASTART: {
520 // vastart just stores the address of the VarArgsFrameIndex slot into the
521 // memory location argument.
522 // FIXME: Replace MVT::i32 with PointerTy
523 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
524 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
525 Op.getOperand(1), Op.getOperand(2));
526 }
Nate Begemanee625572006-01-27 21:09:22 +0000527 case ISD::RET: {
528 SDOperand Copy;
529
530 switch(Op.getNumOperands()) {
531 default:
532 assert(0 && "Do not know how to return this many arguments!");
533 abort();
534 case 1:
535 return SDOperand(); // ret void is legal
536 case 2: {
537 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
538 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
539 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
540 SDOperand());
541 break;
542 }
543 case 3:
544 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
545 SDOperand());
546 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
547 break;
548 }
549 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
550 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000551 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000552 return SDOperand();
553}
554
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000555std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000556PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000557 //
558 // add beautiful description of PPC stack frame format, or at least some docs
559 //
560 MachineFunction &MF = DAG.getMachineFunction();
561 MachineFrameInfo *MFI = MF.getFrameInfo();
562 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000563 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000564 std::vector<SDOperand> ArgValues;
565
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000566 unsigned ArgOffset = 24;
567 unsigned GPR_remaining = 8;
568 unsigned FPR_remaining = 13;
569 unsigned GPR_idx = 0, FPR_idx = 0;
570 static const unsigned GPR[] = {
571 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
572 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
573 };
574 static const unsigned FPR[] = {
575 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
576 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
577 };
578
579 // Add DAG nodes to load the arguments... On entry to a function on PPC,
580 // the arguments start at offset 24, although they are likely to be passed
581 // in registers.
582 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
583 SDOperand newroot, argt;
584 unsigned ObjSize;
585 bool needsLoad = false;
586 bool ArgLive = !I->use_empty();
587 MVT::ValueType ObjectVT = getValueType(I->getType());
588
589 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000590 default: assert(0 && "Unhandled argument type!");
591 case MVT::i1:
592 case MVT::i8:
593 case MVT::i16:
594 case MVT::i32:
595 ObjSize = 4;
596 if (!ArgLive) break;
597 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000598 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000599 MF.addLiveIn(GPR[GPR_idx], VReg);
600 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000601 if (ObjectVT != MVT::i32) {
602 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
603 : ISD::AssertZext;
604 argt = DAG.getNode(AssertOp, MVT::i32, argt,
605 DAG.getValueType(ObjectVT));
606 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
607 }
Chris Lattner915fb302005-08-30 00:19:00 +0000608 } else {
609 needsLoad = true;
610 }
611 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000612 case MVT::i64:
613 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000614 if (!ArgLive) break;
615 if (GPR_remaining > 0) {
616 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000617 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000618 MF.addLiveIn(GPR[GPR_idx], VReg);
619 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000620 // If we have two or more remaining argument registers, then both halves
621 // of the i64 can be sourced from there. Otherwise, the lower half will
622 // have to come off the stack. This can happen when an i64 is preceded
623 // by 28 bytes of arguments.
624 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000625 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000626 MF.addLiveIn(GPR[GPR_idx+1], VReg);
627 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000628 } else {
629 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
630 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
631 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
632 DAG.getSrcValue(NULL));
633 }
634 // Build the outgoing arg thingy
635 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
636 newroot = argLo;
637 } else {
638 needsLoad = true;
639 }
640 break;
641 case MVT::f32:
642 case MVT::f64:
643 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000644 if (!ArgLive) {
645 if (FPR_remaining > 0) {
646 --FPR_remaining;
647 ++FPR_idx;
648 }
649 break;
650 }
Chris Lattner915fb302005-08-30 00:19:00 +0000651 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000652 unsigned VReg;
653 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000654 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000655 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000656 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000657 MF.addLiveIn(FPR[FPR_idx], VReg);
658 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000659 --FPR_remaining;
660 ++FPR_idx;
661 } else {
662 needsLoad = true;
663 }
664 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000665 }
666
667 // We need to load the argument to a virtual register if we determined above
668 // that we ran out of physical registers of the appropriate type
669 if (needsLoad) {
670 unsigned SubregOffset = 0;
671 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
672 if (ObjectVT == MVT::i16) SubregOffset = 2;
673 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
674 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
675 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
676 DAG.getConstant(SubregOffset, MVT::i32));
677 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
678 DAG.getSrcValue(NULL));
679 }
680
681 // Every 4 bytes of argument space consumes one of the GPRs available for
682 // argument passing.
683 if (GPR_remaining > 0) {
684 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
685 GPR_remaining -= delta;
686 GPR_idx += delta;
687 }
688 ArgOffset += ObjSize;
689 if (newroot.Val)
690 DAG.setRoot(newroot.getValue(1));
691
692 ArgValues.push_back(argt);
693 }
694
695 // If the function takes variable number of arguments, make a frame index for
696 // the start of the first vararg value... for expansion of llvm.va_start.
697 if (F.isVarArg()) {
698 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
699 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
700 // If this function is vararg, store any remaining integer argument regs
701 // to their spots on the stack so that they may be loaded by deferencing the
702 // result of va_next.
703 std::vector<SDOperand> MemOps;
704 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000705 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000706 MF.addLiveIn(GPR[GPR_idx], VReg);
707 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000708 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
709 Val, FIN, DAG.getSrcValue(NULL));
710 MemOps.push_back(Store);
711 // Increment the address by four for the next argument to store
712 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
713 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
714 }
Chris Lattner80720a92005-11-30 20:40:54 +0000715 if (!MemOps.empty()) {
716 MemOps.push_back(DAG.getRoot());
717 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
718 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000719 }
720
721 // Finally, inform the code generator which regs we return values in.
722 switch (getValueType(F.getReturnType())) {
723 default: assert(0 && "Unknown type!");
724 case MVT::isVoid: break;
725 case MVT::i1:
726 case MVT::i8:
727 case MVT::i16:
728 case MVT::i32:
729 MF.addLiveOut(PPC::R3);
730 break;
731 case MVT::i64:
732 MF.addLiveOut(PPC::R3);
733 MF.addLiveOut(PPC::R4);
734 break;
735 case MVT::f32:
736 case MVT::f64:
737 MF.addLiveOut(PPC::F1);
738 break;
739 }
740
741 return ArgValues;
742}
743
744std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000745PPCTargetLowering::LowerCallTo(SDOperand Chain,
746 const Type *RetTy, bool isVarArg,
747 unsigned CallingConv, bool isTailCall,
748 SDOperand Callee, ArgListTy &Args,
749 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000750 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000751 // SelectExpr to use to put the arguments in the appropriate registers.
752 std::vector<SDOperand> args_to_use;
753
754 // Count how many bytes are to be pushed on the stack, including the linkage
755 // area, and parameter passing area.
756 unsigned NumBytes = 24;
757
758 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000759 Chain = DAG.getCALLSEQ_START(Chain,
760 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000761 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000762 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000763 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000764 default: assert(0 && "Unknown value type!");
765 case MVT::i1:
766 case MVT::i8:
767 case MVT::i16:
768 case MVT::i32:
769 case MVT::f32:
770 NumBytes += 4;
771 break;
772 case MVT::i64:
773 case MVT::f64:
774 NumBytes += 8;
775 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000776 }
Chris Lattner915fb302005-08-30 00:19:00 +0000777 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000778
Chris Lattner915fb302005-08-30 00:19:00 +0000779 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
780 // plus 32 bytes of argument space in case any called code gets funky on us.
781 // (Required by ABI to support var arg)
782 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000783
784 // Adjust the stack pointer for the new arguments...
785 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000786 Chain = DAG.getCALLSEQ_START(Chain,
787 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000788
789 // Set up a copy of the stack pointer for use loading and storing any
790 // arguments that may not fit in the registers available for argument
791 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000792 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000793
794 // Figure out which arguments are going to go in registers, and which in
795 // memory. Also, if this is a vararg function, floating point operations
796 // must be stored to our stack, and loaded into integer regs as well, if
797 // any integer regs are available for argument passing.
798 unsigned ArgOffset = 24;
799 unsigned GPR_remaining = 8;
800 unsigned FPR_remaining = 13;
801
802 std::vector<SDOperand> MemOps;
803 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
804 // PtrOff will be used to store the current argument to the stack if a
805 // register cannot be found for it.
806 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
807 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
808 MVT::ValueType ArgVT = getValueType(Args[i].second);
809
810 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000811 default: assert(0 && "Unexpected ValueType for argument!");
812 case MVT::i1:
813 case MVT::i8:
814 case MVT::i16:
815 // Promote the integer to 32 bits. If the input type is signed use a
816 // sign extend, otherwise use a zero extend.
817 if (Args[i].second->isSigned())
818 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
819 else
820 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
821 // FALL THROUGH
822 case MVT::i32:
823 if (GPR_remaining > 0) {
824 args_to_use.push_back(Args[i].first);
825 --GPR_remaining;
826 } else {
827 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
828 Args[i].first, PtrOff,
829 DAG.getSrcValue(NULL)));
830 }
831 ArgOffset += 4;
832 break;
833 case MVT::i64:
834 // If we have one free GPR left, we can place the upper half of the i64
835 // in it, and store the other half to the stack. If we have two or more
836 // free GPRs, then we can pass both halves of the i64 in registers.
837 if (GPR_remaining > 0) {
838 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
839 Args[i].first, DAG.getConstant(1, MVT::i32));
840 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
841 Args[i].first, DAG.getConstant(0, MVT::i32));
842 args_to_use.push_back(Hi);
843 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000844 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000845 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000846 --GPR_remaining;
847 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000848 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
849 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000850 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000851 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000852 }
Chris Lattner915fb302005-08-30 00:19:00 +0000853 } else {
854 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
855 Args[i].first, PtrOff,
856 DAG.getSrcValue(NULL)));
857 }
858 ArgOffset += 8;
859 break;
860 case MVT::f32:
861 case MVT::f64:
862 if (FPR_remaining > 0) {
863 args_to_use.push_back(Args[i].first);
864 --FPR_remaining;
865 if (isVarArg) {
866 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
867 Args[i].first, PtrOff,
868 DAG.getSrcValue(NULL));
869 MemOps.push_back(Store);
870 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000871 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000872 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
873 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000874 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000875 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000876 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000877 }
878 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000879 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
880 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000881 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
882 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000883 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000884 args_to_use.push_back(Load);
885 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000886 }
887 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000888 // If we have any FPRs remaining, we may also have GPRs remaining.
889 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
890 // GPRs.
891 if (GPR_remaining > 0) {
892 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
893 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000894 }
Chris Lattner915fb302005-08-30 00:19:00 +0000895 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
896 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
897 --GPR_remaining;
898 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000899 }
Chris Lattner915fb302005-08-30 00:19:00 +0000900 } else {
901 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
902 Args[i].first, PtrOff,
903 DAG.getSrcValue(NULL)));
904 }
905 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
906 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000907 }
908 }
909 if (!MemOps.empty())
910 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
911 }
912
913 std::vector<MVT::ValueType> RetVals;
914 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000915 MVT::ValueType ActualRetTyVT = RetTyVT;
916 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
917 ActualRetTyVT = MVT::i32; // Promote result to i32.
918
Chris Lattnere00ebf02006-01-28 07:33:03 +0000919 if (RetTyVT == MVT::i64) {
920 RetVals.push_back(MVT::i32);
921 RetVals.push_back(MVT::i32);
922 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000923 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000924 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000925 RetVals.push_back(MVT::Other);
926
Chris Lattner2823b3e2005-11-17 05:56:14 +0000927 // If the callee is a GlobalAddress node (quite common, every direct call is)
928 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
929 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
930 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
931
Chris Lattner281b55e2006-01-27 23:34:02 +0000932 std::vector<SDOperand> Ops;
933 Ops.push_back(Chain);
934 Ops.push_back(Callee);
935 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
936 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000937 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000938 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
939 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000940 SDOperand RetVal = TheCall;
941
942 // If the result is a small value, add a note so that we keep track of the
943 // information about whether it is sign or zero extended.
944 if (RetTyVT != ActualRetTyVT) {
945 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
946 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
947 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000948 } else if (RetTyVT == MVT::i64) {
949 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000950 }
951
952 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000953}
954
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000955MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000956PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
957 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000958 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000959 MI->getOpcode() == PPC::SELECT_CC_F4 ||
960 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000961 "Unexpected instr type to insert");
962
963 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
964 // control-flow pattern. The incoming instruction knows the destination vreg
965 // to set, the condition code register to branch on, the true/false values to
966 // select between, and a branch opcode to use.
967 const BasicBlock *LLVM_BB = BB->getBasicBlock();
968 ilist<MachineBasicBlock>::iterator It = BB;
969 ++It;
970
971 // thisMBB:
972 // ...
973 // TrueVal = ...
974 // cmpTY ccX, r1, r2
975 // bCC copy1MBB
976 // fallthrough --> copy0MBB
977 MachineBasicBlock *thisMBB = BB;
978 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
979 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
980 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
981 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
982 MachineFunction *F = BB->getParent();
983 F->getBasicBlockList().insert(It, copy0MBB);
984 F->getBasicBlockList().insert(It, sinkMBB);
985 // Update machine-CFG edges
986 BB->addSuccessor(copy0MBB);
987 BB->addSuccessor(sinkMBB);
988
989 // copy0MBB:
990 // %FalseValue = ...
991 // # fallthrough to sinkMBB
992 BB = copy0MBB;
993
994 // Update machine-CFG edges
995 BB->addSuccessor(sinkMBB);
996
997 // sinkMBB:
998 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
999 // ...
1000 BB = sinkMBB;
1001 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1002 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1003 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1004
1005 delete MI; // The pseudo instruction is gone now.
1006 return BB;
1007}
1008
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001009SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1010 DAGCombinerInfo &DCI) const {
1011 TargetMachine &TM = getTargetMachine();
1012 SelectionDAG &DAG = DCI.DAG;
1013 switch (N->getOpcode()) {
1014 default: break;
1015 case ISD::SINT_TO_FP:
1016 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
1017 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1018 // We allow the src/dst to be either f32/f64, but force the intermediate
1019 // type to be i64.
1020 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT &&
1021 N->getOperand(0).getValueType() == MVT::i64) {
1022
1023 SDOperand Val = N->getOperand(0).getOperand(0);
1024 if (Val.getValueType() == MVT::f32) {
1025 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1026 DCI.AddToWorklist(Val.Val);
1027 }
1028
1029 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
1030 DCI.AddToWorklist(Val.Val);
1031 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
1032 DCI.AddToWorklist(Val.Val);
1033 if (N->getValueType(0) == MVT::f32) {
1034 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1035 DCI.AddToWorklist(Val.Val);
1036 }
1037 return Val;
1038 }
1039 }
1040 break;
Chris Lattner51269842006-03-01 05:50:56 +00001041 case ISD::STORE:
1042 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1043 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1044 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1045 N->getOperand(1).getValueType() == MVT::i32) {
1046 SDOperand Val = N->getOperand(1).getOperand(0);
1047 if (Val.getValueType() == MVT::f32) {
1048 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1049 DCI.AddToWorklist(Val.Val);
1050 }
1051 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1052 DCI.AddToWorklist(Val.Val);
1053
1054 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1055 N->getOperand(2), N->getOperand(3));
1056 DCI.AddToWorklist(Val.Val);
1057 return Val;
1058 }
1059 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001060 }
1061
1062 return SDOperand();
1063}
1064
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001065/// getConstraintType - Given a constraint letter, return the type of
1066/// constraint it is for this target.
1067PPCTargetLowering::ConstraintType
1068PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1069 switch (ConstraintLetter) {
1070 default: break;
1071 case 'b':
1072 case 'r':
1073 case 'f':
1074 case 'v':
1075 case 'y':
1076 return C_RegisterClass;
1077 }
1078 return TargetLowering::getConstraintType(ConstraintLetter);
1079}
1080
1081
Chris Lattnerddc787d2006-01-31 19:20:21 +00001082std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001083getRegClassForInlineAsmConstraint(const std::string &Constraint,
1084 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001085 if (Constraint.size() == 1) {
1086 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1087 default: break; // Unknown constriant letter
1088 case 'b':
1089 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1090 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1091 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1092 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1093 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1094 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1095 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1096 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1097 0);
1098 case 'r':
1099 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1100 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1101 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1102 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1103 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1104 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1105 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1106 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1107 0);
1108 case 'f':
1109 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1110 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1111 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1112 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1113 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1114 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1115 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1116 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1117 0);
1118 case 'v':
1119 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1120 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1121 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1122 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1123 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1124 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1125 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1126 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1127 0);
1128 case 'y':
1129 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1130 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1131 0);
1132 }
1133 }
1134
Chris Lattner1efa40f2006-02-22 00:56:39 +00001135 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001136}
Chris Lattner763317d2006-02-07 00:47:13 +00001137
1138// isOperandValidForConstraint
1139bool PPCTargetLowering::
1140isOperandValidForConstraint(SDOperand Op, char Letter) {
1141 switch (Letter) {
1142 default: break;
1143 case 'I':
1144 case 'J':
1145 case 'K':
1146 case 'L':
1147 case 'M':
1148 case 'N':
1149 case 'O':
1150 case 'P': {
1151 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1152 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1153 switch (Letter) {
1154 default: assert(0 && "Unknown constraint letter!");
1155 case 'I': // "I" is a signed 16-bit constant.
1156 return (short)Value == (int)Value;
1157 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1158 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1159 return (short)Value == 0;
1160 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1161 return (Value >> 16) == 0;
1162 case 'M': // "M" is a constant that is greater than 31.
1163 return Value > 31;
1164 case 'N': // "N" is a positive constant that is an exact power of two.
1165 return (int)Value > 0 && isPowerOf2_32(Value);
1166 case 'O': // "O" is the constant zero.
1167 return Value == 0;
1168 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1169 return (short)-Value == (int)-Value;
1170 }
1171 break;
1172 }
1173 }
1174
1175 // Handle standard constraint letters.
1176 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1177}
Evan Chengc4c62572006-03-13 23:20:37 +00001178
1179/// isLegalAddressImmediate - Return true if the integer value can be used
1180/// as the offset of the target addressing mode.
1181bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1182 // PPC allows a sign-extended 16-bit immediate field.
1183 return (V > -(1 << 16) && V < (1 << 16)-1);
1184}