blob: 6f798c38ed8225b0093ec9bc9c287bbe16f62bd2 [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: {
Chris Lattner860e8862005-11-17 07:30:41 +0000336 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
337 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
338 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000339
340 if (PPCGenerateStaticCode) {
341 // Generate non-pic code that has direct accesses to globals. To do this
342 // the address of the global is just (hi(&g)+lo(&g)).
343 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
344 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
345 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
346 }
Chris Lattner860e8862005-11-17 07:30:41 +0000347
Chris Lattner1d05cb42005-11-17 18:55:48 +0000348 // Only lower GlobalAddress on Darwin.
349 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000350 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
351 if (PICEnabled) {
352 // With PIC, the first instruction is actually "GR+hi(&G)".
353 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000354 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000355 }
356
357 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
358 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
359
360 if (!GV->hasWeakLinkage() && !GV->isExternal())
361 return Lo;
362
363 // If the global is weak or external, we have to go through the lazy
364 // resolution stub.
365 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
366 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000367 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000368 return SDOperand();
369}
370
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000371std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000372PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000373 //
374 // add beautiful description of PPC stack frame format, or at least some docs
375 //
376 MachineFunction &MF = DAG.getMachineFunction();
377 MachineFrameInfo *MFI = MF.getFrameInfo();
378 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000379 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000380 std::vector<SDOperand> ArgValues;
381
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000382 unsigned ArgOffset = 24;
383 unsigned GPR_remaining = 8;
384 unsigned FPR_remaining = 13;
385 unsigned GPR_idx = 0, FPR_idx = 0;
386 static const unsigned GPR[] = {
387 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
388 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
389 };
390 static const unsigned FPR[] = {
391 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
392 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
393 };
394
395 // Add DAG nodes to load the arguments... On entry to a function on PPC,
396 // the arguments start at offset 24, although they are likely to be passed
397 // in registers.
398 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
399 SDOperand newroot, argt;
400 unsigned ObjSize;
401 bool needsLoad = false;
402 bool ArgLive = !I->use_empty();
403 MVT::ValueType ObjectVT = getValueType(I->getType());
404
405 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000406 default: assert(0 && "Unhandled argument type!");
407 case MVT::i1:
408 case MVT::i8:
409 case MVT::i16:
410 case MVT::i32:
411 ObjSize = 4;
412 if (!ArgLive) break;
413 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000414 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000415 MF.addLiveIn(GPR[GPR_idx], VReg);
416 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000417 if (ObjectVT != MVT::i32) {
418 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
419 : ISD::AssertZext;
420 argt = DAG.getNode(AssertOp, MVT::i32, argt,
421 DAG.getValueType(ObjectVT));
422 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
423 }
Chris Lattner915fb302005-08-30 00:19:00 +0000424 } else {
425 needsLoad = true;
426 }
427 break;
428 case MVT::i64: ObjSize = 8;
429 if (!ArgLive) break;
430 if (GPR_remaining > 0) {
431 SDOperand argHi, argLo;
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], VReg);
434 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000435 // If we have two or more remaining argument registers, then both halves
436 // of the i64 can be sourced from there. Otherwise, the lower half will
437 // have to come off the stack. This can happen when an i64 is preceded
438 // by 28 bytes of arguments.
439 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000440 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000441 MF.addLiveIn(GPR[GPR_idx+1], VReg);
442 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000443 } else {
444 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
445 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
446 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
447 DAG.getSrcValue(NULL));
448 }
449 // Build the outgoing arg thingy
450 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
451 newroot = argLo;
452 } else {
453 needsLoad = true;
454 }
455 break;
456 case MVT::f32:
457 case MVT::f64:
458 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
459 if (!ArgLive) break;
460 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000461 unsigned VReg;
462 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000463 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000464 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000465 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000466 MF.addLiveIn(FPR[FPR_idx], VReg);
467 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000468 --FPR_remaining;
469 ++FPR_idx;
470 } else {
471 needsLoad = true;
472 }
473 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000474 }
475
476 // We need to load the argument to a virtual register if we determined above
477 // that we ran out of physical registers of the appropriate type
478 if (needsLoad) {
479 unsigned SubregOffset = 0;
480 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
481 if (ObjectVT == MVT::i16) SubregOffset = 2;
482 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
483 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
484 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
485 DAG.getConstant(SubregOffset, MVT::i32));
486 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
487 DAG.getSrcValue(NULL));
488 }
489
490 // Every 4 bytes of argument space consumes one of the GPRs available for
491 // argument passing.
492 if (GPR_remaining > 0) {
493 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
494 GPR_remaining -= delta;
495 GPR_idx += delta;
496 }
497 ArgOffset += ObjSize;
498 if (newroot.Val)
499 DAG.setRoot(newroot.getValue(1));
500
501 ArgValues.push_back(argt);
502 }
503
504 // If the function takes variable number of arguments, make a frame index for
505 // the start of the first vararg value... for expansion of llvm.va_start.
506 if (F.isVarArg()) {
507 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
508 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
509 // If this function is vararg, store any remaining integer argument regs
510 // to their spots on the stack so that they may be loaded by deferencing the
511 // result of va_next.
512 std::vector<SDOperand> MemOps;
513 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000514 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000515 MF.addLiveIn(GPR[GPR_idx], VReg);
516 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000517 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
518 Val, FIN, DAG.getSrcValue(NULL));
519 MemOps.push_back(Store);
520 // Increment the address by four for the next argument to store
521 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
522 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
523 }
524 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
525 }
526
527 // Finally, inform the code generator which regs we return values in.
528 switch (getValueType(F.getReturnType())) {
529 default: assert(0 && "Unknown type!");
530 case MVT::isVoid: break;
531 case MVT::i1:
532 case MVT::i8:
533 case MVT::i16:
534 case MVT::i32:
535 MF.addLiveOut(PPC::R3);
536 break;
537 case MVT::i64:
538 MF.addLiveOut(PPC::R3);
539 MF.addLiveOut(PPC::R4);
540 break;
541 case MVT::f32:
542 case MVT::f64:
543 MF.addLiveOut(PPC::F1);
544 break;
545 }
546
547 return ArgValues;
548}
549
550std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000551PPCTargetLowering::LowerCallTo(SDOperand Chain,
552 const Type *RetTy, bool isVarArg,
553 unsigned CallingConv, bool isTailCall,
554 SDOperand Callee, ArgListTy &Args,
555 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000556 // args_to_use will accumulate outgoing args for the ISD::CALL case in
557 // SelectExpr to use to put the arguments in the appropriate registers.
558 std::vector<SDOperand> args_to_use;
559
560 // Count how many bytes are to be pushed on the stack, including the linkage
561 // area, and parameter passing area.
562 unsigned NumBytes = 24;
563
564 if (Args.empty()) {
565 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
566 DAG.getConstant(NumBytes, getPointerTy()));
567 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000568 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000569 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000570 default: assert(0 && "Unknown value type!");
571 case MVT::i1:
572 case MVT::i8:
573 case MVT::i16:
574 case MVT::i32:
575 case MVT::f32:
576 NumBytes += 4;
577 break;
578 case MVT::i64:
579 case MVT::f64:
580 NumBytes += 8;
581 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000582 }
Chris Lattner915fb302005-08-30 00:19:00 +0000583 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000584
Chris Lattner915fb302005-08-30 00:19:00 +0000585 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
586 // plus 32 bytes of argument space in case any called code gets funky on us.
587 // (Required by ABI to support var arg)
588 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000589
590 // Adjust the stack pointer for the new arguments...
591 // These operations are automatically eliminated by the prolog/epilog pass
592 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
593 DAG.getConstant(NumBytes, getPointerTy()));
594
595 // Set up a copy of the stack pointer for use loading and storing any
596 // arguments that may not fit in the registers available for argument
597 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000598 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
599 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000600
601 // Figure out which arguments are going to go in registers, and which in
602 // memory. Also, if this is a vararg function, floating point operations
603 // must be stored to our stack, and loaded into integer regs as well, if
604 // any integer regs are available for argument passing.
605 unsigned ArgOffset = 24;
606 unsigned GPR_remaining = 8;
607 unsigned FPR_remaining = 13;
608
609 std::vector<SDOperand> MemOps;
610 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
611 // PtrOff will be used to store the current argument to the stack if a
612 // register cannot be found for it.
613 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
614 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
615 MVT::ValueType ArgVT = getValueType(Args[i].second);
616
617 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000618 default: assert(0 && "Unexpected ValueType for argument!");
619 case MVT::i1:
620 case MVT::i8:
621 case MVT::i16:
622 // Promote the integer to 32 bits. If the input type is signed use a
623 // sign extend, otherwise use a zero extend.
624 if (Args[i].second->isSigned())
625 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
626 else
627 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
628 // FALL THROUGH
629 case MVT::i32:
630 if (GPR_remaining > 0) {
631 args_to_use.push_back(Args[i].first);
632 --GPR_remaining;
633 } else {
634 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
635 Args[i].first, PtrOff,
636 DAG.getSrcValue(NULL)));
637 }
638 ArgOffset += 4;
639 break;
640 case MVT::i64:
641 // If we have one free GPR left, we can place the upper half of the i64
642 // in it, and store the other half to the stack. If we have two or more
643 // free GPRs, then we can pass both halves of the i64 in registers.
644 if (GPR_remaining > 0) {
645 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
646 Args[i].first, DAG.getConstant(1, MVT::i32));
647 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
648 Args[i].first, DAG.getConstant(0, MVT::i32));
649 args_to_use.push_back(Hi);
650 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000651 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000652 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000653 --GPR_remaining;
654 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000655 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
656 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000657 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000658 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000659 }
Chris Lattner915fb302005-08-30 00:19:00 +0000660 } else {
661 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
662 Args[i].first, PtrOff,
663 DAG.getSrcValue(NULL)));
664 }
665 ArgOffset += 8;
666 break;
667 case MVT::f32:
668 case MVT::f64:
669 if (FPR_remaining > 0) {
670 args_to_use.push_back(Args[i].first);
671 --FPR_remaining;
672 if (isVarArg) {
673 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
674 Args[i].first, PtrOff,
675 DAG.getSrcValue(NULL));
676 MemOps.push_back(Store);
677 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000678 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000679 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
680 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000681 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000682 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000683 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000684 }
685 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000686 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
687 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000688 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
689 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000690 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000691 args_to_use.push_back(Load);
692 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000693 }
694 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000695 // If we have any FPRs remaining, we may also have GPRs remaining.
696 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
697 // GPRs.
698 if (GPR_remaining > 0) {
699 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
700 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000701 }
Chris Lattner915fb302005-08-30 00:19:00 +0000702 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
703 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
704 --GPR_remaining;
705 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000706 }
Chris Lattner915fb302005-08-30 00:19:00 +0000707 } else {
708 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
709 Args[i].first, PtrOff,
710 DAG.getSrcValue(NULL)));
711 }
712 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
713 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000714 }
715 }
716 if (!MemOps.empty())
717 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
718 }
719
720 std::vector<MVT::ValueType> RetVals;
721 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000722 MVT::ValueType ActualRetTyVT = RetTyVT;
723 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
724 ActualRetTyVT = MVT::i32; // Promote result to i32.
725
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000726 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000727 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000728 RetVals.push_back(MVT::Other);
729
Chris Lattner2823b3e2005-11-17 05:56:14 +0000730 // If the callee is a GlobalAddress node (quite common, every direct call is)
731 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
732 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
733 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
734
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000735 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
736 Chain, Callee, args_to_use), 0);
737 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
738 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
739 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000740 SDOperand RetVal = TheCall;
741
742 // If the result is a small value, add a note so that we keep track of the
743 // information about whether it is sign or zero extended.
744 if (RetTyVT != ActualRetTyVT) {
745 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
746 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
747 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
748 }
749
750 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000751}
752
Nate Begeman4a959452005-10-18 23:23:37 +0000753SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
754 SelectionDAG &DAG) {
755 if (Op.getValueType() == MVT::i64) {
756 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
757 DAG.getConstant(1, MVT::i32));
758 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
759 DAG.getConstant(0, MVT::i32));
760 return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
761 } else {
762 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
763 }
764}
765
Nate Begeman21e463b2005-10-16 05:39:50 +0000766SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
767 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000768 // vastart just stores the address of the VarArgsFrameIndex slot into the
769 // memory location argument.
770 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
771 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
772 DAG.getSrcValue(VAListV));
773}
774
775std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000776PPCTargetLowering::LowerVAArg(SDOperand Chain,
777 SDOperand VAListP, Value *VAListV,
778 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000779 MVT::ValueType ArgVT = getValueType(ArgTy);
780
781 SDOperand VAList =
782 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
783 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
784 unsigned Amt;
785 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
786 Amt = 4;
787 else {
788 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
789 "Other types should have been promoted for varargs!");
790 Amt = 8;
791 }
792 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
793 DAG.getConstant(Amt, VAList.getValueType()));
794 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
795 VAList, VAListP, DAG.getSrcValue(VAListV));
796 return std::make_pair(Result, Chain);
797}
798
799
Nate Begeman21e463b2005-10-16 05:39:50 +0000800std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000801LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
802 SelectionDAG &DAG) {
803 assert(0 && "LowerFrameReturnAddress unimplemented");
804 abort();
805}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000806
807MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000808PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
809 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000810 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000811 MI->getOpcode() == PPC::SELECT_CC_F4 ||
812 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000813 "Unexpected instr type to insert");
814
815 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
816 // control-flow pattern. The incoming instruction knows the destination vreg
817 // to set, the condition code register to branch on, the true/false values to
818 // select between, and a branch opcode to use.
819 const BasicBlock *LLVM_BB = BB->getBasicBlock();
820 ilist<MachineBasicBlock>::iterator It = BB;
821 ++It;
822
823 // thisMBB:
824 // ...
825 // TrueVal = ...
826 // cmpTY ccX, r1, r2
827 // bCC copy1MBB
828 // fallthrough --> copy0MBB
829 MachineBasicBlock *thisMBB = BB;
830 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
831 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
832 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
833 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
834 MachineFunction *F = BB->getParent();
835 F->getBasicBlockList().insert(It, copy0MBB);
836 F->getBasicBlockList().insert(It, sinkMBB);
837 // Update machine-CFG edges
838 BB->addSuccessor(copy0MBB);
839 BB->addSuccessor(sinkMBB);
840
841 // copy0MBB:
842 // %FalseValue = ...
843 // # fallthrough to sinkMBB
844 BB = copy0MBB;
845
846 // Update machine-CFG edges
847 BB->addSuccessor(sinkMBB);
848
849 // sinkMBB:
850 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
851 // ...
852 BB = sinkMBB;
853 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
854 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
855 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
856
857 delete MI; // The pseudo instruction is gone now.
858 return BB;
859}
860