blob: c3583771e8e52f23953d040329e1b6e59afed7d0 [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 Lattnerec4a0c72006-01-29 06:32:58 +0000182 // FIXME: We don't support any ConstantVec's yet. We should custom expand
183 // the ones we do!
Chris Lattnerd9b55dd2006-01-29 08:41:37 +0000184 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
185 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000186 }
187
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000188 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000189 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000190
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000191 // We have target-specific dag combine patterns for the following nodes:
192 setTargetDAGCombine(ISD::SINT_TO_FP);
Chris Lattner51269842006-03-01 05:50:56 +0000193 setTargetDAGCombine(ISD::STORE);
Chris Lattner8c13d0a2006-03-01 04:57:39 +0000194
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000195 computeRegisterProperties();
196}
197
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000198const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
199 switch (Opcode) {
200 default: return 0;
201 case PPCISD::FSEL: return "PPCISD::FSEL";
202 case PPCISD::FCFID: return "PPCISD::FCFID";
203 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
204 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
Chris Lattner51269842006-03-01 05:50:56 +0000205 case PPCISD::STFIWX: return "PPCISD::STFIWX";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000206 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
207 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
208 case PPCISD::Hi: return "PPCISD::Hi";
209 case PPCISD::Lo: return "PPCISD::Lo";
210 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
211 case PPCISD::SRL: return "PPCISD::SRL";
212 case PPCISD::SRA: return "PPCISD::SRA";
213 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000214 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000215 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
216 }
217}
218
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000219/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
220static bool isFloatingPointZero(SDOperand Op) {
221 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
222 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
223 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
224 // Maybe this has already been legalized into the constant pool?
225 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
226 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
227 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
228 }
229 return false;
230}
231
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000232/// LowerOperation - Provide custom lowering hooks for some operations.
233///
Nate Begeman21e463b2005-10-16 05:39:50 +0000234SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000235 switch (Op.getOpcode()) {
236 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000237 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000238 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000239 SDOperand Src = Op.getOperand(0);
240 if (Src.getValueType() == MVT::f32)
241 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
242
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000243 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000244 switch (Op.getValueType()) {
245 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
246 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000247 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000248 break;
249 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000250 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000251 break;
252 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000253
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000254 // Convert the FP value to an int value through memory.
255 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
256 if (Op.getValueType() == MVT::i32)
257 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
258 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000259 }
260 case ISD::SINT_TO_FP: {
261 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
262 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000263 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
264 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000265 if (MVT::f32 == Op.getValueType())
266 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
267 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000268 }
269 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000270 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000271 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
272 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
273 break;
274
275 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
276
277 // Cannot handle SETEQ/SETNE.
278 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
279
280 MVT::ValueType ResVT = Op.getValueType();
281 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
282 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
283 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000284
Chris Lattnerf7605322005-08-31 21:09:52 +0000285 // If the RHS of the comparison is a 0.0, we don't need to do the
286 // subtraction at all.
287 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000288 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000289 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000290 case ISD::SETULT:
291 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000292 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000293 case ISD::SETUGE:
294 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000295 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
296 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000297 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000298 case ISD::SETUGT:
299 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000300 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000301 case ISD::SETULE:
302 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000303 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
304 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000305 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000306 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000307 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000308
Chris Lattnereb255f22005-10-25 20:54:57 +0000309 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000310 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000311 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000312 case ISD::SETULT:
313 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000314 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
315 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
316 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
317 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000318 case ISD::SETUGE:
319 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000320 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
321 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
322 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
323 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000324 case ISD::SETUGT:
325 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000326 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
327 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
328 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
329 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000330 case ISD::SETULE:
331 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000332 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
333 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
334 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
335 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000336 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000337 break;
338 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000339 case ISD::SHL: {
340 assert(Op.getValueType() == MVT::i64 &&
341 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
342 // The generic code does a fine job expanding shift by a constant.
343 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
344
345 // Otherwise, expand into a bunch of logical ops. Note that these ops
346 // depend on the PPC behavior for oversized shift amounts.
347 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
348 DAG.getConstant(0, MVT::i32));
349 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
350 DAG.getConstant(1, MVT::i32));
351 SDOperand Amt = Op.getOperand(1);
352
353 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
354 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000355 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
356 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000357 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
358 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
359 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000360 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000361 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000362 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000363 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
364 }
365 case ISD::SRL: {
366 assert(Op.getValueType() == MVT::i64 &&
367 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
368 // The generic code does a fine job expanding shift by a constant.
369 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
370
371 // Otherwise, expand into a bunch of logical ops. Note that these ops
372 // depend on the PPC behavior for oversized shift amounts.
373 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
374 DAG.getConstant(0, MVT::i32));
375 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
376 DAG.getConstant(1, MVT::i32));
377 SDOperand Amt = Op.getOperand(1);
378
379 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
380 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000381 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
382 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000383 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
384 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
385 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000386 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000387 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000388 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000389 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
390 }
391 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000392 assert(Op.getValueType() == MVT::i64 &&
393 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
394 // The generic code does a fine job expanding shift by a constant.
395 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
396
397 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
398 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
399 DAG.getConstant(0, MVT::i32));
400 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
401 DAG.getConstant(1, MVT::i32));
402 SDOperand Amt = Op.getOperand(1);
403
404 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
405 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000406 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
407 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000408 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
409 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
410 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000411 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
412 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000413 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
414 Tmp4, Tmp6, ISD::SETLE);
415 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000416 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000417 case ISD::ConstantPool: {
Evan Chengb8973bd2006-01-31 22:23:14 +0000418 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
419 Constant *C = CP->get();
420 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment());
Nate Begeman28a6b022005-12-10 02:36:00 +0000421 SDOperand Zero = DAG.getConstant(0, MVT::i32);
422
Evan Cheng4c1aa862006-02-22 20:19:42 +0000423 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000424 // Generate non-pic code that has direct accesses to the constant pool.
425 // The address of the global is just (hi(&g)+lo(&g)).
426 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
427 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
428 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
429 }
430
431 // Only lower ConstantPool on Darwin.
432 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
433 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000434 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000435 // With PIC, the first instruction is actually "GR+hi(&G)".
436 Hi = DAG.getNode(ISD::ADD, MVT::i32,
437 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
438 }
439
440 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
441 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
442 return Lo;
443 }
Chris Lattner860e8862005-11-17 07:30:41 +0000444 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000445 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
446 GlobalValue *GV = GSDN->getGlobal();
447 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000448 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000449
Evan Cheng4c1aa862006-02-22 20:19:42 +0000450 if (getTargetMachine().getRelocationModel() == Reloc::Static) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000451 // Generate non-pic code that has direct accesses to globals.
452 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000453 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
454 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
455 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
456 }
Chris Lattner860e8862005-11-17 07:30:41 +0000457
Chris Lattner1d05cb42005-11-17 18:55:48 +0000458 // Only lower GlobalAddress on Darwin.
459 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000460
Chris Lattner860e8862005-11-17 07:30:41 +0000461 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000462 if (getTargetMachine().getRelocationModel() == Reloc::PIC) {
Chris Lattner860e8862005-11-17 07:30:41 +0000463 // With PIC, the first instruction is actually "GR+hi(&G)".
464 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000465 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000466 }
467
468 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
469 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
470
Chris Lattner37dd6f12006-01-29 20:49:17 +0000471 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
472 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000473 return Lo;
474
475 // If the global is weak or external, we have to go through the lazy
476 // resolution stub.
477 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
478 }
Nate Begeman44775902006-01-31 08:17:29 +0000479 case ISD::SETCC: {
480 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Nate Begeman750ac1b2006-02-01 07:19:44 +0000481
482 // If we're comparing for equality to zero, expose the fact that this is
483 // implented as a ctlz/srl pair on ppc, so that the dag combiner can
484 // fold the new nodes.
485 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
486 if (C->isNullValue() && CC == ISD::SETEQ) {
487 MVT::ValueType VT = Op.getOperand(0).getValueType();
488 SDOperand Zext = Op.getOperand(0);
489 if (VT < MVT::i32) {
490 VT = MVT::i32;
491 Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0));
492 }
493 unsigned Log2b = Log2_32(MVT::getSizeInBits(VT));
494 SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext);
495 SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz,
496 DAG.getConstant(Log2b, getShiftAmountTy()));
497 return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc);
498 }
499 // Leave comparisons against 0 and -1 alone for now, since they're usually
500 // optimized. FIXME: revisit this when we can custom lower all setcc
501 // optimizations.
502 if (C->isAllOnesValue() || C->isNullValue())
503 break;
504 }
505
506 // If we have an integer seteq/setne, turn it into a compare against zero
507 // by subtracting the rhs from the lhs, which is faster than setting a
508 // condition register, reading it back out, and masking the correct bit.
509 MVT::ValueType LHSVT = Op.getOperand(0).getValueType();
510 if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
511 MVT::ValueType VT = Op.getValueType();
512 SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0),
513 Op.getOperand(1));
514 return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC);
515 }
Nate Begeman44775902006-01-31 08:17:29 +0000516 break;
517 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000518 case ISD::VASTART: {
519 // vastart just stores the address of the VarArgsFrameIndex slot into the
520 // memory location argument.
521 // FIXME: Replace MVT::i32 with PointerTy
522 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
523 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
524 Op.getOperand(1), Op.getOperand(2));
525 }
Nate Begemanee625572006-01-27 21:09:22 +0000526 case ISD::RET: {
527 SDOperand Copy;
528
529 switch(Op.getNumOperands()) {
530 default:
531 assert(0 && "Do not know how to return this many arguments!");
532 abort();
533 case 1:
534 return SDOperand(); // ret void is legal
535 case 2: {
536 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
537 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
538 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
539 SDOperand());
540 break;
541 }
542 case 3:
543 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
544 SDOperand());
545 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
546 break;
547 }
548 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
549 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000550 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000551 return SDOperand();
552}
553
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000554std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000555PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000556 //
557 // add beautiful description of PPC stack frame format, or at least some docs
558 //
559 MachineFunction &MF = DAG.getMachineFunction();
560 MachineFrameInfo *MFI = MF.getFrameInfo();
561 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000562 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000563 std::vector<SDOperand> ArgValues;
564
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000565 unsigned ArgOffset = 24;
566 unsigned GPR_remaining = 8;
567 unsigned FPR_remaining = 13;
568 unsigned GPR_idx = 0, FPR_idx = 0;
569 static const unsigned GPR[] = {
570 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
571 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
572 };
573 static const unsigned FPR[] = {
574 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
575 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
576 };
577
578 // Add DAG nodes to load the arguments... On entry to a function on PPC,
579 // the arguments start at offset 24, although they are likely to be passed
580 // in registers.
581 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
582 SDOperand newroot, argt;
583 unsigned ObjSize;
584 bool needsLoad = false;
585 bool ArgLive = !I->use_empty();
586 MVT::ValueType ObjectVT = getValueType(I->getType());
587
588 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000589 default: assert(0 && "Unhandled argument type!");
590 case MVT::i1:
591 case MVT::i8:
592 case MVT::i16:
593 case MVT::i32:
594 ObjSize = 4;
595 if (!ArgLive) break;
596 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000597 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000598 MF.addLiveIn(GPR[GPR_idx], VReg);
599 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000600 if (ObjectVT != MVT::i32) {
601 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
602 : ISD::AssertZext;
603 argt = DAG.getNode(AssertOp, MVT::i32, argt,
604 DAG.getValueType(ObjectVT));
605 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
606 }
Chris Lattner915fb302005-08-30 00:19:00 +0000607 } else {
608 needsLoad = true;
609 }
610 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000611 case MVT::i64:
612 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000613 if (!ArgLive) break;
614 if (GPR_remaining > 0) {
615 SDOperand argHi, argLo;
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 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000619 // If we have two or more remaining argument registers, then both halves
620 // of the i64 can be sourced from there. Otherwise, the lower half will
621 // have to come off the stack. This can happen when an i64 is preceded
622 // by 28 bytes of arguments.
623 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000624 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000625 MF.addLiveIn(GPR[GPR_idx+1], VReg);
626 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000627 } else {
628 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
629 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
630 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
631 DAG.getSrcValue(NULL));
632 }
633 // Build the outgoing arg thingy
634 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
635 newroot = argLo;
636 } else {
637 needsLoad = true;
638 }
639 break;
640 case MVT::f32:
641 case MVT::f64:
642 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000643 if (!ArgLive) {
644 if (FPR_remaining > 0) {
645 --FPR_remaining;
646 ++FPR_idx;
647 }
648 break;
649 }
Chris Lattner915fb302005-08-30 00:19:00 +0000650 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000651 unsigned VReg;
652 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000653 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000654 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000655 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000656 MF.addLiveIn(FPR[FPR_idx], VReg);
657 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000658 --FPR_remaining;
659 ++FPR_idx;
660 } else {
661 needsLoad = true;
662 }
663 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000664 }
665
666 // We need to load the argument to a virtual register if we determined above
667 // that we ran out of physical registers of the appropriate type
668 if (needsLoad) {
669 unsigned SubregOffset = 0;
670 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
671 if (ObjectVT == MVT::i16) SubregOffset = 2;
672 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
673 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
674 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
675 DAG.getConstant(SubregOffset, MVT::i32));
676 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
677 DAG.getSrcValue(NULL));
678 }
679
680 // Every 4 bytes of argument space consumes one of the GPRs available for
681 // argument passing.
682 if (GPR_remaining > 0) {
683 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
684 GPR_remaining -= delta;
685 GPR_idx += delta;
686 }
687 ArgOffset += ObjSize;
688 if (newroot.Val)
689 DAG.setRoot(newroot.getValue(1));
690
691 ArgValues.push_back(argt);
692 }
693
694 // If the function takes variable number of arguments, make a frame index for
695 // the start of the first vararg value... for expansion of llvm.va_start.
696 if (F.isVarArg()) {
697 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
698 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
699 // If this function is vararg, store any remaining integer argument regs
700 // to their spots on the stack so that they may be loaded by deferencing the
701 // result of va_next.
702 std::vector<SDOperand> MemOps;
703 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000704 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000705 MF.addLiveIn(GPR[GPR_idx], VReg);
706 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000707 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
708 Val, FIN, DAG.getSrcValue(NULL));
709 MemOps.push_back(Store);
710 // Increment the address by four for the next argument to store
711 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
712 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
713 }
Chris Lattner80720a92005-11-30 20:40:54 +0000714 if (!MemOps.empty()) {
715 MemOps.push_back(DAG.getRoot());
716 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
717 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000718 }
719
720 // Finally, inform the code generator which regs we return values in.
721 switch (getValueType(F.getReturnType())) {
722 default: assert(0 && "Unknown type!");
723 case MVT::isVoid: break;
724 case MVT::i1:
725 case MVT::i8:
726 case MVT::i16:
727 case MVT::i32:
728 MF.addLiveOut(PPC::R3);
729 break;
730 case MVT::i64:
731 MF.addLiveOut(PPC::R3);
732 MF.addLiveOut(PPC::R4);
733 break;
734 case MVT::f32:
735 case MVT::f64:
736 MF.addLiveOut(PPC::F1);
737 break;
738 }
739
740 return ArgValues;
741}
742
743std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000744PPCTargetLowering::LowerCallTo(SDOperand Chain,
745 const Type *RetTy, bool isVarArg,
746 unsigned CallingConv, bool isTailCall,
747 SDOperand Callee, ArgListTy &Args,
748 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000749 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000750 // SelectExpr to use to put the arguments in the appropriate registers.
751 std::vector<SDOperand> args_to_use;
752
753 // Count how many bytes are to be pushed on the stack, including the linkage
754 // area, and parameter passing area.
755 unsigned NumBytes = 24;
756
757 if (Args.empty()) {
Chris Lattner45b39762006-02-13 08:55:29 +0000758 Chain = DAG.getCALLSEQ_START(Chain,
759 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000760 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000761 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000762 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000763 default: assert(0 && "Unknown value type!");
764 case MVT::i1:
765 case MVT::i8:
766 case MVT::i16:
767 case MVT::i32:
768 case MVT::f32:
769 NumBytes += 4;
770 break;
771 case MVT::i64:
772 case MVT::f64:
773 NumBytes += 8;
774 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000775 }
Chris Lattner915fb302005-08-30 00:19:00 +0000776 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000777
Chris Lattner915fb302005-08-30 00:19:00 +0000778 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
779 // plus 32 bytes of argument space in case any called code gets funky on us.
780 // (Required by ABI to support var arg)
781 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000782
783 // Adjust the stack pointer for the new arguments...
784 // These operations are automatically eliminated by the prolog/epilog pass
Chris Lattner45b39762006-02-13 08:55:29 +0000785 Chain = DAG.getCALLSEQ_START(Chain,
786 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000787
788 // Set up a copy of the stack pointer for use loading and storing any
789 // arguments that may not fit in the registers available for argument
790 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000791 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000792
793 // Figure out which arguments are going to go in registers, and which in
794 // memory. Also, if this is a vararg function, floating point operations
795 // must be stored to our stack, and loaded into integer regs as well, if
796 // any integer regs are available for argument passing.
797 unsigned ArgOffset = 24;
798 unsigned GPR_remaining = 8;
799 unsigned FPR_remaining = 13;
800
801 std::vector<SDOperand> MemOps;
802 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
803 // PtrOff will be used to store the current argument to the stack if a
804 // register cannot be found for it.
805 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
806 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
807 MVT::ValueType ArgVT = getValueType(Args[i].second);
808
809 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000810 default: assert(0 && "Unexpected ValueType for argument!");
811 case MVT::i1:
812 case MVT::i8:
813 case MVT::i16:
814 // Promote the integer to 32 bits. If the input type is signed use a
815 // sign extend, otherwise use a zero extend.
816 if (Args[i].second->isSigned())
817 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
818 else
819 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
820 // FALL THROUGH
821 case MVT::i32:
822 if (GPR_remaining > 0) {
823 args_to_use.push_back(Args[i].first);
824 --GPR_remaining;
825 } else {
826 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
827 Args[i].first, PtrOff,
828 DAG.getSrcValue(NULL)));
829 }
830 ArgOffset += 4;
831 break;
832 case MVT::i64:
833 // If we have one free GPR left, we can place the upper half of the i64
834 // in it, and store the other half to the stack. If we have two or more
835 // free GPRs, then we can pass both halves of the i64 in registers.
836 if (GPR_remaining > 0) {
837 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
838 Args[i].first, DAG.getConstant(1, MVT::i32));
839 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
840 Args[i].first, DAG.getConstant(0, MVT::i32));
841 args_to_use.push_back(Hi);
842 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000843 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000844 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000845 --GPR_remaining;
846 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000847 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
848 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000849 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000850 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000851 }
Chris Lattner915fb302005-08-30 00:19:00 +0000852 } else {
853 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
854 Args[i].first, PtrOff,
855 DAG.getSrcValue(NULL)));
856 }
857 ArgOffset += 8;
858 break;
859 case MVT::f32:
860 case MVT::f64:
861 if (FPR_remaining > 0) {
862 args_to_use.push_back(Args[i].first);
863 --FPR_remaining;
864 if (isVarArg) {
865 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
866 Args[i].first, PtrOff,
867 DAG.getSrcValue(NULL));
868 MemOps.push_back(Store);
869 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000870 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000871 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
872 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000873 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000874 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000875 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000876 }
877 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000878 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
879 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000880 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
881 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000882 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000883 args_to_use.push_back(Load);
884 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000885 }
886 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000887 // If we have any FPRs remaining, we may also have GPRs remaining.
888 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
889 // GPRs.
890 if (GPR_remaining > 0) {
891 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
892 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000893 }
Chris Lattner915fb302005-08-30 00:19:00 +0000894 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
895 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
896 --GPR_remaining;
897 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000898 }
Chris Lattner915fb302005-08-30 00:19:00 +0000899 } else {
900 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
901 Args[i].first, PtrOff,
902 DAG.getSrcValue(NULL)));
903 }
904 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
905 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000906 }
907 }
908 if (!MemOps.empty())
909 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
910 }
911
912 std::vector<MVT::ValueType> RetVals;
913 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000914 MVT::ValueType ActualRetTyVT = RetTyVT;
915 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
916 ActualRetTyVT = MVT::i32; // Promote result to i32.
917
Chris Lattnere00ebf02006-01-28 07:33:03 +0000918 if (RetTyVT == MVT::i64) {
919 RetVals.push_back(MVT::i32);
920 RetVals.push_back(MVT::i32);
921 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000922 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000923 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000924 RetVals.push_back(MVT::Other);
925
Chris Lattner2823b3e2005-11-17 05:56:14 +0000926 // If the callee is a GlobalAddress node (quite common, every direct call is)
927 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
928 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
929 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
930
Chris Lattner281b55e2006-01-27 23:34:02 +0000931 std::vector<SDOperand> Ops;
932 Ops.push_back(Chain);
933 Ops.push_back(Callee);
934 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
935 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000936 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000937 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
938 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000939 SDOperand RetVal = TheCall;
940
941 // If the result is a small value, add a note so that we keep track of the
942 // information about whether it is sign or zero extended.
943 if (RetTyVT != ActualRetTyVT) {
944 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
945 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
946 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000947 } else if (RetTyVT == MVT::i64) {
948 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000949 }
950
951 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000952}
953
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000954MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000955PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
956 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000957 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000958 MI->getOpcode() == PPC::SELECT_CC_F4 ||
959 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000960 "Unexpected instr type to insert");
961
962 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
963 // control-flow pattern. The incoming instruction knows the destination vreg
964 // to set, the condition code register to branch on, the true/false values to
965 // select between, and a branch opcode to use.
966 const BasicBlock *LLVM_BB = BB->getBasicBlock();
967 ilist<MachineBasicBlock>::iterator It = BB;
968 ++It;
969
970 // thisMBB:
971 // ...
972 // TrueVal = ...
973 // cmpTY ccX, r1, r2
974 // bCC copy1MBB
975 // fallthrough --> copy0MBB
976 MachineBasicBlock *thisMBB = BB;
977 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
978 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
979 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
980 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
981 MachineFunction *F = BB->getParent();
982 F->getBasicBlockList().insert(It, copy0MBB);
983 F->getBasicBlockList().insert(It, sinkMBB);
984 // Update machine-CFG edges
985 BB->addSuccessor(copy0MBB);
986 BB->addSuccessor(sinkMBB);
987
988 // copy0MBB:
989 // %FalseValue = ...
990 // # fallthrough to sinkMBB
991 BB = copy0MBB;
992
993 // Update machine-CFG edges
994 BB->addSuccessor(sinkMBB);
995
996 // sinkMBB:
997 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
998 // ...
999 BB = sinkMBB;
1000 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
1001 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
1002 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1003
1004 delete MI; // The pseudo instruction is gone now.
1005 return BB;
1006}
1007
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001008SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N,
1009 DAGCombinerInfo &DCI) const {
1010 TargetMachine &TM = getTargetMachine();
1011 SelectionDAG &DAG = DCI.DAG;
1012 switch (N->getOpcode()) {
1013 default: break;
1014 case ISD::SINT_TO_FP:
1015 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
1016 // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
1017 // We allow the src/dst to be either f32/f64, but force the intermediate
1018 // type to be i64.
1019 if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT &&
1020 N->getOperand(0).getValueType() == MVT::i64) {
1021
1022 SDOperand Val = N->getOperand(0).getOperand(0);
1023 if (Val.getValueType() == MVT::f32) {
1024 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1025 DCI.AddToWorklist(Val.Val);
1026 }
1027
1028 Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val);
1029 DCI.AddToWorklist(Val.Val);
1030 Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val);
1031 DCI.AddToWorklist(Val.Val);
1032 if (N->getValueType(0) == MVT::f32) {
1033 Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val);
1034 DCI.AddToWorklist(Val.Val);
1035 }
1036 return Val;
1037 }
1038 }
1039 break;
Chris Lattner51269842006-03-01 05:50:56 +00001040 case ISD::STORE:
1041 // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
1042 if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
1043 N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
1044 N->getOperand(1).getValueType() == MVT::i32) {
1045 SDOperand Val = N->getOperand(1).getOperand(0);
1046 if (Val.getValueType() == MVT::f32) {
1047 Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val);
1048 DCI.AddToWorklist(Val.Val);
1049 }
1050 Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val);
1051 DCI.AddToWorklist(Val.Val);
1052
1053 Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val,
1054 N->getOperand(2), N->getOperand(3));
1055 DCI.AddToWorklist(Val.Val);
1056 return Val;
1057 }
1058 break;
Chris Lattner8c13d0a2006-03-01 04:57:39 +00001059 }
1060
1061 return SDOperand();
1062}
1063
Chris Lattnerad3bc8d2006-02-07 20:16:30 +00001064/// getConstraintType - Given a constraint letter, return the type of
1065/// constraint it is for this target.
1066PPCTargetLowering::ConstraintType
1067PPCTargetLowering::getConstraintType(char ConstraintLetter) const {
1068 switch (ConstraintLetter) {
1069 default: break;
1070 case 'b':
1071 case 'r':
1072 case 'f':
1073 case 'v':
1074 case 'y':
1075 return C_RegisterClass;
1076 }
1077 return TargetLowering::getConstraintType(ConstraintLetter);
1078}
1079
1080
Chris Lattnerddc787d2006-01-31 19:20:21 +00001081std::vector<unsigned> PPCTargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00001082getRegClassForInlineAsmConstraint(const std::string &Constraint,
1083 MVT::ValueType VT) const {
Chris Lattnerddc787d2006-01-31 19:20:21 +00001084 if (Constraint.size() == 1) {
1085 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
1086 default: break; // Unknown constriant letter
1087 case 'b':
1088 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
1089 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1090 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1091 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1092 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1093 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1094 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1095 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1096 0);
1097 case 'r':
1098 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
1099 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
1100 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
1101 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
1102 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
1103 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
1104 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
1105 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
1106 0);
1107 case 'f':
1108 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
1109 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
1110 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
1111 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
1112 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
1113 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
1114 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
1115 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
1116 0);
1117 case 'v':
1118 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
1119 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
1120 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
1121 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
1122 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
1123 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
1124 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
1125 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
1126 0);
1127 case 'y':
1128 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1129 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1130 0);
1131 }
1132 }
1133
Chris Lattner1efa40f2006-02-22 00:56:39 +00001134 return std::vector<unsigned>();
Chris Lattnerddc787d2006-01-31 19:20:21 +00001135}
Chris Lattner763317d2006-02-07 00:47:13 +00001136
1137// isOperandValidForConstraint
1138bool PPCTargetLowering::
1139isOperandValidForConstraint(SDOperand Op, char Letter) {
1140 switch (Letter) {
1141 default: break;
1142 case 'I':
1143 case 'J':
1144 case 'K':
1145 case 'L':
1146 case 'M':
1147 case 'N':
1148 case 'O':
1149 case 'P': {
1150 if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate.
1151 unsigned Value = cast<ConstantSDNode>(Op)->getValue();
1152 switch (Letter) {
1153 default: assert(0 && "Unknown constraint letter!");
1154 case 'I': // "I" is a signed 16-bit constant.
1155 return (short)Value == (int)Value;
1156 case 'J': // "J" is a constant with only the high-order 16 bits nonzero.
1157 case 'L': // "L" is a signed 16-bit constant shifted left 16 bits.
1158 return (short)Value == 0;
1159 case 'K': // "K" is a constant with only the low-order 16 bits nonzero.
1160 return (Value >> 16) == 0;
1161 case 'M': // "M" is a constant that is greater than 31.
1162 return Value > 31;
1163 case 'N': // "N" is a positive constant that is an exact power of two.
1164 return (int)Value > 0 && isPowerOf2_32(Value);
1165 case 'O': // "O" is the constant zero.
1166 return Value == 0;
1167 case 'P': // "P" is a constant whose negation is a signed 16-bit constant.
1168 return (short)-Value == (int)-Value;
1169 }
1170 break;
1171 }
1172 }
1173
1174 // Handle standard constraint letters.
1175 return TargetLowering::isOperandValidForConstraint(Op, Letter);
1176}
Evan Chengc4c62572006-03-13 23:20:37 +00001177
1178/// isLegalAddressImmediate - Return true if the integer value can be used
1179/// as the offset of the target addressing mode.
1180bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const {
1181 // PPC allows a sign-extended 16-bit immediate field.
1182 return (V > -(1 << 16) && V < (1 << 16)-1);
1183}