blob: 9526ae1f2a7c755742a36e84f1c5a9c32402bda0 [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);
Chris Lattnerf73bae12005-11-29 06:16:21 +000093
94 // PowerPC doesn't have line number support yet.
95 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +000096
Chris Lattner860e8862005-11-17 07:30:41 +000097 // We want to legalize GlobalAddress into the appropriate instructions to
98 // materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +000099 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Chris Lattner860e8862005-11-17 07:30:41 +0000100
Nate Begemanc09eeec2005-09-06 22:03:27 +0000101 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000102 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000103 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
104 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000105 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
106 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
107 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000108 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000109 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000110 }
111
112 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
113 // 64 bit PowerPC implementations can support i64 types directly
114 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000115 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
116 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000117 } else {
118 // 32 bit PowerPC wants to expand i64 shifts itself.
119 setOperationAction(ISD::SHL, MVT::i64, Custom);
120 setOperationAction(ISD::SRL, MVT::i64, Custom);
121 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000122 }
123
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000124 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000125
126 computeRegisterProperties();
127}
128
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000129/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
130static bool isFloatingPointZero(SDOperand Op) {
131 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
132 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
133 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
134 // Maybe this has already been legalized into the constant pool?
135 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
136 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
137 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
138 }
139 return false;
140}
141
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000142/// LowerOperation - Provide custom lowering hooks for some operations.
143///
Nate Begeman21e463b2005-10-16 05:39:50 +0000144SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000145 switch (Op.getOpcode()) {
146 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000147 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000148 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000149 SDOperand Src = Op.getOperand(0);
150 if (Src.getValueType() == MVT::f32)
151 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
152
Nate Begemanc09eeec2005-09-06 22:03:27 +0000153 switch (Op.getValueType()) {
154 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
155 case MVT::i32:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000156 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000157 break;
158 case MVT::i64:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000159 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000160 break;
161 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000162
163 int FrameIdx =
164 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
165 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
166 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
167 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000168 if (Op.getOpcode() == PPCISD::FCTIDZ) {
169 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
170 } else {
171 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
172 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
173 }
174 return Op;
175 }
176 case ISD::SINT_TO_FP: {
177 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
178 "Unhandled SINT_TO_FP type in custom expander!");
179 int FrameIdx =
180 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
181 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
182 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
183 Op.getOperand(0), FI, DAG.getSrcValue(0));
184 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
185 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
186 if (MVT::f32 == Op.getValueType())
187 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
188 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000189 }
190 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000191 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000192 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
193 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
194 break;
195
196 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
197
198 // Cannot handle SETEQ/SETNE.
199 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
200
201 MVT::ValueType ResVT = Op.getValueType();
202 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
203 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
204 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000205
Chris Lattnerf7605322005-08-31 21:09:52 +0000206 // If the RHS of the comparison is a 0.0, we don't need to do the
207 // subtraction at all.
208 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000209 switch (CC) {
210 default: assert(0 && "Invalid FSEL condition"); abort();
211 case ISD::SETULT:
212 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000213 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000214 case ISD::SETUGE:
215 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000216 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
217 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000218 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000219 case ISD::SETUGT:
220 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000221 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000222 case ISD::SETULE:
223 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000224 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
225 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000226 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000227 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000228 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000229
Chris Lattnereb255f22005-10-25 20:54:57 +0000230 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000231 switch (CC) {
232 default: assert(0 && "Invalid FSEL condition"); abort();
233 case ISD::SETULT:
234 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000235 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
236 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
237 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
238 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000239 case ISD::SETUGE:
240 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000241 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
242 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
243 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
244 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000245 case ISD::SETUGT:
246 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000247 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
248 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
249 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
250 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000251 case ISD::SETULE:
252 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000253 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
254 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
255 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
256 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000257 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000258 break;
259 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000260 case ISD::SHL: {
261 assert(Op.getValueType() == MVT::i64 &&
262 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
263 // The generic code does a fine job expanding shift by a constant.
264 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
265
266 // Otherwise, expand into a bunch of logical ops. Note that these ops
267 // depend on the PPC behavior for oversized shift amounts.
268 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
269 DAG.getConstant(0, MVT::i32));
270 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
271 DAG.getConstant(1, MVT::i32));
272 SDOperand Amt = Op.getOperand(1);
273
274 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
275 DAG.getConstant(32, MVT::i32), Amt);
276 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
277 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
278 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
279 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
280 DAG.getConstant(-32U, MVT::i32));
281 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
282 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
283 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
284 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
285 }
286 case ISD::SRL: {
287 assert(Op.getValueType() == MVT::i64 &&
288 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
289 // The generic code does a fine job expanding shift by a constant.
290 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
291
292 // Otherwise, expand into a bunch of logical ops. Note that these ops
293 // depend on the PPC behavior for oversized shift amounts.
294 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
295 DAG.getConstant(0, MVT::i32));
296 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
297 DAG.getConstant(1, MVT::i32));
298 SDOperand Amt = Op.getOperand(1);
299
300 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
301 DAG.getConstant(32, MVT::i32), Amt);
302 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
303 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
304 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
305 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
306 DAG.getConstant(-32U, MVT::i32));
307 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
308 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
309 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
310 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
311 }
312 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000313 assert(Op.getValueType() == MVT::i64 &&
314 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
315 // The generic code does a fine job expanding shift by a constant.
316 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
317
318 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
319 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
320 DAG.getConstant(0, MVT::i32));
321 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
322 DAG.getConstant(1, MVT::i32));
323 SDOperand Amt = Op.getOperand(1);
324
325 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
326 DAG.getConstant(32, MVT::i32), Amt);
327 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
328 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
329 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
330 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
331 DAG.getConstant(-32U, MVT::i32));
332 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
333 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
334 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
335 Tmp4, Tmp6, ISD::SETLE);
336 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000337 }
Chris Lattner860e8862005-11-17 07:30:41 +0000338 case ISD::GlobalAddress: {
Chris Lattner860e8862005-11-17 07:30:41 +0000339 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
340 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
341 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000342
343 if (PPCGenerateStaticCode) {
344 // Generate non-pic code that has direct accesses to globals. To do this
345 // the address of the global is just (hi(&g)+lo(&g)).
346 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
347 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
348 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
349 }
Chris Lattner860e8862005-11-17 07:30:41 +0000350
Chris Lattner1d05cb42005-11-17 18:55:48 +0000351 // Only lower GlobalAddress on Darwin.
352 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000353 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
354 if (PICEnabled) {
355 // With PIC, the first instruction is actually "GR+hi(&G)".
356 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000357 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000358 }
359
360 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
361 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
362
363 if (!GV->hasWeakLinkage() && !GV->isExternal())
364 return Lo;
365
366 // If the global is weak or external, we have to go through the lazy
367 // resolution stub.
368 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
369 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000370 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000371 return SDOperand();
372}
373
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000374std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000375PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000376 //
377 // add beautiful description of PPC stack frame format, or at least some docs
378 //
379 MachineFunction &MF = DAG.getMachineFunction();
380 MachineFrameInfo *MFI = MF.getFrameInfo();
381 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000382 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000383 std::vector<SDOperand> ArgValues;
384
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000385 unsigned ArgOffset = 24;
386 unsigned GPR_remaining = 8;
387 unsigned FPR_remaining = 13;
388 unsigned GPR_idx = 0, FPR_idx = 0;
389 static const unsigned GPR[] = {
390 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
391 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
392 };
393 static const unsigned FPR[] = {
394 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
395 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
396 };
397
398 // Add DAG nodes to load the arguments... On entry to a function on PPC,
399 // the arguments start at offset 24, although they are likely to be passed
400 // in registers.
401 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
402 SDOperand newroot, argt;
403 unsigned ObjSize;
404 bool needsLoad = false;
405 bool ArgLive = !I->use_empty();
406 MVT::ValueType ObjectVT = getValueType(I->getType());
407
408 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000409 default: assert(0 && "Unhandled argument type!");
410 case MVT::i1:
411 case MVT::i8:
412 case MVT::i16:
413 case MVT::i32:
414 ObjSize = 4;
415 if (!ArgLive) break;
416 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000417 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000418 MF.addLiveIn(GPR[GPR_idx], VReg);
419 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000420 if (ObjectVT != MVT::i32) {
421 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
422 : ISD::AssertZext;
423 argt = DAG.getNode(AssertOp, MVT::i32, argt,
424 DAG.getValueType(ObjectVT));
425 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
426 }
Chris Lattner915fb302005-08-30 00:19:00 +0000427 } else {
428 needsLoad = true;
429 }
430 break;
431 case MVT::i64: ObjSize = 8;
432 if (!ArgLive) break;
433 if (GPR_remaining > 0) {
434 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000435 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000436 MF.addLiveIn(GPR[GPR_idx], VReg);
437 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000438 // If we have two or more remaining argument registers, then both halves
439 // of the i64 can be sourced from there. Otherwise, the lower half will
440 // have to come off the stack. This can happen when an i64 is preceded
441 // by 28 bytes of arguments.
442 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000443 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000444 MF.addLiveIn(GPR[GPR_idx+1], VReg);
445 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000446 } else {
447 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
448 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
449 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
450 DAG.getSrcValue(NULL));
451 }
452 // Build the outgoing arg thingy
453 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
454 newroot = argLo;
455 } else {
456 needsLoad = true;
457 }
458 break;
459 case MVT::f32:
460 case MVT::f64:
461 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
462 if (!ArgLive) break;
463 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000464 unsigned VReg;
465 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000466 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000467 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000468 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000469 MF.addLiveIn(FPR[FPR_idx], VReg);
470 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000471 --FPR_remaining;
472 ++FPR_idx;
473 } else {
474 needsLoad = true;
475 }
476 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000477 }
478
479 // We need to load the argument to a virtual register if we determined above
480 // that we ran out of physical registers of the appropriate type
481 if (needsLoad) {
482 unsigned SubregOffset = 0;
483 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
484 if (ObjectVT == MVT::i16) SubregOffset = 2;
485 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
486 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
487 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
488 DAG.getConstant(SubregOffset, MVT::i32));
489 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
490 DAG.getSrcValue(NULL));
491 }
492
493 // Every 4 bytes of argument space consumes one of the GPRs available for
494 // argument passing.
495 if (GPR_remaining > 0) {
496 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
497 GPR_remaining -= delta;
498 GPR_idx += delta;
499 }
500 ArgOffset += ObjSize;
501 if (newroot.Val)
502 DAG.setRoot(newroot.getValue(1));
503
504 ArgValues.push_back(argt);
505 }
506
507 // If the function takes variable number of arguments, make a frame index for
508 // the start of the first vararg value... for expansion of llvm.va_start.
509 if (F.isVarArg()) {
510 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
511 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
512 // If this function is vararg, store any remaining integer argument regs
513 // to their spots on the stack so that they may be loaded by deferencing the
514 // result of va_next.
515 std::vector<SDOperand> MemOps;
516 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000517 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000518 MF.addLiveIn(GPR[GPR_idx], VReg);
519 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000520 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
521 Val, FIN, DAG.getSrcValue(NULL));
522 MemOps.push_back(Store);
523 // Increment the address by four for the next argument to store
524 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
525 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
526 }
527 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
528 }
529
530 // Finally, inform the code generator which regs we return values in.
531 switch (getValueType(F.getReturnType())) {
532 default: assert(0 && "Unknown type!");
533 case MVT::isVoid: break;
534 case MVT::i1:
535 case MVT::i8:
536 case MVT::i16:
537 case MVT::i32:
538 MF.addLiveOut(PPC::R3);
539 break;
540 case MVT::i64:
541 MF.addLiveOut(PPC::R3);
542 MF.addLiveOut(PPC::R4);
543 break;
544 case MVT::f32:
545 case MVT::f64:
546 MF.addLiveOut(PPC::F1);
547 break;
548 }
549
550 return ArgValues;
551}
552
553std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000554PPCTargetLowering::LowerCallTo(SDOperand Chain,
555 const Type *RetTy, bool isVarArg,
556 unsigned CallingConv, bool isTailCall,
557 SDOperand Callee, ArgListTy &Args,
558 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000559 // args_to_use will accumulate outgoing args for the ISD::CALL case in
560 // SelectExpr to use to put the arguments in the appropriate registers.
561 std::vector<SDOperand> args_to_use;
562
563 // Count how many bytes are to be pushed on the stack, including the linkage
564 // area, and parameter passing area.
565 unsigned NumBytes = 24;
566
567 if (Args.empty()) {
568 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
569 DAG.getConstant(NumBytes, getPointerTy()));
570 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000571 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000572 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000573 default: assert(0 && "Unknown value type!");
574 case MVT::i1:
575 case MVT::i8:
576 case MVT::i16:
577 case MVT::i32:
578 case MVT::f32:
579 NumBytes += 4;
580 break;
581 case MVT::i64:
582 case MVT::f64:
583 NumBytes += 8;
584 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000585 }
Chris Lattner915fb302005-08-30 00:19:00 +0000586 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000587
Chris Lattner915fb302005-08-30 00:19:00 +0000588 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
589 // plus 32 bytes of argument space in case any called code gets funky on us.
590 // (Required by ABI to support var arg)
591 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000592
593 // Adjust the stack pointer for the new arguments...
594 // These operations are automatically eliminated by the prolog/epilog pass
595 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
596 DAG.getConstant(NumBytes, getPointerTy()));
597
598 // Set up a copy of the stack pointer for use loading and storing any
599 // arguments that may not fit in the registers available for argument
600 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000601 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
602 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000603
604 // Figure out which arguments are going to go in registers, and which in
605 // memory. Also, if this is a vararg function, floating point operations
606 // must be stored to our stack, and loaded into integer regs as well, if
607 // any integer regs are available for argument passing.
608 unsigned ArgOffset = 24;
609 unsigned GPR_remaining = 8;
610 unsigned FPR_remaining = 13;
611
612 std::vector<SDOperand> MemOps;
613 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
614 // PtrOff will be used to store the current argument to the stack if a
615 // register cannot be found for it.
616 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
617 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
618 MVT::ValueType ArgVT = getValueType(Args[i].second);
619
620 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000621 default: assert(0 && "Unexpected ValueType for argument!");
622 case MVT::i1:
623 case MVT::i8:
624 case MVT::i16:
625 // Promote the integer to 32 bits. If the input type is signed use a
626 // sign extend, otherwise use a zero extend.
627 if (Args[i].second->isSigned())
628 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
629 else
630 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
631 // FALL THROUGH
632 case MVT::i32:
633 if (GPR_remaining > 0) {
634 args_to_use.push_back(Args[i].first);
635 --GPR_remaining;
636 } else {
637 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
638 Args[i].first, PtrOff,
639 DAG.getSrcValue(NULL)));
640 }
641 ArgOffset += 4;
642 break;
643 case MVT::i64:
644 // If we have one free GPR left, we can place the upper half of the i64
645 // in it, and store the other half to the stack. If we have two or more
646 // free GPRs, then we can pass both halves of the i64 in registers.
647 if (GPR_remaining > 0) {
648 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
649 Args[i].first, DAG.getConstant(1, MVT::i32));
650 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
651 Args[i].first, DAG.getConstant(0, MVT::i32));
652 args_to_use.push_back(Hi);
653 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000654 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000655 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000656 --GPR_remaining;
657 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000658 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
659 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000660 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000661 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000662 }
Chris Lattner915fb302005-08-30 00:19:00 +0000663 } else {
664 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
665 Args[i].first, PtrOff,
666 DAG.getSrcValue(NULL)));
667 }
668 ArgOffset += 8;
669 break;
670 case MVT::f32:
671 case MVT::f64:
672 if (FPR_remaining > 0) {
673 args_to_use.push_back(Args[i].first);
674 --FPR_remaining;
675 if (isVarArg) {
676 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
677 Args[i].first, PtrOff,
678 DAG.getSrcValue(NULL));
679 MemOps.push_back(Store);
680 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000681 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000682 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
683 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000684 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000685 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000686 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000687 }
688 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000689 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
690 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000691 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
692 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000693 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000694 args_to_use.push_back(Load);
695 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000696 }
697 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000698 // If we have any FPRs remaining, we may also have GPRs remaining.
699 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
700 // GPRs.
701 if (GPR_remaining > 0) {
702 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
703 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000704 }
Chris Lattner915fb302005-08-30 00:19:00 +0000705 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
706 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
707 --GPR_remaining;
708 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000709 }
Chris Lattner915fb302005-08-30 00:19:00 +0000710 } else {
711 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
712 Args[i].first, PtrOff,
713 DAG.getSrcValue(NULL)));
714 }
715 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
716 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000717 }
718 }
719 if (!MemOps.empty())
720 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
721 }
722
723 std::vector<MVT::ValueType> RetVals;
724 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000725 MVT::ValueType ActualRetTyVT = RetTyVT;
726 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
727 ActualRetTyVT = MVT::i32; // Promote result to i32.
728
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000729 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000730 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000731 RetVals.push_back(MVT::Other);
732
Chris Lattner2823b3e2005-11-17 05:56:14 +0000733 // If the callee is a GlobalAddress node (quite common, every direct call is)
734 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
735 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
736 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
737
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000738 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
739 Chain, Callee, args_to_use), 0);
740 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
741 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
742 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000743 SDOperand RetVal = TheCall;
744
745 // If the result is a small value, add a note so that we keep track of the
746 // information about whether it is sign or zero extended.
747 if (RetTyVT != ActualRetTyVT) {
748 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
749 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
750 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
751 }
752
753 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000754}
755
Nate Begeman4a959452005-10-18 23:23:37 +0000756SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
757 SelectionDAG &DAG) {
758 if (Op.getValueType() == MVT::i64) {
759 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
760 DAG.getConstant(1, MVT::i32));
761 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
762 DAG.getConstant(0, MVT::i32));
763 return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
764 } else {
765 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
766 }
767}
768
Nate Begeman21e463b2005-10-16 05:39:50 +0000769SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
770 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000771 // vastart just stores the address of the VarArgsFrameIndex slot into the
772 // memory location argument.
773 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
774 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
775 DAG.getSrcValue(VAListV));
776}
777
778std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000779PPCTargetLowering::LowerVAArg(SDOperand Chain,
780 SDOperand VAListP, Value *VAListV,
781 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000782 MVT::ValueType ArgVT = getValueType(ArgTy);
783
784 SDOperand VAList =
785 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
786 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
787 unsigned Amt;
788 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
789 Amt = 4;
790 else {
791 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
792 "Other types should have been promoted for varargs!");
793 Amt = 8;
794 }
795 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
796 DAG.getConstant(Amt, VAList.getValueType()));
797 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
798 VAList, VAListP, DAG.getSrcValue(VAListV));
799 return std::make_pair(Result, Chain);
800}
801
802
Nate Begeman21e463b2005-10-16 05:39:50 +0000803std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000804LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
805 SelectionDAG &DAG) {
806 assert(0 && "LowerFrameReturnAddress unimplemented");
807 abort();
808}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000809
810MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000811PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
812 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000813 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000814 MI->getOpcode() == PPC::SELECT_CC_F4 ||
815 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000816 "Unexpected instr type to insert");
817
818 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
819 // control-flow pattern. The incoming instruction knows the destination vreg
820 // to set, the condition code register to branch on, the true/false values to
821 // select between, and a branch opcode to use.
822 const BasicBlock *LLVM_BB = BB->getBasicBlock();
823 ilist<MachineBasicBlock>::iterator It = BB;
824 ++It;
825
826 // thisMBB:
827 // ...
828 // TrueVal = ...
829 // cmpTY ccX, r1, r2
830 // bCC copy1MBB
831 // fallthrough --> copy0MBB
832 MachineBasicBlock *thisMBB = BB;
833 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
834 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
835 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
836 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
837 MachineFunction *F = BB->getParent();
838 F->getBasicBlockList().insert(It, copy0MBB);
839 F->getBasicBlockList().insert(It, sinkMBB);
840 // Update machine-CFG edges
841 BB->addSuccessor(copy0MBB);
842 BB->addSuccessor(sinkMBB);
843
844 // copy0MBB:
845 // %FalseValue = ...
846 // # fallthrough to sinkMBB
847 BB = copy0MBB;
848
849 // Update machine-CFG edges
850 BB->addSuccessor(sinkMBB);
851
852 // sinkMBB:
853 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
854 // ...
855 BB = sinkMBB;
856 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
857 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
858 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
859
860 delete MI; // The pseudo instruction is gone now.
861 return BB;
862}
863