blob: a5ba28d0b058940204faa0721a939f08c13cb0c6 [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 Lattner860e8862005-11-17 07:30:41 +0000111
Nate Begemanc09eeec2005-09-06 22:03:27 +0000112 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000113 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000114 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
115 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000116 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
117 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
118 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000119 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000120 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000121 }
122
123 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
124 // 64 bit PowerPC implementations can support i64 types directly
125 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000126 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
127 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000128 } else {
129 // 32 bit PowerPC wants to expand i64 shifts itself.
130 setOperationAction(ISD::SHL, MVT::i64, Custom);
131 setOperationAction(ISD::SRL, MVT::i64, Custom);
132 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000133 }
134
Nate Begeman425a9692005-11-29 08:17:20 +0000135 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000136 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000137 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000138 }
139
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000140 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000141
142 computeRegisterProperties();
143}
144
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000145const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
146 switch (Opcode) {
147 default: return 0;
148 case PPCISD::FSEL: return "PPCISD::FSEL";
149 case PPCISD::FCFID: return "PPCISD::FCFID";
150 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
151 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
152 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
153 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
154 case PPCISD::Hi: return "PPCISD::Hi";
155 case PPCISD::Lo: return "PPCISD::Lo";
156 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
157 case PPCISD::SRL: return "PPCISD::SRL";
158 case PPCISD::SRA: return "PPCISD::SRA";
159 case PPCISD::SHL: return "PPCISD::SHL";
160 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
161 }
162}
163
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000164/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
165static bool isFloatingPointZero(SDOperand Op) {
166 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
167 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
168 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
169 // Maybe this has already been legalized into the constant pool?
170 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
171 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
172 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
173 }
174 return false;
175}
176
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000177/// LowerOperation - Provide custom lowering hooks for some operations.
178///
Nate Begeman21e463b2005-10-16 05:39:50 +0000179SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000180 switch (Op.getOpcode()) {
181 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000182 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000183 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000184 SDOperand Src = Op.getOperand(0);
185 if (Src.getValueType() == MVT::f32)
186 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
187
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000188 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000189 switch (Op.getValueType()) {
190 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
191 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000192 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000193 break;
194 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000195 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000196 break;
197 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000198
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000199 // Convert the FP value to an int value through memory.
200 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
201 if (Op.getValueType() == MVT::i32)
202 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
203 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000204 }
205 case ISD::SINT_TO_FP: {
206 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
207 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000208 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
209 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000210 if (MVT::f32 == Op.getValueType())
211 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
212 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000213 }
214 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000215 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000216 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
217 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
218 break;
219
220 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
221
222 // Cannot handle SETEQ/SETNE.
223 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
224
225 MVT::ValueType ResVT = Op.getValueType();
226 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
227 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
228 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000229
Chris Lattnerf7605322005-08-31 21:09:52 +0000230 // If the RHS of the comparison is a 0.0, we don't need to do the
231 // subtraction at all.
232 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000233 switch (CC) {
234 default: assert(0 && "Invalid FSEL condition"); abort();
235 case ISD::SETULT:
236 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000237 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000238 case ISD::SETUGE:
239 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000240 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
241 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000242 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000243 case ISD::SETUGT:
244 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000245 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000246 case ISD::SETULE:
247 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000248 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
249 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000250 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000251 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000252 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000253
Chris Lattnereb255f22005-10-25 20:54:57 +0000254 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000255 switch (CC) {
256 default: assert(0 && "Invalid FSEL condition"); abort();
257 case ISD::SETULT:
258 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000259 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
260 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
261 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
262 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000263 case ISD::SETUGE:
264 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000265 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
266 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
267 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
268 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000269 case ISD::SETUGT:
270 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000271 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
272 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
273 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
274 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000275 case ISD::SETULE:
276 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000277 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
278 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
279 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
280 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000281 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000282 break;
283 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000284 case ISD::SHL: {
285 assert(Op.getValueType() == MVT::i64 &&
286 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
287 // The generic code does a fine job expanding shift by a constant.
288 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
289
290 // Otherwise, expand into a bunch of logical ops. Note that these ops
291 // depend on the PPC behavior for oversized shift amounts.
292 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
293 DAG.getConstant(0, MVT::i32));
294 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
295 DAG.getConstant(1, MVT::i32));
296 SDOperand Amt = Op.getOperand(1);
297
298 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
299 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000300 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
301 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000302 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
303 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
304 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000305 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000306 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000307 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000308 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
309 }
310 case ISD::SRL: {
311 assert(Op.getValueType() == MVT::i64 &&
312 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
313 // The generic code does a fine job expanding shift by a constant.
314 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
315
316 // Otherwise, expand into a bunch of logical ops. Note that these ops
317 // depend on the PPC behavior for oversized shift amounts.
318 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
319 DAG.getConstant(0, MVT::i32));
320 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
321 DAG.getConstant(1, MVT::i32));
322 SDOperand Amt = Op.getOperand(1);
323
324 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
325 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000326 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
327 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000328 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
329 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
330 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000331 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000332 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000333 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000334 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
335 }
336 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000337 assert(Op.getValueType() == MVT::i64 &&
338 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
339 // The generic code does a fine job expanding shift by a constant.
340 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
341
342 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
343 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
344 DAG.getConstant(0, MVT::i32));
345 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
346 DAG.getConstant(1, MVT::i32));
347 SDOperand Amt = Op.getOperand(1);
348
349 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
350 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000351 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
352 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000353 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
354 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
355 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000356 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
357 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000358 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
359 Tmp4, Tmp6, ISD::SETLE);
360 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000361 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000362 case ISD::ConstantPool: {
363 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
364 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
365 SDOperand Zero = DAG.getConstant(0, MVT::i32);
366
367 if (PPCGenerateStaticCode) {
368 // Generate non-pic code that has direct accesses to the constant pool.
369 // The address of the global is just (hi(&g)+lo(&g)).
370 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
371 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
372 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
373 }
374
375 // Only lower ConstantPool on Darwin.
376 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
377 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
378 if (PICEnabled) {
379 // With PIC, the first instruction is actually "GR+hi(&G)".
380 Hi = DAG.getNode(ISD::ADD, MVT::i32,
381 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
382 }
383
384 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
385 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
386 return Lo;
387 }
Chris Lattner860e8862005-11-17 07:30:41 +0000388 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000389 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
390 GlobalValue *GV = GSDN->getGlobal();
391 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000392 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000393
394 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000395 // Generate non-pic code that has direct accesses to globals.
396 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000397 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
398 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
399 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
400 }
Chris Lattner860e8862005-11-17 07:30:41 +0000401
Chris Lattner1d05cb42005-11-17 18:55:48 +0000402 // Only lower GlobalAddress on Darwin.
403 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000404
Chris Lattner860e8862005-11-17 07:30:41 +0000405 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
406 if (PICEnabled) {
407 // With PIC, the first instruction is actually "GR+hi(&G)".
408 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000409 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000410 }
411
412 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
413 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
414
Chris Lattnera35ef632006-01-06 01:04:03 +0000415 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000416 return Lo;
417
418 // If the global is weak or external, we have to go through the lazy
419 // resolution stub.
420 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
421 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000422 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000423 return SDOperand();
424}
425
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000426std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000427PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000428 //
429 // add beautiful description of PPC stack frame format, or at least some docs
430 //
431 MachineFunction &MF = DAG.getMachineFunction();
432 MachineFrameInfo *MFI = MF.getFrameInfo();
433 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000434 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000435 std::vector<SDOperand> ArgValues;
436
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000437 unsigned ArgOffset = 24;
438 unsigned GPR_remaining = 8;
439 unsigned FPR_remaining = 13;
440 unsigned GPR_idx = 0, FPR_idx = 0;
441 static const unsigned GPR[] = {
442 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
443 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
444 };
445 static const unsigned FPR[] = {
446 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
447 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
448 };
449
450 // Add DAG nodes to load the arguments... On entry to a function on PPC,
451 // the arguments start at offset 24, although they are likely to be passed
452 // in registers.
453 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
454 SDOperand newroot, argt;
455 unsigned ObjSize;
456 bool needsLoad = false;
457 bool ArgLive = !I->use_empty();
458 MVT::ValueType ObjectVT = getValueType(I->getType());
459
460 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000461 default: assert(0 && "Unhandled argument type!");
462 case MVT::i1:
463 case MVT::i8:
464 case MVT::i16:
465 case MVT::i32:
466 ObjSize = 4;
467 if (!ArgLive) break;
468 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000469 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000470 MF.addLiveIn(GPR[GPR_idx], VReg);
471 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000472 if (ObjectVT != MVT::i32) {
473 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
474 : ISD::AssertZext;
475 argt = DAG.getNode(AssertOp, MVT::i32, argt,
476 DAG.getValueType(ObjectVT));
477 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
478 }
Chris Lattner915fb302005-08-30 00:19:00 +0000479 } else {
480 needsLoad = true;
481 }
482 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000483 case MVT::i64:
484 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000485 if (!ArgLive) break;
486 if (GPR_remaining > 0) {
487 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000488 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000489 MF.addLiveIn(GPR[GPR_idx], VReg);
490 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000491 // If we have two or more remaining argument registers, then both halves
492 // of the i64 can be sourced from there. Otherwise, the lower half will
493 // have to come off the stack. This can happen when an i64 is preceded
494 // by 28 bytes of arguments.
495 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000496 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000497 MF.addLiveIn(GPR[GPR_idx+1], VReg);
498 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000499 } else {
500 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
501 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
502 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
503 DAG.getSrcValue(NULL));
504 }
505 // Build the outgoing arg thingy
506 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
507 newroot = argLo;
508 } else {
509 needsLoad = true;
510 }
511 break;
512 case MVT::f32:
513 case MVT::f64:
514 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000515 if (!ArgLive) {
516 if (FPR_remaining > 0) {
517 --FPR_remaining;
518 ++FPR_idx;
519 }
520 break;
521 }
Chris Lattner915fb302005-08-30 00:19:00 +0000522 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000523 unsigned VReg;
524 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000525 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000526 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000527 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000528 MF.addLiveIn(FPR[FPR_idx], VReg);
529 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000530 --FPR_remaining;
531 ++FPR_idx;
532 } else {
533 needsLoad = true;
534 }
535 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000536 }
537
538 // We need to load the argument to a virtual register if we determined above
539 // that we ran out of physical registers of the appropriate type
540 if (needsLoad) {
541 unsigned SubregOffset = 0;
542 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
543 if (ObjectVT == MVT::i16) SubregOffset = 2;
544 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
545 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
546 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
547 DAG.getConstant(SubregOffset, MVT::i32));
548 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
549 DAG.getSrcValue(NULL));
550 }
551
552 // Every 4 bytes of argument space consumes one of the GPRs available for
553 // argument passing.
554 if (GPR_remaining > 0) {
555 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
556 GPR_remaining -= delta;
557 GPR_idx += delta;
558 }
559 ArgOffset += ObjSize;
560 if (newroot.Val)
561 DAG.setRoot(newroot.getValue(1));
562
563 ArgValues.push_back(argt);
564 }
565
566 // If the function takes variable number of arguments, make a frame index for
567 // the start of the first vararg value... for expansion of llvm.va_start.
568 if (F.isVarArg()) {
569 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
570 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
571 // If this function is vararg, store any remaining integer argument regs
572 // to their spots on the stack so that they may be loaded by deferencing the
573 // result of va_next.
574 std::vector<SDOperand> MemOps;
575 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000576 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000577 MF.addLiveIn(GPR[GPR_idx], VReg);
578 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000579 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
580 Val, FIN, DAG.getSrcValue(NULL));
581 MemOps.push_back(Store);
582 // Increment the address by four for the next argument to store
583 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
584 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
585 }
Chris Lattner80720a92005-11-30 20:40:54 +0000586 if (!MemOps.empty()) {
587 MemOps.push_back(DAG.getRoot());
588 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
589 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000590 }
591
592 // Finally, inform the code generator which regs we return values in.
593 switch (getValueType(F.getReturnType())) {
594 default: assert(0 && "Unknown type!");
595 case MVT::isVoid: break;
596 case MVT::i1:
597 case MVT::i8:
598 case MVT::i16:
599 case MVT::i32:
600 MF.addLiveOut(PPC::R3);
601 break;
602 case MVT::i64:
603 MF.addLiveOut(PPC::R3);
604 MF.addLiveOut(PPC::R4);
605 break;
606 case MVT::f32:
607 case MVT::f64:
608 MF.addLiveOut(PPC::F1);
609 break;
610 }
611
612 return ArgValues;
613}
614
615std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000616PPCTargetLowering::LowerCallTo(SDOperand Chain,
617 const Type *RetTy, bool isVarArg,
618 unsigned CallingConv, bool isTailCall,
619 SDOperand Callee, ArgListTy &Args,
620 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000621 // args_to_use will accumulate outgoing args for the ISD::CALL case in
622 // SelectExpr to use to put the arguments in the appropriate registers.
623 std::vector<SDOperand> args_to_use;
624
625 // Count how many bytes are to be pushed on the stack, including the linkage
626 // area, and parameter passing area.
627 unsigned NumBytes = 24;
628
629 if (Args.empty()) {
630 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
631 DAG.getConstant(NumBytes, getPointerTy()));
632 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000633 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000634 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000635 default: assert(0 && "Unknown value type!");
636 case MVT::i1:
637 case MVT::i8:
638 case MVT::i16:
639 case MVT::i32:
640 case MVT::f32:
641 NumBytes += 4;
642 break;
643 case MVT::i64:
644 case MVT::f64:
645 NumBytes += 8;
646 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000647 }
Chris Lattner915fb302005-08-30 00:19:00 +0000648 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000649
Chris Lattner915fb302005-08-30 00:19:00 +0000650 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
651 // plus 32 bytes of argument space in case any called code gets funky on us.
652 // (Required by ABI to support var arg)
653 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000654
655 // Adjust the stack pointer for the new arguments...
656 // These operations are automatically eliminated by the prolog/epilog pass
657 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
658 DAG.getConstant(NumBytes, getPointerTy()));
659
660 // Set up a copy of the stack pointer for use loading and storing any
661 // arguments that may not fit in the registers available for argument
662 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000663 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000664
665 // Figure out which arguments are going to go in registers, and which in
666 // memory. Also, if this is a vararg function, floating point operations
667 // must be stored to our stack, and loaded into integer regs as well, if
668 // any integer regs are available for argument passing.
669 unsigned ArgOffset = 24;
670 unsigned GPR_remaining = 8;
671 unsigned FPR_remaining = 13;
672
673 std::vector<SDOperand> MemOps;
674 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
675 // PtrOff will be used to store the current argument to the stack if a
676 // register cannot be found for it.
677 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
678 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
679 MVT::ValueType ArgVT = getValueType(Args[i].second);
680
681 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000682 default: assert(0 && "Unexpected ValueType for argument!");
683 case MVT::i1:
684 case MVT::i8:
685 case MVT::i16:
686 // Promote the integer to 32 bits. If the input type is signed use a
687 // sign extend, otherwise use a zero extend.
688 if (Args[i].second->isSigned())
689 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
690 else
691 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
692 // FALL THROUGH
693 case MVT::i32:
694 if (GPR_remaining > 0) {
695 args_to_use.push_back(Args[i].first);
696 --GPR_remaining;
697 } else {
698 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
699 Args[i].first, PtrOff,
700 DAG.getSrcValue(NULL)));
701 }
702 ArgOffset += 4;
703 break;
704 case MVT::i64:
705 // If we have one free GPR left, we can place the upper half of the i64
706 // in it, and store the other half to the stack. If we have two or more
707 // free GPRs, then we can pass both halves of the i64 in registers.
708 if (GPR_remaining > 0) {
709 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
710 Args[i].first, DAG.getConstant(1, MVT::i32));
711 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
712 Args[i].first, DAG.getConstant(0, MVT::i32));
713 args_to_use.push_back(Hi);
714 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000715 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000716 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000717 --GPR_remaining;
718 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000719 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
720 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000721 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000722 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000723 }
Chris Lattner915fb302005-08-30 00:19:00 +0000724 } else {
725 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
726 Args[i].first, PtrOff,
727 DAG.getSrcValue(NULL)));
728 }
729 ArgOffset += 8;
730 break;
731 case MVT::f32:
732 case MVT::f64:
733 if (FPR_remaining > 0) {
734 args_to_use.push_back(Args[i].first);
735 --FPR_remaining;
736 if (isVarArg) {
737 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
738 Args[i].first, PtrOff,
739 DAG.getSrcValue(NULL));
740 MemOps.push_back(Store);
741 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000742 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000743 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
744 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000745 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000746 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000747 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000748 }
749 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000750 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
751 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000752 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
753 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000754 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000755 args_to_use.push_back(Load);
756 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000757 }
758 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000759 // If we have any FPRs remaining, we may also have GPRs remaining.
760 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
761 // GPRs.
762 if (GPR_remaining > 0) {
763 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
764 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000765 }
Chris Lattner915fb302005-08-30 00:19:00 +0000766 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
767 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
768 --GPR_remaining;
769 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000770 }
Chris Lattner915fb302005-08-30 00:19:00 +0000771 } else {
772 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
773 Args[i].first, PtrOff,
774 DAG.getSrcValue(NULL)));
775 }
776 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
777 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000778 }
779 }
780 if (!MemOps.empty())
781 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
782 }
783
784 std::vector<MVT::ValueType> RetVals;
785 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000786 MVT::ValueType ActualRetTyVT = RetTyVT;
787 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
788 ActualRetTyVT = MVT::i32; // Promote result to i32.
789
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000790 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000791 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000792 RetVals.push_back(MVT::Other);
793
Chris Lattner2823b3e2005-11-17 05:56:14 +0000794 // If the callee is a GlobalAddress node (quite common, every direct call is)
795 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
796 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
797 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
798
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000799 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
800 Chain, Callee, args_to_use), 0);
801 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
802 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
803 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000804 SDOperand RetVal = TheCall;
805
806 // If the result is a small value, add a note so that we keep track of the
807 // information about whether it is sign or zero extended.
808 if (RetTyVT != ActualRetTyVT) {
809 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
810 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
811 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
812 }
813
814 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000815}
816
Nate Begeman4a959452005-10-18 23:23:37 +0000817SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
818 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000819 SDOperand Copy;
820 switch (Op.getValueType()) {
821 default: assert(0 && "Unknown type to return!");
822 case MVT::i32:
823 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
824 break;
825 case MVT::f32:
826 case MVT::f64:
827 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
828 break;
829 case MVT::i64:
830 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
831 DAG.getConstant(1, MVT::i32));
832 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
833 DAG.getConstant(0, MVT::i32));
834 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
835 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
836 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000837 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000838 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000839}
840
Nate Begeman21e463b2005-10-16 05:39:50 +0000841SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
842 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000843 // vastart just stores the address of the VarArgsFrameIndex slot into the
844 // memory location argument.
845 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
846 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
847 DAG.getSrcValue(VAListV));
848}
849
850std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000851PPCTargetLowering::LowerVAArg(SDOperand Chain,
852 SDOperand VAListP, Value *VAListV,
853 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000854 MVT::ValueType ArgVT = getValueType(ArgTy);
855
856 SDOperand VAList =
857 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
858 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
859 unsigned Amt;
860 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
861 Amt = 4;
862 else {
863 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
864 "Other types should have been promoted for varargs!");
865 Amt = 8;
866 }
867 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
868 DAG.getConstant(Amt, VAList.getValueType()));
869 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
870 VAList, VAListP, DAG.getSrcValue(VAListV));
871 return std::make_pair(Result, Chain);
872}
873
874
Nate Begeman21e463b2005-10-16 05:39:50 +0000875std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000876LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
877 SelectionDAG &DAG) {
878 assert(0 && "LowerFrameReturnAddress unimplemented");
879 abort();
880}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000881
882MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000883PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
884 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000885 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000886 MI->getOpcode() == PPC::SELECT_CC_F4 ||
887 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000888 "Unexpected instr type to insert");
889
890 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
891 // control-flow pattern. The incoming instruction knows the destination vreg
892 // to set, the condition code register to branch on, the true/false values to
893 // select between, and a branch opcode to use.
894 const BasicBlock *LLVM_BB = BB->getBasicBlock();
895 ilist<MachineBasicBlock>::iterator It = BB;
896 ++It;
897
898 // thisMBB:
899 // ...
900 // TrueVal = ...
901 // cmpTY ccX, r1, r2
902 // bCC copy1MBB
903 // fallthrough --> copy0MBB
904 MachineBasicBlock *thisMBB = BB;
905 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
906 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
907 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
908 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
909 MachineFunction *F = BB->getParent();
910 F->getBasicBlockList().insert(It, copy0MBB);
911 F->getBasicBlockList().insert(It, sinkMBB);
912 // Update machine-CFG edges
913 BB->addSuccessor(copy0MBB);
914 BB->addSuccessor(sinkMBB);
915
916 // copy0MBB:
917 // %FalseValue = ...
918 // # fallthrough to sinkMBB
919 BB = copy0MBB;
920
921 // Update machine-CFG edges
922 BB->addSuccessor(sinkMBB);
923
924 // sinkMBB:
925 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
926 // ...
927 BB = sinkMBB;
928 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
929 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
930 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
931
932 delete MI; // The pseudo instruction is gone now.
933 return BB;
934}
935