blob: 1ef1d3e9d0e7be1db78288e3d075fdd715fb67ce [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;
437 case MVT::i64: ObjSize = 8;
438 if (!ArgLive) break;
439 if (GPR_remaining > 0) {
440 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000441 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000442 MF.addLiveIn(GPR[GPR_idx], VReg);
443 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000444 // If we have two or more remaining argument registers, then both halves
445 // of the i64 can be sourced from there. Otherwise, the lower half will
446 // have to come off the stack. This can happen when an i64 is preceded
447 // by 28 bytes of arguments.
448 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000449 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000450 MF.addLiveIn(GPR[GPR_idx+1], VReg);
451 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000452 } else {
453 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
454 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
455 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
456 DAG.getSrcValue(NULL));
457 }
458 // Build the outgoing arg thingy
459 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
460 newroot = argLo;
461 } else {
462 needsLoad = true;
463 }
464 break;
465 case MVT::f32:
466 case MVT::f64:
467 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
468 if (!ArgLive) break;
469 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000470 unsigned VReg;
471 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000472 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000473 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000474 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000475 MF.addLiveIn(FPR[FPR_idx], VReg);
476 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000477 --FPR_remaining;
478 ++FPR_idx;
479 } else {
480 needsLoad = true;
481 }
482 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000483 }
484
485 // We need to load the argument to a virtual register if we determined above
486 // that we ran out of physical registers of the appropriate type
487 if (needsLoad) {
488 unsigned SubregOffset = 0;
489 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
490 if (ObjectVT == MVT::i16) SubregOffset = 2;
491 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
492 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
493 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
494 DAG.getConstant(SubregOffset, MVT::i32));
495 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
496 DAG.getSrcValue(NULL));
497 }
498
499 // Every 4 bytes of argument space consumes one of the GPRs available for
500 // argument passing.
501 if (GPR_remaining > 0) {
502 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
503 GPR_remaining -= delta;
504 GPR_idx += delta;
505 }
506 ArgOffset += ObjSize;
507 if (newroot.Val)
508 DAG.setRoot(newroot.getValue(1));
509
510 ArgValues.push_back(argt);
511 }
512
513 // If the function takes variable number of arguments, make a frame index for
514 // the start of the first vararg value... for expansion of llvm.va_start.
515 if (F.isVarArg()) {
516 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
517 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
518 // If this function is vararg, store any remaining integer argument regs
519 // to their spots on the stack so that they may be loaded by deferencing the
520 // result of va_next.
521 std::vector<SDOperand> MemOps;
522 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000523 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000524 MF.addLiveIn(GPR[GPR_idx], VReg);
525 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000526 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
527 Val, FIN, DAG.getSrcValue(NULL));
528 MemOps.push_back(Store);
529 // Increment the address by four for the next argument to store
530 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
531 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
532 }
533 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
534 }
535
536 // Finally, inform the code generator which regs we return values in.
537 switch (getValueType(F.getReturnType())) {
538 default: assert(0 && "Unknown type!");
539 case MVT::isVoid: break;
540 case MVT::i1:
541 case MVT::i8:
542 case MVT::i16:
543 case MVT::i32:
544 MF.addLiveOut(PPC::R3);
545 break;
546 case MVT::i64:
547 MF.addLiveOut(PPC::R3);
548 MF.addLiveOut(PPC::R4);
549 break;
550 case MVT::f32:
551 case MVT::f64:
552 MF.addLiveOut(PPC::F1);
553 break;
554 }
555
556 return ArgValues;
557}
558
559std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000560PPCTargetLowering::LowerCallTo(SDOperand Chain,
561 const Type *RetTy, bool isVarArg,
562 unsigned CallingConv, bool isTailCall,
563 SDOperand Callee, ArgListTy &Args,
564 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000565 // args_to_use will accumulate outgoing args for the ISD::CALL case in
566 // SelectExpr to use to put the arguments in the appropriate registers.
567 std::vector<SDOperand> args_to_use;
568
569 // Count how many bytes are to be pushed on the stack, including the linkage
570 // area, and parameter passing area.
571 unsigned NumBytes = 24;
572
573 if (Args.empty()) {
574 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
575 DAG.getConstant(NumBytes, getPointerTy()));
576 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000577 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000578 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000579 default: assert(0 && "Unknown value type!");
580 case MVT::i1:
581 case MVT::i8:
582 case MVT::i16:
583 case MVT::i32:
584 case MVT::f32:
585 NumBytes += 4;
586 break;
587 case MVT::i64:
588 case MVT::f64:
589 NumBytes += 8;
590 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000591 }
Chris Lattner915fb302005-08-30 00:19:00 +0000592 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000593
Chris Lattner915fb302005-08-30 00:19:00 +0000594 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
595 // plus 32 bytes of argument space in case any called code gets funky on us.
596 // (Required by ABI to support var arg)
597 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000598
599 // Adjust the stack pointer for the new arguments...
600 // These operations are automatically eliminated by the prolog/epilog pass
601 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
602 DAG.getConstant(NumBytes, getPointerTy()));
603
604 // Set up a copy of the stack pointer for use loading and storing any
605 // arguments that may not fit in the registers available for argument
606 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000607 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
608 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000609
610 // Figure out which arguments are going to go in registers, and which in
611 // memory. Also, if this is a vararg function, floating point operations
612 // must be stored to our stack, and loaded into integer regs as well, if
613 // any integer regs are available for argument passing.
614 unsigned ArgOffset = 24;
615 unsigned GPR_remaining = 8;
616 unsigned FPR_remaining = 13;
617
618 std::vector<SDOperand> MemOps;
619 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
620 // PtrOff will be used to store the current argument to the stack if a
621 // register cannot be found for it.
622 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
623 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
624 MVT::ValueType ArgVT = getValueType(Args[i].second);
625
626 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000627 default: assert(0 && "Unexpected ValueType for argument!");
628 case MVT::i1:
629 case MVT::i8:
630 case MVT::i16:
631 // Promote the integer to 32 bits. If the input type is signed use a
632 // sign extend, otherwise use a zero extend.
633 if (Args[i].second->isSigned())
634 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
635 else
636 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
637 // FALL THROUGH
638 case MVT::i32:
639 if (GPR_remaining > 0) {
640 args_to_use.push_back(Args[i].first);
641 --GPR_remaining;
642 } else {
643 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
644 Args[i].first, PtrOff,
645 DAG.getSrcValue(NULL)));
646 }
647 ArgOffset += 4;
648 break;
649 case MVT::i64:
650 // If we have one free GPR left, we can place the upper half of the i64
651 // in it, and store the other half to the stack. If we have two or more
652 // free GPRs, then we can pass both halves of the i64 in registers.
653 if (GPR_remaining > 0) {
654 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
655 Args[i].first, DAG.getConstant(1, MVT::i32));
656 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
657 Args[i].first, DAG.getConstant(0, MVT::i32));
658 args_to_use.push_back(Hi);
659 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000660 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000661 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000662 --GPR_remaining;
663 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000664 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
665 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000666 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000667 Lo, PtrOff, DAG.getSrcValue(NULL)));
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 += 8;
675 break;
676 case MVT::f32:
677 case MVT::f64:
678 if (FPR_remaining > 0) {
679 args_to_use.push_back(Args[i].first);
680 --FPR_remaining;
681 if (isVarArg) {
682 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
683 Args[i].first, PtrOff,
684 DAG.getSrcValue(NULL));
685 MemOps.push_back(Store);
686 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000687 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000688 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
689 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000690 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000691 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000692 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000693 }
694 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000695 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
696 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000697 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
698 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000699 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000700 args_to_use.push_back(Load);
701 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000702 }
703 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000704 // If we have any FPRs remaining, we may also have GPRs remaining.
705 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
706 // GPRs.
707 if (GPR_remaining > 0) {
708 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
709 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000710 }
Chris Lattner915fb302005-08-30 00:19:00 +0000711 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
712 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
713 --GPR_remaining;
714 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000715 }
Chris Lattner915fb302005-08-30 00:19:00 +0000716 } else {
717 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
718 Args[i].first, PtrOff,
719 DAG.getSrcValue(NULL)));
720 }
721 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
722 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000723 }
724 }
725 if (!MemOps.empty())
726 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
727 }
728
729 std::vector<MVT::ValueType> RetVals;
730 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000731 MVT::ValueType ActualRetTyVT = RetTyVT;
732 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
733 ActualRetTyVT = MVT::i32; // Promote result to i32.
734
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000735 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000736 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000737 RetVals.push_back(MVT::Other);
738
Chris Lattner2823b3e2005-11-17 05:56:14 +0000739 // If the callee is a GlobalAddress node (quite common, every direct call is)
740 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
741 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
742 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
743
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000744 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
745 Chain, Callee, args_to_use), 0);
746 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
747 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
748 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000749 SDOperand RetVal = TheCall;
750
751 // If the result is a small value, add a note so that we keep track of the
752 // information about whether it is sign or zero extended.
753 if (RetTyVT != ActualRetTyVT) {
754 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
755 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
756 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
757 }
758
759 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000760}
761
Nate Begeman4a959452005-10-18 23:23:37 +0000762SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
763 SelectionDAG &DAG) {
764 if (Op.getValueType() == MVT::i64) {
765 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
766 DAG.getConstant(1, MVT::i32));
767 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
768 DAG.getConstant(0, MVT::i32));
769 return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
770 } else {
771 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
772 }
773}
774
Nate Begeman21e463b2005-10-16 05:39:50 +0000775SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
776 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000777 // vastart just stores the address of the VarArgsFrameIndex slot into the
778 // memory location argument.
779 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
780 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
781 DAG.getSrcValue(VAListV));
782}
783
784std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000785PPCTargetLowering::LowerVAArg(SDOperand Chain,
786 SDOperand VAListP, Value *VAListV,
787 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000788 MVT::ValueType ArgVT = getValueType(ArgTy);
789
790 SDOperand VAList =
791 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
792 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
793 unsigned Amt;
794 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
795 Amt = 4;
796 else {
797 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
798 "Other types should have been promoted for varargs!");
799 Amt = 8;
800 }
801 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
802 DAG.getConstant(Amt, VAList.getValueType()));
803 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
804 VAList, VAListP, DAG.getSrcValue(VAListV));
805 return std::make_pair(Result, Chain);
806}
807
808
Nate Begeman21e463b2005-10-16 05:39:50 +0000809std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000810LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
811 SelectionDAG &DAG) {
812 assert(0 && "LowerFrameReturnAddress unimplemented");
813 abort();
814}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000815
816MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000817PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
818 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000819 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000820 MI->getOpcode() == PPC::SELECT_CC_F4 ||
821 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000822 "Unexpected instr type to insert");
823
824 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
825 // control-flow pattern. The incoming instruction knows the destination vreg
826 // to set, the condition code register to branch on, the true/false values to
827 // select between, and a branch opcode to use.
828 const BasicBlock *LLVM_BB = BB->getBasicBlock();
829 ilist<MachineBasicBlock>::iterator It = BB;
830 ++It;
831
832 // thisMBB:
833 // ...
834 // TrueVal = ...
835 // cmpTY ccX, r1, r2
836 // bCC copy1MBB
837 // fallthrough --> copy0MBB
838 MachineBasicBlock *thisMBB = BB;
839 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
840 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
841 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
842 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
843 MachineFunction *F = BB->getParent();
844 F->getBasicBlockList().insert(It, copy0MBB);
845 F->getBasicBlockList().insert(It, sinkMBB);
846 // Update machine-CFG edges
847 BB->addSuccessor(copy0MBB);
848 BB->addSuccessor(sinkMBB);
849
850 // copy0MBB:
851 // %FalseValue = ...
852 // # fallthrough to sinkMBB
853 BB = copy0MBB;
854
855 // Update machine-CFG edges
856 BB->addSuccessor(sinkMBB);
857
858 // sinkMBB:
859 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
860 // ...
861 BB = sinkMBB;
862 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
863 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
864 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
865
866 delete MI; // The pseudo instruction is gone now.
867 return BB;
868}
869