blob: 0e5000e37fbce07cc1628e30d20b36fd0fc08866 [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
Nate Begeman28a6b022005-12-10 02:36:00 +000097 // We want to legalize GlobalAddress and ConstantPool nodes into the
98 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +000099 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000100 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattner860e8862005-11-17 07:30:41 +0000101
Nate Begemanc09eeec2005-09-06 22:03:27 +0000102 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000103 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000104 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
105 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000106 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
107 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
108 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000109 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000110 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000111 }
112
113 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
114 // 64 bit PowerPC implementations can support i64 types directly
115 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000116 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
117 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000118 } else {
119 // 32 bit PowerPC wants to expand i64 shifts itself.
120 setOperationAction(ISD::SHL, MVT::i64, Custom);
121 setOperationAction(ISD::SRL, MVT::i64, Custom);
122 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000123 }
124
Nate Begeman425a9692005-11-29 08:17:20 +0000125 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
126 // FIXME: AltiVec supports a wide variety of packed types. For now, we're
127 // bringing up support with just v4f32.
128 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000129 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000130 }
131
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000132 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000133
134 computeRegisterProperties();
135}
136
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000137/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
138static bool isFloatingPointZero(SDOperand Op) {
139 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
140 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
141 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
142 // Maybe this has already been legalized into the constant pool?
143 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
144 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
145 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
146 }
147 return false;
148}
149
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000150/// LowerOperation - Provide custom lowering hooks for some operations.
151///
Nate Begeman21e463b2005-10-16 05:39:50 +0000152SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000153 switch (Op.getOpcode()) {
154 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000155 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000156 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000157 SDOperand Src = Op.getOperand(0);
158 if (Src.getValueType() == MVT::f32)
159 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
160
Nate Begemanc09eeec2005-09-06 22:03:27 +0000161 switch (Op.getValueType()) {
162 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
163 case MVT::i32:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000164 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000165 break;
166 case MVT::i64:
Chris Lattner7c0d6642005-10-02 06:37:13 +0000167 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000168 break;
169 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000170
171 int FrameIdx =
172 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
173 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
174 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
175 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000176 if (Op.getOpcode() == PPCISD::FCTIDZ) {
177 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
178 } else {
179 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
180 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
181 }
182 return Op;
183 }
184 case ISD::SINT_TO_FP: {
185 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
186 "Unhandled SINT_TO_FP type in custom expander!");
187 int FrameIdx =
188 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
189 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
190 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
191 Op.getOperand(0), FI, DAG.getSrcValue(0));
192 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
193 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
194 if (MVT::f32 == Op.getValueType())
195 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
196 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000197 }
198 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000199 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000200 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
201 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
202 break;
203
204 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
205
206 // Cannot handle SETEQ/SETNE.
207 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
208
209 MVT::ValueType ResVT = Op.getValueType();
210 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
211 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
212 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000213
Chris Lattnerf7605322005-08-31 21:09:52 +0000214 // If the RHS of the comparison is a 0.0, we don't need to do the
215 // subtraction at all.
216 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000217 switch (CC) {
218 default: assert(0 && "Invalid FSEL condition"); abort();
219 case ISD::SETULT:
220 case ISD::SETLT:
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::SETUGE:
223 case ISD::SETGE:
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 Lattnerf7605322005-08-31 21:09:52 +0000226 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000227 case ISD::SETUGT:
228 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000229 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000230 case ISD::SETULE:
231 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000232 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
233 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000234 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000235 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000236 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000237
Chris Lattnereb255f22005-10-25 20:54:57 +0000238 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000239 switch (CC) {
240 default: assert(0 && "Invalid FSEL condition"); abort();
241 case ISD::SETULT:
242 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000243 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
244 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
245 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
246 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000247 case ISD::SETUGE:
248 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000249 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
250 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
251 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
252 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000253 case ISD::SETUGT:
254 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000255 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
256 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
257 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
258 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000259 case ISD::SETULE:
260 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000261 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
262 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
263 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
264 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000265 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000266 break;
267 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000268 case ISD::SHL: {
269 assert(Op.getValueType() == MVT::i64 &&
270 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
271 // The generic code does a fine job expanding shift by a constant.
272 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
273
274 // Otherwise, expand into a bunch of logical ops. Note that these ops
275 // depend on the PPC behavior for oversized shift amounts.
276 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
277 DAG.getConstant(0, MVT::i32));
278 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
279 DAG.getConstant(1, MVT::i32));
280 SDOperand Amt = Op.getOperand(1);
281
282 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
283 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000284 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
285 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000286 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
287 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
288 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000289 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000290 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000291 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000292 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
293 }
294 case ISD::SRL: {
295 assert(Op.getValueType() == MVT::i64 &&
296 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
297 // The generic code does a fine job expanding shift by a constant.
298 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
299
300 // Otherwise, expand into a bunch of logical ops. Note that these ops
301 // depend on the PPC behavior for oversized shift amounts.
302 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
303 DAG.getConstant(0, MVT::i32));
304 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
305 DAG.getConstant(1, MVT::i32));
306 SDOperand Amt = Op.getOperand(1);
307
308 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
309 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000310 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
311 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000312 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
313 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
314 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000315 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000316 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000317 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000318 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
319 }
320 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000321 assert(Op.getValueType() == MVT::i64 &&
322 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
323 // The generic code does a fine job expanding shift by a constant.
324 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
325
326 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
327 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
328 DAG.getConstant(0, MVT::i32));
329 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
330 DAG.getConstant(1, MVT::i32));
331 SDOperand Amt = Op.getOperand(1);
332
333 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
334 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000335 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
336 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000337 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
338 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
339 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000340 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
341 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000342 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
343 Tmp4, Tmp6, ISD::SETLE);
344 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000345 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000346 case ISD::ConstantPool: {
347 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
348 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
349 SDOperand Zero = DAG.getConstant(0, MVT::i32);
350
351 if (PPCGenerateStaticCode) {
352 // Generate non-pic code that has direct accesses to the constant pool.
353 // The address of the global is just (hi(&g)+lo(&g)).
354 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
355 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
356 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
357 }
358
359 // Only lower ConstantPool on Darwin.
360 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
361 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
362 if (PICEnabled) {
363 // With PIC, the first instruction is actually "GR+hi(&G)".
364 Hi = DAG.getNode(ISD::ADD, MVT::i32,
365 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
366 }
367
368 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
369 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
370 return Lo;
371 }
Chris Lattner860e8862005-11-17 07:30:41 +0000372 case ISD::GlobalAddress: {
Chris Lattner860e8862005-11-17 07:30:41 +0000373 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
374 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
375 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000376
377 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000378 // Generate non-pic code that has direct accesses to globals.
379 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000380 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
381 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
382 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
383 }
Chris Lattner860e8862005-11-17 07:30:41 +0000384
Chris Lattner1d05cb42005-11-17 18:55:48 +0000385 // Only lower GlobalAddress on Darwin.
386 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000387 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
388 if (PICEnabled) {
389 // With PIC, the first instruction is actually "GR+hi(&G)".
390 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000391 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000392 }
393
394 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
395 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
396
397 if (!GV->hasWeakLinkage() && !GV->isExternal())
398 return Lo;
399
400 // If the global is weak or external, we have to go through the lazy
401 // resolution stub.
402 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
403 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000404 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000405 return SDOperand();
406}
407
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000408std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000409PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000410 //
411 // add beautiful description of PPC stack frame format, or at least some docs
412 //
413 MachineFunction &MF = DAG.getMachineFunction();
414 MachineFrameInfo *MFI = MF.getFrameInfo();
415 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000416 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000417 std::vector<SDOperand> ArgValues;
418
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000419 unsigned ArgOffset = 24;
420 unsigned GPR_remaining = 8;
421 unsigned FPR_remaining = 13;
422 unsigned GPR_idx = 0, FPR_idx = 0;
423 static const unsigned GPR[] = {
424 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
425 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
426 };
427 static const unsigned FPR[] = {
428 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
429 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
430 };
431
432 // Add DAG nodes to load the arguments... On entry to a function on PPC,
433 // the arguments start at offset 24, although they are likely to be passed
434 // in registers.
435 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
436 SDOperand newroot, argt;
437 unsigned ObjSize;
438 bool needsLoad = false;
439 bool ArgLive = !I->use_empty();
440 MVT::ValueType ObjectVT = getValueType(I->getType());
441
442 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000443 default: assert(0 && "Unhandled argument type!");
444 case MVT::i1:
445 case MVT::i8:
446 case MVT::i16:
447 case MVT::i32:
448 ObjSize = 4;
449 if (!ArgLive) break;
450 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000451 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000452 MF.addLiveIn(GPR[GPR_idx], VReg);
453 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000454 if (ObjectVT != MVT::i32) {
455 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
456 : ISD::AssertZext;
457 argt = DAG.getNode(AssertOp, MVT::i32, argt,
458 DAG.getValueType(ObjectVT));
459 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
460 }
Chris Lattner915fb302005-08-30 00:19:00 +0000461 } else {
462 needsLoad = true;
463 }
464 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000465 case MVT::i64:
466 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000467 if (!ArgLive) break;
468 if (GPR_remaining > 0) {
469 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000470 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000471 MF.addLiveIn(GPR[GPR_idx], VReg);
472 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000473 // If we have two or more remaining argument registers, then both halves
474 // of the i64 can be sourced from there. Otherwise, the lower half will
475 // have to come off the stack. This can happen when an i64 is preceded
476 // by 28 bytes of arguments.
477 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000478 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000479 MF.addLiveIn(GPR[GPR_idx+1], VReg);
480 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000481 } else {
482 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
483 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
484 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
485 DAG.getSrcValue(NULL));
486 }
487 // Build the outgoing arg thingy
488 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
489 newroot = argLo;
490 } else {
491 needsLoad = true;
492 }
493 break;
494 case MVT::f32:
495 case MVT::f64:
496 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
497 if (!ArgLive) break;
498 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000499 unsigned VReg;
500 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000501 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000502 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000503 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000504 MF.addLiveIn(FPR[FPR_idx], VReg);
505 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000506 --FPR_remaining;
507 ++FPR_idx;
508 } else {
509 needsLoad = true;
510 }
511 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000512 }
513
514 // We need to load the argument to a virtual register if we determined above
515 // that we ran out of physical registers of the appropriate type
516 if (needsLoad) {
517 unsigned SubregOffset = 0;
518 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
519 if (ObjectVT == MVT::i16) SubregOffset = 2;
520 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
521 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
522 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
523 DAG.getConstant(SubregOffset, MVT::i32));
524 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
525 DAG.getSrcValue(NULL));
526 }
527
528 // Every 4 bytes of argument space consumes one of the GPRs available for
529 // argument passing.
530 if (GPR_remaining > 0) {
531 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
532 GPR_remaining -= delta;
533 GPR_idx += delta;
534 }
535 ArgOffset += ObjSize;
536 if (newroot.Val)
537 DAG.setRoot(newroot.getValue(1));
538
539 ArgValues.push_back(argt);
540 }
541
542 // If the function takes variable number of arguments, make a frame index for
543 // the start of the first vararg value... for expansion of llvm.va_start.
544 if (F.isVarArg()) {
545 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
546 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
547 // If this function is vararg, store any remaining integer argument regs
548 // to their spots on the stack so that they may be loaded by deferencing the
549 // result of va_next.
550 std::vector<SDOperand> MemOps;
551 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000552 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000553 MF.addLiveIn(GPR[GPR_idx], VReg);
554 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000555 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
556 Val, FIN, DAG.getSrcValue(NULL));
557 MemOps.push_back(Store);
558 // Increment the address by four for the next argument to store
559 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
560 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
561 }
Chris Lattner80720a92005-11-30 20:40:54 +0000562 if (!MemOps.empty()) {
563 MemOps.push_back(DAG.getRoot());
564 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
565 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000566 }
567
568 // Finally, inform the code generator which regs we return values in.
569 switch (getValueType(F.getReturnType())) {
570 default: assert(0 && "Unknown type!");
571 case MVT::isVoid: break;
572 case MVT::i1:
573 case MVT::i8:
574 case MVT::i16:
575 case MVT::i32:
576 MF.addLiveOut(PPC::R3);
577 break;
578 case MVT::i64:
579 MF.addLiveOut(PPC::R3);
580 MF.addLiveOut(PPC::R4);
581 break;
582 case MVT::f32:
583 case MVT::f64:
584 MF.addLiveOut(PPC::F1);
585 break;
586 }
587
588 return ArgValues;
589}
590
591std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000592PPCTargetLowering::LowerCallTo(SDOperand Chain,
593 const Type *RetTy, bool isVarArg,
594 unsigned CallingConv, bool isTailCall,
595 SDOperand Callee, ArgListTy &Args,
596 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000597 // args_to_use will accumulate outgoing args for the ISD::CALL case in
598 // SelectExpr to use to put the arguments in the appropriate registers.
599 std::vector<SDOperand> args_to_use;
600
601 // Count how many bytes are to be pushed on the stack, including the linkage
602 // area, and parameter passing area.
603 unsigned NumBytes = 24;
604
605 if (Args.empty()) {
606 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
607 DAG.getConstant(NumBytes, getPointerTy()));
608 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000609 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000610 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000611 default: assert(0 && "Unknown value type!");
612 case MVT::i1:
613 case MVT::i8:
614 case MVT::i16:
615 case MVT::i32:
616 case MVT::f32:
617 NumBytes += 4;
618 break;
619 case MVT::i64:
620 case MVT::f64:
621 NumBytes += 8;
622 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000623 }
Chris Lattner915fb302005-08-30 00:19:00 +0000624 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000625
Chris Lattner915fb302005-08-30 00:19:00 +0000626 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
627 // plus 32 bytes of argument space in case any called code gets funky on us.
628 // (Required by ABI to support var arg)
629 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000630
631 // Adjust the stack pointer for the new arguments...
632 // These operations are automatically eliminated by the prolog/epilog pass
633 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
634 DAG.getConstant(NumBytes, getPointerTy()));
635
636 // Set up a copy of the stack pointer for use loading and storing any
637 // arguments that may not fit in the registers available for argument
638 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000639 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
640 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000641
642 // Figure out which arguments are going to go in registers, and which in
643 // memory. Also, if this is a vararg function, floating point operations
644 // must be stored to our stack, and loaded into integer regs as well, if
645 // any integer regs are available for argument passing.
646 unsigned ArgOffset = 24;
647 unsigned GPR_remaining = 8;
648 unsigned FPR_remaining = 13;
649
650 std::vector<SDOperand> MemOps;
651 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
652 // PtrOff will be used to store the current argument to the stack if a
653 // register cannot be found for it.
654 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
655 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
656 MVT::ValueType ArgVT = getValueType(Args[i].second);
657
658 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000659 default: assert(0 && "Unexpected ValueType for argument!");
660 case MVT::i1:
661 case MVT::i8:
662 case MVT::i16:
663 // Promote the integer to 32 bits. If the input type is signed use a
664 // sign extend, otherwise use a zero extend.
665 if (Args[i].second->isSigned())
666 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
667 else
668 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
669 // FALL THROUGH
670 case MVT::i32:
671 if (GPR_remaining > 0) {
672 args_to_use.push_back(Args[i].first);
673 --GPR_remaining;
674 } else {
675 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
676 Args[i].first, PtrOff,
677 DAG.getSrcValue(NULL)));
678 }
679 ArgOffset += 4;
680 break;
681 case MVT::i64:
682 // If we have one free GPR left, we can place the upper half of the i64
683 // in it, and store the other half to the stack. If we have two or more
684 // free GPRs, then we can pass both halves of the i64 in registers.
685 if (GPR_remaining > 0) {
686 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
687 Args[i].first, DAG.getConstant(1, MVT::i32));
688 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
689 Args[i].first, DAG.getConstant(0, MVT::i32));
690 args_to_use.push_back(Hi);
691 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000692 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000693 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000694 --GPR_remaining;
695 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000696 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
697 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000698 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000699 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000700 }
Chris Lattner915fb302005-08-30 00:19:00 +0000701 } else {
702 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
703 Args[i].first, PtrOff,
704 DAG.getSrcValue(NULL)));
705 }
706 ArgOffset += 8;
707 break;
708 case MVT::f32:
709 case MVT::f64:
710 if (FPR_remaining > 0) {
711 args_to_use.push_back(Args[i].first);
712 --FPR_remaining;
713 if (isVarArg) {
714 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
715 Args[i].first, PtrOff,
716 DAG.getSrcValue(NULL));
717 MemOps.push_back(Store);
718 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000719 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000720 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
721 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000722 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000723 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000724 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000725 }
726 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000727 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
728 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000729 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
730 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000731 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000732 args_to_use.push_back(Load);
733 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000734 }
735 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000736 // If we have any FPRs remaining, we may also have GPRs remaining.
737 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
738 // GPRs.
739 if (GPR_remaining > 0) {
740 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
741 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000742 }
Chris Lattner915fb302005-08-30 00:19:00 +0000743 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
744 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
745 --GPR_remaining;
746 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000747 }
Chris Lattner915fb302005-08-30 00:19:00 +0000748 } else {
749 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
750 Args[i].first, PtrOff,
751 DAG.getSrcValue(NULL)));
752 }
753 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
754 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000755 }
756 }
757 if (!MemOps.empty())
758 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
759 }
760
761 std::vector<MVT::ValueType> RetVals;
762 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000763 MVT::ValueType ActualRetTyVT = RetTyVT;
764 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
765 ActualRetTyVT = MVT::i32; // Promote result to i32.
766
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000767 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000768 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000769 RetVals.push_back(MVT::Other);
770
Chris Lattner2823b3e2005-11-17 05:56:14 +0000771 // If the callee is a GlobalAddress node (quite common, every direct call is)
772 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
773 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
774 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
775
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000776 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
777 Chain, Callee, args_to_use), 0);
778 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
779 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
780 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000781 SDOperand RetVal = TheCall;
782
783 // If the result is a small value, add a note so that we keep track of the
784 // information about whether it is sign or zero extended.
785 if (RetTyVT != ActualRetTyVT) {
786 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
787 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
788 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
789 }
790
791 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000792}
793
Nate Begeman4a959452005-10-18 23:23:37 +0000794SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
795 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000796 SDOperand Copy;
797 switch (Op.getValueType()) {
798 default: assert(0 && "Unknown type to return!");
799 case MVT::i32:
800 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
801 break;
802 case MVT::f32:
803 case MVT::f64:
804 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
805 break;
806 case MVT::i64:
807 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
808 DAG.getConstant(1, MVT::i32));
809 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
810 DAG.getConstant(0, MVT::i32));
811 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
812 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
813 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000814 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000815 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000816}
817
Nate Begeman21e463b2005-10-16 05:39:50 +0000818SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
819 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000820 // vastart just stores the address of the VarArgsFrameIndex slot into the
821 // memory location argument.
822 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
823 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
824 DAG.getSrcValue(VAListV));
825}
826
827std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000828PPCTargetLowering::LowerVAArg(SDOperand Chain,
829 SDOperand VAListP, Value *VAListV,
830 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000831 MVT::ValueType ArgVT = getValueType(ArgTy);
832
833 SDOperand VAList =
834 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
835 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
836 unsigned Amt;
837 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
838 Amt = 4;
839 else {
840 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
841 "Other types should have been promoted for varargs!");
842 Amt = 8;
843 }
844 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
845 DAG.getConstant(Amt, VAList.getValueType()));
846 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
847 VAList, VAListP, DAG.getSrcValue(VAListV));
848 return std::make_pair(Result, Chain);
849}
850
851
Nate Begeman21e463b2005-10-16 05:39:50 +0000852std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000853LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
854 SelectionDAG &DAG) {
855 assert(0 && "LowerFrameReturnAddress unimplemented");
856 abort();
857}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000858
859MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000860PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
861 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000862 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000863 MI->getOpcode() == PPC::SELECT_CC_F4 ||
864 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000865 "Unexpected instr type to insert");
866
867 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
868 // control-flow pattern. The incoming instruction knows the destination vreg
869 // to set, the condition code register to branch on, the true/false values to
870 // select between, and a branch opcode to use.
871 const BasicBlock *LLVM_BB = BB->getBasicBlock();
872 ilist<MachineBasicBlock>::iterator It = BB;
873 ++It;
874
875 // thisMBB:
876 // ...
877 // TrueVal = ...
878 // cmpTY ccX, r1, r2
879 // bCC copy1MBB
880 // fallthrough --> copy0MBB
881 MachineBasicBlock *thisMBB = BB;
882 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
883 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
884 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
885 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
886 MachineFunction *F = BB->getParent();
887 F->getBasicBlockList().insert(It, copy0MBB);
888 F->getBasicBlockList().insert(It, sinkMBB);
889 // Update machine-CFG edges
890 BB->addSuccessor(copy0MBB);
891 BB->addSuccessor(sinkMBB);
892
893 // copy0MBB:
894 // %FalseValue = ...
895 // # fallthrough to sinkMBB
896 BB = copy0MBB;
897
898 // Update machine-CFG edges
899 BB->addSuccessor(sinkMBB);
900
901 // sinkMBB:
902 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
903 // ...
904 BB = sinkMBB;
905 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
906 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
907 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
908
909 delete MI; // The pseudo instruction is gone now.
910 return BB;
911}
912