blob: 29507d00e315ad377dc300ec1e82b6115f74251b [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: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000365 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
366 GlobalValue *GV = GSDN->getGlobal();
367 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000368 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000369
370 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000371 // Generate non-pic code that has direct accesses to globals.
372 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000373 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
374 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
375 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
376 }
Chris Lattner860e8862005-11-17 07:30:41 +0000377
Chris Lattner1d05cb42005-11-17 18:55:48 +0000378 // Only lower GlobalAddress on Darwin.
379 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000380 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
381 if (PICEnabled) {
382 // With PIC, the first instruction is actually "GR+hi(&G)".
383 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000384 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000385 }
386
387 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
388 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
389
390 if (!GV->hasWeakLinkage() && !GV->isExternal())
391 return Lo;
392
393 // If the global is weak or external, we have to go through the lazy
394 // resolution stub.
395 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
396 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000397 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000398 return SDOperand();
399}
400
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000401std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000402PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000403 //
404 // add beautiful description of PPC stack frame format, or at least some docs
405 //
406 MachineFunction &MF = DAG.getMachineFunction();
407 MachineFrameInfo *MFI = MF.getFrameInfo();
408 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000409 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000410 std::vector<SDOperand> ArgValues;
411
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000412 unsigned ArgOffset = 24;
413 unsigned GPR_remaining = 8;
414 unsigned FPR_remaining = 13;
415 unsigned GPR_idx = 0, FPR_idx = 0;
416 static const unsigned GPR[] = {
417 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
418 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
419 };
420 static const unsigned FPR[] = {
421 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
422 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
423 };
424
425 // Add DAG nodes to load the arguments... On entry to a function on PPC,
426 // the arguments start at offset 24, although they are likely to be passed
427 // in registers.
428 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
429 SDOperand newroot, argt;
430 unsigned ObjSize;
431 bool needsLoad = false;
432 bool ArgLive = !I->use_empty();
433 MVT::ValueType ObjectVT = getValueType(I->getType());
434
435 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000436 default: assert(0 && "Unhandled argument type!");
437 case MVT::i1:
438 case MVT::i8:
439 case MVT::i16:
440 case MVT::i32:
441 ObjSize = 4;
442 if (!ArgLive) break;
443 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000444 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000445 MF.addLiveIn(GPR[GPR_idx], VReg);
446 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000447 if (ObjectVT != MVT::i32) {
448 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
449 : ISD::AssertZext;
450 argt = DAG.getNode(AssertOp, MVT::i32, argt,
451 DAG.getValueType(ObjectVT));
452 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
453 }
Chris Lattner915fb302005-08-30 00:19:00 +0000454 } else {
455 needsLoad = true;
456 }
457 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000458 case MVT::i64:
459 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000460 if (!ArgLive) break;
461 if (GPR_remaining > 0) {
462 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000463 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000464 MF.addLiveIn(GPR[GPR_idx], VReg);
465 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000466 // If we have two or more remaining argument registers, then both halves
467 // of the i64 can be sourced from there. Otherwise, the lower half will
468 // have to come off the stack. This can happen when an i64 is preceded
469 // by 28 bytes of arguments.
470 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000471 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000472 MF.addLiveIn(GPR[GPR_idx+1], VReg);
473 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000474 } else {
475 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
476 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
477 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
478 DAG.getSrcValue(NULL));
479 }
480 // Build the outgoing arg thingy
481 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
482 newroot = argLo;
483 } else {
484 needsLoad = true;
485 }
486 break;
487 case MVT::f32:
488 case MVT::f64:
489 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
490 if (!ArgLive) break;
491 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000492 unsigned VReg;
493 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000494 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000495 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000496 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000497 MF.addLiveIn(FPR[FPR_idx], VReg);
498 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000499 --FPR_remaining;
500 ++FPR_idx;
501 } else {
502 needsLoad = true;
503 }
504 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000505 }
506
507 // We need to load the argument to a virtual register if we determined above
508 // that we ran out of physical registers of the appropriate type
509 if (needsLoad) {
510 unsigned SubregOffset = 0;
511 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
512 if (ObjectVT == MVT::i16) SubregOffset = 2;
513 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
514 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
515 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
516 DAG.getConstant(SubregOffset, MVT::i32));
517 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
518 DAG.getSrcValue(NULL));
519 }
520
521 // Every 4 bytes of argument space consumes one of the GPRs available for
522 // argument passing.
523 if (GPR_remaining > 0) {
524 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
525 GPR_remaining -= delta;
526 GPR_idx += delta;
527 }
528 ArgOffset += ObjSize;
529 if (newroot.Val)
530 DAG.setRoot(newroot.getValue(1));
531
532 ArgValues.push_back(argt);
533 }
534
535 // If the function takes variable number of arguments, make a frame index for
536 // the start of the first vararg value... for expansion of llvm.va_start.
537 if (F.isVarArg()) {
538 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
539 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
540 // If this function is vararg, store any remaining integer argument regs
541 // to their spots on the stack so that they may be loaded by deferencing the
542 // result of va_next.
543 std::vector<SDOperand> MemOps;
544 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000545 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000546 MF.addLiveIn(GPR[GPR_idx], VReg);
547 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000548 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
549 Val, FIN, DAG.getSrcValue(NULL));
550 MemOps.push_back(Store);
551 // Increment the address by four for the next argument to store
552 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
553 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
554 }
Chris Lattner80720a92005-11-30 20:40:54 +0000555 if (!MemOps.empty()) {
556 MemOps.push_back(DAG.getRoot());
557 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
558 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000559 }
560
561 // Finally, inform the code generator which regs we return values in.
562 switch (getValueType(F.getReturnType())) {
563 default: assert(0 && "Unknown type!");
564 case MVT::isVoid: break;
565 case MVT::i1:
566 case MVT::i8:
567 case MVT::i16:
568 case MVT::i32:
569 MF.addLiveOut(PPC::R3);
570 break;
571 case MVT::i64:
572 MF.addLiveOut(PPC::R3);
573 MF.addLiveOut(PPC::R4);
574 break;
575 case MVT::f32:
576 case MVT::f64:
577 MF.addLiveOut(PPC::F1);
578 break;
579 }
580
581 return ArgValues;
582}
583
584std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000585PPCTargetLowering::LowerCallTo(SDOperand Chain,
586 const Type *RetTy, bool isVarArg,
587 unsigned CallingConv, bool isTailCall,
588 SDOperand Callee, ArgListTy &Args,
589 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000590 // args_to_use will accumulate outgoing args for the ISD::CALL case in
591 // SelectExpr to use to put the arguments in the appropriate registers.
592 std::vector<SDOperand> args_to_use;
593
594 // Count how many bytes are to be pushed on the stack, including the linkage
595 // area, and parameter passing area.
596 unsigned NumBytes = 24;
597
598 if (Args.empty()) {
599 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
600 DAG.getConstant(NumBytes, getPointerTy()));
601 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000602 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000603 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000604 default: assert(0 && "Unknown value type!");
605 case MVT::i1:
606 case MVT::i8:
607 case MVT::i16:
608 case MVT::i32:
609 case MVT::f32:
610 NumBytes += 4;
611 break;
612 case MVT::i64:
613 case MVT::f64:
614 NumBytes += 8;
615 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000616 }
Chris Lattner915fb302005-08-30 00:19:00 +0000617 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000618
Chris Lattner915fb302005-08-30 00:19:00 +0000619 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
620 // plus 32 bytes of argument space in case any called code gets funky on us.
621 // (Required by ABI to support var arg)
622 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000623
624 // Adjust the stack pointer for the new arguments...
625 // These operations are automatically eliminated by the prolog/epilog pass
626 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
627 DAG.getConstant(NumBytes, getPointerTy()));
628
629 // Set up a copy of the stack pointer for use loading and storing any
630 // arguments that may not fit in the registers available for argument
631 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000632 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
633 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000634
635 // Figure out which arguments are going to go in registers, and which in
636 // memory. Also, if this is a vararg function, floating point operations
637 // must be stored to our stack, and loaded into integer regs as well, if
638 // any integer regs are available for argument passing.
639 unsigned ArgOffset = 24;
640 unsigned GPR_remaining = 8;
641 unsigned FPR_remaining = 13;
642
643 std::vector<SDOperand> MemOps;
644 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
645 // PtrOff will be used to store the current argument to the stack if a
646 // register cannot be found for it.
647 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
648 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
649 MVT::ValueType ArgVT = getValueType(Args[i].second);
650
651 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000652 default: assert(0 && "Unexpected ValueType for argument!");
653 case MVT::i1:
654 case MVT::i8:
655 case MVT::i16:
656 // Promote the integer to 32 bits. If the input type is signed use a
657 // sign extend, otherwise use a zero extend.
658 if (Args[i].second->isSigned())
659 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
660 else
661 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
662 // FALL THROUGH
663 case MVT::i32:
664 if (GPR_remaining > 0) {
665 args_to_use.push_back(Args[i].first);
666 --GPR_remaining;
667 } else {
668 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
669 Args[i].first, PtrOff,
670 DAG.getSrcValue(NULL)));
671 }
672 ArgOffset += 4;
673 break;
674 case MVT::i64:
675 // If we have one free GPR left, we can place the upper half of the i64
676 // in it, and store the other half to the stack. If we have two or more
677 // free GPRs, then we can pass both halves of the i64 in registers.
678 if (GPR_remaining > 0) {
679 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
680 Args[i].first, DAG.getConstant(1, MVT::i32));
681 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
682 Args[i].first, DAG.getConstant(0, MVT::i32));
683 args_to_use.push_back(Hi);
684 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000685 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000686 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000687 --GPR_remaining;
688 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000689 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
690 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000691 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000692 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000693 }
Chris Lattner915fb302005-08-30 00:19:00 +0000694 } else {
695 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
696 Args[i].first, PtrOff,
697 DAG.getSrcValue(NULL)));
698 }
699 ArgOffset += 8;
700 break;
701 case MVT::f32:
702 case MVT::f64:
703 if (FPR_remaining > 0) {
704 args_to_use.push_back(Args[i].first);
705 --FPR_remaining;
706 if (isVarArg) {
707 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
708 Args[i].first, PtrOff,
709 DAG.getSrcValue(NULL));
710 MemOps.push_back(Store);
711 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000712 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000713 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
714 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000715 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000716 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000717 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000718 }
719 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000720 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
721 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000722 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
723 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000724 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000725 args_to_use.push_back(Load);
726 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000727 }
728 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000729 // If we have any FPRs remaining, we may also have GPRs remaining.
730 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
731 // GPRs.
732 if (GPR_remaining > 0) {
733 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
734 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000735 }
Chris Lattner915fb302005-08-30 00:19:00 +0000736 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
737 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
738 --GPR_remaining;
739 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000740 }
Chris Lattner915fb302005-08-30 00:19:00 +0000741 } else {
742 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
743 Args[i].first, PtrOff,
744 DAG.getSrcValue(NULL)));
745 }
746 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
747 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000748 }
749 }
750 if (!MemOps.empty())
751 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
752 }
753
754 std::vector<MVT::ValueType> RetVals;
755 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000756 MVT::ValueType ActualRetTyVT = RetTyVT;
757 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
758 ActualRetTyVT = MVT::i32; // Promote result to i32.
759
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000760 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000761 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000762 RetVals.push_back(MVT::Other);
763
Chris Lattner2823b3e2005-11-17 05:56:14 +0000764 // If the callee is a GlobalAddress node (quite common, every direct call is)
765 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
766 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
767 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
768
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000769 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
770 Chain, Callee, args_to_use), 0);
771 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
772 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
773 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000774 SDOperand RetVal = TheCall;
775
776 // If the result is a small value, add a note so that we keep track of the
777 // information about whether it is sign or zero extended.
778 if (RetTyVT != ActualRetTyVT) {
779 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
780 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
781 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
782 }
783
784 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000785}
786
Nate Begeman4a959452005-10-18 23:23:37 +0000787SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
788 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000789 SDOperand Copy;
790 switch (Op.getValueType()) {
791 default: assert(0 && "Unknown type to return!");
792 case MVT::i32:
793 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
794 break;
795 case MVT::f32:
796 case MVT::f64:
797 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
798 break;
799 case MVT::i64:
800 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
801 DAG.getConstant(1, MVT::i32));
802 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
803 DAG.getConstant(0, MVT::i32));
804 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
805 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
806 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000807 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000808 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000809}
810
Nate Begeman21e463b2005-10-16 05:39:50 +0000811SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
812 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000813 // vastart just stores the address of the VarArgsFrameIndex slot into the
814 // memory location argument.
815 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
816 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
817 DAG.getSrcValue(VAListV));
818}
819
820std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000821PPCTargetLowering::LowerVAArg(SDOperand Chain,
822 SDOperand VAListP, Value *VAListV,
823 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000824 MVT::ValueType ArgVT = getValueType(ArgTy);
825
826 SDOperand VAList =
827 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
828 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
829 unsigned Amt;
830 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
831 Amt = 4;
832 else {
833 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
834 "Other types should have been promoted for varargs!");
835 Amt = 8;
836 }
837 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
838 DAG.getConstant(Amt, VAList.getValueType()));
839 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
840 VAList, VAListP, DAG.getSrcValue(VAListV));
841 return std::make_pair(Result, Chain);
842}
843
844
Nate Begeman21e463b2005-10-16 05:39:50 +0000845std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000846LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
847 SelectionDAG &DAG) {
848 assert(0 && "LowerFrameReturnAddress unimplemented");
849 abort();
850}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000851
852MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000853PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
854 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000855 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000856 MI->getOpcode() == PPC::SELECT_CC_F4 ||
857 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000858 "Unexpected instr type to insert");
859
860 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
861 // control-flow pattern. The incoming instruction knows the destination vreg
862 // to set, the condition code register to branch on, the true/false values to
863 // select between, and a branch opcode to use.
864 const BasicBlock *LLVM_BB = BB->getBasicBlock();
865 ilist<MachineBasicBlock>::iterator It = BB;
866 ++It;
867
868 // thisMBB:
869 // ...
870 // TrueVal = ...
871 // cmpTY ccX, r1, r2
872 // bCC copy1MBB
873 // fallthrough --> copy0MBB
874 MachineBasicBlock *thisMBB = BB;
875 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
876 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
877 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
878 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
879 MachineFunction *F = BB->getParent();
880 F->getBasicBlockList().insert(It, copy0MBB);
881 F->getBasicBlockList().insert(It, sinkMBB);
882 // Update machine-CFG edges
883 BB->addSuccessor(copy0MBB);
884 BB->addSuccessor(sinkMBB);
885
886 // copy0MBB:
887 // %FalseValue = ...
888 // # fallthrough to sinkMBB
889 BB = copy0MBB;
890
891 // Update machine-CFG edges
892 BB->addSuccessor(sinkMBB);
893
894 // sinkMBB:
895 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
896 // ...
897 BB = sinkMBB;
898 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
899 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
900 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
901
902 delete MI; // The pseudo instruction is gone now.
903 return BB;
904}
905