blob: cbaa283326ad8b63c6493e6f4a745fcd0ff1be3a [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
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000161 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000162 switch (Op.getValueType()) {
163 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
164 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000165 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000166 break;
167 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000168 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000169 break;
170 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000171
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000172 // Convert the FP value to an int value through memory.
173 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
174 if (Op.getValueType() == MVT::i32)
175 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
176 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000177 }
178 case ISD::SINT_TO_FP: {
179 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
180 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000181 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
182 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000183 if (MVT::f32 == Op.getValueType())
184 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
185 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000186 }
187 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000188 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000189 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
190 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
191 break;
192
193 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
194
195 // Cannot handle SETEQ/SETNE.
196 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
197
198 MVT::ValueType ResVT = Op.getValueType();
199 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
200 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
201 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000202
Chris Lattnerf7605322005-08-31 21:09:52 +0000203 // If the RHS of the comparison is a 0.0, we don't need to do the
204 // subtraction at all.
205 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000206 switch (CC) {
207 default: assert(0 && "Invalid FSEL condition"); abort();
208 case ISD::SETULT:
209 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000210 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000211 case ISD::SETUGE:
212 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000213 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
214 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000215 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000216 case ISD::SETUGT:
217 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000218 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000219 case ISD::SETULE:
220 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000221 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
222 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000223 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000224 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000225 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000226
Chris Lattnereb255f22005-10-25 20:54:57 +0000227 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000228 switch (CC) {
229 default: assert(0 && "Invalid FSEL condition"); abort();
230 case ISD::SETULT:
231 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000232 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
233 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
234 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
235 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000236 case ISD::SETUGE:
237 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000238 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
239 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
240 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
241 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000242 case ISD::SETUGT:
243 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000244 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
245 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
246 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
247 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000248 case ISD::SETULE:
249 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000250 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
251 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
252 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
253 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000254 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000255 break;
256 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000257 case ISD::SHL: {
258 assert(Op.getValueType() == MVT::i64 &&
259 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
260 // The generic code does a fine job expanding shift by a constant.
261 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
262
263 // Otherwise, expand into a bunch of logical ops. Note that these ops
264 // depend on the PPC behavior for oversized shift amounts.
265 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
266 DAG.getConstant(0, MVT::i32));
267 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
268 DAG.getConstant(1, MVT::i32));
269 SDOperand Amt = Op.getOperand(1);
270
271 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
272 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000273 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
274 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000275 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
276 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
277 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000278 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000279 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000280 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000281 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
282 }
283 case ISD::SRL: {
284 assert(Op.getValueType() == MVT::i64 &&
285 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
286 // The generic code does a fine job expanding shift by a constant.
287 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
288
289 // Otherwise, expand into a bunch of logical ops. Note that these ops
290 // depend on the PPC behavior for oversized shift amounts.
291 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
292 DAG.getConstant(0, MVT::i32));
293 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
294 DAG.getConstant(1, MVT::i32));
295 SDOperand Amt = Op.getOperand(1);
296
297 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
298 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000299 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
300 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000301 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
302 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
303 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000304 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000305 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000306 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000307 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
308 }
309 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000310 assert(Op.getValueType() == MVT::i64 &&
311 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
312 // The generic code does a fine job expanding shift by a constant.
313 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
314
315 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
316 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
317 DAG.getConstant(0, MVT::i32));
318 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
319 DAG.getConstant(1, MVT::i32));
320 SDOperand Amt = Op.getOperand(1);
321
322 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
323 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000324 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
325 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000326 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
327 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
328 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000329 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
330 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000331 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
332 Tmp4, Tmp6, ISD::SETLE);
333 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000334 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000335 case ISD::ConstantPool: {
336 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
337 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
338 SDOperand Zero = DAG.getConstant(0, MVT::i32);
339
340 if (PPCGenerateStaticCode) {
341 // Generate non-pic code that has direct accesses to the constant pool.
342 // The address of the global is just (hi(&g)+lo(&g)).
343 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
344 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
345 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
346 }
347
348 // Only lower ConstantPool on Darwin.
349 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
350 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
351 if (PICEnabled) {
352 // With PIC, the first instruction is actually "GR+hi(&G)".
353 Hi = DAG.getNode(ISD::ADD, MVT::i32,
354 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
355 }
356
357 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
358 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
359 return Lo;
360 }
Chris Lattner860e8862005-11-17 07:30:41 +0000361 case ISD::GlobalAddress: {
Chris Lattner860e8862005-11-17 07:30:41 +0000362 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
363 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
364 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000365
366 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000367 // Generate non-pic code that has direct accesses to globals.
368 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000369 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
370 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
371 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
372 }
Chris Lattner860e8862005-11-17 07:30:41 +0000373
Chris Lattner1d05cb42005-11-17 18:55:48 +0000374 // Only lower GlobalAddress on Darwin.
375 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000376 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
377 if (PICEnabled) {
378 // With PIC, the first instruction is actually "GR+hi(&G)".
379 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000380 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000381 }
382
383 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
384 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
385
386 if (!GV->hasWeakLinkage() && !GV->isExternal())
387 return Lo;
388
389 // If the global is weak or external, we have to go through the lazy
390 // resolution stub.
391 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
392 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000393 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000394 return SDOperand();
395}
396
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000397std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000398PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000399 //
400 // add beautiful description of PPC stack frame format, or at least some docs
401 //
402 MachineFunction &MF = DAG.getMachineFunction();
403 MachineFrameInfo *MFI = MF.getFrameInfo();
404 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000405 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000406 std::vector<SDOperand> ArgValues;
407
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000408 unsigned ArgOffset = 24;
409 unsigned GPR_remaining = 8;
410 unsigned FPR_remaining = 13;
411 unsigned GPR_idx = 0, FPR_idx = 0;
412 static const unsigned GPR[] = {
413 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
414 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
415 };
416 static const unsigned FPR[] = {
417 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
418 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
419 };
420
421 // Add DAG nodes to load the arguments... On entry to a function on PPC,
422 // the arguments start at offset 24, although they are likely to be passed
423 // in registers.
424 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
425 SDOperand newroot, argt;
426 unsigned ObjSize;
427 bool needsLoad = false;
428 bool ArgLive = !I->use_empty();
429 MVT::ValueType ObjectVT = getValueType(I->getType());
430
431 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000432 default: assert(0 && "Unhandled argument type!");
433 case MVT::i1:
434 case MVT::i8:
435 case MVT::i16:
436 case MVT::i32:
437 ObjSize = 4;
438 if (!ArgLive) break;
439 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000440 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000441 MF.addLiveIn(GPR[GPR_idx], VReg);
442 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000443 if (ObjectVT != MVT::i32) {
444 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
445 : ISD::AssertZext;
446 argt = DAG.getNode(AssertOp, MVT::i32, argt,
447 DAG.getValueType(ObjectVT));
448 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
449 }
Chris Lattner915fb302005-08-30 00:19:00 +0000450 } else {
451 needsLoad = true;
452 }
453 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000454 case MVT::i64:
455 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000456 if (!ArgLive) break;
457 if (GPR_remaining > 0) {
458 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000459 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000460 MF.addLiveIn(GPR[GPR_idx], VReg);
461 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000462 // If we have two or more remaining argument registers, then both halves
463 // of the i64 can be sourced from there. Otherwise, the lower half will
464 // have to come off the stack. This can happen when an i64 is preceded
465 // by 28 bytes of arguments.
466 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000467 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000468 MF.addLiveIn(GPR[GPR_idx+1], VReg);
469 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000470 } else {
471 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
472 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
473 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
474 DAG.getSrcValue(NULL));
475 }
476 // Build the outgoing arg thingy
477 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
478 newroot = argLo;
479 } else {
480 needsLoad = true;
481 }
482 break;
483 case MVT::f32:
484 case MVT::f64:
485 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
486 if (!ArgLive) break;
487 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000488 unsigned VReg;
489 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000490 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000491 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000492 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000493 MF.addLiveIn(FPR[FPR_idx], VReg);
494 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000495 --FPR_remaining;
496 ++FPR_idx;
497 } else {
498 needsLoad = true;
499 }
500 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000501 }
502
503 // We need to load the argument to a virtual register if we determined above
504 // that we ran out of physical registers of the appropriate type
505 if (needsLoad) {
506 unsigned SubregOffset = 0;
507 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
508 if (ObjectVT == MVT::i16) SubregOffset = 2;
509 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
510 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
511 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
512 DAG.getConstant(SubregOffset, MVT::i32));
513 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
514 DAG.getSrcValue(NULL));
515 }
516
517 // Every 4 bytes of argument space consumes one of the GPRs available for
518 // argument passing.
519 if (GPR_remaining > 0) {
520 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
521 GPR_remaining -= delta;
522 GPR_idx += delta;
523 }
524 ArgOffset += ObjSize;
525 if (newroot.Val)
526 DAG.setRoot(newroot.getValue(1));
527
528 ArgValues.push_back(argt);
529 }
530
531 // If the function takes variable number of arguments, make a frame index for
532 // the start of the first vararg value... for expansion of llvm.va_start.
533 if (F.isVarArg()) {
534 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
535 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
536 // If this function is vararg, store any remaining integer argument regs
537 // to their spots on the stack so that they may be loaded by deferencing the
538 // result of va_next.
539 std::vector<SDOperand> MemOps;
540 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000541 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000542 MF.addLiveIn(GPR[GPR_idx], VReg);
543 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000544 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
545 Val, FIN, DAG.getSrcValue(NULL));
546 MemOps.push_back(Store);
547 // Increment the address by four for the next argument to store
548 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
549 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
550 }
Chris Lattner80720a92005-11-30 20:40:54 +0000551 if (!MemOps.empty()) {
552 MemOps.push_back(DAG.getRoot());
553 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
554 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000555 }
556
557 // Finally, inform the code generator which regs we return values in.
558 switch (getValueType(F.getReturnType())) {
559 default: assert(0 && "Unknown type!");
560 case MVT::isVoid: break;
561 case MVT::i1:
562 case MVT::i8:
563 case MVT::i16:
564 case MVT::i32:
565 MF.addLiveOut(PPC::R3);
566 break;
567 case MVT::i64:
568 MF.addLiveOut(PPC::R3);
569 MF.addLiveOut(PPC::R4);
570 break;
571 case MVT::f32:
572 case MVT::f64:
573 MF.addLiveOut(PPC::F1);
574 break;
575 }
576
577 return ArgValues;
578}
579
580std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000581PPCTargetLowering::LowerCallTo(SDOperand Chain,
582 const Type *RetTy, bool isVarArg,
583 unsigned CallingConv, bool isTailCall,
584 SDOperand Callee, ArgListTy &Args,
585 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000586 // args_to_use will accumulate outgoing args for the ISD::CALL case in
587 // SelectExpr to use to put the arguments in the appropriate registers.
588 std::vector<SDOperand> args_to_use;
589
590 // Count how many bytes are to be pushed on the stack, including the linkage
591 // area, and parameter passing area.
592 unsigned NumBytes = 24;
593
594 if (Args.empty()) {
595 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
596 DAG.getConstant(NumBytes, getPointerTy()));
597 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000598 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000599 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000600 default: assert(0 && "Unknown value type!");
601 case MVT::i1:
602 case MVT::i8:
603 case MVT::i16:
604 case MVT::i32:
605 case MVT::f32:
606 NumBytes += 4;
607 break;
608 case MVT::i64:
609 case MVT::f64:
610 NumBytes += 8;
611 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000612 }
Chris Lattner915fb302005-08-30 00:19:00 +0000613 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000614
Chris Lattner915fb302005-08-30 00:19:00 +0000615 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
616 // plus 32 bytes of argument space in case any called code gets funky on us.
617 // (Required by ABI to support var arg)
618 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000619
620 // Adjust the stack pointer for the new arguments...
621 // These operations are automatically eliminated by the prolog/epilog pass
622 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
623 DAG.getConstant(NumBytes, getPointerTy()));
624
625 // Set up a copy of the stack pointer for use loading and storing any
626 // arguments that may not fit in the registers available for argument
627 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000628 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
629 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000630
631 // Figure out which arguments are going to go in registers, and which in
632 // memory. Also, if this is a vararg function, floating point operations
633 // must be stored to our stack, and loaded into integer regs as well, if
634 // any integer regs are available for argument passing.
635 unsigned ArgOffset = 24;
636 unsigned GPR_remaining = 8;
637 unsigned FPR_remaining = 13;
638
639 std::vector<SDOperand> MemOps;
640 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
641 // PtrOff will be used to store the current argument to the stack if a
642 // register cannot be found for it.
643 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
644 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
645 MVT::ValueType ArgVT = getValueType(Args[i].second);
646
647 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000648 default: assert(0 && "Unexpected ValueType for argument!");
649 case MVT::i1:
650 case MVT::i8:
651 case MVT::i16:
652 // Promote the integer to 32 bits. If the input type is signed use a
653 // sign extend, otherwise use a zero extend.
654 if (Args[i].second->isSigned())
655 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
656 else
657 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
658 // FALL THROUGH
659 case MVT::i32:
660 if (GPR_remaining > 0) {
661 args_to_use.push_back(Args[i].first);
662 --GPR_remaining;
663 } else {
664 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
665 Args[i].first, PtrOff,
666 DAG.getSrcValue(NULL)));
667 }
668 ArgOffset += 4;
669 break;
670 case MVT::i64:
671 // If we have one free GPR left, we can place the upper half of the i64
672 // in it, and store the other half to the stack. If we have two or more
673 // free GPRs, then we can pass both halves of the i64 in registers.
674 if (GPR_remaining > 0) {
675 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
676 Args[i].first, DAG.getConstant(1, MVT::i32));
677 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
678 Args[i].first, DAG.getConstant(0, MVT::i32));
679 args_to_use.push_back(Hi);
680 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000681 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000682 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000683 --GPR_remaining;
684 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000685 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
686 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000687 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000688 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000689 }
Chris Lattner915fb302005-08-30 00:19:00 +0000690 } else {
691 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
692 Args[i].first, PtrOff,
693 DAG.getSrcValue(NULL)));
694 }
695 ArgOffset += 8;
696 break;
697 case MVT::f32:
698 case MVT::f64:
699 if (FPR_remaining > 0) {
700 args_to_use.push_back(Args[i].first);
701 --FPR_remaining;
702 if (isVarArg) {
703 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
704 Args[i].first, PtrOff,
705 DAG.getSrcValue(NULL));
706 MemOps.push_back(Store);
707 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000708 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000709 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
710 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000711 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000712 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000713 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000714 }
715 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000716 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
717 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000718 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
719 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000720 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000721 args_to_use.push_back(Load);
722 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000723 }
724 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000725 // If we have any FPRs remaining, we may also have GPRs remaining.
726 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
727 // GPRs.
728 if (GPR_remaining > 0) {
729 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
730 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000731 }
Chris Lattner915fb302005-08-30 00:19:00 +0000732 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
733 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
734 --GPR_remaining;
735 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000736 }
Chris Lattner915fb302005-08-30 00:19:00 +0000737 } else {
738 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
739 Args[i].first, PtrOff,
740 DAG.getSrcValue(NULL)));
741 }
742 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
743 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000744 }
745 }
746 if (!MemOps.empty())
747 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
748 }
749
750 std::vector<MVT::ValueType> RetVals;
751 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000752 MVT::ValueType ActualRetTyVT = RetTyVT;
753 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
754 ActualRetTyVT = MVT::i32; // Promote result to i32.
755
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000756 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000757 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000758 RetVals.push_back(MVT::Other);
759
Chris Lattner2823b3e2005-11-17 05:56:14 +0000760 // If the callee is a GlobalAddress node (quite common, every direct call is)
761 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
762 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
763 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
764
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000765 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
766 Chain, Callee, args_to_use), 0);
767 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
768 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
769 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000770 SDOperand RetVal = TheCall;
771
772 // If the result is a small value, add a note so that we keep track of the
773 // information about whether it is sign or zero extended.
774 if (RetTyVT != ActualRetTyVT) {
775 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
776 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
777 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
778 }
779
780 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000781}
782
Nate Begeman4a959452005-10-18 23:23:37 +0000783SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
784 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000785 SDOperand Copy;
786 switch (Op.getValueType()) {
787 default: assert(0 && "Unknown type to return!");
788 case MVT::i32:
789 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
790 break;
791 case MVT::f32:
792 case MVT::f64:
793 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
794 break;
795 case MVT::i64:
796 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
797 DAG.getConstant(1, MVT::i32));
798 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
799 DAG.getConstant(0, MVT::i32));
800 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
801 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
802 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000803 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000804 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000805}
806
Nate Begeman21e463b2005-10-16 05:39:50 +0000807SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
808 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000809 // vastart just stores the address of the VarArgsFrameIndex slot into the
810 // memory location argument.
811 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
812 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
813 DAG.getSrcValue(VAListV));
814}
815
816std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000817PPCTargetLowering::LowerVAArg(SDOperand Chain,
818 SDOperand VAListP, Value *VAListV,
819 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000820 MVT::ValueType ArgVT = getValueType(ArgTy);
821
822 SDOperand VAList =
823 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
824 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
825 unsigned Amt;
826 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
827 Amt = 4;
828 else {
829 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
830 "Other types should have been promoted for varargs!");
831 Amt = 8;
832 }
833 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
834 DAG.getConstant(Amt, VAList.getValueType()));
835 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
836 VAList, VAListP, DAG.getSrcValue(VAListV));
837 return std::make_pair(Result, Chain);
838}
839
840
Nate Begeman21e463b2005-10-16 05:39:50 +0000841std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000842LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
843 SelectionDAG &DAG) {
844 assert(0 && "LowerFrameReturnAddress unimplemented");
845 abort();
846}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000847
848MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000849PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
850 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000851 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000852 MI->getOpcode() == PPC::SELECT_CC_F4 ||
853 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000854 "Unexpected instr type to insert");
855
856 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
857 // control-flow pattern. The incoming instruction knows the destination vreg
858 // to set, the condition code register to branch on, the true/false values to
859 // select between, and a branch opcode to use.
860 const BasicBlock *LLVM_BB = BB->getBasicBlock();
861 ilist<MachineBasicBlock>::iterator It = BB;
862 ++It;
863
864 // thisMBB:
865 // ...
866 // TrueVal = ...
867 // cmpTY ccX, r1, r2
868 // bCC copy1MBB
869 // fallthrough --> copy0MBB
870 MachineBasicBlock *thisMBB = BB;
871 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
872 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
873 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
874 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
875 MachineFunction *F = BB->getParent();
876 F->getBasicBlockList().insert(It, copy0MBB);
877 F->getBasicBlockList().insert(It, sinkMBB);
878 // Update machine-CFG edges
879 BB->addSuccessor(copy0MBB);
880 BB->addSuccessor(sinkMBB);
881
882 // copy0MBB:
883 // %FalseValue = ...
884 // # fallthrough to sinkMBB
885 BB = copy0MBB;
886
887 // Update machine-CFG edges
888 BB->addSuccessor(sinkMBB);
889
890 // sinkMBB:
891 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
892 // ...
893 BB = sinkMBB;
894 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
895 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
896 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
897
898 delete MI; // The pseudo instruction is gone now.
899 return BB;
900}
901