blob: 216372987f36e95d3f7211c4740910e3ed1b7a5c [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 Lattnere6ec9f22005-09-10 00:21:06 +000091 // PowerPC does not have truncstore for i1.
92 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
93
Chris Lattner860e8862005-11-17 07:30:41 +000094 // We want to legalize GlobalAddress into the appropriate instructions to
95 // materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +000096 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Chris Lattner860e8862005-11-17 07:30:41 +000097
Nate Begemanc09eeec2005-09-06 22:03:27 +000098 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +000099 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000100 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
101 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000102 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
103 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
104 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000105 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000106 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000107 }
108
109 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
110 // 64 bit PowerPC implementations can support i64 types directly
111 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000112 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
113 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000114 } else {
115 // 32 bit PowerPC wants to expand i64 shifts itself.
116 setOperationAction(ISD::SHL, MVT::i64, Custom);
117 setOperationAction(ISD::SRL, MVT::i64, Custom);
118 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000119 }
120
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000121 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000122
123 computeRegisterProperties();
124}
125
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000126/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
127static bool isFloatingPointZero(SDOperand Op) {
128 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
129 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
130 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
131 // Maybe this has already been legalized into the constant pool?
132 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
133 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
134 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
135 }
136 return false;
137}
138
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000139/// LowerOperation - Provide custom lowering hooks for some operations.
140///
Nate Begeman21e463b2005-10-16 05:39:50 +0000141SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000142 switch (Op.getOpcode()) {
143 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000144 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000145 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000146 SDOperand Src = Op.getOperand(0);
147 if (Src.getValueType() == MVT::f32)
148 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
149
Nate Begemanc09eeec2005-09-06 22:03:27 +0000150 switch (Op.getValueType()) {
151 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
152 case MVT::i32:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000153 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000154 break;
155 case MVT::i64:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000156 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000157 break;
158 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000159
160 int FrameIdx =
161 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
162 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
163 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
164 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000165 if (Op.getOpcode() == PPCISD::FCTIDZ) {
166 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
167 } else {
168 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
169 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
170 }
171 return Op;
172 }
173 case ISD::SINT_TO_FP: {
174 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
175 "Unhandled SINT_TO_FP type in custom expander!");
176 int FrameIdx =
177 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
178 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
179 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
180 Op.getOperand(0), FI, DAG.getSrcValue(0));
181 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
182 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
183 if (MVT::f32 == Op.getValueType())
184 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
185 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000186 }
187 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000188 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000189 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
190 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
191 break;
192
193 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
194
195 // Cannot handle SETEQ/SETNE.
196 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
197
198 MVT::ValueType ResVT = Op.getValueType();
199 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
200 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
201 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000202
Chris Lattnerf7605322005-08-31 21:09:52 +0000203 // If the RHS of the comparison is a 0.0, we don't need to do the
204 // subtraction at all.
205 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000206 switch (CC) {
207 default: assert(0 && "Invalid FSEL condition"); abort();
208 case ISD::SETULT:
209 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000210 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000211 case ISD::SETUGE:
212 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000213 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
214 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000215 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000216 case ISD::SETUGT:
217 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000218 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000219 case ISD::SETULE:
220 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000221 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
222 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000223 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000224 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000225 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000226
Chris Lattnereb255f22005-10-25 20:54:57 +0000227 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000228 switch (CC) {
229 default: assert(0 && "Invalid FSEL condition"); abort();
230 case ISD::SETULT:
231 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000232 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
233 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
234 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
235 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000236 case ISD::SETUGE:
237 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000238 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
239 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
240 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
241 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000242 case ISD::SETUGT:
243 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000244 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
245 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
246 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
247 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000248 case ISD::SETULE:
249 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000250 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
251 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
252 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
253 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000254 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000255 break;
256 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000257 case ISD::SHL: {
258 assert(Op.getValueType() == MVT::i64 &&
259 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
260 // The generic code does a fine job expanding shift by a constant.
261 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
262
263 // Otherwise, expand into a bunch of logical ops. Note that these ops
264 // depend on the PPC behavior for oversized shift amounts.
265 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
266 DAG.getConstant(0, MVT::i32));
267 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
268 DAG.getConstant(1, MVT::i32));
269 SDOperand Amt = Op.getOperand(1);
270
271 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
272 DAG.getConstant(32, MVT::i32), Amt);
273 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
274 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
275 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
276 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
277 DAG.getConstant(-32U, MVT::i32));
278 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
279 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
280 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
281 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
282 }
283 case ISD::SRL: {
284 assert(Op.getValueType() == MVT::i64 &&
285 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
286 // The generic code does a fine job expanding shift by a constant.
287 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
288
289 // Otherwise, expand into a bunch of logical ops. Note that these ops
290 // depend on the PPC behavior for oversized shift amounts.
291 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
292 DAG.getConstant(0, MVT::i32));
293 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
294 DAG.getConstant(1, MVT::i32));
295 SDOperand Amt = Op.getOperand(1);
296
297 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
298 DAG.getConstant(32, MVT::i32), Amt);
299 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
300 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
301 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
302 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
303 DAG.getConstant(-32U, MVT::i32));
304 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
305 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
306 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
307 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
308 }
309 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000310 assert(Op.getValueType() == MVT::i64 &&
311 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
312 // The generic code does a fine job expanding shift by a constant.
313 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
314
315 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
316 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
317 DAG.getConstant(0, MVT::i32));
318 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
319 DAG.getConstant(1, MVT::i32));
320 SDOperand Amt = Op.getOperand(1);
321
322 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
323 DAG.getConstant(32, MVT::i32), Amt);
324 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
325 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
326 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
327 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
328 DAG.getConstant(-32U, MVT::i32));
329 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
330 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
331 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
332 Tmp4, Tmp6, ISD::SETLE);
333 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000334 }
Chris Lattner860e8862005-11-17 07:30:41 +0000335 case ISD::GlobalAddress: {
336 // Only lower GlobalAddress on Darwin.
337 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
338 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
339 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
340 SDOperand Zero = DAG.getConstant(0, MVT::i32);
341
342 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
343 if (PICEnabled) {
344 // With PIC, the first instruction is actually "GR+hi(&G)".
345 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000346 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000347 }
348
349 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
350 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
351
352 if (!GV->hasWeakLinkage() && !GV->isExternal())
353 return Lo;
354
355 // If the global is weak or external, we have to go through the lazy
356 // resolution stub.
357 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
358 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000359 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000360 return SDOperand();
361}
362
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000363std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000364PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000365 //
366 // add beautiful description of PPC stack frame format, or at least some docs
367 //
368 MachineFunction &MF = DAG.getMachineFunction();
369 MachineFrameInfo *MFI = MF.getFrameInfo();
370 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000371 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000372 std::vector<SDOperand> ArgValues;
373
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000374 unsigned ArgOffset = 24;
375 unsigned GPR_remaining = 8;
376 unsigned FPR_remaining = 13;
377 unsigned GPR_idx = 0, FPR_idx = 0;
378 static const unsigned GPR[] = {
379 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
380 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
381 };
382 static const unsigned FPR[] = {
383 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
384 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
385 };
386
387 // Add DAG nodes to load the arguments... On entry to a function on PPC,
388 // the arguments start at offset 24, although they are likely to be passed
389 // in registers.
390 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
391 SDOperand newroot, argt;
392 unsigned ObjSize;
393 bool needsLoad = false;
394 bool ArgLive = !I->use_empty();
395 MVT::ValueType ObjectVT = getValueType(I->getType());
396
397 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000398 default: assert(0 && "Unhandled argument type!");
399 case MVT::i1:
400 case MVT::i8:
401 case MVT::i16:
402 case MVT::i32:
403 ObjSize = 4;
404 if (!ArgLive) break;
405 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000406 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000407 MF.addLiveIn(GPR[GPR_idx], VReg);
408 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000409 if (ObjectVT != MVT::i32) {
410 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
411 : ISD::AssertZext;
412 argt = DAG.getNode(AssertOp, MVT::i32, argt,
413 DAG.getValueType(ObjectVT));
414 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
415 }
Chris Lattner915fb302005-08-30 00:19:00 +0000416 } else {
417 needsLoad = true;
418 }
419 break;
420 case MVT::i64: ObjSize = 8;
421 if (!ArgLive) break;
422 if (GPR_remaining > 0) {
423 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000424 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000425 MF.addLiveIn(GPR[GPR_idx], VReg);
426 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000427 // If we have two or more remaining argument registers, then both halves
428 // of the i64 can be sourced from there. Otherwise, the lower half will
429 // have to come off the stack. This can happen when an i64 is preceded
430 // by 28 bytes of arguments.
431 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000432 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000433 MF.addLiveIn(GPR[GPR_idx+1], VReg);
434 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000435 } else {
436 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
437 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
438 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
439 DAG.getSrcValue(NULL));
440 }
441 // Build the outgoing arg thingy
442 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
443 newroot = argLo;
444 } else {
445 needsLoad = true;
446 }
447 break;
448 case MVT::f32:
449 case MVT::f64:
450 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
451 if (!ArgLive) break;
452 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000453 unsigned VReg;
454 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000455 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000456 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000457 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000458 MF.addLiveIn(FPR[FPR_idx], VReg);
459 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000460 --FPR_remaining;
461 ++FPR_idx;
462 } else {
463 needsLoad = true;
464 }
465 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000466 }
467
468 // We need to load the argument to a virtual register if we determined above
469 // that we ran out of physical registers of the appropriate type
470 if (needsLoad) {
471 unsigned SubregOffset = 0;
472 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
473 if (ObjectVT == MVT::i16) SubregOffset = 2;
474 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
475 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
476 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
477 DAG.getConstant(SubregOffset, MVT::i32));
478 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
479 DAG.getSrcValue(NULL));
480 }
481
482 // Every 4 bytes of argument space consumes one of the GPRs available for
483 // argument passing.
484 if (GPR_remaining > 0) {
485 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
486 GPR_remaining -= delta;
487 GPR_idx += delta;
488 }
489 ArgOffset += ObjSize;
490 if (newroot.Val)
491 DAG.setRoot(newroot.getValue(1));
492
493 ArgValues.push_back(argt);
494 }
495
496 // If the function takes variable number of arguments, make a frame index for
497 // the start of the first vararg value... for expansion of llvm.va_start.
498 if (F.isVarArg()) {
499 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
500 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
501 // If this function is vararg, store any remaining integer argument regs
502 // to their spots on the stack so that they may be loaded by deferencing the
503 // result of va_next.
504 std::vector<SDOperand> MemOps;
505 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000506 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000507 MF.addLiveIn(GPR[GPR_idx], VReg);
508 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000509 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
510 Val, FIN, DAG.getSrcValue(NULL));
511 MemOps.push_back(Store);
512 // Increment the address by four for the next argument to store
513 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
514 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
515 }
516 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
517 }
518
519 // Finally, inform the code generator which regs we return values in.
520 switch (getValueType(F.getReturnType())) {
521 default: assert(0 && "Unknown type!");
522 case MVT::isVoid: break;
523 case MVT::i1:
524 case MVT::i8:
525 case MVT::i16:
526 case MVT::i32:
527 MF.addLiveOut(PPC::R3);
528 break;
529 case MVT::i64:
530 MF.addLiveOut(PPC::R3);
531 MF.addLiveOut(PPC::R4);
532 break;
533 case MVT::f32:
534 case MVT::f64:
535 MF.addLiveOut(PPC::F1);
536 break;
537 }
538
539 return ArgValues;
540}
541
542std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000543PPCTargetLowering::LowerCallTo(SDOperand Chain,
544 const Type *RetTy, bool isVarArg,
545 unsigned CallingConv, bool isTailCall,
546 SDOperand Callee, ArgListTy &Args,
547 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000548 // args_to_use will accumulate outgoing args for the ISD::CALL case in
549 // SelectExpr to use to put the arguments in the appropriate registers.
550 std::vector<SDOperand> args_to_use;
551
552 // Count how many bytes are to be pushed on the stack, including the linkage
553 // area, and parameter passing area.
554 unsigned NumBytes = 24;
555
556 if (Args.empty()) {
557 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
558 DAG.getConstant(NumBytes, getPointerTy()));
559 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000560 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000561 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000562 default: assert(0 && "Unknown value type!");
563 case MVT::i1:
564 case MVT::i8:
565 case MVT::i16:
566 case MVT::i32:
567 case MVT::f32:
568 NumBytes += 4;
569 break;
570 case MVT::i64:
571 case MVT::f64:
572 NumBytes += 8;
573 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000574 }
Chris Lattner915fb302005-08-30 00:19:00 +0000575 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000576
Chris Lattner915fb302005-08-30 00:19:00 +0000577 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
578 // plus 32 bytes of argument space in case any called code gets funky on us.
579 // (Required by ABI to support var arg)
580 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000581
582 // Adjust the stack pointer for the new arguments...
583 // These operations are automatically eliminated by the prolog/epilog pass
584 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
585 DAG.getConstant(NumBytes, getPointerTy()));
586
587 // Set up a copy of the stack pointer for use loading and storing any
588 // arguments that may not fit in the registers available for argument
589 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000590 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
591 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000592
593 // Figure out which arguments are going to go in registers, and which in
594 // memory. Also, if this is a vararg function, floating point operations
595 // must be stored to our stack, and loaded into integer regs as well, if
596 // any integer regs are available for argument passing.
597 unsigned ArgOffset = 24;
598 unsigned GPR_remaining = 8;
599 unsigned FPR_remaining = 13;
600
601 std::vector<SDOperand> MemOps;
602 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
603 // PtrOff will be used to store the current argument to the stack if a
604 // register cannot be found for it.
605 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
606 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
607 MVT::ValueType ArgVT = getValueType(Args[i].second);
608
609 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000610 default: assert(0 && "Unexpected ValueType for argument!");
611 case MVT::i1:
612 case MVT::i8:
613 case MVT::i16:
614 // Promote the integer to 32 bits. If the input type is signed use a
615 // sign extend, otherwise use a zero extend.
616 if (Args[i].second->isSigned())
617 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
618 else
619 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
620 // FALL THROUGH
621 case MVT::i32:
622 if (GPR_remaining > 0) {
623 args_to_use.push_back(Args[i].first);
624 --GPR_remaining;
625 } else {
626 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
627 Args[i].first, PtrOff,
628 DAG.getSrcValue(NULL)));
629 }
630 ArgOffset += 4;
631 break;
632 case MVT::i64:
633 // If we have one free GPR left, we can place the upper half of the i64
634 // in it, and store the other half to the stack. If we have two or more
635 // free GPRs, then we can pass both halves of the i64 in registers.
636 if (GPR_remaining > 0) {
637 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
638 Args[i].first, DAG.getConstant(1, MVT::i32));
639 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
640 Args[i].first, DAG.getConstant(0, MVT::i32));
641 args_to_use.push_back(Hi);
642 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000643 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000644 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000645 --GPR_remaining;
646 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000647 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
648 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000649 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000650 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000651 }
Chris Lattner915fb302005-08-30 00:19:00 +0000652 } else {
653 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
654 Args[i].first, PtrOff,
655 DAG.getSrcValue(NULL)));
656 }
657 ArgOffset += 8;
658 break;
659 case MVT::f32:
660 case MVT::f64:
661 if (FPR_remaining > 0) {
662 args_to_use.push_back(Args[i].first);
663 --FPR_remaining;
664 if (isVarArg) {
665 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
666 Args[i].first, PtrOff,
667 DAG.getSrcValue(NULL));
668 MemOps.push_back(Store);
669 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000670 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000671 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
672 DAG.getSrcValue(NULL));
673 MemOps.push_back(Load);
674 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000675 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000676 }
677 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000678 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
679 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000680 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
681 DAG.getSrcValue(NULL));
682 MemOps.push_back(Load);
683 args_to_use.push_back(Load);
684 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000685 }
686 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000687 // If we have any FPRs remaining, we may also have GPRs remaining.
688 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
689 // GPRs.
690 if (GPR_remaining > 0) {
691 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
692 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000693 }
Chris Lattner915fb302005-08-30 00:19:00 +0000694 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
695 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
696 --GPR_remaining;
697 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000698 }
Chris Lattner915fb302005-08-30 00:19:00 +0000699 } else {
700 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
701 Args[i].first, PtrOff,
702 DAG.getSrcValue(NULL)));
703 }
704 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
705 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000706 }
707 }
708 if (!MemOps.empty())
709 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
710 }
711
712 std::vector<MVT::ValueType> RetVals;
713 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000714 MVT::ValueType ActualRetTyVT = RetTyVT;
715 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
716 ActualRetTyVT = MVT::i32; // Promote result to i32.
717
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000718 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000719 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000720 RetVals.push_back(MVT::Other);
721
Chris Lattner2823b3e2005-11-17 05:56:14 +0000722 // If the callee is a GlobalAddress node (quite common, every direct call is)
723 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
724 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
725 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
726
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000727 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
728 Chain, Callee, args_to_use), 0);
729 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
730 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
731 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000732 SDOperand RetVal = TheCall;
733
734 // If the result is a small value, add a note so that we keep track of the
735 // information about whether it is sign or zero extended.
736 if (RetTyVT != ActualRetTyVT) {
737 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
738 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
739 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
740 }
741
742 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000743}
744
Nate Begeman4a959452005-10-18 23:23:37 +0000745SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
746 SelectionDAG &DAG) {
747 if (Op.getValueType() == MVT::i64) {
748 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
749 DAG.getConstant(1, MVT::i32));
750 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
751 DAG.getConstant(0, MVT::i32));
752 return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
753 } else {
754 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
755 }
756}
757
Nate Begeman21e463b2005-10-16 05:39:50 +0000758SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
759 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000760 // vastart just stores the address of the VarArgsFrameIndex slot into the
761 // memory location argument.
762 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
763 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
764 DAG.getSrcValue(VAListV));
765}
766
767std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000768PPCTargetLowering::LowerVAArg(SDOperand Chain,
769 SDOperand VAListP, Value *VAListV,
770 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000771 MVT::ValueType ArgVT = getValueType(ArgTy);
772
773 SDOperand VAList =
774 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
775 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
776 unsigned Amt;
777 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
778 Amt = 4;
779 else {
780 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
781 "Other types should have been promoted for varargs!");
782 Amt = 8;
783 }
784 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
785 DAG.getConstant(Amt, VAList.getValueType()));
786 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
787 VAList, VAListP, DAG.getSrcValue(VAListV));
788 return std::make_pair(Result, Chain);
789}
790
791
Nate Begeman21e463b2005-10-16 05:39:50 +0000792std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000793LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
794 SelectionDAG &DAG) {
795 assert(0 && "LowerFrameReturnAddress unimplemented");
796 abort();
797}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000798
799MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000800PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
801 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000802 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000803 MI->getOpcode() == PPC::SELECT_CC_F4 ||
804 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000805 "Unexpected instr type to insert");
806
807 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
808 // control-flow pattern. The incoming instruction knows the destination vreg
809 // to set, the condition code register to branch on, the true/false values to
810 // select between, and a branch opcode to use.
811 const BasicBlock *LLVM_BB = BB->getBasicBlock();
812 ilist<MachineBasicBlock>::iterator It = BB;
813 ++It;
814
815 // thisMBB:
816 // ...
817 // TrueVal = ...
818 // cmpTY ccX, r1, r2
819 // bCC copy1MBB
820 // fallthrough --> copy0MBB
821 MachineBasicBlock *thisMBB = BB;
822 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
823 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
824 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
825 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
826 MachineFunction *F = BB->getParent();
827 F->getBasicBlockList().insert(It, copy0MBB);
828 F->getBasicBlockList().insert(It, sinkMBB);
829 // Update machine-CFG edges
830 BB->addSuccessor(copy0MBB);
831 BB->addSuccessor(sinkMBB);
832
833 // copy0MBB:
834 // %FalseValue = ...
835 // # fallthrough to sinkMBB
836 BB = copy0MBB;
837
838 // Update machine-CFG edges
839 BB->addSuccessor(sinkMBB);
840
841 // sinkMBB:
842 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
843 // ...
844 BB = sinkMBB;
845 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
846 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
847 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
848
849 delete MI; // The pseudo instruction is gone now.
850 return BB;
851}
852