blob: 5a2acd46d9887549c297c854d5a4e6dc8aba54d8 [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
Chris Lattnerb2177b92006-03-19 06:55:52 +0000187 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
188 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
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";
Chris Lattnerb2177b92006-03-19 06:55:52 +0000211 case PPCISD::LVE_X: return "PPCISD::LVE_X";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000212 case PPCISD::Hi: return "PPCISD::Hi";
213 case PPCISD::Lo: return "PPCISD::Lo";
214 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
215 case PPCISD::SRL: return "PPCISD::SRL";
216 case PPCISD::SRA: return "PPCISD::SRA";
217 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000218 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000219 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
220 }
221}
222
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000223/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
224static bool isFloatingPointZero(SDOperand Op) {
225 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
226 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
227 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
228 // Maybe this has already been legalized into the constant pool?
229 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
230 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
231 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
232 }
233 return false;
234}
235
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000236/// LowerOperation - Provide custom lowering hooks for some operations.
237///
Nate Begeman21e463b2005-10-16 05:39:50 +0000238SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000239 switch (Op.getOpcode()) {
240 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000241 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000242 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000243 SDOperand Src = Op.getOperand(0);
244 if (Src.getValueType() == MVT::f32)
245 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
246
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000247 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000248 switch (Op.getValueType()) {
249 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
250 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000251 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000252 break;
253 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000254 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000255 break;
256 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000257
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000258 // Convert the FP value to an int value through memory.
259 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
260 if (Op.getValueType() == MVT::i32)
261 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
262 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000263 }
264 case ISD::SINT_TO_FP: {
265 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
266 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000267 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
268 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000269 if (MVT::f32 == Op.getValueType())
270 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
271 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000272 }
273 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000274 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000275 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
276 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
277 break;
278
279 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
280
281 // Cannot handle SETEQ/SETNE.
282 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
283
284 MVT::ValueType ResVT = Op.getValueType();
285 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
286 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
287 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000288
Chris Lattnerf7605322005-08-31 21:09:52 +0000289 // If the RHS of the comparison is a 0.0, we don't need to do the
290 // subtraction at all.
291 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000292 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000293 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000294 case ISD::SETULT:
295 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000296 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000297 case ISD::SETUGE:
298 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000299 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
300 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000301 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000302 case ISD::SETUGT:
303 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000304 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000305 case ISD::SETULE:
306 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000307 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
308 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000309 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000310 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000311 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000312
Chris Lattnereb255f22005-10-25 20:54:57 +0000313 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000314 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000315 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000316 case ISD::SETULT:
317 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000318 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
319 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
320 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
321 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000322 case ISD::SETUGE:
323 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000324 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
325 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
326 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
327 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000328 case ISD::SETUGT:
329 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000330 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
331 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
332 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
333 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000334 case ISD::SETULE:
335 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000336 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
337 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
338 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
339 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000340 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000341 break;
342 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000343 case ISD::SHL: {
344 assert(Op.getValueType() == MVT::i64 &&
345 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
346 // The generic code does a fine job expanding shift by a constant.
347 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
348
349 // Otherwise, expand into a bunch of logical ops. Note that these ops
350 // depend on the PPC behavior for oversized shift amounts.
351 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
352 DAG.getConstant(0, MVT::i32));
353 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
354 DAG.getConstant(1, MVT::i32));
355 SDOperand Amt = Op.getOperand(1);
356
357 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
358 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000359 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
360 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000361 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
362 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
363 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000364 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000365 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000366 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000367 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
368 }
369 case ISD::SRL: {
370 assert(Op.getValueType() == MVT::i64 &&
371 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
372 // The generic code does a fine job expanding shift by a constant.
373 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
374
375 // Otherwise, expand into a bunch of logical ops. Note that these ops
376 // depend on the PPC behavior for oversized shift amounts.
377 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
378 DAG.getConstant(0, MVT::i32));
379 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
380 DAG.getConstant(1, MVT::i32));
381 SDOperand Amt = Op.getOperand(1);
382
383 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
384 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000385 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
386 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000387 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
388 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
389 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000390 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000391 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000392 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000393 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
394 }
395 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000396 assert(Op.getValueType() == MVT::i64 &&
397 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
398 // The generic code does a fine job expanding shift by a constant.
399 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
400
401 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
402 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
403 DAG.getConstant(0, MVT::i32));
404 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
405 DAG.getConstant(1, MVT::i32));
406 SDOperand Amt = Op.getOperand(1);
407
408 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
409 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000410 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
411 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000412 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
413 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
414 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000415 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
416 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000417 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
418 Tmp4, Tmp6, ISD::SETLE);
419 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000420 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000421 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000422 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
423 Constant *C = CP->get();
424 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000425 SDOperand Zero = DAG.getConstant(0, MVT::i32);
426
Evan Cheng4c1aa862006-02-22 20:19:42 +0000427 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000428 // Generate non-pic code that has direct accesses to the constant pool.
429 // The address of the global is just (hi(&g)+lo(&g)).
430 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
431 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
432 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
433 }
434
435 // Only lower ConstantPool on Darwin.
436 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
437 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000438 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000439 // With PIC, the first instruction is actually "GR+hi(&G)".
440 Hi = DAG.getNode(ISD::ADD, MVT::i32,
441 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
442 }
443
444 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
445 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
446 return Lo;
447 }
Chris Lattner860e8862005-11-17 07:30:41 +0000448 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000449 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
450 GlobalValue *GV = GSDN->getGlobal();
451 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000452 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000453
Evan Cheng4c1aa862006-02-22 20:19:42 +0000454 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000455 // Generate non-pic code that has direct accesses to globals.
456 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000457 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
458 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
459 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
460 }
Chris Lattner860e8862005-11-17 07:30:41 +0000461
Chris Lattner1d05cb42005-11-17 18:55:48 +0000462 // Only lower GlobalAddress on Darwin.
463 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000464
Chris Lattner860e8862005-11-17 07:30:41 +0000465 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000466 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000467 // With PIC, the first instruction is actually "GR+hi(&G)".
468 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000469 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000470 }
471
472 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
473 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
474
Chris Lattner37dd6f12006-01-29 20:49:17 +0000475 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
476 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000477 return Lo;
478
479 // If the global is weak or external, we have to go through the lazy
480 // resolution stub.
481 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
482 }
Nate Begeman44775902006-01-31 08:17:29 +0000483 case ISD::SETCC: {
484 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000485
486 // If we're comparing for equality to zero, expose the fact that this is
487 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
488 // fold the new nodes.
489 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
490 if (C->isNullValue() && CC == ISD::SETEQ) {
491 MVT::ValueType VT = Op.getOperand(0).getValueType();
492 SDOperand Zext = Op.getOperand(0);
493 if (VT < MVT::i32) {
494 VT = MVT::i32;
495 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
496 }
497 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
498 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
499 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
500 DAG.getConstant(Log2b, getShiftAmountTy()));
501 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
502 }
503 // Leave comparisons against 0 and -1 alone for now, since they're usually
504 // optimized. FIXME: revisit this when we can custom lower all setcc
505 // optimizations.
506 if (C->isAllOnesValue() || C->isNullValue())
507 break;
508 }
509
510 // If we have an integer seteq/setne, turn it into a compare against zero
511 // by subtracting the rhs from the lhs, which is faster than setting a
512 // condition register, reading it back out, and masking the correct bit.
513 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
514 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
515 MVT::ValueType VT = Op.getValueType();
516 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
517 Op.getOperand(1));
518 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
519 }
Nate Begeman44775902006-01-31 08:17:29 +0000520 break;
521 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000522 case ISD::VASTART: {
523 // vastart just stores the address of the VarArgsFrameIndex slot into the
524 // memory location argument.
525 // FIXME: Replace MVT::i32 with PointerTy
526 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
527 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
528 Op.getOperand(1), Op.getOperand(2));
529 }
Nate Begemanee625572006-01-27 21:09:22 +0000530 case ISD::RET: {
531 SDOperand Copy;
532
533 switch(Op.getNumOperands()) {
534 default:
535 assert(0 && "Do not know how to return this many arguments!");
536 abort();
537 case 1:
538 return SDOperand(); // ret void is legal
539 case 2: {
540 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
541 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
542 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
543 SDOperand());
544 break;
545 }
546 case 3:
547 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
548 SDOperand());
549 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
550 break;
551 }
552 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
553 }
Chris Lattnerb2177b92006-03-19 06:55:52 +0000554 case ISD::SCALAR_TO_VECTOR: {
555 // Create a stack slot that is 16-byte aligned.
556 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
557 int FrameIdx = FrameInfo->CreateStackObject(16, 16);
558 SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32);
559
560 // Store the input value into Value#0 of the stack slot.
561 unsigned InSize = MVT::getSizeInBits(Op.getOperand(0).getValueType())/8;
562 FIdx = DAG.getNode(ISD::ADD, MVT::i32, FIdx,
563 DAG.getConstant(16-InSize, MVT::i32));
564 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
565 Op.getOperand(0), FIdx,DAG.getSrcValue(NULL));
566 return DAG.getNode(PPCISD::LVE_X, Op.getValueType(), Store, FIdx,
567 DAG.getSrcValue(NULL));
568 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000569 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000570 return SDOperand();
571}
572
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000573std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000574PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000575 //
576 // add beautiful description of PPC stack frame format, or at least some docs
577 //
578 MachineFunction &MF = DAG.getMachineFunction();
579 MachineFrameInfo *MFI = MF.getFrameInfo();
580 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000581 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000582 std::vector<SDOperand> ArgValues;
583
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000584 unsigned ArgOffset = 24;
585 unsigned GPR_remaining = 8;
586 unsigned FPR_remaining = 13;
587 unsigned GPR_idx = 0, FPR_idx = 0;
588 static const unsigned GPR[] = {
589 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
590 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
591 };
592 static const unsigned FPR[] = {
593 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
594 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
595 };
596
597 // Add DAG nodes to load the arguments... On entry to a function on PPC,
598 // the arguments start at offset 24, although they are likely to be passed
599 // in registers.
600 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
601 SDOperand newroot, argt;
602 unsigned ObjSize;
603 bool needsLoad = false;
604 bool ArgLive = !I->use_empty();
605 MVT::ValueType ObjectVT = getValueType(I->getType());
606
607 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000608 default: assert(0 && "Unhandled argument type!");
609 case MVT::i1:
610 case MVT::i8:
611 case MVT::i16:
612 case MVT::i32:
613 ObjSize = 4;
614 if (!ArgLive) break;
615 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000616 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000617 MF.addLiveIn(GPR[GPR_idx], VReg);
618 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000619 if (ObjectVT != MVT::i32) {
620 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
621 : ISD::AssertZext;
622 argt = DAG.getNode(AssertOp, MVT::i32, argt,
623 DAG.getValueType(ObjectVT));
624 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
625 }
Chris Lattner915fb302005-08-30 00:19:00 +0000626 } else {
627 needsLoad = true;
628 }
629 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000630 case MVT::i64:
631 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000632 if (!ArgLive) break;
633 if (GPR_remaining > 0) {
634 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000635 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000636 MF.addLiveIn(GPR[GPR_idx], VReg);
637 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000638 // If we have two or more remaining argument registers, then both halves
639 // of the i64 can be sourced from there. Otherwise, the lower half will
640 // have to come off the stack. This can happen when an i64 is preceded
641 // by 28 bytes of arguments.
642 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000643 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000644 MF.addLiveIn(GPR[GPR_idx+1], VReg);
645 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000646 } else {
647 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
648 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
649 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
650 DAG.getSrcValue(NULL));
651 }
652 // Build the outgoing arg thingy
653 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
654 newroot = argLo;
655 } else {
656 needsLoad = true;
657 }
658 break;
659 case MVT::f32:
660 case MVT::f64:
661 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000662 if (!ArgLive) {
663 if (FPR_remaining > 0) {
664 --FPR_remaining;
665 ++FPR_idx;
666 }
667 break;
668 }
Chris Lattner915fb302005-08-30 00:19:00 +0000669 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000670 unsigned VReg;
671 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000672 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000673 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000674 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000675 MF.addLiveIn(FPR[FPR_idx], VReg);
676 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000677 --FPR_remaining;
678 ++FPR_idx;
679 } else {
680 needsLoad = true;
681 }
682 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000683 }
684
685 // We need to load the argument to a virtual register if we determined above
686 // that we ran out of physical registers of the appropriate type
687 if (needsLoad) {
688 unsigned SubregOffset = 0;
689 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
690 if (ObjectVT == MVT::i16) SubregOffset = 2;
691 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
692 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
693 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
694 DAG.getConstant(SubregOffset, MVT::i32));
695 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
696 DAG.getSrcValue(NULL));
697 }
698
699 // Every 4 bytes of argument space consumes one of the GPRs available for
700 // argument passing.
701 if (GPR_remaining > 0) {
702 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
703 GPR_remaining -= delta;
704 GPR_idx += delta;
705 }
706 ArgOffset += ObjSize;
707 if (newroot.Val)
708 DAG.setRoot(newroot.getValue(1));
709
710 ArgValues.push_back(argt);
711 }
712
713 // If the function takes variable number of arguments, make a frame index for
714 // the start of the first vararg value... for expansion of llvm.va_start.
715 if (F.isVarArg()) {
716 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
717 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
718 // If this function is vararg, store any remaining integer argument regs
719 // to their spots on the stack so that they may be loaded by deferencing the
720 // result of va_next.
721 std::vector<SDOperand> MemOps;
722 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000723 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000724 MF.addLiveIn(GPR[GPR_idx], VReg);
725 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000726 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
727 Val, FIN, DAG.getSrcValue(NULL));
728 MemOps.push_back(Store);
729 // Increment the address by four for the next argument to store
730 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
731 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
732 }
Chris Lattner80720a92005-11-30 20:40:54 +0000733 if (!MemOps.empty()) {
734 MemOps.push_back(DAG.getRoot());
735 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
736 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000737 }
738
739 // Finally, inform the code generator which regs we return values in.
740 switch (getValueType(F.getReturnType())) {
741 default: assert(0 && "Unknown type!");
742 case MVT::isVoid: break;
743 case MVT::i1:
744 case MVT::i8:
745 case MVT::i16:
746 case MVT::i32:
747 MF.addLiveOut(PPC::R3);
748 break;
749 case MVT::i64:
750 MF.addLiveOut(PPC::R3);
751 MF.addLiveOut(PPC::R4);
752 break;
753 case MVT::f32:
754 case MVT::f64:
755 MF.addLiveOut(PPC::F1);
756 break;
757 }
758
759 return ArgValues;
760}
761
762std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000763PPCTargetLowering::LowerCallTo(SDOperand Chain,
764 const Type *RetTy, bool isVarArg,
765 unsigned CallingConv, bool isTailCall,
766 SDOperand Callee, ArgListTy &Args,
767 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000768 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000769 // SelectExpr to use to put the arguments in the appropriate registers.
770 std::vector<SDOperand> args_to_use;
771
772 // Count how many bytes are to be pushed on the stack, including the linkage
773 // area, and parameter passing area.
774 unsigned NumBytes = 24;
775
776 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000777 Chain = DAG.getCALLSEQ_START(Chain,
778 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000779 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000780 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000781 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000782 default: assert(0 && "Unknown value type!");
783 case MVT::i1:
784 case MVT::i8:
785 case MVT::i16:
786 case MVT::i32:
787 case MVT::f32:
788 NumBytes += 4;
789 break;
790 case MVT::i64:
791 case MVT::f64:
792 NumBytes += 8;
793 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000794 }
Chris Lattner915fb302005-08-30 00:19:00 +0000795 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000796
Chris Lattner915fb302005-08-30 00:19:00 +0000797 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
798 // plus 32 bytes of argument space in case any called code gets funky on us.
799 // (Required by ABI to support var arg)
800 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000801
802 // Adjust the stack pointer for the new arguments...
803 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000804 Chain = DAG.getCALLSEQ_START(Chain,
805 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000806
807 // Set up a copy of the stack pointer for use loading and storing any
808 // arguments that may not fit in the registers available for argument
809 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000810 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000811
812 // Figure out which arguments are going to go in registers, and which in
813 // memory. Also, if this is a vararg function, floating point operations
814 // must be stored to our stack, and loaded into integer regs as well, if
815 // any integer regs are available for argument passing.
816 unsigned ArgOffset = 24;
817 unsigned GPR_remaining = 8;
818 unsigned FPR_remaining = 13;
819
820 std::vector<SDOperand> MemOps;
821 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
822 // PtrOff will be used to store the current argument to the stack if a
823 // register cannot be found for it.
824 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
825 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
826 MVT::ValueType ArgVT = getValueType(Args[i].second);
827
828 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000829 default: assert(0 && "Unexpected ValueType for argument!");
830 case MVT::i1:
831 case MVT::i8:
832 case MVT::i16:
833 // Promote the integer to 32 bits. If the input type is signed use a
834 // sign extend, otherwise use a zero extend.
835 if (Args[i].second->isSigned())
836 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
837 else
838 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
839 // FALL THROUGH
840 case MVT::i32:
841 if (GPR_remaining > 0) {
842 args_to_use.push_back(Args[i].first);
843 --GPR_remaining;
844 } else {
845 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
846 Args[i].first, PtrOff,
847 DAG.getSrcValue(NULL)));
848 }
849 ArgOffset += 4;
850 break;
851 case MVT::i64:
852 // If we have one free GPR left, we can place the upper half of the i64
853 // in it, and store the other half to the stack. If we have two or more
854 // free GPRs, then we can pass both halves of the i64 in registers.
855 if (GPR_remaining > 0) {
856 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
857 Args[i].first, DAG.getConstant(1, MVT::i32));
858 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
859 Args[i].first, DAG.getConstant(0, MVT::i32));
860 args_to_use.push_back(Hi);
861 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000862 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000863 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000864 --GPR_remaining;
865 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000866 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
867 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000868 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000869 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000870 }
Chris Lattner915fb302005-08-30 00:19:00 +0000871 } else {
872 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
873 Args[i].first, PtrOff,
874 DAG.getSrcValue(NULL)));
875 }
876 ArgOffset += 8;
877 break;
878 case MVT::f32:
879 case MVT::f64:
880 if (FPR_remaining > 0) {
881 args_to_use.push_back(Args[i].first);
882 --FPR_remaining;
883 if (isVarArg) {
884 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
885 Args[i].first, PtrOff,
886 DAG.getSrcValue(NULL));
887 MemOps.push_back(Store);
888 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000889 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000890 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
891 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000892 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000893 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000894 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000895 }
896 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000897 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
898 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000899 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
900 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000901 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000902 args_to_use.push_back(Load);
903 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000904 }
905 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000906 // If we have any FPRs remaining, we may also have GPRs remaining.
907 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
908 // GPRs.
909 if (GPR_remaining > 0) {
910 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
911 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000912 }
Chris Lattner915fb302005-08-30 00:19:00 +0000913 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
914 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
915 --GPR_remaining;
916 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000917 }
Chris Lattner915fb302005-08-30 00:19:00 +0000918 } else {
919 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
920 Args[i].first, PtrOff,
921 DAG.getSrcValue(NULL)));
922 }
923 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
924 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000925 }
926 }
927 if (!MemOps.empty())
928 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
929 }
930
931 std::vector<MVT::ValueType> RetVals;
932 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000933 MVT::ValueType ActualRetTyVT = RetTyVT;
934 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
935 ActualRetTyVT = MVT::i32; // Promote result to i32.
936
Chris Lattnere00ebf02006-01-28 07:33:03 +0000937 if (RetTyVT == MVT::i64) {
938 RetVals.push_back(MVT::i32);
939 RetVals.push_back(MVT::i32);
940 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000941 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000942 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000943 RetVals.push_back(MVT::Other);
944
Chris Lattner2823b3e2005-11-17 05:56:14 +0000945 // If the callee is a GlobalAddress node (quite common, every direct call is)
946 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
947 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
948 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
949
Chris Lattner281b55e2006-01-27 23:34:02 +0000950 std::vector<SDOperand> Ops;
951 Ops.push_back(Chain);
952 Ops.push_back(Callee);
953 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
954 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000955 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000956 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
957 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000958 SDOperand RetVal = TheCall;
959
960 // If the result is a small value, add a note so that we keep track of the
961 // information about whether it is sign or zero extended.
962 if (RetTyVT != ActualRetTyVT) {
963 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
964 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
965 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000966 } else if (RetTyVT == MVT::i64) {
967 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000968 }
969
970 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000971}
972
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000973MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000974PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
975 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000976 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000977 MI->getOpcode() == PPC::SELECT_CC_F4 ||
978 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000979 "Unexpected instr type to insert");
980
981 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
982 // control-flow pattern. The incoming instruction knows the destination vreg
983 // to set, the condition code register to branch on, the true/false values to
984 // select between, and a branch opcode to use.
985 const BasicBlock *LLVM_BB = BB->getBasicBlock();
986 ilist<MachineBasicBlock>::iterator It = BB;
987 ++It;
988
989 // thisMBB:
990 // ...
991 // TrueVal = ...
992 // cmpTY ccX, r1, r2
993 // bCC copy1MBB
994 // fallthrough --> copy0MBB
995 MachineBasicBlock *thisMBB = BB;
996 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
997 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
998 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
999 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
1000 MachineFunction *F = BB->getParent();
1001 F->getBasicBlockList().insert(It, copy0MBB);
1002 F->getBasicBlockList().insert(It, sinkMBB);
1003 // Update machine-CFG edges
1004 BB->addSuccessor(copy0MBB);
1005 BB->addSuccessor(sinkMBB);
1006
1007 // copy0MBB:
1008 // %FalseValue = ...
1009 // # fallthrough to sinkMBB
1010 BB = copy0MBB;
1011
1012 // Update machine-CFG edges
1013 BB->addSuccessor(sinkMBB);
1014
1015 // sinkMBB:
1016 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1017 // ...
1018 BB = sinkMBB;
1019 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1020 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1021 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1022
1023 delete MI; // The pseudo instruction is gone now.
1024 return BB;
1025}
1026
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001027SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1028 DAGCombinerInfo &DCI) const {
1029 TargetMachine &TM = getTargetMachine();
1030 SelectionDAG &DAG = DCI.DAG;
1031 switch (N->getOpcode()) {
1032 default: break;
1033 case ISD::SINT_TO_FP:
1034 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
1035 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1036 // We allow the src/dst to be either f32/f64, but force the intermediate
1037 // type to be i64.
1038 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT &&
1039 N->getOperand(0).getValueType() == MVT::i64) {
1040
1041 SDOperand Val = N->getOperand(0).getOperand(0);
1042 if (Val.getValueType() == MVT::f32) {
1043 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1044 DCI.AddToWorklist(Val.Val);
1045 }
1046
1047 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
1048 DCI.AddToWorklist(Val.Val);
1049 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
1050 DCI.AddToWorklist(Val.Val);
1051 if (N->getValueType(0) == MVT::f32) {
1052 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1053 DCI.AddToWorklist(Val.Val);
1054 }
1055 return Val;
1056 }
1057 }
1058 break;
Chris Lattner51269842006-03-01 05:50:56 +00001059 case ISD::STORE:
1060 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1061 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1062 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1063 N->getOperand(1).getValueType() == MVT::i32) {
1064 SDOperand Val = N->getOperand(1).getOperand(0);
1065 if (Val.getValueType() == MVT::f32) {
1066 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1067 DCI.AddToWorklist(Val.Val);
1068 }
1069 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1070 DCI.AddToWorklist(Val.Val);
1071
1072 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1073 N->getOperand(2), N->getOperand(3));
1074 DCI.AddToWorklist(Val.Val);
1075 return Val;
1076 }
1077 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001078 }
1079
1080 return SDOperand();
1081}
1082
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001083/// getConstraintType - Given a constraint letter, return the type of
1084/// constraint it is for this target.
1085PPCTargetLowering::ConstraintType
1086PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1087 switch (ConstraintLetter) {
1088 default: break;
1089 case 'b':
1090 case 'r':
1091 case 'f':
1092 case 'v':
1093 case 'y':
1094 return C_RegisterClass;
1095 }
1096 return TargetLowering::getConstraintType(ConstraintLetter);
1097}
1098
1099
Chris Lattnerddc787d2006-01-31 19:20:21 +00001100std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001101getRegClassForInlineAsmConstraint(const std::string &Constraint,
1102 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001103 if (Constraint.size() == 1) {
1104 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1105 default: break; // Unknown constriant letter
1106 case 'b':
1107 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1108 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1109 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1110 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1111 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1112 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1113 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1114 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1115 0);
1116 case 'r':
1117 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1118 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1119 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1120 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1121 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1122 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1123 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1124 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1125 0);
1126 case 'f':
1127 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1128 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1129 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1130 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1131 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1132 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1133 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1134 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1135 0);
1136 case 'v':
1137 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1138 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1139 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1140 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1141 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1142 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1143 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1144 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1145 0);
1146 case 'y':
1147 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1148 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1149 0);
1150 }
1151 }
1152
Chris Lattner1efa40f2006-02-22 00:56:39 +00001153 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001154}
Chris Lattner763317d2006-02-07 00:47:13 +00001155
1156// isOperandValidForConstraint
1157bool PPCTargetLowering::
1158isOperandValidForConstraint(SDOperand Op, char Letter) {
1159 switch (Letter) {
1160 default: break;
1161 case 'I':
1162 case 'J':
1163 case 'K':
1164 case 'L':
1165 case 'M':
1166 case 'N':
1167 case 'O':
1168 case 'P': {
1169 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1170 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1171 switch (Letter) {
1172 default: assert(0 && "Unknown constraint letter!");
1173 case 'I': // "I" is a signed 16-bit constant.
1174 return (short)Value == (int)Value;
1175 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1176 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1177 return (short)Value == 0;
1178 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1179 return (Value >> 16) == 0;
1180 case 'M': // "M" is a constant that is greater than 31.
1181 return Value > 31;
1182 case 'N': // "N" is a positive constant that is an exact power of two.
1183 return (int)Value > 0 && isPowerOf2_32(Value);
1184 case 'O': // "O" is the constant zero.
1185 return Value == 0;
1186 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1187 return (short)-Value == (int)-Value;
1188 }
1189 break;
1190 }
1191 }
1192
1193 // Handle standard constraint letters.
1194 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1195}
Evan Chengc4c62572006-03-13 23:20:37 +00001196
1197/// isLegalAddressImmediate - Return true if the integer value can be used
1198/// as the offset of the target addressing mode.
1199bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1200 // PPC allows a sign-extended 16-bit immediate field.
1201 return (V > -(1 << 16) && V < (1 << 16)-1);
1202}