blob: 9fb1a21125fee1d74aab246dfca4aca8fc42333b [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 Lattnera35ef632006-01-06 01:04:03 +0000382
Chris Lattner860e8862005-11-17 07:30:41 +0000383 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
384 if (PICEnabled) {
385 // With PIC, the first instruction is actually "GR+hi(&G)".
386 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000387 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000388 }
389
390 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
391 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
392
Chris Lattnera35ef632006-01-06 01:04:03 +0000393 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000394 return Lo;
395
396 // If the global is weak or external, we have to go through the lazy
397 // resolution stub.
398 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
399 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000400 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000401 return SDOperand();
402}
403
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000404std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000405PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000406 //
407 // add beautiful description of PPC stack frame format, or at least some docs
408 //
409 MachineFunction &MF = DAG.getMachineFunction();
410 MachineFrameInfo *MFI = MF.getFrameInfo();
411 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000412 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000413 std::vector<SDOperand> ArgValues;
414
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000415 unsigned ArgOffset = 24;
416 unsigned GPR_remaining = 8;
417 unsigned FPR_remaining = 13;
418 unsigned GPR_idx = 0, FPR_idx = 0;
419 static const unsigned GPR[] = {
420 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
421 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
422 };
423 static const unsigned FPR[] = {
424 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
425 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
426 };
427
428 // Add DAG nodes to load the arguments... On entry to a function on PPC,
429 // the arguments start at offset 24, although they are likely to be passed
430 // in registers.
431 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
432 SDOperand newroot, argt;
433 unsigned ObjSize;
434 bool needsLoad = false;
435 bool ArgLive = !I->use_empty();
436 MVT::ValueType ObjectVT = getValueType(I->getType());
437
438 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000439 default: assert(0 && "Unhandled argument type!");
440 case MVT::i1:
441 case MVT::i8:
442 case MVT::i16:
443 case MVT::i32:
444 ObjSize = 4;
445 if (!ArgLive) break;
446 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000447 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000448 MF.addLiveIn(GPR[GPR_idx], VReg);
449 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000450 if (ObjectVT != MVT::i32) {
451 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
452 : ISD::AssertZext;
453 argt = DAG.getNode(AssertOp, MVT::i32, argt,
454 DAG.getValueType(ObjectVT));
455 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
456 }
Chris Lattner915fb302005-08-30 00:19:00 +0000457 } else {
458 needsLoad = true;
459 }
460 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000461 case MVT::i64:
462 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000463 if (!ArgLive) break;
464 if (GPR_remaining > 0) {
465 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000466 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000467 MF.addLiveIn(GPR[GPR_idx], VReg);
468 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000469 // If we have two or more remaining argument registers, then both halves
470 // of the i64 can be sourced from there. Otherwise, the lower half will
471 // have to come off the stack. This can happen when an i64 is preceded
472 // by 28 bytes of arguments.
473 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000474 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000475 MF.addLiveIn(GPR[GPR_idx+1], VReg);
476 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000477 } else {
478 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
479 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
480 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
481 DAG.getSrcValue(NULL));
482 }
483 // Build the outgoing arg thingy
484 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
485 newroot = argLo;
486 } else {
487 needsLoad = true;
488 }
489 break;
490 case MVT::f32:
491 case MVT::f64:
492 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
493 if (!ArgLive) break;
494 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000495 unsigned VReg;
496 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000497 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000498 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000499 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000500 MF.addLiveIn(FPR[FPR_idx], VReg);
501 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000502 --FPR_remaining;
503 ++FPR_idx;
504 } else {
505 needsLoad = true;
506 }
507 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000508 }
509
510 // We need to load the argument to a virtual register if we determined above
511 // that we ran out of physical registers of the appropriate type
512 if (needsLoad) {
513 unsigned SubregOffset = 0;
514 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
515 if (ObjectVT == MVT::i16) SubregOffset = 2;
516 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
517 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
518 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
519 DAG.getConstant(SubregOffset, MVT::i32));
520 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
521 DAG.getSrcValue(NULL));
522 }
523
524 // Every 4 bytes of argument space consumes one of the GPRs available for
525 // argument passing.
526 if (GPR_remaining > 0) {
527 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
528 GPR_remaining -= delta;
529 GPR_idx += delta;
530 }
531 ArgOffset += ObjSize;
532 if (newroot.Val)
533 DAG.setRoot(newroot.getValue(1));
534
535 ArgValues.push_back(argt);
536 }
537
538 // If the function takes variable number of arguments, make a frame index for
539 // the start of the first vararg value... for expansion of llvm.va_start.
540 if (F.isVarArg()) {
541 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
542 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
543 // If this function is vararg, store any remaining integer argument regs
544 // to their spots on the stack so that they may be loaded by deferencing the
545 // result of va_next.
546 std::vector<SDOperand> MemOps;
547 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000548 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000549 MF.addLiveIn(GPR[GPR_idx], VReg);
550 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000551 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
552 Val, FIN, DAG.getSrcValue(NULL));
553 MemOps.push_back(Store);
554 // Increment the address by four for the next argument to store
555 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
556 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
557 }
Chris Lattner80720a92005-11-30 20:40:54 +0000558 if (!MemOps.empty()) {
559 MemOps.push_back(DAG.getRoot());
560 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
561 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000562 }
563
564 // Finally, inform the code generator which regs we return values in.
565 switch (getValueType(F.getReturnType())) {
566 default: assert(0 && "Unknown type!");
567 case MVT::isVoid: break;
568 case MVT::i1:
569 case MVT::i8:
570 case MVT::i16:
571 case MVT::i32:
572 MF.addLiveOut(PPC::R3);
573 break;
574 case MVT::i64:
575 MF.addLiveOut(PPC::R3);
576 MF.addLiveOut(PPC::R4);
577 break;
578 case MVT::f32:
579 case MVT::f64:
580 MF.addLiveOut(PPC::F1);
581 break;
582 }
583
584 return ArgValues;
585}
586
587std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000588PPCTargetLowering::LowerCallTo(SDOperand Chain,
589 const Type *RetTy, bool isVarArg,
590 unsigned CallingConv, bool isTailCall,
591 SDOperand Callee, ArgListTy &Args,
592 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000593 // args_to_use will accumulate outgoing args for the ISD::CALL case in
594 // SelectExpr to use to put the arguments in the appropriate registers.
595 std::vector<SDOperand> args_to_use;
596
597 // Count how many bytes are to be pushed on the stack, including the linkage
598 // area, and parameter passing area.
599 unsigned NumBytes = 24;
600
601 if (Args.empty()) {
602 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
603 DAG.getConstant(NumBytes, getPointerTy()));
604 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000605 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000606 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000607 default: assert(0 && "Unknown value type!");
608 case MVT::i1:
609 case MVT::i8:
610 case MVT::i16:
611 case MVT::i32:
612 case MVT::f32:
613 NumBytes += 4;
614 break;
615 case MVT::i64:
616 case MVT::f64:
617 NumBytes += 8;
618 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000619 }
Chris Lattner915fb302005-08-30 00:19:00 +0000620 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000621
Chris Lattner915fb302005-08-30 00:19:00 +0000622 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
623 // plus 32 bytes of argument space in case any called code gets funky on us.
624 // (Required by ABI to support var arg)
625 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000626
627 // Adjust the stack pointer for the new arguments...
628 // These operations are automatically eliminated by the prolog/epilog pass
629 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
630 DAG.getConstant(NumBytes, getPointerTy()));
631
632 // Set up a copy of the stack pointer for use loading and storing any
633 // arguments that may not fit in the registers available for argument
634 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000635 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
636 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000637
638 // Figure out which arguments are going to go in registers, and which in
639 // memory. Also, if this is a vararg function, floating point operations
640 // must be stored to our stack, and loaded into integer regs as well, if
641 // any integer regs are available for argument passing.
642 unsigned ArgOffset = 24;
643 unsigned GPR_remaining = 8;
644 unsigned FPR_remaining = 13;
645
646 std::vector<SDOperand> MemOps;
647 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
648 // PtrOff will be used to store the current argument to the stack if a
649 // register cannot be found for it.
650 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
651 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
652 MVT::ValueType ArgVT = getValueType(Args[i].second);
653
654 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000655 default: assert(0 && "Unexpected ValueType for argument!");
656 case MVT::i1:
657 case MVT::i8:
658 case MVT::i16:
659 // Promote the integer to 32 bits. If the input type is signed use a
660 // sign extend, otherwise use a zero extend.
661 if (Args[i].second->isSigned())
662 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
663 else
664 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
665 // FALL THROUGH
666 case MVT::i32:
667 if (GPR_remaining > 0) {
668 args_to_use.push_back(Args[i].first);
669 --GPR_remaining;
670 } else {
671 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
672 Args[i].first, PtrOff,
673 DAG.getSrcValue(NULL)));
674 }
675 ArgOffset += 4;
676 break;
677 case MVT::i64:
678 // If we have one free GPR left, we can place the upper half of the i64
679 // in it, and store the other half to the stack. If we have two or more
680 // free GPRs, then we can pass both halves of the i64 in registers.
681 if (GPR_remaining > 0) {
682 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
683 Args[i].first, DAG.getConstant(1, MVT::i32));
684 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
685 Args[i].first, DAG.getConstant(0, MVT::i32));
686 args_to_use.push_back(Hi);
687 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000688 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000689 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000690 --GPR_remaining;
691 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000692 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
693 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000694 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000695 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000696 }
Chris Lattner915fb302005-08-30 00:19:00 +0000697 } else {
698 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
699 Args[i].first, PtrOff,
700 DAG.getSrcValue(NULL)));
701 }
702 ArgOffset += 8;
703 break;
704 case MVT::f32:
705 case MVT::f64:
706 if (FPR_remaining > 0) {
707 args_to_use.push_back(Args[i].first);
708 --FPR_remaining;
709 if (isVarArg) {
710 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
711 Args[i].first, PtrOff,
712 DAG.getSrcValue(NULL));
713 MemOps.push_back(Store);
714 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000715 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000716 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
717 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000718 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000719 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000720 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000721 }
722 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000723 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
724 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000725 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
726 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000727 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000728 args_to_use.push_back(Load);
729 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000730 }
731 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000732 // If we have any FPRs remaining, we may also have GPRs remaining.
733 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
734 // GPRs.
735 if (GPR_remaining > 0) {
736 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
737 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000738 }
Chris Lattner915fb302005-08-30 00:19:00 +0000739 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
740 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
741 --GPR_remaining;
742 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000743 }
Chris Lattner915fb302005-08-30 00:19:00 +0000744 } else {
745 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
746 Args[i].first, PtrOff,
747 DAG.getSrcValue(NULL)));
748 }
749 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
750 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000751 }
752 }
753 if (!MemOps.empty())
754 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
755 }
756
757 std::vector<MVT::ValueType> RetVals;
758 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000759 MVT::ValueType ActualRetTyVT = RetTyVT;
760 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
761 ActualRetTyVT = MVT::i32; // Promote result to i32.
762
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000763 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000764 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000765 RetVals.push_back(MVT::Other);
766
Chris Lattner2823b3e2005-11-17 05:56:14 +0000767 // If the callee is a GlobalAddress node (quite common, every direct call is)
768 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
769 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
770 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
771
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000772 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
773 Chain, Callee, args_to_use), 0);
774 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
775 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
776 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000777 SDOperand RetVal = TheCall;
778
779 // If the result is a small value, add a note so that we keep track of the
780 // information about whether it is sign or zero extended.
781 if (RetTyVT != ActualRetTyVT) {
782 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
783 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
784 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
785 }
786
787 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000788}
789
Nate Begeman4a959452005-10-18 23:23:37 +0000790SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
791 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000792 SDOperand Copy;
793 switch (Op.getValueType()) {
794 default: assert(0 && "Unknown type to return!");
795 case MVT::i32:
796 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
797 break;
798 case MVT::f32:
799 case MVT::f64:
800 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
801 break;
802 case MVT::i64:
803 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
804 DAG.getConstant(1, MVT::i32));
805 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
806 DAG.getConstant(0, MVT::i32));
807 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
808 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
809 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000810 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000811 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000812}
813
Nate Begeman21e463b2005-10-16 05:39:50 +0000814SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
815 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000816 // vastart just stores the address of the VarArgsFrameIndex slot into the
817 // memory location argument.
818 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
819 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
820 DAG.getSrcValue(VAListV));
821}
822
823std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000824PPCTargetLowering::LowerVAArg(SDOperand Chain,
825 SDOperand VAListP, Value *VAListV,
826 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000827 MVT::ValueType ArgVT = getValueType(ArgTy);
828
829 SDOperand VAList =
830 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
831 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
832 unsigned Amt;
833 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
834 Amt = 4;
835 else {
836 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
837 "Other types should have been promoted for varargs!");
838 Amt = 8;
839 }
840 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
841 DAG.getConstant(Amt, VAList.getValueType()));
842 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
843 VAList, VAListP, DAG.getSrcValue(VAListV));
844 return std::make_pair(Result, Chain);
845}
846
847
Nate Begeman21e463b2005-10-16 05:39:50 +0000848std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000849LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
850 SelectionDAG &DAG) {
851 assert(0 && "LowerFrameReturnAddress unimplemented");
852 abort();
853}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000854
855MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000856PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
857 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000858 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000859 MI->getOpcode() == PPC::SELECT_CC_F4 ||
860 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000861 "Unexpected instr type to insert");
862
863 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
864 // control-flow pattern. The incoming instruction knows the destination vreg
865 // to set, the condition code register to branch on, the true/false values to
866 // select between, and a branch opcode to use.
867 const BasicBlock *LLVM_BB = BB->getBasicBlock();
868 ilist<MachineBasicBlock>::iterator It = BB;
869 ++It;
870
871 // thisMBB:
872 // ...
873 // TrueVal = ...
874 // cmpTY ccX, r1, r2
875 // bCC copy1MBB
876 // fallthrough --> copy0MBB
877 MachineBasicBlock *thisMBB = BB;
878 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
879 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
880 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
881 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
882 MachineFunction *F = BB->getParent();
883 F->getBasicBlockList().insert(It, copy0MBB);
884 F->getBasicBlockList().insert(It, sinkMBB);
885 // Update machine-CFG edges
886 BB->addSuccessor(copy0MBB);
887 BB->addSuccessor(sinkMBB);
888
889 // copy0MBB:
890 // %FalseValue = ...
891 // # fallthrough to sinkMBB
892 BB = copy0MBB;
893
894 // Update machine-CFG edges
895 BB->addSuccessor(sinkMBB);
896
897 // sinkMBB:
898 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
899 // ...
900 BB = sinkMBB;
901 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
902 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
903 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
904
905 delete MI; // The pseudo instruction is gone now.
906 return BB;
907}
908