blob: 0f7afbc7fbfda7d988fe013d8e61189f8db2e79b [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
Nate Begeman425a9692005-11-29 08:17:20 +0000124 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
125 // FIXME: AltiVec supports a wide variety of packed types. For now, we're
126 // bringing up support with just v4f32.
127 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
128 }
129
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000130 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000131
132 computeRegisterProperties();
133}
134
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000135/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
136static bool isFloatingPointZero(SDOperand Op) {
137 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
138 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
139 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
140 // Maybe this has already been legalized into the constant pool?
141 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
142 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
143 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
144 }
145 return false;
146}
147
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000148/// LowerOperation - Provide custom lowering hooks for some operations.
149///
Nate Begeman21e463b2005-10-16 05:39:50 +0000150SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000151 switch (Op.getOpcode()) {
152 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000153 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000154 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000155 SDOperand Src = Op.getOperand(0);
156 if (Src.getValueType() == MVT::f32)
157 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
158
Nate Begemanc09eeec2005-09-06 22:03:27 +0000159 switch (Op.getValueType()) {
160 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
161 case MVT::i32:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000162 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000163 break;
164 case MVT::i64:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000165 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000166 break;
167 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000168
169 int FrameIdx =
170 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
171 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
172 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
173 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000174 if (Op.getOpcode() == PPCISD::FCTIDZ) {
175 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
176 } else {
177 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
178 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
179 }
180 return Op;
181 }
182 case ISD::SINT_TO_FP: {
183 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
184 "Unhandled SINT_TO_FP type in custom expander!");
185 int FrameIdx =
186 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
187 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
188 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
189 Op.getOperand(0), FI, DAG.getSrcValue(0));
190 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
191 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
192 if (MVT::f32 == Op.getValueType())
193 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
194 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000195 }
196 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000197 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000198 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
199 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
200 break;
201
202 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
203
204 // Cannot handle SETEQ/SETNE.
205 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
206
207 MVT::ValueType ResVT = Op.getValueType();
208 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
209 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
210 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000211
Chris Lattnerf7605322005-08-31 21:09:52 +0000212 // If the RHS of the comparison is a 0.0, we don't need to do the
213 // subtraction at all.
214 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000215 switch (CC) {
216 default: assert(0 && "Invalid FSEL condition"); abort();
217 case ISD::SETULT:
218 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000219 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000220 case ISD::SETUGE:
221 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000222 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
223 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000224 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000225 case ISD::SETUGT:
226 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000227 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000228 case ISD::SETULE:
229 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000230 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
231 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000232 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000233 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000234 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000235
Chris Lattnereb255f22005-10-25 20:54:57 +0000236 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000237 switch (CC) {
238 default: assert(0 && "Invalid FSEL condition"); abort();
239 case ISD::SETULT:
240 case ISD::SETLT:
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000245 case ISD::SETUGE:
246 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000247 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
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, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000251 case ISD::SETUGT:
252 case ISD::SETGT:
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000257 case ISD::SETULE:
258 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000259 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
260 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
261 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
262 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000263 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000264 break;
265 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000266 case ISD::SHL: {
267 assert(Op.getValueType() == MVT::i64 &&
268 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
269 // The generic code does a fine job expanding shift by a constant.
270 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
271
272 // Otherwise, expand into a bunch of logical ops. Note that these ops
273 // depend on the PPC behavior for oversized shift amounts.
274 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
275 DAG.getConstant(0, MVT::i32));
276 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
277 DAG.getConstant(1, MVT::i32));
278 SDOperand Amt = Op.getOperand(1);
279
280 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
281 DAG.getConstant(32, MVT::i32), Amt);
282 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
283 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
284 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
285 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
286 DAG.getConstant(-32U, MVT::i32));
287 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
288 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
289 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
290 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
291 }
292 case ISD::SRL: {
293 assert(Op.getValueType() == MVT::i64 &&
294 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
295 // The generic code does a fine job expanding shift by a constant.
296 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
297
298 // Otherwise, expand into a bunch of logical ops. Note that these ops
299 // depend on the PPC behavior for oversized shift amounts.
300 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
301 DAG.getConstant(0, MVT::i32));
302 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
303 DAG.getConstant(1, MVT::i32));
304 SDOperand Amt = Op.getOperand(1);
305
306 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
307 DAG.getConstant(32, MVT::i32), Amt);
308 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
309 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
310 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
311 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
312 DAG.getConstant(-32U, MVT::i32));
313 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
314 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
315 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
316 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
317 }
318 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000319 assert(Op.getValueType() == MVT::i64 &&
320 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
321 // The generic code does a fine job expanding shift by a constant.
322 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
323
324 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
325 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
326 DAG.getConstant(0, MVT::i32));
327 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
328 DAG.getConstant(1, MVT::i32));
329 SDOperand Amt = Op.getOperand(1);
330
331 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
332 DAG.getConstant(32, MVT::i32), Amt);
333 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
334 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
335 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
336 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
337 DAG.getConstant(-32U, MVT::i32));
338 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
339 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
340 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
341 Tmp4, Tmp6, ISD::SETLE);
342 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000343 }
Chris Lattner860e8862005-11-17 07:30:41 +0000344 case ISD::GlobalAddress: {
Chris Lattner860e8862005-11-17 07:30:41 +0000345 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
346 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
347 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000348
349 if (PPCGenerateStaticCode) {
350 // Generate non-pic code that has direct accesses to globals. To do this
351 // the address of the global is just (hi(&g)+lo(&g)).
352 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
353 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
354 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
355 }
Chris Lattner860e8862005-11-17 07:30:41 +0000356
Chris Lattner1d05cb42005-11-17 18:55:48 +0000357 // Only lower GlobalAddress on Darwin.
358 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000359 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
360 if (PICEnabled) {
361 // With PIC, the first instruction is actually "GR+hi(&G)".
362 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000363 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000364 }
365
366 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
367 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
368
369 if (!GV->hasWeakLinkage() && !GV->isExternal())
370 return Lo;
371
372 // If the global is weak or external, we have to go through the lazy
373 // resolution stub.
374 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
375 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000376 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000377 return SDOperand();
378}
379
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000380std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000381PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000382 //
383 // add beautiful description of PPC stack frame format, or at least some docs
384 //
385 MachineFunction &MF = DAG.getMachineFunction();
386 MachineFrameInfo *MFI = MF.getFrameInfo();
387 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000388 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000389 std::vector<SDOperand> ArgValues;
390
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000391 unsigned ArgOffset = 24;
392 unsigned GPR_remaining = 8;
393 unsigned FPR_remaining = 13;
394 unsigned GPR_idx = 0, FPR_idx = 0;
395 static const unsigned GPR[] = {
396 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
397 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
398 };
399 static const unsigned FPR[] = {
400 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
401 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
402 };
403
404 // Add DAG nodes to load the arguments... On entry to a function on PPC,
405 // the arguments start at offset 24, although they are likely to be passed
406 // in registers.
407 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
408 SDOperand newroot, argt;
409 unsigned ObjSize;
410 bool needsLoad = false;
411 bool ArgLive = !I->use_empty();
412 MVT::ValueType ObjectVT = getValueType(I->getType());
413
414 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000415 default: assert(0 && "Unhandled argument type!");
416 case MVT::i1:
417 case MVT::i8:
418 case MVT::i16:
419 case MVT::i32:
420 ObjSize = 4;
421 if (!ArgLive) break;
422 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000423 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000424 MF.addLiveIn(GPR[GPR_idx], VReg);
425 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000426 if (ObjectVT != MVT::i32) {
427 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
428 : ISD::AssertZext;
429 argt = DAG.getNode(AssertOp, MVT::i32, argt,
430 DAG.getValueType(ObjectVT));
431 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
432 }
Chris Lattner915fb302005-08-30 00:19:00 +0000433 } else {
434 needsLoad = true;
435 }
436 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000437 case MVT::i64:
438 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000439 if (!ArgLive) break;
440 if (GPR_remaining > 0) {
441 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000442 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000443 MF.addLiveIn(GPR[GPR_idx], VReg);
444 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000445 // If we have two or more remaining argument registers, then both halves
446 // of the i64 can be sourced from there. Otherwise, the lower half will
447 // have to come off the stack. This can happen when an i64 is preceded
448 // by 28 bytes of arguments.
449 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000450 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000451 MF.addLiveIn(GPR[GPR_idx+1], VReg);
452 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000453 } else {
454 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
455 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
456 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
457 DAG.getSrcValue(NULL));
458 }
459 // Build the outgoing arg thingy
460 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
461 newroot = argLo;
462 } else {
463 needsLoad = true;
464 }
465 break;
466 case MVT::f32:
467 case MVT::f64:
468 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
469 if (!ArgLive) break;
470 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000471 unsigned VReg;
472 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000473 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000474 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000475 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000476 MF.addLiveIn(FPR[FPR_idx], VReg);
477 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000478 --FPR_remaining;
479 ++FPR_idx;
480 } else {
481 needsLoad = true;
482 }
483 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000484 }
485
486 // We need to load the argument to a virtual register if we determined above
487 // that we ran out of physical registers of the appropriate type
488 if (needsLoad) {
489 unsigned SubregOffset = 0;
490 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
491 if (ObjectVT == MVT::i16) SubregOffset = 2;
492 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
493 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
494 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
495 DAG.getConstant(SubregOffset, MVT::i32));
496 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
497 DAG.getSrcValue(NULL));
498 }
499
500 // Every 4 bytes of argument space consumes one of the GPRs available for
501 // argument passing.
502 if (GPR_remaining > 0) {
503 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
504 GPR_remaining -= delta;
505 GPR_idx += delta;
506 }
507 ArgOffset += ObjSize;
508 if (newroot.Val)
509 DAG.setRoot(newroot.getValue(1));
510
511 ArgValues.push_back(argt);
512 }
513
514 // If the function takes variable number of arguments, make a frame index for
515 // the start of the first vararg value... for expansion of llvm.va_start.
516 if (F.isVarArg()) {
517 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
518 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
519 // If this function is vararg, store any remaining integer argument regs
520 // to their spots on the stack so that they may be loaded by deferencing the
521 // result of va_next.
522 std::vector<SDOperand> MemOps;
523 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000524 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000525 MF.addLiveIn(GPR[GPR_idx], VReg);
526 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000527 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
528 Val, FIN, DAG.getSrcValue(NULL));
529 MemOps.push_back(Store);
530 // Increment the address by four for the next argument to store
531 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
532 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
533 }
Chris Lattner80720a92005-11-30 20:40:54 +0000534 if (!MemOps.empty()) {
535 MemOps.push_back(DAG.getRoot());
536 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
537 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000538 }
539
540 // Finally, inform the code generator which regs we return values in.
541 switch (getValueType(F.getReturnType())) {
542 default: assert(0 && "Unknown type!");
543 case MVT::isVoid: break;
544 case MVT::i1:
545 case MVT::i8:
546 case MVT::i16:
547 case MVT::i32:
548 MF.addLiveOut(PPC::R3);
549 break;
550 case MVT::i64:
551 MF.addLiveOut(PPC::R3);
552 MF.addLiveOut(PPC::R4);
553 break;
554 case MVT::f32:
555 case MVT::f64:
556 MF.addLiveOut(PPC::F1);
557 break;
558 }
559
560 return ArgValues;
561}
562
563std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000564PPCTargetLowering::LowerCallTo(SDOperand Chain,
565 const Type *RetTy, bool isVarArg,
566 unsigned CallingConv, bool isTailCall,
567 SDOperand Callee, ArgListTy &Args,
568 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000569 // args_to_use will accumulate outgoing args for the ISD::CALL case in
570 // SelectExpr to use to put the arguments in the appropriate registers.
571 std::vector<SDOperand> args_to_use;
572
573 // Count how many bytes are to be pushed on the stack, including the linkage
574 // area, and parameter passing area.
575 unsigned NumBytes = 24;
576
577 if (Args.empty()) {
578 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
579 DAG.getConstant(NumBytes, getPointerTy()));
580 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000581 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000582 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000583 default: assert(0 && "Unknown value type!");
584 case MVT::i1:
585 case MVT::i8:
586 case MVT::i16:
587 case MVT::i32:
588 case MVT::f32:
589 NumBytes += 4;
590 break;
591 case MVT::i64:
592 case MVT::f64:
593 NumBytes += 8;
594 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000595 }
Chris Lattner915fb302005-08-30 00:19:00 +0000596 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000597
Chris Lattner915fb302005-08-30 00:19:00 +0000598 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
599 // plus 32 bytes of argument space in case any called code gets funky on us.
600 // (Required by ABI to support var arg)
601 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000602
603 // Adjust the stack pointer for the new arguments...
604 // These operations are automatically eliminated by the prolog/epilog pass
605 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
606 DAG.getConstant(NumBytes, getPointerTy()));
607
608 // Set up a copy of the stack pointer for use loading and storing any
609 // arguments that may not fit in the registers available for argument
610 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000611 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
612 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000613
614 // Figure out which arguments are going to go in registers, and which in
615 // memory. Also, if this is a vararg function, floating point operations
616 // must be stored to our stack, and loaded into integer regs as well, if
617 // any integer regs are available for argument passing.
618 unsigned ArgOffset = 24;
619 unsigned GPR_remaining = 8;
620 unsigned FPR_remaining = 13;
621
622 std::vector<SDOperand> MemOps;
623 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
624 // PtrOff will be used to store the current argument to the stack if a
625 // register cannot be found for it.
626 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
627 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
628 MVT::ValueType ArgVT = getValueType(Args[i].second);
629
630 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000631 default: assert(0 && "Unexpected ValueType for argument!");
632 case MVT::i1:
633 case MVT::i8:
634 case MVT::i16:
635 // Promote the integer to 32 bits. If the input type is signed use a
636 // sign extend, otherwise use a zero extend.
637 if (Args[i].second->isSigned())
638 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
639 else
640 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
641 // FALL THROUGH
642 case MVT::i32:
643 if (GPR_remaining > 0) {
644 args_to_use.push_back(Args[i].first);
645 --GPR_remaining;
646 } else {
647 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
648 Args[i].first, PtrOff,
649 DAG.getSrcValue(NULL)));
650 }
651 ArgOffset += 4;
652 break;
653 case MVT::i64:
654 // If we have one free GPR left, we can place the upper half of the i64
655 // in it, and store the other half to the stack. If we have two or more
656 // free GPRs, then we can pass both halves of the i64 in registers.
657 if (GPR_remaining > 0) {
658 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
659 Args[i].first, DAG.getConstant(1, MVT::i32));
660 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
661 Args[i].first, DAG.getConstant(0, MVT::i32));
662 args_to_use.push_back(Hi);
663 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000664 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000665 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000666 --GPR_remaining;
667 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000668 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
669 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000670 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000671 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000672 }
Chris Lattner915fb302005-08-30 00:19:00 +0000673 } else {
674 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
675 Args[i].first, PtrOff,
676 DAG.getSrcValue(NULL)));
677 }
678 ArgOffset += 8;
679 break;
680 case MVT::f32:
681 case MVT::f64:
682 if (FPR_remaining > 0) {
683 args_to_use.push_back(Args[i].first);
684 --FPR_remaining;
685 if (isVarArg) {
686 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
687 Args[i].first, PtrOff,
688 DAG.getSrcValue(NULL));
689 MemOps.push_back(Store);
690 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000691 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000692 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
693 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000694 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000695 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000696 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000697 }
698 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000699 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
700 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000701 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
702 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000703 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000704 args_to_use.push_back(Load);
705 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000706 }
707 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000708 // If we have any FPRs remaining, we may also have GPRs remaining.
709 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
710 // GPRs.
711 if (GPR_remaining > 0) {
712 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
713 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000714 }
Chris Lattner915fb302005-08-30 00:19:00 +0000715 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
716 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
717 --GPR_remaining;
718 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000719 }
Chris Lattner915fb302005-08-30 00:19:00 +0000720 } else {
721 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
722 Args[i].first, PtrOff,
723 DAG.getSrcValue(NULL)));
724 }
725 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
726 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000727 }
728 }
729 if (!MemOps.empty())
730 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
731 }
732
733 std::vector<MVT::ValueType> RetVals;
734 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000735 MVT::ValueType ActualRetTyVT = RetTyVT;
736 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
737 ActualRetTyVT = MVT::i32; // Promote result to i32.
738
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000739 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000740 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000741 RetVals.push_back(MVT::Other);
742
Chris Lattner2823b3e2005-11-17 05:56:14 +0000743 // If the callee is a GlobalAddress node (quite common, every direct call is)
744 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
745 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
746 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
747
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000748 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
749 Chain, Callee, args_to_use), 0);
750 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
751 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
752 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000753 SDOperand RetVal = TheCall;
754
755 // If the result is a small value, add a note so that we keep track of the
756 // information about whether it is sign or zero extended.
757 if (RetTyVT != ActualRetTyVT) {
758 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
759 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
760 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
761 }
762
763 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000764}
765
Nate Begeman4a959452005-10-18 23:23:37 +0000766SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
767 SelectionDAG &DAG) {
768 if (Op.getValueType() == MVT::i64) {
769 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
770 DAG.getConstant(1, MVT::i32));
771 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
772 DAG.getConstant(0, MVT::i32));
773 return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
774 } else {
775 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
776 }
777}
778
Nate Begeman21e463b2005-10-16 05:39:50 +0000779SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
780 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000781 // vastart just stores the address of the VarArgsFrameIndex slot into the
782 // memory location argument.
783 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
784 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
785 DAG.getSrcValue(VAListV));
786}
787
788std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000789PPCTargetLowering::LowerVAArg(SDOperand Chain,
790 SDOperand VAListP, Value *VAListV,
791 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000792 MVT::ValueType ArgVT = getValueType(ArgTy);
793
794 SDOperand VAList =
795 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
796 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
797 unsigned Amt;
798 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
799 Amt = 4;
800 else {
801 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
802 "Other types should have been promoted for varargs!");
803 Amt = 8;
804 }
805 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
806 DAG.getConstant(Amt, VAList.getValueType()));
807 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
808 VAList, VAListP, DAG.getSrcValue(VAListV));
809 return std::make_pair(Result, Chain);
810}
811
812
Nate Begeman21e463b2005-10-16 05:39:50 +0000813std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000814LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
815 SelectionDAG &DAG) {
816 assert(0 && "LowerFrameReturnAddress unimplemented");
817 abort();
818}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000819
820MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000821PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
822 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000823 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000824 MI->getOpcode() == PPC::SELECT_CC_F4 ||
825 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000826 "Unexpected instr type to insert");
827
828 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
829 // control-flow pattern. The incoming instruction knows the destination vreg
830 // to set, the condition code register to branch on, the true/false values to
831 // select between, and a branch opcode to use.
832 const BasicBlock *LLVM_BB = BB->getBasicBlock();
833 ilist<MachineBasicBlock>::iterator It = BB;
834 ++It;
835
836 // thisMBB:
837 // ...
838 // TrueVal = ...
839 // cmpTY ccX, r1, r2
840 // bCC copy1MBB
841 // fallthrough --> copy0MBB
842 MachineBasicBlock *thisMBB = BB;
843 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
844 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
845 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
846 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
847 MachineFunction *F = BB->getParent();
848 F->getBasicBlockList().insert(It, copy0MBB);
849 F->getBasicBlockList().insert(It, sinkMBB);
850 // Update machine-CFG edges
851 BB->addSuccessor(copy0MBB);
852 BB->addSuccessor(sinkMBB);
853
854 // copy0MBB:
855 // %FalseValue = ...
856 // # fallthrough to sinkMBB
857 BB = copy0MBB;
858
859 // Update machine-CFG edges
860 BB->addSuccessor(sinkMBB);
861
862 // sinkMBB:
863 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
864 // ...
865 BB = sinkMBB;
866 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
867 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
868 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
869
870 delete MI; // The pseudo instruction is gone now.
871 return BB;
872}
873