blob: 976f80fafa95f88e455db4adba0ee5cff664ff32 [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 Lattner53e88452005-12-23 05:13:35 +000091 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
92 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
93
Chris Lattnere6ec9f22005-09-10 00:21:06 +000094 // PowerPC does not have truncstore for i1.
95 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +000096
Jim Laskeyabf6d172006-01-05 01:25:28 +000097 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +000098 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +000099 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000100 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000101 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000102 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000103
Nate Begeman28a6b022005-12-10 02:36:00 +0000104 // We want to legalize GlobalAddress and ConstantPool nodes into the
105 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000106 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000107 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattner860e8862005-11-17 07:30:41 +0000108
Nate Begemanc09eeec2005-09-06 22:03:27 +0000109 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000110 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000111 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
112 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000113 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
114 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
115 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000116 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000117 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000118 }
119
120 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
121 // 64 bit PowerPC implementations can support i64 types directly
122 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000123 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
124 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000125 } else {
126 // 32 bit PowerPC wants to expand i64 shifts itself.
127 setOperationAction(ISD::SHL, MVT::i64, Custom);
128 setOperationAction(ISD::SRL, MVT::i64, Custom);
129 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000130 }
131
Nate Begeman425a9692005-11-29 08:17:20 +0000132 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000133 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000134 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000135 }
136
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000137 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000138
139 computeRegisterProperties();
140}
141
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000142const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
143 switch (Opcode) {
144 default: return 0;
145 case PPCISD::FSEL: return "PPCISD::FSEL";
146 case PPCISD::FCFID: return "PPCISD::FCFID";
147 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
148 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
149 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
150 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
151 case PPCISD::Hi: return "PPCISD::Hi";
152 case PPCISD::Lo: return "PPCISD::Lo";
153 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
154 case PPCISD::SRL: return "PPCISD::SRL";
155 case PPCISD::SRA: return "PPCISD::SRA";
156 case PPCISD::SHL: return "PPCISD::SHL";
157 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
158 }
159}
160
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000161/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
162static bool isFloatingPointZero(SDOperand Op) {
163 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
164 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
165 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
166 // Maybe this has already been legalized into the constant pool?
167 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
168 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
169 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
170 }
171 return false;
172}
173
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000174/// LowerOperation - Provide custom lowering hooks for some operations.
175///
Nate Begeman21e463b2005-10-16 05:39:50 +0000176SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000177 switch (Op.getOpcode()) {
178 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000179 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000180 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000181 SDOperand Src = Op.getOperand(0);
182 if (Src.getValueType() == MVT::f32)
183 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
184
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000185 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000186 switch (Op.getValueType()) {
187 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
188 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000189 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000190 break;
191 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000192 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000193 break;
194 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000195
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000196 // Convert the FP value to an int value through memory.
197 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
198 if (Op.getValueType() == MVT::i32)
199 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
200 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000201 }
202 case ISD::SINT_TO_FP: {
203 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
204 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000205 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
206 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000207 if (MVT::f32 == Op.getValueType())
208 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
209 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000210 }
211 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000212 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000213 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
214 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
215 break;
216
217 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
218
219 // Cannot handle SETEQ/SETNE.
220 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
221
222 MVT::ValueType ResVT = Op.getValueType();
223 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
224 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
225 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000226
Chris Lattnerf7605322005-08-31 21:09:52 +0000227 // If the RHS of the comparison is a 0.0, we don't need to do the
228 // subtraction at all.
229 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000230 switch (CC) {
231 default: assert(0 && "Invalid FSEL condition"); abort();
232 case ISD::SETULT:
233 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000234 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000235 case ISD::SETUGE:
236 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000237 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
238 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000239 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000240 case ISD::SETUGT:
241 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000242 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000243 case ISD::SETULE:
244 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000245 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
246 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000247 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000248 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000249 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000250
Chris Lattnereb255f22005-10-25 20:54:57 +0000251 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000252 switch (CC) {
253 default: assert(0 && "Invalid FSEL condition"); abort();
254 case ISD::SETULT:
255 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000256 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
257 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
258 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
259 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000260 case ISD::SETUGE:
261 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000262 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
263 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
264 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
265 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000266 case ISD::SETUGT:
267 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000268 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
269 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
270 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
271 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000272 case ISD::SETULE:
273 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000274 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
275 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
276 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
277 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000278 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000279 break;
280 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000281 case ISD::SHL: {
282 assert(Op.getValueType() == MVT::i64 &&
283 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
284 // The generic code does a fine job expanding shift by a constant.
285 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
286
287 // Otherwise, expand into a bunch of logical ops. Note that these ops
288 // depend on the PPC behavior for oversized shift amounts.
289 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
290 DAG.getConstant(0, MVT::i32));
291 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
292 DAG.getConstant(1, MVT::i32));
293 SDOperand Amt = Op.getOperand(1);
294
295 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
296 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000297 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
298 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000299 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
300 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
301 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000302 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000303 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000304 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000305 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
306 }
307 case ISD::SRL: {
308 assert(Op.getValueType() == MVT::i64 &&
309 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
310 // The generic code does a fine job expanding shift by a constant.
311 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
312
313 // Otherwise, expand into a bunch of logical ops. Note that these ops
314 // depend on the PPC behavior for oversized shift amounts.
315 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
316 DAG.getConstant(0, MVT::i32));
317 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
318 DAG.getConstant(1, MVT::i32));
319 SDOperand Amt = Op.getOperand(1);
320
321 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
322 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000323 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
324 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000325 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
326 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
327 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000328 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000329 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000330 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000331 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
332 }
333 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000334 assert(Op.getValueType() == MVT::i64 &&
335 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
336 // The generic code does a fine job expanding shift by a constant.
337 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
338
339 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
340 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
341 DAG.getConstant(0, MVT::i32));
342 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
343 DAG.getConstant(1, MVT::i32));
344 SDOperand Amt = Op.getOperand(1);
345
346 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
347 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000348 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
349 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000350 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
351 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
352 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000353 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
354 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000355 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
356 Tmp4, Tmp6, ISD::SETLE);
357 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000358 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000359 case ISD::ConstantPool: {
360 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
361 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
362 SDOperand Zero = DAG.getConstant(0, MVT::i32);
363
364 if (PPCGenerateStaticCode) {
365 // Generate non-pic code that has direct accesses to the constant pool.
366 // The address of the global is just (hi(&g)+lo(&g)).
367 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
368 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
369 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
370 }
371
372 // Only lower ConstantPool on Darwin.
373 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
374 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
375 if (PICEnabled) {
376 // With PIC, the first instruction is actually "GR+hi(&G)".
377 Hi = DAG.getNode(ISD::ADD, MVT::i32,
378 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
379 }
380
381 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
382 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
383 return Lo;
384 }
Chris Lattner860e8862005-11-17 07:30:41 +0000385 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000386 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
387 GlobalValue *GV = GSDN->getGlobal();
388 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000389 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000390
391 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000392 // Generate non-pic code that has direct accesses to globals.
393 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000394 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
395 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
396 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
397 }
Chris Lattner860e8862005-11-17 07:30:41 +0000398
Chris Lattner1d05cb42005-11-17 18:55:48 +0000399 // Only lower GlobalAddress on Darwin.
400 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000401
Chris Lattner860e8862005-11-17 07:30:41 +0000402 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
403 if (PICEnabled) {
404 // With PIC, the first instruction is actually "GR+hi(&G)".
405 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000406 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000407 }
408
409 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
410 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
411
Chris Lattnera35ef632006-01-06 01:04:03 +0000412 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000413 return Lo;
414
415 // If the global is weak or external, we have to go through the lazy
416 // resolution stub.
417 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
418 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000419 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000420 return SDOperand();
421}
422
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000423std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000424PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000425 //
426 // add beautiful description of PPC stack frame format, or at least some docs
427 //
428 MachineFunction &MF = DAG.getMachineFunction();
429 MachineFrameInfo *MFI = MF.getFrameInfo();
430 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000431 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000432 std::vector<SDOperand> ArgValues;
433
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000434 unsigned ArgOffset = 24;
435 unsigned GPR_remaining = 8;
436 unsigned FPR_remaining = 13;
437 unsigned GPR_idx = 0, FPR_idx = 0;
438 static const unsigned GPR[] = {
439 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
440 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
441 };
442 static const unsigned FPR[] = {
443 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
444 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
445 };
446
447 // Add DAG nodes to load the arguments... On entry to a function on PPC,
448 // the arguments start at offset 24, although they are likely to be passed
449 // in registers.
450 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
451 SDOperand newroot, argt;
452 unsigned ObjSize;
453 bool needsLoad = false;
454 bool ArgLive = !I->use_empty();
455 MVT::ValueType ObjectVT = getValueType(I->getType());
456
457 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000458 default: assert(0 && "Unhandled argument type!");
459 case MVT::i1:
460 case MVT::i8:
461 case MVT::i16:
462 case MVT::i32:
463 ObjSize = 4;
464 if (!ArgLive) break;
465 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000466 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000467 MF.addLiveIn(GPR[GPR_idx], VReg);
468 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000469 if (ObjectVT != MVT::i32) {
470 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
471 : ISD::AssertZext;
472 argt = DAG.getNode(AssertOp, MVT::i32, argt,
473 DAG.getValueType(ObjectVT));
474 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
475 }
Chris Lattner915fb302005-08-30 00:19:00 +0000476 } else {
477 needsLoad = true;
478 }
479 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000480 case MVT::i64:
481 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000482 if (!ArgLive) break;
483 if (GPR_remaining > 0) {
484 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000485 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000486 MF.addLiveIn(GPR[GPR_idx], VReg);
487 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000488 // If we have two or more remaining argument registers, then both halves
489 // of the i64 can be sourced from there. Otherwise, the lower half will
490 // have to come off the stack. This can happen when an i64 is preceded
491 // by 28 bytes of arguments.
492 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000493 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000494 MF.addLiveIn(GPR[GPR_idx+1], VReg);
495 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000496 } else {
497 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
498 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
499 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
500 DAG.getSrcValue(NULL));
501 }
502 // Build the outgoing arg thingy
503 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
504 newroot = argLo;
505 } else {
506 needsLoad = true;
507 }
508 break;
509 case MVT::f32:
510 case MVT::f64:
511 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000512 if (!ArgLive) {
513 if (FPR_remaining > 0) {
514 --FPR_remaining;
515 ++FPR_idx;
516 }
517 break;
518 }
Chris Lattner915fb302005-08-30 00:19:00 +0000519 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000520 unsigned VReg;
521 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000522 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000523 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000524 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000525 MF.addLiveIn(FPR[FPR_idx], VReg);
526 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000527 --FPR_remaining;
528 ++FPR_idx;
529 } else {
530 needsLoad = true;
531 }
532 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000533 }
534
535 // We need to load the argument to a virtual register if we determined above
536 // that we ran out of physical registers of the appropriate type
537 if (needsLoad) {
538 unsigned SubregOffset = 0;
539 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
540 if (ObjectVT == MVT::i16) SubregOffset = 2;
541 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
542 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
543 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
544 DAG.getConstant(SubregOffset, MVT::i32));
545 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
546 DAG.getSrcValue(NULL));
547 }
548
549 // Every 4 bytes of argument space consumes one of the GPRs available for
550 // argument passing.
551 if (GPR_remaining > 0) {
552 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
553 GPR_remaining -= delta;
554 GPR_idx += delta;
555 }
556 ArgOffset += ObjSize;
557 if (newroot.Val)
558 DAG.setRoot(newroot.getValue(1));
559
560 ArgValues.push_back(argt);
561 }
562
563 // If the function takes variable number of arguments, make a frame index for
564 // the start of the first vararg value... for expansion of llvm.va_start.
565 if (F.isVarArg()) {
566 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
567 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
568 // If this function is vararg, store any remaining integer argument regs
569 // to their spots on the stack so that they may be loaded by deferencing the
570 // result of va_next.
571 std::vector<SDOperand> MemOps;
572 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000573 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000574 MF.addLiveIn(GPR[GPR_idx], VReg);
575 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000576 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
577 Val, FIN, DAG.getSrcValue(NULL));
578 MemOps.push_back(Store);
579 // Increment the address by four for the next argument to store
580 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
581 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
582 }
Chris Lattner80720a92005-11-30 20:40:54 +0000583 if (!MemOps.empty()) {
584 MemOps.push_back(DAG.getRoot());
585 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
586 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000587 }
588
589 // Finally, inform the code generator which regs we return values in.
590 switch (getValueType(F.getReturnType())) {
591 default: assert(0 && "Unknown type!");
592 case MVT::isVoid: break;
593 case MVT::i1:
594 case MVT::i8:
595 case MVT::i16:
596 case MVT::i32:
597 MF.addLiveOut(PPC::R3);
598 break;
599 case MVT::i64:
600 MF.addLiveOut(PPC::R3);
601 MF.addLiveOut(PPC::R4);
602 break;
603 case MVT::f32:
604 case MVT::f64:
605 MF.addLiveOut(PPC::F1);
606 break;
607 }
608
609 return ArgValues;
610}
611
612std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000613PPCTargetLowering::LowerCallTo(SDOperand Chain,
614 const Type *RetTy, bool isVarArg,
615 unsigned CallingConv, bool isTailCall,
616 SDOperand Callee, ArgListTy &Args,
617 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000618 // args_to_use will accumulate outgoing args for the ISD::CALL case in
619 // SelectExpr to use to put the arguments in the appropriate registers.
620 std::vector<SDOperand> args_to_use;
621
622 // Count how many bytes are to be pushed on the stack, including the linkage
623 // area, and parameter passing area.
624 unsigned NumBytes = 24;
625
626 if (Args.empty()) {
627 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
628 DAG.getConstant(NumBytes, getPointerTy()));
629 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000630 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000631 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000632 default: assert(0 && "Unknown value type!");
633 case MVT::i1:
634 case MVT::i8:
635 case MVT::i16:
636 case MVT::i32:
637 case MVT::f32:
638 NumBytes += 4;
639 break;
640 case MVT::i64:
641 case MVT::f64:
642 NumBytes += 8;
643 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000644 }
Chris Lattner915fb302005-08-30 00:19:00 +0000645 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000646
Chris Lattner915fb302005-08-30 00:19:00 +0000647 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
648 // plus 32 bytes of argument space in case any called code gets funky on us.
649 // (Required by ABI to support var arg)
650 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000651
652 // Adjust the stack pointer for the new arguments...
653 // These operations are automatically eliminated by the prolog/epilog pass
654 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
655 DAG.getConstant(NumBytes, getPointerTy()));
656
657 // Set up a copy of the stack pointer for use loading and storing any
658 // arguments that may not fit in the registers available for argument
659 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000660 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
661 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000662
663 // Figure out which arguments are going to go in registers, and which in
664 // memory. Also, if this is a vararg function, floating point operations
665 // must be stored to our stack, and loaded into integer regs as well, if
666 // any integer regs are available for argument passing.
667 unsigned ArgOffset = 24;
668 unsigned GPR_remaining = 8;
669 unsigned FPR_remaining = 13;
670
671 std::vector<SDOperand> MemOps;
672 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
673 // PtrOff will be used to store the current argument to the stack if a
674 // register cannot be found for it.
675 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
676 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
677 MVT::ValueType ArgVT = getValueType(Args[i].second);
678
679 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000680 default: assert(0 && "Unexpected ValueType for argument!");
681 case MVT::i1:
682 case MVT::i8:
683 case MVT::i16:
684 // Promote the integer to 32 bits. If the input type is signed use a
685 // sign extend, otherwise use a zero extend.
686 if (Args[i].second->isSigned())
687 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
688 else
689 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
690 // FALL THROUGH
691 case MVT::i32:
692 if (GPR_remaining > 0) {
693 args_to_use.push_back(Args[i].first);
694 --GPR_remaining;
695 } else {
696 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
697 Args[i].first, PtrOff,
698 DAG.getSrcValue(NULL)));
699 }
700 ArgOffset += 4;
701 break;
702 case MVT::i64:
703 // If we have one free GPR left, we can place the upper half of the i64
704 // in it, and store the other half to the stack. If we have two or more
705 // free GPRs, then we can pass both halves of the i64 in registers.
706 if (GPR_remaining > 0) {
707 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
708 Args[i].first, DAG.getConstant(1, MVT::i32));
709 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
710 Args[i].first, DAG.getConstant(0, MVT::i32));
711 args_to_use.push_back(Hi);
712 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000713 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000714 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000715 --GPR_remaining;
716 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000717 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
718 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000719 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000720 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000721 }
Chris Lattner915fb302005-08-30 00:19:00 +0000722 } else {
723 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
724 Args[i].first, PtrOff,
725 DAG.getSrcValue(NULL)));
726 }
727 ArgOffset += 8;
728 break;
729 case MVT::f32:
730 case MVT::f64:
731 if (FPR_remaining > 0) {
732 args_to_use.push_back(Args[i].first);
733 --FPR_remaining;
734 if (isVarArg) {
735 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
736 Args[i].first, PtrOff,
737 DAG.getSrcValue(NULL));
738 MemOps.push_back(Store);
739 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000740 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000741 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
742 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000743 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000744 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000745 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000746 }
747 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000748 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
749 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000750 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
751 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000752 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000753 args_to_use.push_back(Load);
754 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000755 }
756 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000757 // If we have any FPRs remaining, we may also have GPRs remaining.
758 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
759 // GPRs.
760 if (GPR_remaining > 0) {
761 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
762 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000763 }
Chris Lattner915fb302005-08-30 00:19:00 +0000764 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
765 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
766 --GPR_remaining;
767 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000768 }
Chris Lattner915fb302005-08-30 00:19:00 +0000769 } else {
770 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
771 Args[i].first, PtrOff,
772 DAG.getSrcValue(NULL)));
773 }
774 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
775 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000776 }
777 }
778 if (!MemOps.empty())
779 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
780 }
781
782 std::vector<MVT::ValueType> RetVals;
783 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000784 MVT::ValueType ActualRetTyVT = RetTyVT;
785 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
786 ActualRetTyVT = MVT::i32; // Promote result to i32.
787
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000788 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000789 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000790 RetVals.push_back(MVT::Other);
791
Chris Lattner2823b3e2005-11-17 05:56:14 +0000792 // If the callee is a GlobalAddress node (quite common, every direct call is)
793 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
794 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
795 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
796
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000797 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
798 Chain, Callee, args_to_use), 0);
799 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
800 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
801 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000802 SDOperand RetVal = TheCall;
803
804 // If the result is a small value, add a note so that we keep track of the
805 // information about whether it is sign or zero extended.
806 if (RetTyVT != ActualRetTyVT) {
807 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
808 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
809 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
810 }
811
812 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000813}
814
Nate Begeman4a959452005-10-18 23:23:37 +0000815SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
816 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000817 SDOperand Copy;
818 switch (Op.getValueType()) {
819 default: assert(0 && "Unknown type to return!");
820 case MVT::i32:
821 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
822 break;
823 case MVT::f32:
824 case MVT::f64:
825 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
826 break;
827 case MVT::i64:
828 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
829 DAG.getConstant(1, MVT::i32));
830 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
831 DAG.getConstant(0, MVT::i32));
832 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
833 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
834 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000835 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000836 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000837}
838
Nate Begeman21e463b2005-10-16 05:39:50 +0000839SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
840 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000841 // vastart just stores the address of the VarArgsFrameIndex slot into the
842 // memory location argument.
843 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
844 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
845 DAG.getSrcValue(VAListV));
846}
847
848std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000849PPCTargetLowering::LowerVAArg(SDOperand Chain,
850 SDOperand VAListP, Value *VAListV,
851 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000852 MVT::ValueType ArgVT = getValueType(ArgTy);
853
854 SDOperand VAList =
855 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
856 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
857 unsigned Amt;
858 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
859 Amt = 4;
860 else {
861 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
862 "Other types should have been promoted for varargs!");
863 Amt = 8;
864 }
865 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
866 DAG.getConstant(Amt, VAList.getValueType()));
867 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
868 VAList, VAListP, DAG.getSrcValue(VAListV));
869 return std::make_pair(Result, Chain);
870}
871
872
Nate Begeman21e463b2005-10-16 05:39:50 +0000873std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000874LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
875 SelectionDAG &DAG) {
876 assert(0 && "LowerFrameReturnAddress unimplemented");
877 abort();
878}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000879
880MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000881PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
882 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000883 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000884 MI->getOpcode() == PPC::SELECT_CC_F4 ||
885 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000886 "Unexpected instr type to insert");
887
888 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
889 // control-flow pattern. The incoming instruction knows the destination vreg
890 // to set, the condition code register to branch on, the true/false values to
891 // select between, and a branch opcode to use.
892 const BasicBlock *LLVM_BB = BB->getBasicBlock();
893 ilist<MachineBasicBlock>::iterator It = BB;
894 ++It;
895
896 // thisMBB:
897 // ...
898 // TrueVal = ...
899 // cmpTY ccX, r1, r2
900 // bCC copy1MBB
901 // fallthrough --> copy0MBB
902 MachineBasicBlock *thisMBB = BB;
903 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
904 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
905 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
906 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
907 MachineFunction *F = BB->getParent();
908 F->getBasicBlockList().insert(It, copy0MBB);
909 F->getBasicBlockList().insert(It, sinkMBB);
910 // Update machine-CFG edges
911 BB->addSuccessor(copy0MBB);
912 BB->addSuccessor(sinkMBB);
913
914 // copy0MBB:
915 // %FalseValue = ...
916 // # fallthrough to sinkMBB
917 BB = copy0MBB;
918
919 // Update machine-CFG edges
920 BB->addSuccessor(sinkMBB);
921
922 // sinkMBB:
923 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
924 // ...
925 BB = sinkMBB;
926 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
927 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
928 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
929
930 delete MI; // The pseudo instruction is gone now.
931 return BB;
932}
933