blob: 743807bc62a79c7d860b2cdd0ca91ef26a82fb60 [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
84 // PowerPC does not have FP_TO_UINT
85 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
86
Chris Lattnerf7605322005-08-31 21:09:52 +000087 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
88 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000089
Jim Laskeyad23c9d2005-08-17 00:40:22 +000090 // PowerPC does not have [U|S]INT_TO_FP
91 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
92 setOperationAction(ISD::UINT_TO_FP, 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);
96
Nate Begemanc09eeec2005-09-06 22:03:27 +000097 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +000098 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +000099 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
100 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000101 }
102
103 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
104 // 64 bit PowerPC implementations can support i64 types directly
105 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000106 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
107 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000108 } else {
109 // 32 bit PowerPC wants to expand i64 shifts itself.
110 setOperationAction(ISD::SHL, MVT::i64, Custom);
111 setOperationAction(ISD::SRL, MVT::i64, Custom);
112 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000113 }
114
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000115 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000116
117 computeRegisterProperties();
118}
119
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000120/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
121static bool isFloatingPointZero(SDOperand Op) {
122 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
123 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
124 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
125 // Maybe this has already been legalized into the constant pool?
126 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
127 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
128 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
129 }
130 return false;
131}
132
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000133/// LowerOperation - Provide custom lowering hooks for some operations.
134///
Nate Begeman21e463b2005-10-16 05:39:50 +0000135SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000136 switch (Op.getOpcode()) {
137 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000138 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000139 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000140 SDOperand Src = Op.getOperand(0);
141 if (Src.getValueType() == MVT::f32)
142 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
143
Nate Begemanc09eeec2005-09-06 22:03:27 +0000144 switch (Op.getValueType()) {
145 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
146 case MVT::i32:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000147 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000148 break;
149 case MVT::i64:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000150 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000151 break;
152 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000153
154 int FrameIdx =
155 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
156 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
157 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
158 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000159 if (Op.getOpcode() == PPCISD::FCTIDZ) {
160 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
161 } else {
162 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
163 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
164 }
165 return Op;
166 }
167 case ISD::SINT_TO_FP: {
168 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
169 "Unhandled SINT_TO_FP type in custom expander!");
170 int FrameIdx =
171 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
172 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
173 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
174 Op.getOperand(0), FI, DAG.getSrcValue(0));
175 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
176 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
177 if (MVT::f32 == Op.getValueType())
178 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
179 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000180 }
181 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000182 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000183 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
184 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
185 break;
186
187 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
188
189 // Cannot handle SETEQ/SETNE.
190 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
191
192 MVT::ValueType ResVT = Op.getValueType();
193 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
194 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
195 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000196
Chris Lattnerf7605322005-08-31 21:09:52 +0000197 // If the RHS of the comparison is a 0.0, we don't need to do the
198 // subtraction at all.
199 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000200 switch (CC) {
201 default: assert(0 && "Invalid FSEL condition"); abort();
202 case ISD::SETULT:
203 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000204 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000205 case ISD::SETUGE:
206 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000207 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
208 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000209 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000210 case ISD::SETUGT:
211 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000212 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000213 case ISD::SETULE:
214 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000215 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
216 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000217 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattnerf7605322005-08-31 21:09:52 +0000218 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000219 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000220
Chris Lattnereb255f22005-10-25 20:54:57 +0000221 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000222 switch (CC) {
223 default: assert(0 && "Invalid FSEL condition"); abort();
224 case ISD::SETULT:
225 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000226 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
227 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
228 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
229 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000230 case ISD::SETUGE:
231 case ISD::SETGE:
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, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000236 case ISD::SETUGT:
237 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000238 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000242 case ISD::SETULE:
243 case ISD::SETLE:
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, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000248 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000249 break;
250 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000251 case ISD::SHL: {
252 assert(Op.getValueType() == MVT::i64 &&
253 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
254 // The generic code does a fine job expanding shift by a constant.
255 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
256
257 // Otherwise, expand into a bunch of logical ops. Note that these ops
258 // depend on the PPC behavior for oversized shift amounts.
259 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
260 DAG.getConstant(0, MVT::i32));
261 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
262 DAG.getConstant(1, MVT::i32));
263 SDOperand Amt = Op.getOperand(1);
264
265 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
266 DAG.getConstant(32, MVT::i32), Amt);
267 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
268 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
269 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
270 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
271 DAG.getConstant(-32U, MVT::i32));
272 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
273 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
274 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
275 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
276 }
277 case ISD::SRL: {
278 assert(Op.getValueType() == MVT::i64 &&
279 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
280 // The generic code does a fine job expanding shift by a constant.
281 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
282
283 // Otherwise, expand into a bunch of logical ops. Note that these ops
284 // depend on the PPC behavior for oversized shift amounts.
285 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
286 DAG.getConstant(0, MVT::i32));
287 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
288 DAG.getConstant(1, MVT::i32));
289 SDOperand Amt = Op.getOperand(1);
290
291 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
292 DAG.getConstant(32, MVT::i32), Amt);
293 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
294 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
295 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
296 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
297 DAG.getConstant(-32U, MVT::i32));
298 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
299 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
300 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
301 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
302 }
303 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000304 assert(Op.getValueType() == MVT::i64 &&
305 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
306 // The generic code does a fine job expanding shift by a constant.
307 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
308
309 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
310 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
311 DAG.getConstant(0, MVT::i32));
312 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
313 DAG.getConstant(1, MVT::i32));
314 SDOperand Amt = Op.getOperand(1);
315
316 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
317 DAG.getConstant(32, MVT::i32), Amt);
318 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
319 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
320 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
321 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
322 DAG.getConstant(-32U, MVT::i32));
323 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
324 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
325 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
326 Tmp4, Tmp6, ISD::SETLE);
327 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000328 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000329 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000330 return SDOperand();
331}
332
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000333std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000334PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000335 //
336 // add beautiful description of PPC stack frame format, or at least some docs
337 //
338 MachineFunction &MF = DAG.getMachineFunction();
339 MachineFrameInfo *MFI = MF.getFrameInfo();
340 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000341 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000342 std::vector<SDOperand> ArgValues;
343
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000344 unsigned ArgOffset = 24;
345 unsigned GPR_remaining = 8;
346 unsigned FPR_remaining = 13;
347 unsigned GPR_idx = 0, FPR_idx = 0;
348 static const unsigned GPR[] = {
349 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
350 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
351 };
352 static const unsigned FPR[] = {
353 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
354 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
355 };
356
357 // Add DAG nodes to load the arguments... On entry to a function on PPC,
358 // the arguments start at offset 24, although they are likely to be passed
359 // in registers.
360 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
361 SDOperand newroot, argt;
362 unsigned ObjSize;
363 bool needsLoad = false;
364 bool ArgLive = !I->use_empty();
365 MVT::ValueType ObjectVT = getValueType(I->getType());
366
367 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000368 default: assert(0 && "Unhandled argument type!");
369 case MVT::i1:
370 case MVT::i8:
371 case MVT::i16:
372 case MVT::i32:
373 ObjSize = 4;
374 if (!ArgLive) break;
375 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000376 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000377 MF.addLiveIn(GPR[GPR_idx], VReg);
378 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000379 if (ObjectVT != MVT::i32) {
380 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
381 : ISD::AssertZext;
382 argt = DAG.getNode(AssertOp, MVT::i32, argt,
383 DAG.getValueType(ObjectVT));
384 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
385 }
Chris Lattner915fb302005-08-30 00:19:00 +0000386 } else {
387 needsLoad = true;
388 }
389 break;
390 case MVT::i64: ObjSize = 8;
391 if (!ArgLive) break;
392 if (GPR_remaining > 0) {
393 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000394 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000395 MF.addLiveIn(GPR[GPR_idx], VReg);
396 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000397 // If we have two or more remaining argument registers, then both halves
398 // of the i64 can be sourced from there. Otherwise, the lower half will
399 // have to come off the stack. This can happen when an i64 is preceded
400 // by 28 bytes of arguments.
401 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000402 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000403 MF.addLiveIn(GPR[GPR_idx+1], VReg);
404 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000405 } else {
406 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
407 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
408 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
409 DAG.getSrcValue(NULL));
410 }
411 // Build the outgoing arg thingy
412 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
413 newroot = argLo;
414 } else {
415 needsLoad = true;
416 }
417 break;
418 case MVT::f32:
419 case MVT::f64:
420 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
421 if (!ArgLive) break;
422 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000423 unsigned VReg;
424 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000425 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000426 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000427 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000428 MF.addLiveIn(FPR[FPR_idx], VReg);
429 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000430 --FPR_remaining;
431 ++FPR_idx;
432 } else {
433 needsLoad = true;
434 }
435 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000436 }
437
438 // We need to load the argument to a virtual register if we determined above
439 // that we ran out of physical registers of the appropriate type
440 if (needsLoad) {
441 unsigned SubregOffset = 0;
442 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
443 if (ObjectVT == MVT::i16) SubregOffset = 2;
444 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
445 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
446 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
447 DAG.getConstant(SubregOffset, MVT::i32));
448 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
449 DAG.getSrcValue(NULL));
450 }
451
452 // Every 4 bytes of argument space consumes one of the GPRs available for
453 // argument passing.
454 if (GPR_remaining > 0) {
455 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
456 GPR_remaining -= delta;
457 GPR_idx += delta;
458 }
459 ArgOffset += ObjSize;
460 if (newroot.Val)
461 DAG.setRoot(newroot.getValue(1));
462
463 ArgValues.push_back(argt);
464 }
465
466 // If the function takes variable number of arguments, make a frame index for
467 // the start of the first vararg value... for expansion of llvm.va_start.
468 if (F.isVarArg()) {
469 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
470 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
471 // If this function is vararg, store any remaining integer argument regs
472 // to their spots on the stack so that they may be loaded by deferencing the
473 // result of va_next.
474 std::vector<SDOperand> MemOps;
475 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000476 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000477 MF.addLiveIn(GPR[GPR_idx], VReg);
478 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000479 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
480 Val, FIN, DAG.getSrcValue(NULL));
481 MemOps.push_back(Store);
482 // Increment the address by four for the next argument to store
483 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
484 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
485 }
486 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
487 }
488
489 // Finally, inform the code generator which regs we return values in.
490 switch (getValueType(F.getReturnType())) {
491 default: assert(0 && "Unknown type!");
492 case MVT::isVoid: break;
493 case MVT::i1:
494 case MVT::i8:
495 case MVT::i16:
496 case MVT::i32:
497 MF.addLiveOut(PPC::R3);
498 break;
499 case MVT::i64:
500 MF.addLiveOut(PPC::R3);
501 MF.addLiveOut(PPC::R4);
502 break;
503 case MVT::f32:
504 case MVT::f64:
505 MF.addLiveOut(PPC::F1);
506 break;
507 }
508
509 return ArgValues;
510}
511
512std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000513PPCTargetLowering::LowerCallTo(SDOperand Chain,
514 const Type *RetTy, bool isVarArg,
515 unsigned CallingConv, bool isTailCall,
516 SDOperand Callee, ArgListTy &Args,
517 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000518 // args_to_use will accumulate outgoing args for the ISD::CALL case in
519 // SelectExpr to use to put the arguments in the appropriate registers.
520 std::vector<SDOperand> args_to_use;
521
522 // Count how many bytes are to be pushed on the stack, including the linkage
523 // area, and parameter passing area.
524 unsigned NumBytes = 24;
525
526 if (Args.empty()) {
527 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
528 DAG.getConstant(NumBytes, getPointerTy()));
529 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000530 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000531 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000532 default: assert(0 && "Unknown value type!");
533 case MVT::i1:
534 case MVT::i8:
535 case MVT::i16:
536 case MVT::i32:
537 case MVT::f32:
538 NumBytes += 4;
539 break;
540 case MVT::i64:
541 case MVT::f64:
542 NumBytes += 8;
543 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000544 }
Chris Lattner915fb302005-08-30 00:19:00 +0000545 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000546
Chris Lattner915fb302005-08-30 00:19:00 +0000547 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
548 // plus 32 bytes of argument space in case any called code gets funky on us.
549 // (Required by ABI to support var arg)
550 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000551
552 // Adjust the stack pointer for the new arguments...
553 // These operations are automatically eliminated by the prolog/epilog pass
554 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
555 DAG.getConstant(NumBytes, getPointerTy()));
556
557 // Set up a copy of the stack pointer for use loading and storing any
558 // arguments that may not fit in the registers available for argument
559 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000560 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
561 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000562
563 // Figure out which arguments are going to go in registers, and which in
564 // memory. Also, if this is a vararg function, floating point operations
565 // must be stored to our stack, and loaded into integer regs as well, if
566 // any integer regs are available for argument passing.
567 unsigned ArgOffset = 24;
568 unsigned GPR_remaining = 8;
569 unsigned FPR_remaining = 13;
570
571 std::vector<SDOperand> MemOps;
572 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
573 // PtrOff will be used to store the current argument to the stack if a
574 // register cannot be found for it.
575 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
576 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
577 MVT::ValueType ArgVT = getValueType(Args[i].second);
578
579 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000580 default: assert(0 && "Unexpected ValueType for argument!");
581 case MVT::i1:
582 case MVT::i8:
583 case MVT::i16:
584 // Promote the integer to 32 bits. If the input type is signed use a
585 // sign extend, otherwise use a zero extend.
586 if (Args[i].second->isSigned())
587 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
588 else
589 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
590 // FALL THROUGH
591 case MVT::i32:
592 if (GPR_remaining > 0) {
593 args_to_use.push_back(Args[i].first);
594 --GPR_remaining;
595 } else {
596 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
597 Args[i].first, PtrOff,
598 DAG.getSrcValue(NULL)));
599 }
600 ArgOffset += 4;
601 break;
602 case MVT::i64:
603 // If we have one free GPR left, we can place the upper half of the i64
604 // in it, and store the other half to the stack. If we have two or more
605 // free GPRs, then we can pass both halves of the i64 in registers.
606 if (GPR_remaining > 0) {
607 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
608 Args[i].first, DAG.getConstant(1, MVT::i32));
609 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
610 Args[i].first, DAG.getConstant(0, MVT::i32));
611 args_to_use.push_back(Hi);
612 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000613 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000614 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000615 --GPR_remaining;
616 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000617 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
618 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000619 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000620 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000621 }
Chris Lattner915fb302005-08-30 00:19:00 +0000622 } else {
623 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
624 Args[i].first, PtrOff,
625 DAG.getSrcValue(NULL)));
626 }
627 ArgOffset += 8;
628 break;
629 case MVT::f32:
630 case MVT::f64:
631 if (FPR_remaining > 0) {
632 args_to_use.push_back(Args[i].first);
633 --FPR_remaining;
634 if (isVarArg) {
635 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
636 Args[i].first, PtrOff,
637 DAG.getSrcValue(NULL));
638 MemOps.push_back(Store);
639 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000640 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000641 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
642 DAG.getSrcValue(NULL));
643 MemOps.push_back(Load);
644 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000645 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000646 }
647 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000648 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
649 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000650 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
651 DAG.getSrcValue(NULL));
652 MemOps.push_back(Load);
653 args_to_use.push_back(Load);
654 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000655 }
656 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000657 // If we have any FPRs remaining, we may also have GPRs remaining.
658 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
659 // GPRs.
660 if (GPR_remaining > 0) {
661 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
662 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000663 }
Chris Lattner915fb302005-08-30 00:19:00 +0000664 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
665 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
666 --GPR_remaining;
667 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000668 }
Chris Lattner915fb302005-08-30 00:19:00 +0000669 } else {
670 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
671 Args[i].first, PtrOff,
672 DAG.getSrcValue(NULL)));
673 }
674 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
675 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000676 }
677 }
678 if (!MemOps.empty())
679 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
680 }
681
682 std::vector<MVT::ValueType> RetVals;
683 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000684 MVT::ValueType ActualRetTyVT = RetTyVT;
685 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
686 ActualRetTyVT = MVT::i32; // Promote result to i32.
687
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000688 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000689 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000690 RetVals.push_back(MVT::Other);
691
692 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
693 Chain, Callee, args_to_use), 0);
694 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
695 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
696 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000697 SDOperand RetVal = TheCall;
698
699 // If the result is a small value, add a note so that we keep track of the
700 // information about whether it is sign or zero extended.
701 if (RetTyVT != ActualRetTyVT) {
702 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
703 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
704 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
705 }
706
707 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000708}
709
Nate Begeman4a959452005-10-18 23:23:37 +0000710SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
711 SelectionDAG &DAG) {
712 if (Op.getValueType() == MVT::i64) {
713 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
714 DAG.getConstant(1, MVT::i32));
715 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
716 DAG.getConstant(0, MVT::i32));
717 return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
718 } else {
719 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
720 }
721}
722
Nate Begeman21e463b2005-10-16 05:39:50 +0000723SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
724 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000725 // vastart just stores the address of the VarArgsFrameIndex slot into the
726 // memory location argument.
727 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
728 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
729 DAG.getSrcValue(VAListV));
730}
731
732std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000733PPCTargetLowering::LowerVAArg(SDOperand Chain,
734 SDOperand VAListP, Value *VAListV,
735 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000736 MVT::ValueType ArgVT = getValueType(ArgTy);
737
738 SDOperand VAList =
739 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
740 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
741 unsigned Amt;
742 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
743 Amt = 4;
744 else {
745 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
746 "Other types should have been promoted for varargs!");
747 Amt = 8;
748 }
749 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
750 DAG.getConstant(Amt, VAList.getValueType()));
751 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
752 VAList, VAListP, DAG.getSrcValue(VAListV));
753 return std::make_pair(Result, Chain);
754}
755
756
Nate Begeman21e463b2005-10-16 05:39:50 +0000757std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000758LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
759 SelectionDAG &DAG) {
760 assert(0 && "LowerFrameReturnAddress unimplemented");
761 abort();
762}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000763
764MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000765PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
766 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000767 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000768 MI->getOpcode() == PPC::SELECT_CC_F4 ||
769 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000770 "Unexpected instr type to insert");
771
772 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
773 // control-flow pattern. The incoming instruction knows the destination vreg
774 // to set, the condition code register to branch on, the true/false values to
775 // select between, and a branch opcode to use.
776 const BasicBlock *LLVM_BB = BB->getBasicBlock();
777 ilist<MachineBasicBlock>::iterator It = BB;
778 ++It;
779
780 // thisMBB:
781 // ...
782 // TrueVal = ...
783 // cmpTY ccX, r1, r2
784 // bCC copy1MBB
785 // fallthrough --> copy0MBB
786 MachineBasicBlock *thisMBB = BB;
787 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
788 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
789 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
790 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
791 MachineFunction *F = BB->getParent();
792 F->getBasicBlockList().insert(It, copy0MBB);
793 F->getBasicBlockList().insert(It, sinkMBB);
794 // Update machine-CFG edges
795 BB->addSuccessor(copy0MBB);
796 BB->addSuccessor(sinkMBB);
797
798 // copy0MBB:
799 // %FalseValue = ...
800 // # fallthrough to sinkMBB
801 BB = copy0MBB;
802
803 // Update machine-CFG edges
804 BB->addSuccessor(sinkMBB);
805
806 // sinkMBB:
807 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
808 // ...
809 BB = sinkMBB;
810 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
811 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
812 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
813
814 delete MI; // The pseudo instruction is gone now.
815 return BB;
816}
817