blob: 565eb0a02562432a263b6244cdefb3f76632e754 [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
71 // PowerPC does not have Select
72 setOperationAction(ISD::SELECT, MVT::i32, Expand);
73 setOperationAction(ISD::SELECT, MVT::f32, Expand);
74 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000075
Chris Lattner0b1e4e52005-08-26 17:36:52 +000076 // PowerPC wants to turn select_cc of FP into fsel when possible.
77 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
78 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000079
Nate Begeman7cbd5252005-08-16 19:49:35 +000080 // PowerPC does not have BRCOND* which requires SetCC
81 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
82 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000083
Chris Lattnerf7605322005-08-31 21:09:52 +000084 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
85 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000086
Jim Laskeyad23c9d2005-08-17 00:40:22 +000087 // PowerPC does not have [U|S]INT_TO_FP
88 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
89 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
90
Chris Lattner53e88452005-12-23 05:13:35 +000091 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
92 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
93
Chris Lattnere6ec9f22005-09-10 00:21:06 +000094 // PowerPC does not have truncstore for i1.
95 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +000096
97 // PowerPC doesn't have line number support yet.
98 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +000099
Nate Begeman28a6b022005-12-10 02:36:00 +0000100 // We want to legalize GlobalAddress and ConstantPool nodes into the
101 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000102 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000103 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattner860e8862005-11-17 07:30:41 +0000104
Nate Begemanc09eeec2005-09-06 22:03:27 +0000105 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000106 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000107 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
108 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000109 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
110 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
111 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000112 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000113 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000114 }
115
116 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
117 // 64 bit PowerPC implementations can support i64 types directly
118 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000119 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
120 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000121 } else {
122 // 32 bit PowerPC wants to expand i64 shifts itself.
123 setOperationAction(ISD::SHL, MVT::i64, Custom);
124 setOperationAction(ISD::SRL, MVT::i64, Custom);
125 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000126 }
127
Nate Begeman425a9692005-11-29 08:17:20 +0000128 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
129 // FIXME: AltiVec supports a wide variety of packed types. For now, we're
130 // bringing up support with just v4f32.
131 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000132 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000133 }
134
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000135 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000136
137 computeRegisterProperties();
138}
139
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000140/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
141static bool isFloatingPointZero(SDOperand Op) {
142 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
143 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
144 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
145 // Maybe this has already been legalized into the constant pool?
146 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
147 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
148 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
149 }
150 return false;
151}
152
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000153/// LowerOperation - Provide custom lowering hooks for some operations.
154///
Nate Begeman21e463b2005-10-16 05:39:50 +0000155SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000156 switch (Op.getOpcode()) {
157 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000158 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000159 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000160 SDOperand Src = Op.getOperand(0);
161 if (Src.getValueType() == MVT::f32)
162 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
163
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000164 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000165 switch (Op.getValueType()) {
166 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
167 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000168 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000169 break;
170 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000171 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000172 break;
173 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000174
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000175 // Convert the FP value to an int value through memory.
176 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
177 if (Op.getValueType() == MVT::i32)
178 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
179 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000180 }
181 case ISD::SINT_TO_FP: {
182 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
183 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000184 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
185 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000186 if (MVT::f32 == Op.getValueType())
187 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
188 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000189 }
190 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000191 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000192 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
193 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
194 break;
195
196 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
197
198 // Cannot handle SETEQ/SETNE.
199 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
200
201 MVT::ValueType ResVT = Op.getValueType();
202 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
203 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
204 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000205
Chris Lattnerf7605322005-08-31 21:09:52 +0000206 // If the RHS of the comparison is a 0.0, we don't need to do the
207 // subtraction at all.
208 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000209 switch (CC) {
210 default: assert(0 && "Invalid FSEL condition"); abort();
211 case ISD::SETULT:
212 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000213 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000214 case ISD::SETUGE:
215 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000216 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
217 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000218 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000219 case ISD::SETUGT:
220 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000221 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000222 case ISD::SETULE:
223 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000224 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
225 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000226 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000227 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000228 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000229
Chris Lattnereb255f22005-10-25 20:54:57 +0000230 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000231 switch (CC) {
232 default: assert(0 && "Invalid FSEL condition"); abort();
233 case ISD::SETULT:
234 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000235 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
236 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
237 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
238 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000239 case ISD::SETUGE:
240 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000241 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
242 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
243 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
244 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000245 case ISD::SETUGT:
246 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000247 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
248 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
249 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
250 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000251 case ISD::SETULE:
252 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000253 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
254 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
255 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
256 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000257 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000258 break;
259 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000260 case ISD::SHL: {
261 assert(Op.getValueType() == MVT::i64 &&
262 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
263 // The generic code does a fine job expanding shift by a constant.
264 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
265
266 // Otherwise, expand into a bunch of logical ops. Note that these ops
267 // depend on the PPC behavior for oversized shift amounts.
268 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
269 DAG.getConstant(0, MVT::i32));
270 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
271 DAG.getConstant(1, MVT::i32));
272 SDOperand Amt = Op.getOperand(1);
273
274 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
275 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000276 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
277 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000278 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
279 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
280 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000281 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000282 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000283 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000284 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
285 }
286 case ISD::SRL: {
287 assert(Op.getValueType() == MVT::i64 &&
288 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
289 // The generic code does a fine job expanding shift by a constant.
290 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
291
292 // Otherwise, expand into a bunch of logical ops. Note that these ops
293 // depend on the PPC behavior for oversized shift amounts.
294 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
295 DAG.getConstant(0, MVT::i32));
296 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
297 DAG.getConstant(1, MVT::i32));
298 SDOperand Amt = Op.getOperand(1);
299
300 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
301 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000302 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
303 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000304 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
305 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
306 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000307 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000308 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000309 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000310 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
311 }
312 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000313 assert(Op.getValueType() == MVT::i64 &&
314 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
315 // The generic code does a fine job expanding shift by a constant.
316 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
317
318 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
319 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
320 DAG.getConstant(0, MVT::i32));
321 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
322 DAG.getConstant(1, MVT::i32));
323 SDOperand Amt = Op.getOperand(1);
324
325 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
326 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000327 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
328 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000329 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
330 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
331 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000332 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
333 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000334 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
335 Tmp4, Tmp6, ISD::SETLE);
336 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000337 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000338 case ISD::ConstantPool: {
339 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
340 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
341 SDOperand Zero = DAG.getConstant(0, MVT::i32);
342
343 if (PPCGenerateStaticCode) {
344 // Generate non-pic code that has direct accesses to the constant pool.
345 // The address of the global is just (hi(&g)+lo(&g)).
346 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
347 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
348 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
349 }
350
351 // Only lower ConstantPool on Darwin.
352 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
353 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
354 if (PICEnabled) {
355 // With PIC, the first instruction is actually "GR+hi(&G)".
356 Hi = DAG.getNode(ISD::ADD, MVT::i32,
357 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
358 }
359
360 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
361 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
362 return Lo;
363 }
Chris Lattner860e8862005-11-17 07:30:41 +0000364 case ISD::GlobalAddress: {
Chris Lattner860e8862005-11-17 07:30:41 +0000365 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
366 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
367 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000368
369 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000370 // Generate non-pic code that has direct accesses to globals.
371 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000372 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
373 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
374 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
375 }
Chris Lattner860e8862005-11-17 07:30:41 +0000376
Chris Lattner1d05cb42005-11-17 18:55:48 +0000377 // Only lower GlobalAddress on Darwin.
378 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000379 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
380 if (PICEnabled) {
381 // With PIC, the first instruction is actually "GR+hi(&G)".
382 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000383 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000384 }
385
386 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
387 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
388
389 if (!GV->hasWeakLinkage() && !GV->isExternal())
390 return Lo;
391
392 // If the global is weak or external, we have to go through the lazy
393 // resolution stub.
394 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
395 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000396 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000397 return SDOperand();
398}
399
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000400std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000401PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000402 //
403 // add beautiful description of PPC stack frame format, or at least some docs
404 //
405 MachineFunction &MF = DAG.getMachineFunction();
406 MachineFrameInfo *MFI = MF.getFrameInfo();
407 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000408 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000409 std::vector<SDOperand> ArgValues;
410
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000411 unsigned ArgOffset = 24;
412 unsigned GPR_remaining = 8;
413 unsigned FPR_remaining = 13;
414 unsigned GPR_idx = 0, FPR_idx = 0;
415 static const unsigned GPR[] = {
416 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
417 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
418 };
419 static const unsigned FPR[] = {
420 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
421 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
422 };
423
424 // Add DAG nodes to load the arguments... On entry to a function on PPC,
425 // the arguments start at offset 24, although they are likely to be passed
426 // in registers.
427 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
428 SDOperand newroot, argt;
429 unsigned ObjSize;
430 bool needsLoad = false;
431 bool ArgLive = !I->use_empty();
432 MVT::ValueType ObjectVT = getValueType(I->getType());
433
434 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000435 default: assert(0 && "Unhandled argument type!");
436 case MVT::i1:
437 case MVT::i8:
438 case MVT::i16:
439 case MVT::i32:
440 ObjSize = 4;
441 if (!ArgLive) break;
442 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000443 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000444 MF.addLiveIn(GPR[GPR_idx], VReg);
445 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000446 if (ObjectVT != MVT::i32) {
447 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
448 : ISD::AssertZext;
449 argt = DAG.getNode(AssertOp, MVT::i32, argt,
450 DAG.getValueType(ObjectVT));
451 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
452 }
Chris Lattner915fb302005-08-30 00:19:00 +0000453 } else {
454 needsLoad = true;
455 }
456 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000457 case MVT::i64:
458 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000459 if (!ArgLive) break;
460 if (GPR_remaining > 0) {
461 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000462 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000463 MF.addLiveIn(GPR[GPR_idx], VReg);
464 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000465 // If we have two or more remaining argument registers, then both halves
466 // of the i64 can be sourced from there. Otherwise, the lower half will
467 // have to come off the stack. This can happen when an i64 is preceded
468 // by 28 bytes of arguments.
469 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000470 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000471 MF.addLiveIn(GPR[GPR_idx+1], VReg);
472 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000473 } else {
474 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
475 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
476 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
477 DAG.getSrcValue(NULL));
478 }
479 // Build the outgoing arg thingy
480 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
481 newroot = argLo;
482 } else {
483 needsLoad = true;
484 }
485 break;
486 case MVT::f32:
487 case MVT::f64:
488 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
489 if (!ArgLive) break;
490 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000491 unsigned VReg;
492 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000493 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000494 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000495 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000496 MF.addLiveIn(FPR[FPR_idx], VReg);
497 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000498 --FPR_remaining;
499 ++FPR_idx;
500 } else {
501 needsLoad = true;
502 }
503 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000504 }
505
506 // We need to load the argument to a virtual register if we determined above
507 // that we ran out of physical registers of the appropriate type
508 if (needsLoad) {
509 unsigned SubregOffset = 0;
510 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
511 if (ObjectVT == MVT::i16) SubregOffset = 2;
512 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
513 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
514 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
515 DAG.getConstant(SubregOffset, MVT::i32));
516 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
517 DAG.getSrcValue(NULL));
518 }
519
520 // Every 4 bytes of argument space consumes one of the GPRs available for
521 // argument passing.
522 if (GPR_remaining > 0) {
523 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
524 GPR_remaining -= delta;
525 GPR_idx += delta;
526 }
527 ArgOffset += ObjSize;
528 if (newroot.Val)
529 DAG.setRoot(newroot.getValue(1));
530
531 ArgValues.push_back(argt);
532 }
533
534 // If the function takes variable number of arguments, make a frame index for
535 // the start of the first vararg value... for expansion of llvm.va_start.
536 if (F.isVarArg()) {
537 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
538 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
539 // If this function is vararg, store any remaining integer argument regs
540 // to their spots on the stack so that they may be loaded by deferencing the
541 // result of va_next.
542 std::vector<SDOperand> MemOps;
543 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000544 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000545 MF.addLiveIn(GPR[GPR_idx], VReg);
546 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000547 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
548 Val, FIN, DAG.getSrcValue(NULL));
549 MemOps.push_back(Store);
550 // Increment the address by four for the next argument to store
551 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
552 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
553 }
Chris Lattner80720a92005-11-30 20:40:54 +0000554 if (!MemOps.empty()) {
555 MemOps.push_back(DAG.getRoot());
556 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
557 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000558 }
559
560 // Finally, inform the code generator which regs we return values in.
561 switch (getValueType(F.getReturnType())) {
562 default: assert(0 && "Unknown type!");
563 case MVT::isVoid: break;
564 case MVT::i1:
565 case MVT::i8:
566 case MVT::i16:
567 case MVT::i32:
568 MF.addLiveOut(PPC::R3);
569 break;
570 case MVT::i64:
571 MF.addLiveOut(PPC::R3);
572 MF.addLiveOut(PPC::R4);
573 break;
574 case MVT::f32:
575 case MVT::f64:
576 MF.addLiveOut(PPC::F1);
577 break;
578 }
579
580 return ArgValues;
581}
582
583std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000584PPCTargetLowering::LowerCallTo(SDOperand Chain,
585 const Type *RetTy, bool isVarArg,
586 unsigned CallingConv, bool isTailCall,
587 SDOperand Callee, ArgListTy &Args,
588 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000589 // args_to_use will accumulate outgoing args for the ISD::CALL case in
590 // SelectExpr to use to put the arguments in the appropriate registers.
591 std::vector<SDOperand> args_to_use;
592
593 // Count how many bytes are to be pushed on the stack, including the linkage
594 // area, and parameter passing area.
595 unsigned NumBytes = 24;
596
597 if (Args.empty()) {
598 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
599 DAG.getConstant(NumBytes, getPointerTy()));
600 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000601 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000602 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000603 default: assert(0 && "Unknown value type!");
604 case MVT::i1:
605 case MVT::i8:
606 case MVT::i16:
607 case MVT::i32:
608 case MVT::f32:
609 NumBytes += 4;
610 break;
611 case MVT::i64:
612 case MVT::f64:
613 NumBytes += 8;
614 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000615 }
Chris Lattner915fb302005-08-30 00:19:00 +0000616 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000617
Chris Lattner915fb302005-08-30 00:19:00 +0000618 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
619 // plus 32 bytes of argument space in case any called code gets funky on us.
620 // (Required by ABI to support var arg)
621 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000622
623 // Adjust the stack pointer for the new arguments...
624 // These operations are automatically eliminated by the prolog/epilog pass
625 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
626 DAG.getConstant(NumBytes, getPointerTy()));
627
628 // Set up a copy of the stack pointer for use loading and storing any
629 // arguments that may not fit in the registers available for argument
630 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000631 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
632 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000633
634 // Figure out which arguments are going to go in registers, and which in
635 // memory. Also, if this is a vararg function, floating point operations
636 // must be stored to our stack, and loaded into integer regs as well, if
637 // any integer regs are available for argument passing.
638 unsigned ArgOffset = 24;
639 unsigned GPR_remaining = 8;
640 unsigned FPR_remaining = 13;
641
642 std::vector<SDOperand> MemOps;
643 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
644 // PtrOff will be used to store the current argument to the stack if a
645 // register cannot be found for it.
646 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
647 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
648 MVT::ValueType ArgVT = getValueType(Args[i].second);
649
650 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000651 default: assert(0 && "Unexpected ValueType for argument!");
652 case MVT::i1:
653 case MVT::i8:
654 case MVT::i16:
655 // Promote the integer to 32 bits. If the input type is signed use a
656 // sign extend, otherwise use a zero extend.
657 if (Args[i].second->isSigned())
658 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
659 else
660 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
661 // FALL THROUGH
662 case MVT::i32:
663 if (GPR_remaining > 0) {
664 args_to_use.push_back(Args[i].first);
665 --GPR_remaining;
666 } else {
667 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
668 Args[i].first, PtrOff,
669 DAG.getSrcValue(NULL)));
670 }
671 ArgOffset += 4;
672 break;
673 case MVT::i64:
674 // If we have one free GPR left, we can place the upper half of the i64
675 // in it, and store the other half to the stack. If we have two or more
676 // free GPRs, then we can pass both halves of the i64 in registers.
677 if (GPR_remaining > 0) {
678 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
679 Args[i].first, DAG.getConstant(1, MVT::i32));
680 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
681 Args[i].first, DAG.getConstant(0, MVT::i32));
682 args_to_use.push_back(Hi);
683 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000684 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000685 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000686 --GPR_remaining;
687 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000688 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
689 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000690 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000691 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000692 }
Chris Lattner915fb302005-08-30 00:19:00 +0000693 } else {
694 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
695 Args[i].first, PtrOff,
696 DAG.getSrcValue(NULL)));
697 }
698 ArgOffset += 8;
699 break;
700 case MVT::f32:
701 case MVT::f64:
702 if (FPR_remaining > 0) {
703 args_to_use.push_back(Args[i].first);
704 --FPR_remaining;
705 if (isVarArg) {
706 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
707 Args[i].first, PtrOff,
708 DAG.getSrcValue(NULL));
709 MemOps.push_back(Store);
710 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000711 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000712 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
713 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000714 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000715 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000716 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000717 }
718 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000719 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
720 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000721 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
722 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000723 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000724 args_to_use.push_back(Load);
725 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000726 }
727 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000728 // If we have any FPRs remaining, we may also have GPRs remaining.
729 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
730 // GPRs.
731 if (GPR_remaining > 0) {
732 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
733 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000734 }
Chris Lattner915fb302005-08-30 00:19:00 +0000735 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
736 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
737 --GPR_remaining;
738 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000739 }
Chris Lattner915fb302005-08-30 00:19:00 +0000740 } else {
741 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
742 Args[i].first, PtrOff,
743 DAG.getSrcValue(NULL)));
744 }
745 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
746 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000747 }
748 }
749 if (!MemOps.empty())
750 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
751 }
752
753 std::vector<MVT::ValueType> RetVals;
754 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000755 MVT::ValueType ActualRetTyVT = RetTyVT;
756 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
757 ActualRetTyVT = MVT::i32; // Promote result to i32.
758
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000759 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000760 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000761 RetVals.push_back(MVT::Other);
762
Chris Lattner2823b3e2005-11-17 05:56:14 +0000763 // If the callee is a GlobalAddress node (quite common, every direct call is)
764 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
765 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
766 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
767
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000768 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
769 Chain, Callee, args_to_use), 0);
770 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
771 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
772 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000773 SDOperand RetVal = TheCall;
774
775 // If the result is a small value, add a note so that we keep track of the
776 // information about whether it is sign or zero extended.
777 if (RetTyVT != ActualRetTyVT) {
778 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
779 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
780 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
781 }
782
783 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000784}
785
Nate Begeman4a959452005-10-18 23:23:37 +0000786SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
787 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000788 SDOperand Copy;
789 switch (Op.getValueType()) {
790 default: assert(0 && "Unknown type to return!");
791 case MVT::i32:
792 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
793 break;
794 case MVT::f32:
795 case MVT::f64:
796 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
797 break;
798 case MVT::i64:
799 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
800 DAG.getConstant(1, MVT::i32));
801 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
802 DAG.getConstant(0, MVT::i32));
803 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
804 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
805 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000806 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000807 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000808}
809
Nate Begeman21e463b2005-10-16 05:39:50 +0000810SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
811 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000812 // vastart just stores the address of the VarArgsFrameIndex slot into the
813 // memory location argument.
814 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
815 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
816 DAG.getSrcValue(VAListV));
817}
818
819std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000820PPCTargetLowering::LowerVAArg(SDOperand Chain,
821 SDOperand VAListP, Value *VAListV,
822 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000823 MVT::ValueType ArgVT = getValueType(ArgTy);
824
825 SDOperand VAList =
826 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
827 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
828 unsigned Amt;
829 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
830 Amt = 4;
831 else {
832 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
833 "Other types should have been promoted for varargs!");
834 Amt = 8;
835 }
836 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
837 DAG.getConstant(Amt, VAList.getValueType()));
838 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
839 VAList, VAListP, DAG.getSrcValue(VAListV));
840 return std::make_pair(Result, Chain);
841}
842
843
Nate Begeman21e463b2005-10-16 05:39:50 +0000844std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000845LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
846 SelectionDAG &DAG) {
847 assert(0 && "LowerFrameReturnAddress unimplemented");
848 abort();
849}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000850
851MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000852PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
853 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000854 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000855 MI->getOpcode() == PPC::SELECT_CC_F4 ||
856 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000857 "Unexpected instr type to insert");
858
859 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
860 // control-flow pattern. The incoming instruction knows the destination vreg
861 // to set, the condition code register to branch on, the true/false values to
862 // select between, and a branch opcode to use.
863 const BasicBlock *LLVM_BB = BB->getBasicBlock();
864 ilist<MachineBasicBlock>::iterator It = BB;
865 ++It;
866
867 // thisMBB:
868 // ...
869 // TrueVal = ...
870 // cmpTY ccX, r1, r2
871 // bCC copy1MBB
872 // fallthrough --> copy0MBB
873 MachineBasicBlock *thisMBB = BB;
874 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
875 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
876 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
877 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
878 MachineFunction *F = BB->getParent();
879 F->getBasicBlockList().insert(It, copy0MBB);
880 F->getBasicBlockList().insert(It, sinkMBB);
881 // Update machine-CFG edges
882 BB->addSuccessor(copy0MBB);
883 BB->addSuccessor(sinkMBB);
884
885 // copy0MBB:
886 // %FalseValue = ...
887 // # fallthrough to sinkMBB
888 BB = copy0MBB;
889
890 // Update machine-CFG edges
891 BB->addSuccessor(sinkMBB);
892
893 // sinkMBB:
894 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
895 // ...
896 BB = sinkMBB;
897 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
898 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
899 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
900
901 delete MI; // The pseudo instruction is gone now.
902 return BB;
903}
904