blob: d8c00dcb1aa71179850bbcc2bcc616b4121d9b9b [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"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000019#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000021#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022#include "llvm/Function.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000023using namespace llvm;
24
Nate Begeman21e463b2005-10-16 05:39:50 +000025PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026 : TargetLowering(TM) {
27
28 // Fold away setcc operations if possible.
29 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000030 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000031
Chris Lattnerd145a612005-09-27 22:18:25 +000032 // Use _setjmp/_longjmp instead of setjmp/longjmp.
33 setUseUnderscoreSetJmpLongJmp(true);
34
Chris Lattner7c5a3d32005-08-16 17:14:42 +000035 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000036 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
37 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
38 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000039
40 // PowerPC has no intrinsics for these particular operations
41 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
42 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
43 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
44
45 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
46 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
47 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
48
49 // PowerPC has no SREM/UREM instructions
50 setOperationAction(ISD::SREM, MVT::i32, Expand);
51 setOperationAction(ISD::UREM, MVT::i32, Expand);
52
53 // We don't support sin/cos/sqrt/fmod
54 setOperationAction(ISD::FSIN , MVT::f64, Expand);
55 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000056 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000057 setOperationAction(ISD::FSIN , MVT::f32, Expand);
58 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000059 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000060
61 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000062 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000063 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
64 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
65 }
66
67 // PowerPC does not have CTPOP or CTTZ
68 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
69 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
70
Nate Begeman35ef9132006-01-11 21:21:00 +000071 // PowerPC does not have ROTR
72 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
73
Chris Lattner7c5a3d32005-08-16 17:14:42 +000074 // PowerPC does not have Select
75 setOperationAction(ISD::SELECT, MVT::i32, Expand);
76 setOperationAction(ISD::SELECT, MVT::f32, Expand);
77 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000078
Chris Lattner0b1e4e52005-08-26 17:36:52 +000079 // PowerPC wants to turn select_cc of FP into fsel when possible.
80 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
81 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000082
Nate Begeman7cbd5252005-08-16 19:49:35 +000083 // PowerPC does not have BRCOND* which requires SetCC
84 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
85 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000086
Chris Lattnerf7605322005-08-31 21:09:52 +000087 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
88 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000089
Jim Laskeyad23c9d2005-08-17 00:40:22 +000090 // PowerPC does not have [U|S]INT_TO_FP
91 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
92 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
93
Chris Lattner53e88452005-12-23 05:13:35 +000094 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
95 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
96
Chris Lattnere6ec9f22005-09-10 00:21:06 +000097 // PowerPC does not have truncstore for i1.
98 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +000099
Jim Laskeyabf6d172006-01-05 01:25:28 +0000100 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000101 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000102 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000103 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000104 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000105 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000106
Nate Begeman28a6b022005-12-10 02:36:00 +0000107 // We want to legalize GlobalAddress and ConstantPool nodes into the
108 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000109 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000110 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000111
112 // Not implemented yet.
113 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
114 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000115
Nate Begemanc09eeec2005-09-06 22:03:27 +0000116 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000117 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000118 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
119 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000120 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
121 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
122 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000123 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000124 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000125 }
126
127 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
128 // 64 bit PowerPC implementations can support i64 types directly
129 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000130 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
131 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000132 } else {
133 // 32 bit PowerPC wants to expand i64 shifts itself.
134 setOperationAction(ISD::SHL, MVT::i64, Custom);
135 setOperationAction(ISD::SRL, MVT::i64, Custom);
136 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000137 }
138
Nate Begeman425a9692005-11-29 08:17:20 +0000139 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000140 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000141 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000142 }
143
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000144 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000145 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000146
147 computeRegisterProperties();
148}
149
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000150const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
151 switch (Opcode) {
152 default: return 0;
153 case PPCISD::FSEL: return "PPCISD::FSEL";
154 case PPCISD::FCFID: return "PPCISD::FCFID";
155 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
156 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
157 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
158 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
159 case PPCISD::Hi: return "PPCISD::Hi";
160 case PPCISD::Lo: return "PPCISD::Lo";
161 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
162 case PPCISD::SRL: return "PPCISD::SRL";
163 case PPCISD::SRA: return "PPCISD::SRA";
164 case PPCISD::SHL: return "PPCISD::SHL";
165 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
166 }
167}
168
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000169/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
170static bool isFloatingPointZero(SDOperand Op) {
171 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
172 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
173 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
174 // Maybe this has already been legalized into the constant pool?
175 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
176 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
177 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
178 }
179 return false;
180}
181
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000182/// LowerOperation - Provide custom lowering hooks for some operations.
183///
Nate Begeman21e463b2005-10-16 05:39:50 +0000184SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000185 switch (Op.getOpcode()) {
186 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000187 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000188 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000189 SDOperand Src = Op.getOperand(0);
190 if (Src.getValueType() == MVT::f32)
191 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
192
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000193 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000194 switch (Op.getValueType()) {
195 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
196 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000197 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000198 break;
199 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000200 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000201 break;
202 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000203
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000204 // Convert the FP value to an int value through memory.
205 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
206 if (Op.getValueType() == MVT::i32)
207 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
208 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000209 }
210 case ISD::SINT_TO_FP: {
211 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
212 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000213 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
214 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000215 if (MVT::f32 == Op.getValueType())
216 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
217 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000218 }
219 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000220 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000221 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
222 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
223 break;
224
225 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
226
227 // Cannot handle SETEQ/SETNE.
228 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
229
230 MVT::ValueType ResVT = Op.getValueType();
231 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
232 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
233 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000234
Chris Lattnerf7605322005-08-31 21:09:52 +0000235 // If the RHS of the comparison is a 0.0, we don't need to do the
236 // subtraction at all.
237 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000238 switch (CC) {
239 default: assert(0 && "Invalid FSEL condition"); abort();
240 case ISD::SETULT:
241 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000242 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000243 case ISD::SETUGE:
244 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000245 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
246 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000247 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000248 case ISD::SETUGT:
249 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000250 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000251 case ISD::SETULE:
252 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000253 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
254 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000255 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000256 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000257 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000258
Chris Lattnereb255f22005-10-25 20:54:57 +0000259 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000260 switch (CC) {
261 default: assert(0 && "Invalid FSEL condition"); abort();
262 case ISD::SETULT:
263 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000264 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
265 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
266 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
267 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000268 case ISD::SETUGE:
269 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000270 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
271 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
272 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
273 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000274 case ISD::SETUGT:
275 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000276 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
277 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
278 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
279 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000280 case ISD::SETULE:
281 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000282 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
283 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
284 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
285 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000286 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000287 break;
288 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000289 case ISD::SHL: {
290 assert(Op.getValueType() == MVT::i64 &&
291 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
292 // The generic code does a fine job expanding shift by a constant.
293 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
294
295 // Otherwise, expand into a bunch of logical ops. Note that these ops
296 // depend on the PPC behavior for oversized shift amounts.
297 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
298 DAG.getConstant(0, MVT::i32));
299 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
300 DAG.getConstant(1, MVT::i32));
301 SDOperand Amt = Op.getOperand(1);
302
303 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
304 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000305 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
306 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000307 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
308 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
309 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000310 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000311 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000312 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000313 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
314 }
315 case ISD::SRL: {
316 assert(Op.getValueType() == MVT::i64 &&
317 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
318 // The generic code does a fine job expanding shift by a constant.
319 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
320
321 // Otherwise, expand into a bunch of logical ops. Note that these ops
322 // depend on the PPC behavior for oversized shift amounts.
323 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
324 DAG.getConstant(0, MVT::i32));
325 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
326 DAG.getConstant(1, MVT::i32));
327 SDOperand Amt = Op.getOperand(1);
328
329 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
330 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000331 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
332 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000333 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
334 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
335 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000336 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000337 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000338 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000339 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
340 }
341 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000342 assert(Op.getValueType() == MVT::i64 &&
343 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
344 // The generic code does a fine job expanding shift by a constant.
345 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
346
347 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
348 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
349 DAG.getConstant(0, MVT::i32));
350 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
351 DAG.getConstant(1, MVT::i32));
352 SDOperand Amt = Op.getOperand(1);
353
354 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
355 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000356 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
357 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000358 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
359 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
360 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000361 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
362 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000363 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
364 Tmp4, Tmp6, ISD::SETLE);
365 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000366 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000367 case ISD::ConstantPool: {
368 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
369 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
370 SDOperand Zero = DAG.getConstant(0, MVT::i32);
371
372 if (PPCGenerateStaticCode) {
373 // Generate non-pic code that has direct accesses to the constant pool.
374 // The address of the global is just (hi(&g)+lo(&g)).
375 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
376 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
377 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
378 }
379
380 // Only lower ConstantPool on Darwin.
381 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
382 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
383 if (PICEnabled) {
384 // With PIC, the first instruction is actually "GR+hi(&G)".
385 Hi = DAG.getNode(ISD::ADD, MVT::i32,
386 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
387 }
388
389 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
390 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
391 return Lo;
392 }
Chris Lattner860e8862005-11-17 07:30:41 +0000393 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000394 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
395 GlobalValue *GV = GSDN->getGlobal();
396 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000397 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000398
399 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000400 // Generate non-pic code that has direct accesses to globals.
401 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000402 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
403 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
404 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
405 }
Chris Lattner860e8862005-11-17 07:30:41 +0000406
Chris Lattner1d05cb42005-11-17 18:55:48 +0000407 // Only lower GlobalAddress on Darwin.
408 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000409
Chris Lattner860e8862005-11-17 07:30:41 +0000410 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
411 if (PICEnabled) {
412 // With PIC, the first instruction is actually "GR+hi(&G)".
413 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000414 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000415 }
416
417 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
418 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
419
Chris Lattnera35ef632006-01-06 01:04:03 +0000420 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000421 return Lo;
422
423 // If the global is weak or external, we have to go through the lazy
424 // resolution stub.
425 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
426 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000427 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000428 return SDOperand();
429}
430
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000431std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000432PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000433 //
434 // add beautiful description of PPC stack frame format, or at least some docs
435 //
436 MachineFunction &MF = DAG.getMachineFunction();
437 MachineFrameInfo *MFI = MF.getFrameInfo();
438 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000439 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000440 std::vector<SDOperand> ArgValues;
441
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000442 unsigned ArgOffset = 24;
443 unsigned GPR_remaining = 8;
444 unsigned FPR_remaining = 13;
445 unsigned GPR_idx = 0, FPR_idx = 0;
446 static const unsigned GPR[] = {
447 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
448 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
449 };
450 static const unsigned FPR[] = {
451 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
452 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
453 };
454
455 // Add DAG nodes to load the arguments... On entry to a function on PPC,
456 // the arguments start at offset 24, although they are likely to be passed
457 // in registers.
458 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
459 SDOperand newroot, argt;
460 unsigned ObjSize;
461 bool needsLoad = false;
462 bool ArgLive = !I->use_empty();
463 MVT::ValueType ObjectVT = getValueType(I->getType());
464
465 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000466 default: assert(0 && "Unhandled argument type!");
467 case MVT::i1:
468 case MVT::i8:
469 case MVT::i16:
470 case MVT::i32:
471 ObjSize = 4;
472 if (!ArgLive) break;
473 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000474 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000475 MF.addLiveIn(GPR[GPR_idx], VReg);
476 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000477 if (ObjectVT != MVT::i32) {
478 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
479 : ISD::AssertZext;
480 argt = DAG.getNode(AssertOp, MVT::i32, argt,
481 DAG.getValueType(ObjectVT));
482 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
483 }
Chris Lattner915fb302005-08-30 00:19:00 +0000484 } else {
485 needsLoad = true;
486 }
487 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000488 case MVT::i64:
489 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000490 if (!ArgLive) break;
491 if (GPR_remaining > 0) {
492 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000493 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000494 MF.addLiveIn(GPR[GPR_idx], VReg);
495 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000496 // If we have two or more remaining argument registers, then both halves
497 // of the i64 can be sourced from there. Otherwise, the lower half will
498 // have to come off the stack. This can happen when an i64 is preceded
499 // by 28 bytes of arguments.
500 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000501 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000502 MF.addLiveIn(GPR[GPR_idx+1], VReg);
503 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000504 } else {
505 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
506 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
507 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
508 DAG.getSrcValue(NULL));
509 }
510 // Build the outgoing arg thingy
511 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
512 newroot = argLo;
513 } else {
514 needsLoad = true;
515 }
516 break;
517 case MVT::f32:
518 case MVT::f64:
519 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000520 if (!ArgLive) {
521 if (FPR_remaining > 0) {
522 --FPR_remaining;
523 ++FPR_idx;
524 }
525 break;
526 }
Chris Lattner915fb302005-08-30 00:19:00 +0000527 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000528 unsigned VReg;
529 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000530 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000531 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000532 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000533 MF.addLiveIn(FPR[FPR_idx], VReg);
534 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000535 --FPR_remaining;
536 ++FPR_idx;
537 } else {
538 needsLoad = true;
539 }
540 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000541 }
542
543 // We need to load the argument to a virtual register if we determined above
544 // that we ran out of physical registers of the appropriate type
545 if (needsLoad) {
546 unsigned SubregOffset = 0;
547 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
548 if (ObjectVT == MVT::i16) SubregOffset = 2;
549 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
550 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
551 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
552 DAG.getConstant(SubregOffset, MVT::i32));
553 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
554 DAG.getSrcValue(NULL));
555 }
556
557 // Every 4 bytes of argument space consumes one of the GPRs available for
558 // argument passing.
559 if (GPR_remaining > 0) {
560 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
561 GPR_remaining -= delta;
562 GPR_idx += delta;
563 }
564 ArgOffset += ObjSize;
565 if (newroot.Val)
566 DAG.setRoot(newroot.getValue(1));
567
568 ArgValues.push_back(argt);
569 }
570
571 // If the function takes variable number of arguments, make a frame index for
572 // the start of the first vararg value... for expansion of llvm.va_start.
573 if (F.isVarArg()) {
574 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
575 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
576 // If this function is vararg, store any remaining integer argument regs
577 // to their spots on the stack so that they may be loaded by deferencing the
578 // result of va_next.
579 std::vector<SDOperand> MemOps;
580 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000581 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000582 MF.addLiveIn(GPR[GPR_idx], VReg);
583 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000584 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
585 Val, FIN, DAG.getSrcValue(NULL));
586 MemOps.push_back(Store);
587 // Increment the address by four for the next argument to store
588 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
589 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
590 }
Chris Lattner80720a92005-11-30 20:40:54 +0000591 if (!MemOps.empty()) {
592 MemOps.push_back(DAG.getRoot());
593 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
594 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000595 }
596
597 // Finally, inform the code generator which regs we return values in.
598 switch (getValueType(F.getReturnType())) {
599 default: assert(0 && "Unknown type!");
600 case MVT::isVoid: break;
601 case MVT::i1:
602 case MVT::i8:
603 case MVT::i16:
604 case MVT::i32:
605 MF.addLiveOut(PPC::R3);
606 break;
607 case MVT::i64:
608 MF.addLiveOut(PPC::R3);
609 MF.addLiveOut(PPC::R4);
610 break;
611 case MVT::f32:
612 case MVT::f64:
613 MF.addLiveOut(PPC::F1);
614 break;
615 }
616
617 return ArgValues;
618}
619
620std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000621PPCTargetLowering::LowerCallTo(SDOperand Chain,
622 const Type *RetTy, bool isVarArg,
623 unsigned CallingConv, bool isTailCall,
624 SDOperand Callee, ArgListTy &Args,
625 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000626 // args_to_use will accumulate outgoing args for the ISD::CALL case in
627 // SelectExpr to use to put the arguments in the appropriate registers.
628 std::vector<SDOperand> args_to_use;
629
630 // Count how many bytes are to be pushed on the stack, including the linkage
631 // area, and parameter passing area.
632 unsigned NumBytes = 24;
633
634 if (Args.empty()) {
635 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
636 DAG.getConstant(NumBytes, getPointerTy()));
637 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000638 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000639 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000640 default: assert(0 && "Unknown value type!");
641 case MVT::i1:
642 case MVT::i8:
643 case MVT::i16:
644 case MVT::i32:
645 case MVT::f32:
646 NumBytes += 4;
647 break;
648 case MVT::i64:
649 case MVT::f64:
650 NumBytes += 8;
651 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000652 }
Chris Lattner915fb302005-08-30 00:19:00 +0000653 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000654
Chris Lattner915fb302005-08-30 00:19:00 +0000655 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
656 // plus 32 bytes of argument space in case any called code gets funky on us.
657 // (Required by ABI to support var arg)
658 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000659
660 // Adjust the stack pointer for the new arguments...
661 // These operations are automatically eliminated by the prolog/epilog pass
662 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
663 DAG.getConstant(NumBytes, getPointerTy()));
664
665 // Set up a copy of the stack pointer for use loading and storing any
666 // arguments that may not fit in the registers available for argument
667 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000668 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000669
670 // Figure out which arguments are going to go in registers, and which in
671 // memory. Also, if this is a vararg function, floating point operations
672 // must be stored to our stack, and loaded into integer regs as well, if
673 // any integer regs are available for argument passing.
674 unsigned ArgOffset = 24;
675 unsigned GPR_remaining = 8;
676 unsigned FPR_remaining = 13;
677
678 std::vector<SDOperand> MemOps;
679 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
680 // PtrOff will be used to store the current argument to the stack if a
681 // register cannot be found for it.
682 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
683 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
684 MVT::ValueType ArgVT = getValueType(Args[i].second);
685
686 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000687 default: assert(0 && "Unexpected ValueType for argument!");
688 case MVT::i1:
689 case MVT::i8:
690 case MVT::i16:
691 // Promote the integer to 32 bits. If the input type is signed use a
692 // sign extend, otherwise use a zero extend.
693 if (Args[i].second->isSigned())
694 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
695 else
696 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
697 // FALL THROUGH
698 case MVT::i32:
699 if (GPR_remaining > 0) {
700 args_to_use.push_back(Args[i].first);
701 --GPR_remaining;
702 } else {
703 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
704 Args[i].first, PtrOff,
705 DAG.getSrcValue(NULL)));
706 }
707 ArgOffset += 4;
708 break;
709 case MVT::i64:
710 // If we have one free GPR left, we can place the upper half of the i64
711 // in it, and store the other half to the stack. If we have two or more
712 // free GPRs, then we can pass both halves of the i64 in registers.
713 if (GPR_remaining > 0) {
714 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
715 Args[i].first, DAG.getConstant(1, MVT::i32));
716 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
717 Args[i].first, DAG.getConstant(0, MVT::i32));
718 args_to_use.push_back(Hi);
719 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000720 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000721 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000722 --GPR_remaining;
723 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000724 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
725 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000726 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000727 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000728 }
Chris Lattner915fb302005-08-30 00:19:00 +0000729 } else {
730 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
731 Args[i].first, PtrOff,
732 DAG.getSrcValue(NULL)));
733 }
734 ArgOffset += 8;
735 break;
736 case MVT::f32:
737 case MVT::f64:
738 if (FPR_remaining > 0) {
739 args_to_use.push_back(Args[i].first);
740 --FPR_remaining;
741 if (isVarArg) {
742 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
743 Args[i].first, PtrOff,
744 DAG.getSrcValue(NULL));
745 MemOps.push_back(Store);
746 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000747 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000748 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
749 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000750 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000751 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000752 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000753 }
754 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000755 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
756 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000757 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
758 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000759 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000760 args_to_use.push_back(Load);
761 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000762 }
763 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000764 // If we have any FPRs remaining, we may also have GPRs remaining.
765 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
766 // GPRs.
767 if (GPR_remaining > 0) {
768 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
769 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000770 }
Chris Lattner915fb302005-08-30 00:19:00 +0000771 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
772 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
773 --GPR_remaining;
774 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000775 }
Chris Lattner915fb302005-08-30 00:19:00 +0000776 } else {
777 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
778 Args[i].first, PtrOff,
779 DAG.getSrcValue(NULL)));
780 }
781 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
782 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000783 }
784 }
785 if (!MemOps.empty())
786 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
787 }
788
789 std::vector<MVT::ValueType> RetVals;
790 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000791 MVT::ValueType ActualRetTyVT = RetTyVT;
792 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
793 ActualRetTyVT = MVT::i32; // Promote result to i32.
794
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000795 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000796 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000797 RetVals.push_back(MVT::Other);
798
Chris Lattner2823b3e2005-11-17 05:56:14 +0000799 // If the callee is a GlobalAddress node (quite common, every direct call is)
800 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
801 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
802 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
803
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000804 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
805 Chain, Callee, args_to_use), 0);
806 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
807 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
808 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000809 SDOperand RetVal = TheCall;
810
811 // If the result is a small value, add a note so that we keep track of the
812 // information about whether it is sign or zero extended.
813 if (RetTyVT != ActualRetTyVT) {
814 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
815 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
816 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
817 }
818
819 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000820}
821
Nate Begeman4a959452005-10-18 23:23:37 +0000822SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
823 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000824 SDOperand Copy;
825 switch (Op.getValueType()) {
826 default: assert(0 && "Unknown type to return!");
827 case MVT::i32:
828 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
829 break;
830 case MVT::f32:
831 case MVT::f64:
832 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
833 break;
834 case MVT::i64:
835 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
836 DAG.getConstant(1, MVT::i32));
837 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
838 DAG.getConstant(0, MVT::i32));
839 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
840 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
841 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000842 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000843 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000844}
845
Nate Begeman21e463b2005-10-16 05:39:50 +0000846SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
847 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000848 // vastart just stores the address of the VarArgsFrameIndex slot into the
849 // memory location argument.
850 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
851 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
852 DAG.getSrcValue(VAListV));
853}
854
855std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000856PPCTargetLowering::LowerVAArg(SDOperand Chain,
857 SDOperand VAListP, Value *VAListV,
858 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000859 MVT::ValueType ArgVT = getValueType(ArgTy);
860
861 SDOperand VAList =
862 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
863 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
864 unsigned Amt;
865 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
866 Amt = 4;
867 else {
868 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
869 "Other types should have been promoted for varargs!");
870 Amt = 8;
871 }
872 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
873 DAG.getConstant(Amt, VAList.getValueType()));
874 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
875 VAList, VAListP, DAG.getSrcValue(VAListV));
876 return std::make_pair(Result, Chain);
877}
878
879
Nate Begeman21e463b2005-10-16 05:39:50 +0000880std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000881LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
882 SelectionDAG &DAG) {
883 assert(0 && "LowerFrameReturnAddress unimplemented");
884 abort();
885}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000886
887MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000888PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
889 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000890 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000891 MI->getOpcode() == PPC::SELECT_CC_F4 ||
892 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000893 "Unexpected instr type to insert");
894
895 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
896 // control-flow pattern. The incoming instruction knows the destination vreg
897 // to set, the condition code register to branch on, the true/false values to
898 // select between, and a branch opcode to use.
899 const BasicBlock *LLVM_BB = BB->getBasicBlock();
900 ilist<MachineBasicBlock>::iterator It = BB;
901 ++It;
902
903 // thisMBB:
904 // ...
905 // TrueVal = ...
906 // cmpTY ccX, r1, r2
907 // bCC copy1MBB
908 // fallthrough --> copy0MBB
909 MachineBasicBlock *thisMBB = BB;
910 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
911 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
912 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
913 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
914 MachineFunction *F = BB->getParent();
915 F->getBasicBlockList().insert(It, copy0MBB);
916 F->getBasicBlockList().insert(It, sinkMBB);
917 // Update machine-CFG edges
918 BB->addSuccessor(copy0MBB);
919 BB->addSuccessor(sinkMBB);
920
921 // copy0MBB:
922 // %FalseValue = ...
923 // # fallthrough to sinkMBB
924 BB = copy0MBB;
925
926 // Update machine-CFG edges
927 BB->addSuccessor(sinkMBB);
928
929 // sinkMBB:
930 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
931 // ...
932 BB = sinkMBB;
933 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
934 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
935 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
936
937 delete MI; // The pseudo instruction is gone now.
938 return BB;
939}
940