blob: 9547b9e9d4f8bc4ad0a48c58204d212a1ca7f088 [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
Jim Laskeyabf6d172006-01-05 01:25:28 +000097 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +000098 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +000099 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000100 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000101 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000102 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000103
Nate Begeman28a6b022005-12-10 02:36:00 +0000104 // We want to legalize GlobalAddress and ConstantPool nodes into the
105 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000106 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000107 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattner860e8862005-11-17 07:30:41 +0000108
Nate Begemanc09eeec2005-09-06 22:03:27 +0000109 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000110 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000111 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
112 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000113 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
114 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
115 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000116 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000117 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000118 }
119
120 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
121 // 64 bit PowerPC implementations can support i64 types directly
122 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000123 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
124 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000125 } else {
126 // 32 bit PowerPC wants to expand i64 shifts itself.
127 setOperationAction(ISD::SHL, MVT::i64, Custom);
128 setOperationAction(ISD::SRL, MVT::i64, Custom);
129 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000130 }
131
Nate Begeman425a9692005-11-29 08:17:20 +0000132 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000133 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000134 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000135 }
136
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000137 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000138
139 computeRegisterProperties();
140}
141
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000142/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
143static bool isFloatingPointZero(SDOperand Op) {
144 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
145 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
146 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
147 // Maybe this has already been legalized into the constant pool?
148 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
149 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
150 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
151 }
152 return false;
153}
154
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000155/// LowerOperation - Provide custom lowering hooks for some operations.
156///
Nate Begeman21e463b2005-10-16 05:39:50 +0000157SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000158 switch (Op.getOpcode()) {
159 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000160 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000161 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000162 SDOperand Src = Op.getOperand(0);
163 if (Src.getValueType() == MVT::f32)
164 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
165
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000166 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000167 switch (Op.getValueType()) {
168 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
169 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000170 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000171 break;
172 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000173 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000174 break;
175 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000176
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000177 // Convert the FP value to an int value through memory.
178 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
179 if (Op.getValueType() == MVT::i32)
180 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
181 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000182 }
183 case ISD::SINT_TO_FP: {
184 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
185 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000186 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
187 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000188 if (MVT::f32 == Op.getValueType())
189 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
190 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000191 }
192 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000193 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000194 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
195 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
196 break;
197
198 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
199
200 // Cannot handle SETEQ/SETNE.
201 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
202
203 MVT::ValueType ResVT = Op.getValueType();
204 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
205 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
206 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000207
Chris Lattnerf7605322005-08-31 21:09:52 +0000208 // If the RHS of the comparison is a 0.0, we don't need to do the
209 // subtraction at all.
210 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000211 switch (CC) {
212 default: assert(0 && "Invalid FSEL condition"); abort();
213 case ISD::SETULT:
214 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000215 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000216 case ISD::SETUGE:
217 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000218 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
219 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000220 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000221 case ISD::SETUGT:
222 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000223 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000224 case ISD::SETULE:
225 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000226 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
227 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000228 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000229 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000230 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000231
Chris Lattnereb255f22005-10-25 20:54:57 +0000232 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000233 switch (CC) {
234 default: assert(0 && "Invalid FSEL condition"); abort();
235 case ISD::SETULT:
236 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000237 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
238 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
239 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
240 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000241 case ISD::SETUGE:
242 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000243 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
244 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
245 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
246 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000247 case ISD::SETUGT:
248 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000249 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
250 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
251 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
252 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000253 case ISD::SETULE:
254 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000255 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
256 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
257 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
258 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000259 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000260 break;
261 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000262 case ISD::SHL: {
263 assert(Op.getValueType() == MVT::i64 &&
264 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
265 // The generic code does a fine job expanding shift by a constant.
266 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
267
268 // Otherwise, expand into a bunch of logical ops. Note that these ops
269 // depend on the PPC behavior for oversized shift amounts.
270 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
271 DAG.getConstant(0, MVT::i32));
272 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
273 DAG.getConstant(1, MVT::i32));
274 SDOperand Amt = Op.getOperand(1);
275
276 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
277 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000278 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
279 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000280 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
281 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
282 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000283 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000284 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000285 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000286 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
287 }
288 case ISD::SRL: {
289 assert(Op.getValueType() == MVT::i64 &&
290 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
291 // The generic code does a fine job expanding shift by a constant.
292 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
293
294 // Otherwise, expand into a bunch of logical ops. Note that these ops
295 // depend on the PPC behavior for oversized shift amounts.
296 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
297 DAG.getConstant(0, MVT::i32));
298 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
299 DAG.getConstant(1, MVT::i32));
300 SDOperand Amt = Op.getOperand(1);
301
302 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
303 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000304 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
305 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000306 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
307 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
308 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000309 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000310 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000311 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000312 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
313 }
314 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000315 assert(Op.getValueType() == MVT::i64 &&
316 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
317 // The generic code does a fine job expanding shift by a constant.
318 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
319
320 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
321 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
322 DAG.getConstant(0, MVT::i32));
323 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
324 DAG.getConstant(1, MVT::i32));
325 SDOperand Amt = Op.getOperand(1);
326
327 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
328 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000329 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
330 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000331 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
332 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
333 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000334 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
335 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000336 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
337 Tmp4, Tmp6, ISD::SETLE);
338 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000339 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000340 case ISD::ConstantPool: {
341 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
342 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
343 SDOperand Zero = DAG.getConstant(0, MVT::i32);
344
345 if (PPCGenerateStaticCode) {
346 // Generate non-pic code that has direct accesses to the constant pool.
347 // The address of the global is just (hi(&g)+lo(&g)).
348 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
349 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
350 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
351 }
352
353 // Only lower ConstantPool on Darwin.
354 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
355 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
356 if (PICEnabled) {
357 // With PIC, the first instruction is actually "GR+hi(&G)".
358 Hi = DAG.getNode(ISD::ADD, MVT::i32,
359 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
360 }
361
362 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
363 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
364 return Lo;
365 }
Chris Lattner860e8862005-11-17 07:30:41 +0000366 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000367 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
368 GlobalValue *GV = GSDN->getGlobal();
369 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000370 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000371
372 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000373 // Generate non-pic code that has direct accesses to globals.
374 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000375 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
376 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
377 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
378 }
Chris Lattner860e8862005-11-17 07:30:41 +0000379
Chris Lattner1d05cb42005-11-17 18:55:48 +0000380 // Only lower GlobalAddress on Darwin.
381 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000382 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
383 if (PICEnabled) {
384 // With PIC, the first instruction is actually "GR+hi(&G)".
385 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000386 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000387 }
388
389 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
390 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
391
392 if (!GV->hasWeakLinkage() && !GV->isExternal())
393 return Lo;
394
395 // If the global is weak or external, we have to go through the lazy
396 // resolution stub.
397 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
398 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000399 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000400 return SDOperand();
401}
402
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000403std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000404PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000405 //
406 // add beautiful description of PPC stack frame format, or at least some docs
407 //
408 MachineFunction &MF = DAG.getMachineFunction();
409 MachineFrameInfo *MFI = MF.getFrameInfo();
410 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000411 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000412 std::vector<SDOperand> ArgValues;
413
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000414 unsigned ArgOffset = 24;
415 unsigned GPR_remaining = 8;
416 unsigned FPR_remaining = 13;
417 unsigned GPR_idx = 0, FPR_idx = 0;
418 static const unsigned GPR[] = {
419 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
420 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
421 };
422 static const unsigned FPR[] = {
423 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
424 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
425 };
426
427 // Add DAG nodes to load the arguments... On entry to a function on PPC,
428 // the arguments start at offset 24, although they are likely to be passed
429 // in registers.
430 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
431 SDOperand newroot, argt;
432 unsigned ObjSize;
433 bool needsLoad = false;
434 bool ArgLive = !I->use_empty();
435 MVT::ValueType ObjectVT = getValueType(I->getType());
436
437 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000438 default: assert(0 && "Unhandled argument type!");
439 case MVT::i1:
440 case MVT::i8:
441 case MVT::i16:
442 case MVT::i32:
443 ObjSize = 4;
444 if (!ArgLive) break;
445 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000446 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000447 MF.addLiveIn(GPR[GPR_idx], VReg);
448 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000449 if (ObjectVT != MVT::i32) {
450 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
451 : ISD::AssertZext;
452 argt = DAG.getNode(AssertOp, MVT::i32, argt,
453 DAG.getValueType(ObjectVT));
454 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
455 }
Chris Lattner915fb302005-08-30 00:19:00 +0000456 } else {
457 needsLoad = true;
458 }
459 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000460 case MVT::i64:
461 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000462 if (!ArgLive) break;
463 if (GPR_remaining > 0) {
464 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000465 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000466 MF.addLiveIn(GPR[GPR_idx], VReg);
467 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000468 // If we have two or more remaining argument registers, then both halves
469 // of the i64 can be sourced from there. Otherwise, the lower half will
470 // have to come off the stack. This can happen when an i64 is preceded
471 // by 28 bytes of arguments.
472 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000473 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000474 MF.addLiveIn(GPR[GPR_idx+1], VReg);
475 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000476 } else {
477 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
478 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
479 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
480 DAG.getSrcValue(NULL));
481 }
482 // Build the outgoing arg thingy
483 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
484 newroot = argLo;
485 } else {
486 needsLoad = true;
487 }
488 break;
489 case MVT::f32:
490 case MVT::f64:
491 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
492 if (!ArgLive) break;
493 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000494 unsigned VReg;
495 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000496 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000497 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000498 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000499 MF.addLiveIn(FPR[FPR_idx], VReg);
500 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000501 --FPR_remaining;
502 ++FPR_idx;
503 } else {
504 needsLoad = true;
505 }
506 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000507 }
508
509 // We need to load the argument to a virtual register if we determined above
510 // that we ran out of physical registers of the appropriate type
511 if (needsLoad) {
512 unsigned SubregOffset = 0;
513 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
514 if (ObjectVT == MVT::i16) SubregOffset = 2;
515 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
516 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
517 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
518 DAG.getConstant(SubregOffset, MVT::i32));
519 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
520 DAG.getSrcValue(NULL));
521 }
522
523 // Every 4 bytes of argument space consumes one of the GPRs available for
524 // argument passing.
525 if (GPR_remaining > 0) {
526 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
527 GPR_remaining -= delta;
528 GPR_idx += delta;
529 }
530 ArgOffset += ObjSize;
531 if (newroot.Val)
532 DAG.setRoot(newroot.getValue(1));
533
534 ArgValues.push_back(argt);
535 }
536
537 // If the function takes variable number of arguments, make a frame index for
538 // the start of the first vararg value... for expansion of llvm.va_start.
539 if (F.isVarArg()) {
540 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
541 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
542 // If this function is vararg, store any remaining integer argument regs
543 // to their spots on the stack so that they may be loaded by deferencing the
544 // result of va_next.
545 std::vector<SDOperand> MemOps;
546 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000547 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000548 MF.addLiveIn(GPR[GPR_idx], VReg);
549 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000550 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
551 Val, FIN, DAG.getSrcValue(NULL));
552 MemOps.push_back(Store);
553 // Increment the address by four for the next argument to store
554 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
555 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
556 }
Chris Lattner80720a92005-11-30 20:40:54 +0000557 if (!MemOps.empty()) {
558 MemOps.push_back(DAG.getRoot());
559 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
560 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000561 }
562
563 // Finally, inform the code generator which regs we return values in.
564 switch (getValueType(F.getReturnType())) {
565 default: assert(0 && "Unknown type!");
566 case MVT::isVoid: break;
567 case MVT::i1:
568 case MVT::i8:
569 case MVT::i16:
570 case MVT::i32:
571 MF.addLiveOut(PPC::R3);
572 break;
573 case MVT::i64:
574 MF.addLiveOut(PPC::R3);
575 MF.addLiveOut(PPC::R4);
576 break;
577 case MVT::f32:
578 case MVT::f64:
579 MF.addLiveOut(PPC::F1);
580 break;
581 }
582
583 return ArgValues;
584}
585
586std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000587PPCTargetLowering::LowerCallTo(SDOperand Chain,
588 const Type *RetTy, bool isVarArg,
589 unsigned CallingConv, bool isTailCall,
590 SDOperand Callee, ArgListTy &Args,
591 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000592 // args_to_use will accumulate outgoing args for the ISD::CALL case in
593 // SelectExpr to use to put the arguments in the appropriate registers.
594 std::vector<SDOperand> args_to_use;
595
596 // Count how many bytes are to be pushed on the stack, including the linkage
597 // area, and parameter passing area.
598 unsigned NumBytes = 24;
599
600 if (Args.empty()) {
601 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
602 DAG.getConstant(NumBytes, getPointerTy()));
603 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000604 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000605 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000606 default: assert(0 && "Unknown value type!");
607 case MVT::i1:
608 case MVT::i8:
609 case MVT::i16:
610 case MVT::i32:
611 case MVT::f32:
612 NumBytes += 4;
613 break;
614 case MVT::i64:
615 case MVT::f64:
616 NumBytes += 8;
617 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000618 }
Chris Lattner915fb302005-08-30 00:19:00 +0000619 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000620
Chris Lattner915fb302005-08-30 00:19:00 +0000621 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
622 // plus 32 bytes of argument space in case any called code gets funky on us.
623 // (Required by ABI to support var arg)
624 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000625
626 // Adjust the stack pointer for the new arguments...
627 // These operations are automatically eliminated by the prolog/epilog pass
628 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
629 DAG.getConstant(NumBytes, getPointerTy()));
630
631 // Set up a copy of the stack pointer for use loading and storing any
632 // arguments that may not fit in the registers available for argument
633 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000634 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
635 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000636
637 // Figure out which arguments are going to go in registers, and which in
638 // memory. Also, if this is a vararg function, floating point operations
639 // must be stored to our stack, and loaded into integer regs as well, if
640 // any integer regs are available for argument passing.
641 unsigned ArgOffset = 24;
642 unsigned GPR_remaining = 8;
643 unsigned FPR_remaining = 13;
644
645 std::vector<SDOperand> MemOps;
646 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
647 // PtrOff will be used to store the current argument to the stack if a
648 // register cannot be found for it.
649 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
650 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
651 MVT::ValueType ArgVT = getValueType(Args[i].second);
652
653 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000654 default: assert(0 && "Unexpected ValueType for argument!");
655 case MVT::i1:
656 case MVT::i8:
657 case MVT::i16:
658 // Promote the integer to 32 bits. If the input type is signed use a
659 // sign extend, otherwise use a zero extend.
660 if (Args[i].second->isSigned())
661 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
662 else
663 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
664 // FALL THROUGH
665 case MVT::i32:
666 if (GPR_remaining > 0) {
667 args_to_use.push_back(Args[i].first);
668 --GPR_remaining;
669 } else {
670 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
671 Args[i].first, PtrOff,
672 DAG.getSrcValue(NULL)));
673 }
674 ArgOffset += 4;
675 break;
676 case MVT::i64:
677 // If we have one free GPR left, we can place the upper half of the i64
678 // in it, and store the other half to the stack. If we have two or more
679 // free GPRs, then we can pass both halves of the i64 in registers.
680 if (GPR_remaining > 0) {
681 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
682 Args[i].first, DAG.getConstant(1, MVT::i32));
683 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
684 Args[i].first, DAG.getConstant(0, MVT::i32));
685 args_to_use.push_back(Hi);
686 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000687 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000688 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000689 --GPR_remaining;
690 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000691 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
692 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000693 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000694 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000695 }
Chris Lattner915fb302005-08-30 00:19:00 +0000696 } else {
697 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
698 Args[i].first, PtrOff,
699 DAG.getSrcValue(NULL)));
700 }
701 ArgOffset += 8;
702 break;
703 case MVT::f32:
704 case MVT::f64:
705 if (FPR_remaining > 0) {
706 args_to_use.push_back(Args[i].first);
707 --FPR_remaining;
708 if (isVarArg) {
709 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
710 Args[i].first, PtrOff,
711 DAG.getSrcValue(NULL));
712 MemOps.push_back(Store);
713 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000714 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000715 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
716 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000717 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000718 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000719 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000720 }
721 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000722 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
723 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000724 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
725 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000726 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000727 args_to_use.push_back(Load);
728 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000729 }
730 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000731 // If we have any FPRs remaining, we may also have GPRs remaining.
732 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
733 // GPRs.
734 if (GPR_remaining > 0) {
735 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
736 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000737 }
Chris Lattner915fb302005-08-30 00:19:00 +0000738 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
739 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
740 --GPR_remaining;
741 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000742 }
Chris Lattner915fb302005-08-30 00:19:00 +0000743 } else {
744 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
745 Args[i].first, PtrOff,
746 DAG.getSrcValue(NULL)));
747 }
748 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
749 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000750 }
751 }
752 if (!MemOps.empty())
753 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
754 }
755
756 std::vector<MVT::ValueType> RetVals;
757 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000758 MVT::ValueType ActualRetTyVT = RetTyVT;
759 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
760 ActualRetTyVT = MVT::i32; // Promote result to i32.
761
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000762 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000763 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000764 RetVals.push_back(MVT::Other);
765
Chris Lattner2823b3e2005-11-17 05:56:14 +0000766 // If the callee is a GlobalAddress node (quite common, every direct call is)
767 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
768 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
769 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
770
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000771 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
772 Chain, Callee, args_to_use), 0);
773 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
774 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
775 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000776 SDOperand RetVal = TheCall;
777
778 // If the result is a small value, add a note so that we keep track of the
779 // information about whether it is sign or zero extended.
780 if (RetTyVT != ActualRetTyVT) {
781 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
782 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
783 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
784 }
785
786 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000787}
788
Nate Begeman4a959452005-10-18 23:23:37 +0000789SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
790 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000791 SDOperand Copy;
792 switch (Op.getValueType()) {
793 default: assert(0 && "Unknown type to return!");
794 case MVT::i32:
795 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
796 break;
797 case MVT::f32:
798 case MVT::f64:
799 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
800 break;
801 case MVT::i64:
802 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
803 DAG.getConstant(1, MVT::i32));
804 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
805 DAG.getConstant(0, MVT::i32));
806 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
807 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
808 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000809 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000810 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000811}
812
Nate Begeman21e463b2005-10-16 05:39:50 +0000813SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
814 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000815 // vastart just stores the address of the VarArgsFrameIndex slot into the
816 // memory location argument.
817 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
818 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
819 DAG.getSrcValue(VAListV));
820}
821
822std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000823PPCTargetLowering::LowerVAArg(SDOperand Chain,
824 SDOperand VAListP, Value *VAListV,
825 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000826 MVT::ValueType ArgVT = getValueType(ArgTy);
827
828 SDOperand VAList =
829 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
830 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
831 unsigned Amt;
832 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
833 Amt = 4;
834 else {
835 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
836 "Other types should have been promoted for varargs!");
837 Amt = 8;
838 }
839 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
840 DAG.getConstant(Amt, VAList.getValueType()));
841 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
842 VAList, VAListP, DAG.getSrcValue(VAListV));
843 return std::make_pair(Result, Chain);
844}
845
846
Nate Begeman21e463b2005-10-16 05:39:50 +0000847std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000848LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
849 SelectionDAG &DAG) {
850 assert(0 && "LowerFrameReturnAddress unimplemented");
851 abort();
852}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000853
854MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000855PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
856 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000857 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000858 MI->getOpcode() == PPC::SELECT_CC_F4 ||
859 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000860 "Unexpected instr type to insert");
861
862 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
863 // control-flow pattern. The incoming instruction knows the destination vreg
864 // to set, the condition code register to branch on, the true/false values to
865 // select between, and a branch opcode to use.
866 const BasicBlock *LLVM_BB = BB->getBasicBlock();
867 ilist<MachineBasicBlock>::iterator It = BB;
868 ++It;
869
870 // thisMBB:
871 // ...
872 // TrueVal = ...
873 // cmpTY ccX, r1, r2
874 // bCC copy1MBB
875 // fallthrough --> copy0MBB
876 MachineBasicBlock *thisMBB = BB;
877 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
878 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
879 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
880 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
881 MachineFunction *F = BB->getParent();
882 F->getBasicBlockList().insert(It, copy0MBB);
883 F->getBasicBlockList().insert(It, sinkMBB);
884 // Update machine-CFG edges
885 BB->addSuccessor(copy0MBB);
886 BB->addSuccessor(sinkMBB);
887
888 // copy0MBB:
889 // %FalseValue = ...
890 // # fallthrough to sinkMBB
891 BB = copy0MBB;
892
893 // Update machine-CFG edges
894 BB->addSuccessor(sinkMBB);
895
896 // sinkMBB:
897 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
898 // ...
899 BB = sinkMBB;
900 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
901 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
902 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
903
904 delete MI; // The pseudo instruction is gone now.
905 return BB;
906}
907