blob: 68f84f6e21b39611d8680e0981ab50217aafd4e6 [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 Begeman81e80972006-03-17 01:40:33 +000097 // PowerPC does not have BRCOND which requires SetCC
98 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000099
Chris Lattnerf7605322005-08-31 21:09:52 +0000100 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
101 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000102
Jim Laskeyad23c9d2005-08-17 00:40:22 +0000103 // PowerPC does not have [U|S]INT_TO_FP
104 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
105 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
106
Chris Lattner53e88452005-12-23 05:13:35 +0000107 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
108 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
109
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000110 // PowerPC does not have truncstore for i1.
111 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000112
Jim Laskeyabf6d172006-01-05 01:25:28 +0000113 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000114 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000115 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000116 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000117 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000118 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000119
Nate Begeman28a6b022005-12-10 02:36:00 +0000120 // We want to legalize GlobalAddress and ConstantPool nodes into the
121 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000122 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000123 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000124
Nate Begemanee625572006-01-27 21:09:22 +0000125 // RET must be custom lowered, to meet ABI requirements
126 setOperationAction(ISD::RET , MVT::Other, Custom);
127
Nate Begemanacc398c2006-01-25 18:21:52 +0000128 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
129 setOperationAction(ISD::VASTART , MVT::Other, Custom);
130
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000131 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000132 setOperationAction(ISD::VAARG , MVT::Other, Expand);
133 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
134 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000135 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
136 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
137 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000138
Nate Begemanc09eeec2005-09-06 22:03:27 +0000139 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000140 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000141 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
142 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000143 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
144 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
145 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000146 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000147 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000148 }
149
150 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
151 // 64 bit PowerPC implementations can support i64 types directly
152 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000153 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
154 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000155 } else {
156 // 32 bit PowerPC wants to expand i64 shifts itself.
157 setOperationAction(ISD::SHL, MVT::i64, Custom);
158 setOperationAction(ISD::SRL, MVT::i64, Custom);
159 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000160 }
161
Evan Chengd30bf012006-03-01 01:11:20 +0000162 // First set operation action for all vector types to expand. Then we
163 // will selectively turn on ones that can be effectively codegen'd.
164 for (unsigned VT = (unsigned)MVT::Vector + 1;
165 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
166 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
167 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
168 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
169 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
170 }
171
Nate Begeman425a9692005-11-29 08:17:20 +0000172 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000173 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000174 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000175
Evan Chengd30bf012006-03-01 01:11:20 +0000176 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
177 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
178 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
179 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
180 setOperationAction(ISD::ADD , MVT::v4i32, Legal);
181 setOperationAction(ISD::LOAD , MVT::v4i32, Legal);
Chris Lattnera064d282006-03-19 01:13:28 +0000182 // FIXME: We don't support any BUILD_VECTOR's yet. We should custom expand
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000183 // the ones we do!
Chris Lattnera064d282006-03-19 01:13:28 +0000184 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Expand);
185 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
Chris Lattnerab515b02006-03-19 06:17:19 +0000186
187 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Expand);
188 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000189 }
190
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000191 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000192 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000193
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000194 // We have target-specific dag combine patterns for the following nodes:
195 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000196 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000197
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000198 computeRegisterProperties();
199}
200
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000201const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
202 switch (Opcode) {
203 default: return 0;
204 case PPCISD::FSEL: return "PPCISD::FSEL";
205 case PPCISD::FCFID: return "PPCISD::FCFID";
206 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
207 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000208 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000209 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
210 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
211 case PPCISD::Hi: return "PPCISD::Hi";
212 case PPCISD::Lo: return "PPCISD::Lo";
213 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
214 case PPCISD::SRL: return "PPCISD::SRL";
215 case PPCISD::SRA: return "PPCISD::SRA";
216 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000217 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000218 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
219 }
220}
221
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000222/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
223static bool isFloatingPointZero(SDOperand Op) {
224 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
225 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
226 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
227 // Maybe this has already been legalized into the constant pool?
228 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
229 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
230 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
231 }
232 return false;
233}
234
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000235/// LowerOperation - Provide custom lowering hooks for some operations.
236///
Nate Begeman21e463b2005-10-16 05:39:50 +0000237SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000238 switch (Op.getOpcode()) {
239 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000240 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000241 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000242 SDOperand Src = Op.getOperand(0);
243 if (Src.getValueType() == MVT::f32)
244 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
245
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000246 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000247 switch (Op.getValueType()) {
248 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
249 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000250 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000251 break;
252 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000253 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000254 break;
255 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000256
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000257 // Convert the FP value to an int value through memory.
258 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
259 if (Op.getValueType() == MVT::i32)
260 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
261 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000262 }
263 case ISD::SINT_TO_FP: {
264 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
265 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000266 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
267 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000268 if (MVT::f32 == Op.getValueType())
269 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
270 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000271 }
272 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000273 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000274 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
275 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
276 break;
277
278 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
279
280 // Cannot handle SETEQ/SETNE.
281 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
282
283 MVT::ValueType ResVT = Op.getValueType();
284 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
285 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
286 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000287
Chris Lattnerf7605322005-08-31 21:09:52 +0000288 // If the RHS of the comparison is a 0.0, we don't need to do the
289 // subtraction at all.
290 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000291 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000292 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000293 case ISD::SETULT:
294 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000295 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000296 case ISD::SETUGE:
297 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000298 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
299 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000300 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000301 case ISD::SETUGT:
302 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000303 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000304 case ISD::SETULE:
305 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000306 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
307 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000308 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000309 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000310 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000311
Chris Lattnereb255f22005-10-25 20:54:57 +0000312 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000313 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000314 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000315 case ISD::SETULT:
316 case ISD::SETLT:
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000321 case ISD::SETUGE:
322 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000323 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
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, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000327 case ISD::SETUGT:
328 case ISD::SETGT:
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000333 case ISD::SETULE:
334 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000335 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
336 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
337 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
338 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000339 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000340 break;
341 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000342 case ISD::SHL: {
343 assert(Op.getValueType() == MVT::i64 &&
344 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
345 // The generic code does a fine job expanding shift by a constant.
346 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
347
348 // Otherwise, expand into a bunch of logical ops. Note that these ops
349 // depend on the PPC behavior for oversized shift amounts.
350 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
351 DAG.getConstant(0, MVT::i32));
352 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
353 DAG.getConstant(1, MVT::i32));
354 SDOperand Amt = Op.getOperand(1);
355
356 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
357 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000358 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
359 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000360 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
361 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
362 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000363 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000364 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000365 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000366 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
367 }
368 case ISD::SRL: {
369 assert(Op.getValueType() == MVT::i64 &&
370 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
371 // The generic code does a fine job expanding shift by a constant.
372 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
373
374 // Otherwise, expand into a bunch of logical ops. Note that these ops
375 // depend on the PPC behavior for oversized shift amounts.
376 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
377 DAG.getConstant(0, MVT::i32));
378 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
379 DAG.getConstant(1, MVT::i32));
380 SDOperand Amt = Op.getOperand(1);
381
382 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
383 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000384 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
385 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000386 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
387 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
388 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000389 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000390 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000391 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000392 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
393 }
394 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000395 assert(Op.getValueType() == MVT::i64 &&
396 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
397 // The generic code does a fine job expanding shift by a constant.
398 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
399
400 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
401 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
402 DAG.getConstant(0, MVT::i32));
403 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
404 DAG.getConstant(1, MVT::i32));
405 SDOperand Amt = Op.getOperand(1);
406
407 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
408 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000409 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
410 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000411 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
412 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
413 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000414 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
415 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000416 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
417 Tmp4, Tmp6, ISD::SETLE);
418 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000419 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000420 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000421 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
422 Constant *C = CP->get();
423 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000424 SDOperand Zero = DAG.getConstant(0, MVT::i32);
425
Evan Cheng4c1aa862006-02-22 20:19:42 +0000426 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000427 // Generate non-pic code that has direct accesses to the constant pool.
428 // The address of the global is just (hi(&g)+lo(&g)).
429 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
430 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
431 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
432 }
433
434 // Only lower ConstantPool on Darwin.
435 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
436 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000437 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000438 // With PIC, the first instruction is actually "GR+hi(&G)".
439 Hi = DAG.getNode(ISD::ADD, MVT::i32,
440 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
441 }
442
443 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
444 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
445 return Lo;
446 }
Chris Lattner860e8862005-11-17 07:30:41 +0000447 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000448 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
449 GlobalValue *GV = GSDN->getGlobal();
450 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000451 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000452
Evan Cheng4c1aa862006-02-22 20:19:42 +0000453 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000454 // Generate non-pic code that has direct accesses to globals.
455 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000456 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
457 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
458 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
459 }
Chris Lattner860e8862005-11-17 07:30:41 +0000460
Chris Lattner1d05cb42005-11-17 18:55:48 +0000461 // Only lower GlobalAddress on Darwin.
462 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000463
Chris Lattner860e8862005-11-17 07:30:41 +0000464 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000465 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000466 // With PIC, the first instruction is actually "GR+hi(&G)".
467 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000468 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000469 }
470
471 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
472 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
473
Chris Lattner37dd6f12006-01-29 20:49:17 +0000474 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
475 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000476 return Lo;
477
478 // If the global is weak or external, we have to go through the lazy
479 // resolution stub.
480 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
481 }
Nate Begeman44775902006-01-31 08:17:29 +0000482 case ISD::SETCC: {
483 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000484
485 // If we're comparing for equality to zero, expose the fact that this is
486 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
487 // fold the new nodes.
488 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
489 if (C->isNullValue() && CC == ISD::SETEQ) {
490 MVT::ValueType VT = Op.getOperand(0).getValueType();
491 SDOperand Zext = Op.getOperand(0);
492 if (VT < MVT::i32) {
493 VT = MVT::i32;
494 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
495 }
496 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
497 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
498 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
499 DAG.getConstant(Log2b, getShiftAmountTy()));
500 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
501 }
502 // Leave comparisons against 0 and -1 alone for now, since they're usually
503 // optimized. FIXME: revisit this when we can custom lower all setcc
504 // optimizations.
505 if (C->isAllOnesValue() || C->isNullValue())
506 break;
507 }
508
509 // If we have an integer seteq/setne, turn it into a compare against zero
510 // by subtracting the rhs from the lhs, which is faster than setting a
511 // condition register, reading it back out, and masking the correct bit.
512 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
513 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
514 MVT::ValueType VT = Op.getValueType();
515 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
516 Op.getOperand(1));
517 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
518 }
Nate Begeman44775902006-01-31 08:17:29 +0000519 break;
520 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000521 case ISD::VASTART: {
522 // vastart just stores the address of the VarArgsFrameIndex slot into the
523 // memory location argument.
524 // FIXME: Replace MVT::i32 with PointerTy
525 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
526 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
527 Op.getOperand(1), Op.getOperand(2));
528 }
Nate Begemanee625572006-01-27 21:09:22 +0000529 case ISD::RET: {
530 SDOperand Copy;
531
532 switch(Op.getNumOperands()) {
533 default:
534 assert(0 && "Do not know how to return this many arguments!");
535 abort();
536 case 1:
537 return SDOperand(); // ret void is legal
538 case 2: {
539 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
540 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
541 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
542 SDOperand());
543 break;
544 }
545 case 3:
546 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
547 SDOperand());
548 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
549 break;
550 }
551 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
552 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000553 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000554 return SDOperand();
555}
556
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000557std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000558PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000559 //
560 // add beautiful description of PPC stack frame format, or at least some docs
561 //
562 MachineFunction &MF = DAG.getMachineFunction();
563 MachineFrameInfo *MFI = MF.getFrameInfo();
564 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000565 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000566 std::vector<SDOperand> ArgValues;
567
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000568 unsigned ArgOffset = 24;
569 unsigned GPR_remaining = 8;
570 unsigned FPR_remaining = 13;
571 unsigned GPR_idx = 0, FPR_idx = 0;
572 static const unsigned GPR[] = {
573 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
574 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
575 };
576 static const unsigned FPR[] = {
577 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
578 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
579 };
580
581 // Add DAG nodes to load the arguments... On entry to a function on PPC,
582 // the arguments start at offset 24, although they are likely to be passed
583 // in registers.
584 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
585 SDOperand newroot, argt;
586 unsigned ObjSize;
587 bool needsLoad = false;
588 bool ArgLive = !I->use_empty();
589 MVT::ValueType ObjectVT = getValueType(I->getType());
590
591 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000592 default: assert(0 && "Unhandled argument type!");
593 case MVT::i1:
594 case MVT::i8:
595 case MVT::i16:
596 case MVT::i32:
597 ObjSize = 4;
598 if (!ArgLive) break;
599 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000600 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000601 MF.addLiveIn(GPR[GPR_idx], VReg);
602 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000603 if (ObjectVT != MVT::i32) {
604 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
605 : ISD::AssertZext;
606 argt = DAG.getNode(AssertOp, MVT::i32, argt,
607 DAG.getValueType(ObjectVT));
608 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
609 }
Chris Lattner915fb302005-08-30 00:19:00 +0000610 } else {
611 needsLoad = true;
612 }
613 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000614 case MVT::i64:
615 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000616 if (!ArgLive) break;
617 if (GPR_remaining > 0) {
618 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000619 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000620 MF.addLiveIn(GPR[GPR_idx], VReg);
621 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000622 // If we have two or more remaining argument registers, then both halves
623 // of the i64 can be sourced from there. Otherwise, the lower half will
624 // have to come off the stack. This can happen when an i64 is preceded
625 // by 28 bytes of arguments.
626 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000627 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000628 MF.addLiveIn(GPR[GPR_idx+1], VReg);
629 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000630 } else {
631 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
632 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
633 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
634 DAG.getSrcValue(NULL));
635 }
636 // Build the outgoing arg thingy
637 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
638 newroot = argLo;
639 } else {
640 needsLoad = true;
641 }
642 break;
643 case MVT::f32:
644 case MVT::f64:
645 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000646 if (!ArgLive) {
647 if (FPR_remaining > 0) {
648 --FPR_remaining;
649 ++FPR_idx;
650 }
651 break;
652 }
Chris Lattner915fb302005-08-30 00:19:00 +0000653 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000654 unsigned VReg;
655 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000656 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000657 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000658 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000659 MF.addLiveIn(FPR[FPR_idx], VReg);
660 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000661 --FPR_remaining;
662 ++FPR_idx;
663 } else {
664 needsLoad = true;
665 }
666 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000667 }
668
669 // We need to load the argument to a virtual register if we determined above
670 // that we ran out of physical registers of the appropriate type
671 if (needsLoad) {
672 unsigned SubregOffset = 0;
673 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
674 if (ObjectVT == MVT::i16) SubregOffset = 2;
675 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
676 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
677 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
678 DAG.getConstant(SubregOffset, MVT::i32));
679 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
680 DAG.getSrcValue(NULL));
681 }
682
683 // Every 4 bytes of argument space consumes one of the GPRs available for
684 // argument passing.
685 if (GPR_remaining > 0) {
686 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
687 GPR_remaining -= delta;
688 GPR_idx += delta;
689 }
690 ArgOffset += ObjSize;
691 if (newroot.Val)
692 DAG.setRoot(newroot.getValue(1));
693
694 ArgValues.push_back(argt);
695 }
696
697 // If the function takes variable number of arguments, make a frame index for
698 // the start of the first vararg value... for expansion of llvm.va_start.
699 if (F.isVarArg()) {
700 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
701 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
702 // If this function is vararg, store any remaining integer argument regs
703 // to their spots on the stack so that they may be loaded by deferencing the
704 // result of va_next.
705 std::vector<SDOperand> MemOps;
706 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000707 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000708 MF.addLiveIn(GPR[GPR_idx], VReg);
709 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000710 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
711 Val, FIN, DAG.getSrcValue(NULL));
712 MemOps.push_back(Store);
713 // Increment the address by four for the next argument to store
714 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
715 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
716 }
Chris Lattner80720a92005-11-30 20:40:54 +0000717 if (!MemOps.empty()) {
718 MemOps.push_back(DAG.getRoot());
719 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
720 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000721 }
722
723 // Finally, inform the code generator which regs we return values in.
724 switch (getValueType(F.getReturnType())) {
725 default: assert(0 && "Unknown type!");
726 case MVT::isVoid: break;
727 case MVT::i1:
728 case MVT::i8:
729 case MVT::i16:
730 case MVT::i32:
731 MF.addLiveOut(PPC::R3);
732 break;
733 case MVT::i64:
734 MF.addLiveOut(PPC::R3);
735 MF.addLiveOut(PPC::R4);
736 break;
737 case MVT::f32:
738 case MVT::f64:
739 MF.addLiveOut(PPC::F1);
740 break;
741 }
742
743 return ArgValues;
744}
745
746std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000747PPCTargetLowering::LowerCallTo(SDOperand Chain,
748 const Type *RetTy, bool isVarArg,
749 unsigned CallingConv, bool isTailCall,
750 SDOperand Callee, ArgListTy &Args,
751 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000752 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000753 // SelectExpr to use to put the arguments in the appropriate registers.
754 std::vector<SDOperand> args_to_use;
755
756 // Count how many bytes are to be pushed on the stack, including the linkage
757 // area, and parameter passing area.
758 unsigned NumBytes = 24;
759
760 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000761 Chain = DAG.getCALLSEQ_START(Chain,
762 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000763 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000764 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000765 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000766 default: assert(0 && "Unknown value type!");
767 case MVT::i1:
768 case MVT::i8:
769 case MVT::i16:
770 case MVT::i32:
771 case MVT::f32:
772 NumBytes += 4;
773 break;
774 case MVT::i64:
775 case MVT::f64:
776 NumBytes += 8;
777 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000778 }
Chris Lattner915fb302005-08-30 00:19:00 +0000779 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000780
Chris Lattner915fb302005-08-30 00:19:00 +0000781 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
782 // plus 32 bytes of argument space in case any called code gets funky on us.
783 // (Required by ABI to support var arg)
784 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000785
786 // Adjust the stack pointer for the new arguments...
787 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000788 Chain = DAG.getCALLSEQ_START(Chain,
789 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000790
791 // Set up a copy of the stack pointer for use loading and storing any
792 // arguments that may not fit in the registers available for argument
793 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000794 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000795
796 // Figure out which arguments are going to go in registers, and which in
797 // memory. Also, if this is a vararg function, floating point operations
798 // must be stored to our stack, and loaded into integer regs as well, if
799 // any integer regs are available for argument passing.
800 unsigned ArgOffset = 24;
801 unsigned GPR_remaining = 8;
802 unsigned FPR_remaining = 13;
803
804 std::vector<SDOperand> MemOps;
805 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
806 // PtrOff will be used to store the current argument to the stack if a
807 // register cannot be found for it.
808 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
809 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
810 MVT::ValueType ArgVT = getValueType(Args[i].second);
811
812 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000813 default: assert(0 && "Unexpected ValueType for argument!");
814 case MVT::i1:
815 case MVT::i8:
816 case MVT::i16:
817 // Promote the integer to 32 bits. If the input type is signed use a
818 // sign extend, otherwise use a zero extend.
819 if (Args[i].second->isSigned())
820 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
821 else
822 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
823 // FALL THROUGH
824 case MVT::i32:
825 if (GPR_remaining > 0) {
826 args_to_use.push_back(Args[i].first);
827 --GPR_remaining;
828 } else {
829 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
830 Args[i].first, PtrOff,
831 DAG.getSrcValue(NULL)));
832 }
833 ArgOffset += 4;
834 break;
835 case MVT::i64:
836 // If we have one free GPR left, we can place the upper half of the i64
837 // in it, and store the other half to the stack. If we have two or more
838 // free GPRs, then we can pass both halves of the i64 in registers.
839 if (GPR_remaining > 0) {
840 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
841 Args[i].first, DAG.getConstant(1, MVT::i32));
842 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
843 Args[i].first, DAG.getConstant(0, MVT::i32));
844 args_to_use.push_back(Hi);
845 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000846 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000847 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000848 --GPR_remaining;
849 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000850 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
851 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000852 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000853 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000854 }
Chris Lattner915fb302005-08-30 00:19:00 +0000855 } else {
856 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
857 Args[i].first, PtrOff,
858 DAG.getSrcValue(NULL)));
859 }
860 ArgOffset += 8;
861 break;
862 case MVT::f32:
863 case MVT::f64:
864 if (FPR_remaining > 0) {
865 args_to_use.push_back(Args[i].first);
866 --FPR_remaining;
867 if (isVarArg) {
868 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
869 Args[i].first, PtrOff,
870 DAG.getSrcValue(NULL));
871 MemOps.push_back(Store);
872 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000873 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000874 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
875 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000876 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000877 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000878 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000879 }
880 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000881 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
882 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000883 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
884 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000885 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000886 args_to_use.push_back(Load);
887 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000888 }
889 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000890 // If we have any FPRs remaining, we may also have GPRs remaining.
891 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
892 // GPRs.
893 if (GPR_remaining > 0) {
894 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
895 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000896 }
Chris Lattner915fb302005-08-30 00:19:00 +0000897 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
898 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
899 --GPR_remaining;
900 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000901 }
Chris Lattner915fb302005-08-30 00:19:00 +0000902 } else {
903 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
904 Args[i].first, PtrOff,
905 DAG.getSrcValue(NULL)));
906 }
907 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
908 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000909 }
910 }
911 if (!MemOps.empty())
912 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
913 }
914
915 std::vector<MVT::ValueType> RetVals;
916 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000917 MVT::ValueType ActualRetTyVT = RetTyVT;
918 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
919 ActualRetTyVT = MVT::i32; // Promote result to i32.
920
Chris Lattnere00ebf02006-01-28 07:33:03 +0000921 if (RetTyVT == MVT::i64) {
922 RetVals.push_back(MVT::i32);
923 RetVals.push_back(MVT::i32);
924 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000925 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000926 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000927 RetVals.push_back(MVT::Other);
928
Chris Lattner2823b3e2005-11-17 05:56:14 +0000929 // If the callee is a GlobalAddress node (quite common, every direct call is)
930 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
931 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
932 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
933
Chris Lattner281b55e2006-01-27 23:34:02 +0000934 std::vector<SDOperand> Ops;
935 Ops.push_back(Chain);
936 Ops.push_back(Callee);
937 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
938 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000939 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000940 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
941 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000942 SDOperand RetVal = TheCall;
943
944 // If the result is a small value, add a note so that we keep track of the
945 // information about whether it is sign or zero extended.
946 if (RetTyVT != ActualRetTyVT) {
947 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
948 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
949 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000950 } else if (RetTyVT == MVT::i64) {
951 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000952 }
953
954 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000955}
956
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000957MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000958PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
959 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000960 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000961 MI->getOpcode() == PPC::SELECT_CC_F4 ||
962 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000963 "Unexpected instr type to insert");
964
965 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
966 // control-flow pattern. The incoming instruction knows the destination vreg
967 // to set, the condition code register to branch on, the true/false values to
968 // select between, and a branch opcode to use.
969 const BasicBlock *LLVM_BB = BB->getBasicBlock();
970 ilist<MachineBasicBlock>::iterator It = BB;
971 ++It;
972
973 // thisMBB:
974 // ...
975 // TrueVal = ...
976 // cmpTY ccX, r1, r2
977 // bCC copy1MBB
978 // fallthrough --> copy0MBB
979 MachineBasicBlock *thisMBB = BB;
980 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
981 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
982 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
983 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
984 MachineFunction *F = BB->getParent();
985 F->getBasicBlockList().insert(It, copy0MBB);
986 F->getBasicBlockList().insert(It, sinkMBB);
987 // Update machine-CFG edges
988 BB->addSuccessor(copy0MBB);
989 BB->addSuccessor(sinkMBB);
990
991 // copy0MBB:
992 // %FalseValue = ...
993 // # fallthrough to sinkMBB
994 BB = copy0MBB;
995
996 // Update machine-CFG edges
997 BB->addSuccessor(sinkMBB);
998
999 // sinkMBB:
1000 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1001 // ...
1002 BB = sinkMBB;
1003 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1004 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1005 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1006
1007 delete MI; // The pseudo instruction is gone now.
1008 return BB;
1009}
1010
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001011SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1012 DAGCombinerInfo &DCI) const {
1013 TargetMachine &TM = getTargetMachine();
1014 SelectionDAG &DAG = DCI.DAG;
1015 switch (N->getOpcode()) {
1016 default: break;
1017 case ISD::SINT_TO_FP:
1018 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
1019 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1020 // We allow the src/dst to be either f32/f64, but force the intermediate
1021 // type to be i64.
1022 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT &&
1023 N->getOperand(0).getValueType() == MVT::i64) {
1024
1025 SDOperand Val = N->getOperand(0).getOperand(0);
1026 if (Val.getValueType() == MVT::f32) {
1027 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1028 DCI.AddToWorklist(Val.Val);
1029 }
1030
1031 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
1032 DCI.AddToWorklist(Val.Val);
1033 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
1034 DCI.AddToWorklist(Val.Val);
1035 if (N->getValueType(0) == MVT::f32) {
1036 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1037 DCI.AddToWorklist(Val.Val);
1038 }
1039 return Val;
1040 }
1041 }
1042 break;
Chris Lattner51269842006-03-01 05:50:56 +00001043 case ISD::STORE:
1044 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1045 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1046 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1047 N->getOperand(1).getValueType() == MVT::i32) {
1048 SDOperand Val = N->getOperand(1).getOperand(0);
1049 if (Val.getValueType() == MVT::f32) {
1050 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1051 DCI.AddToWorklist(Val.Val);
1052 }
1053 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1054 DCI.AddToWorklist(Val.Val);
1055
1056 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1057 N->getOperand(2), N->getOperand(3));
1058 DCI.AddToWorklist(Val.Val);
1059 return Val;
1060 }
1061 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001062 }
1063
1064 return SDOperand();
1065}
1066
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001067/// getConstraintType - Given a constraint letter, return the type of
1068/// constraint it is for this target.
1069PPCTargetLowering::ConstraintType
1070PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1071 switch (ConstraintLetter) {
1072 default: break;
1073 case 'b':
1074 case 'r':
1075 case 'f':
1076 case 'v':
1077 case 'y':
1078 return C_RegisterClass;
1079 }
1080 return TargetLowering::getConstraintType(ConstraintLetter);
1081}
1082
1083
Chris Lattnerddc787d2006-01-31 19:20:21 +00001084std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001085getRegClassForInlineAsmConstraint(const std::string &Constraint,
1086 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001087 if (Constraint.size() == 1) {
1088 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1089 default: break; // Unknown constriant letter
1090 case 'b':
1091 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1092 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1093 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1094 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1095 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1096 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1097 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1098 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1099 0);
1100 case 'r':
1101 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1102 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1103 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1104 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1105 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1106 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1107 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1108 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1109 0);
1110 case 'f':
1111 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1112 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1113 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1114 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1115 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1116 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1117 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1118 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1119 0);
1120 case 'v':
1121 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1122 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1123 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1124 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1125 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1126 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1127 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1128 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1129 0);
1130 case 'y':
1131 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1132 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1133 0);
1134 }
1135 }
1136
Chris Lattner1efa40f2006-02-22 00:56:39 +00001137 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001138}
Chris Lattner763317d2006-02-07 00:47:13 +00001139
1140// isOperandValidForConstraint
1141bool PPCTargetLowering::
1142isOperandValidForConstraint(SDOperand Op, char Letter) {
1143 switch (Letter) {
1144 default: break;
1145 case 'I':
1146 case 'J':
1147 case 'K':
1148 case 'L':
1149 case 'M':
1150 case 'N':
1151 case 'O':
1152 case 'P': {
1153 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1154 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1155 switch (Letter) {
1156 default: assert(0 && "Unknown constraint letter!");
1157 case 'I': // "I" is a signed 16-bit constant.
1158 return (short)Value == (int)Value;
1159 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1160 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1161 return (short)Value == 0;
1162 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1163 return (Value >> 16) == 0;
1164 case 'M': // "M" is a constant that is greater than 31.
1165 return Value > 31;
1166 case 'N': // "N" is a positive constant that is an exact power of two.
1167 return (int)Value > 0 && isPowerOf2_32(Value);
1168 case 'O': // "O" is the constant zero.
1169 return Value == 0;
1170 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1171 return (short)-Value == (int)-Value;
1172 }
1173 break;
1174 }
1175 }
1176
1177 // Handle standard constraint letters.
1178 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1179}
Evan Chengc4c62572006-03-13 23:20:37 +00001180
1181/// isLegalAddressImmediate - Return true if the integer value can be used
1182/// as the offset of the target addressing mode.
1183bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1184 // PPC allows a sign-extended 16-bit immediate field.
1185 return (V > -(1 << 16) && V < (1 << 16)-1);
1186}