blob: d7ffbdc9e89e21b74d1c2301306b3510c7c55c69 [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
Nate Begeman35ef9132006-01-11 21:21:00 +000071 // PowerPC does not have ROTR
72 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
73
Chris Lattner7c5a3d32005-08-16 17:14:42 +000074 // PowerPC does not have Select
75 setOperationAction(ISD::SELECT, MVT::i32, Expand);
76 setOperationAction(ISD::SELECT, MVT::f32, Expand);
77 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000078
Chris Lattner0b1e4e52005-08-26 17:36:52 +000079 // PowerPC wants to turn select_cc of FP into fsel when possible.
80 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
81 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000082
Nate Begeman7cbd5252005-08-16 19:49:35 +000083 // PowerPC does not have BRCOND* which requires SetCC
84 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
85 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000086
Chris Lattnerf7605322005-08-31 21:09:52 +000087 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
88 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000089
Jim Laskeyad23c9d2005-08-17 00:40:22 +000090 // PowerPC does not have [U|S]INT_TO_FP
91 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
92 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
93
Chris Lattner53e88452005-12-23 05:13:35 +000094 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
95 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
96
Chris Lattnere6ec9f22005-09-10 00:21:06 +000097 // PowerPC does not have truncstore for i1.
98 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +000099
Jim Laskeyabf6d172006-01-05 01:25:28 +0000100 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000101 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000102 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000103 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000104 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000105 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000106
Nate Begeman28a6b022005-12-10 02:36:00 +0000107 // We want to legalize GlobalAddress and ConstantPool nodes into the
108 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000109 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000110 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000111
112 // Not implemented yet.
113 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
114 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000115
Nate Begemanc09eeec2005-09-06 22:03:27 +0000116 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000117 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000118 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
119 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000120 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
121 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
122 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000123 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000124 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000125 }
126
127 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
128 // 64 bit PowerPC implementations can support i64 types directly
129 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000130 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
131 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000132 } else {
133 // 32 bit PowerPC wants to expand i64 shifts itself.
134 setOperationAction(ISD::SHL, MVT::i64, Custom);
135 setOperationAction(ISD::SRL, MVT::i64, Custom);
136 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000137 }
138
Nate Begeman425a9692005-11-29 08:17:20 +0000139 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000140 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000141 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000142 }
143
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000144 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000145
146 computeRegisterProperties();
147}
148
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000149const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
150 switch (Opcode) {
151 default: return 0;
152 case PPCISD::FSEL: return "PPCISD::FSEL";
153 case PPCISD::FCFID: return "PPCISD::FCFID";
154 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
155 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
156 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
157 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
158 case PPCISD::Hi: return "PPCISD::Hi";
159 case PPCISD::Lo: return "PPCISD::Lo";
160 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
161 case PPCISD::SRL: return "PPCISD::SRL";
162 case PPCISD::SRA: return "PPCISD::SRA";
163 case PPCISD::SHL: return "PPCISD::SHL";
164 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
165 }
166}
167
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000168/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
169static bool isFloatingPointZero(SDOperand Op) {
170 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
171 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
172 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
173 // Maybe this has already been legalized into the constant pool?
174 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
175 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
176 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
177 }
178 return false;
179}
180
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000181/// LowerOperation - Provide custom lowering hooks for some operations.
182///
Nate Begeman21e463b2005-10-16 05:39:50 +0000183SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000184 switch (Op.getOpcode()) {
185 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000186 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000187 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000188 SDOperand Src = Op.getOperand(0);
189 if (Src.getValueType() == MVT::f32)
190 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
191
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000192 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000193 switch (Op.getValueType()) {
194 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
195 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000196 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000197 break;
198 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000199 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000200 break;
201 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000202
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000203 // Convert the FP value to an int value through memory.
204 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
205 if (Op.getValueType() == MVT::i32)
206 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
207 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000208 }
209 case ISD::SINT_TO_FP: {
210 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
211 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000212 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
213 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000214 if (MVT::f32 == Op.getValueType())
215 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
216 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000217 }
218 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000219 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000220 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
221 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
222 break;
223
224 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
225
226 // Cannot handle SETEQ/SETNE.
227 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
228
229 MVT::ValueType ResVT = Op.getValueType();
230 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
231 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
232 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000233
Chris Lattnerf7605322005-08-31 21:09:52 +0000234 // If the RHS of the comparison is a 0.0, we don't need to do the
235 // subtraction at all.
236 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000237 switch (CC) {
238 default: assert(0 && "Invalid FSEL condition"); abort();
239 case ISD::SETULT:
240 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000241 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000242 case ISD::SETUGE:
243 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000244 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
245 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000246 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000247 case ISD::SETUGT:
248 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000249 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000250 case ISD::SETULE:
251 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000252 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
253 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000254 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000255 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000256 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000257
Chris Lattnereb255f22005-10-25 20:54:57 +0000258 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000259 switch (CC) {
260 default: assert(0 && "Invalid FSEL condition"); abort();
261 case ISD::SETULT:
262 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000263 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
264 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
265 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
266 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000267 case ISD::SETUGE:
268 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000269 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
270 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
271 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
272 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000273 case ISD::SETUGT:
274 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000275 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
276 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
277 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
278 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000279 case ISD::SETULE:
280 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000281 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
282 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
283 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
284 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000285 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000286 break;
287 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000288 case ISD::SHL: {
289 assert(Op.getValueType() == MVT::i64 &&
290 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
291 // The generic code does a fine job expanding shift by a constant.
292 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
293
294 // Otherwise, expand into a bunch of logical ops. Note that these ops
295 // depend on the PPC behavior for oversized shift amounts.
296 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
297 DAG.getConstant(0, MVT::i32));
298 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
299 DAG.getConstant(1, MVT::i32));
300 SDOperand Amt = Op.getOperand(1);
301
302 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
303 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000304 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
305 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000306 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
307 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
308 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000309 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000310 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000311 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000312 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
313 }
314 case ISD::SRL: {
315 assert(Op.getValueType() == MVT::i64 &&
316 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
317 // The generic code does a fine job expanding shift by a constant.
318 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
319
320 // Otherwise, expand into a bunch of logical ops. Note that these ops
321 // depend on the PPC behavior for oversized shift amounts.
322 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
323 DAG.getConstant(0, MVT::i32));
324 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
325 DAG.getConstant(1, MVT::i32));
326 SDOperand Amt = Op.getOperand(1);
327
328 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
329 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000330 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
331 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000332 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
333 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
334 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000335 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000336 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000337 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000338 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
339 }
340 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000341 assert(Op.getValueType() == MVT::i64 &&
342 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
343 // The generic code does a fine job expanding shift by a constant.
344 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
345
346 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
347 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
348 DAG.getConstant(0, MVT::i32));
349 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
350 DAG.getConstant(1, MVT::i32));
351 SDOperand Amt = Op.getOperand(1);
352
353 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
354 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000355 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
356 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000357 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
358 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
359 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000360 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
361 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000362 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
363 Tmp4, Tmp6, ISD::SETLE);
364 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000365 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000366 case ISD::ConstantPool: {
367 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
368 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
369 SDOperand Zero = DAG.getConstant(0, MVT::i32);
370
371 if (PPCGenerateStaticCode) {
372 // Generate non-pic code that has direct accesses to the constant pool.
373 // The address of the global is just (hi(&g)+lo(&g)).
374 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
375 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
376 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
377 }
378
379 // Only lower ConstantPool on Darwin.
380 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
381 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
382 if (PICEnabled) {
383 // With PIC, the first instruction is actually "GR+hi(&G)".
384 Hi = DAG.getNode(ISD::ADD, MVT::i32,
385 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
386 }
387
388 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
389 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
390 return Lo;
391 }
Chris Lattner860e8862005-11-17 07:30:41 +0000392 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000393 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
394 GlobalValue *GV = GSDN->getGlobal();
395 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000396 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000397
398 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000399 // Generate non-pic code that has direct accesses to globals.
400 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000401 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
402 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
403 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
404 }
Chris Lattner860e8862005-11-17 07:30:41 +0000405
Chris Lattner1d05cb42005-11-17 18:55:48 +0000406 // Only lower GlobalAddress on Darwin.
407 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000408
Chris Lattner860e8862005-11-17 07:30:41 +0000409 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
410 if (PICEnabled) {
411 // With PIC, the first instruction is actually "GR+hi(&G)".
412 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000413 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000414 }
415
416 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
417 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
418
Chris Lattnera35ef632006-01-06 01:04:03 +0000419 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000420 return Lo;
421
422 // If the global is weak or external, we have to go through the lazy
423 // resolution stub.
424 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
425 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000426 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000427 return SDOperand();
428}
429
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000430std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000431PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000432 //
433 // add beautiful description of PPC stack frame format, or at least some docs
434 //
435 MachineFunction &MF = DAG.getMachineFunction();
436 MachineFrameInfo *MFI = MF.getFrameInfo();
437 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000438 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000439 std::vector<SDOperand> ArgValues;
440
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000441 unsigned ArgOffset = 24;
442 unsigned GPR_remaining = 8;
443 unsigned FPR_remaining = 13;
444 unsigned GPR_idx = 0, FPR_idx = 0;
445 static const unsigned GPR[] = {
446 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
447 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
448 };
449 static const unsigned FPR[] = {
450 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
451 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
452 };
453
454 // Add DAG nodes to load the arguments... On entry to a function on PPC,
455 // the arguments start at offset 24, although they are likely to be passed
456 // in registers.
457 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
458 SDOperand newroot, argt;
459 unsigned ObjSize;
460 bool needsLoad = false;
461 bool ArgLive = !I->use_empty();
462 MVT::ValueType ObjectVT = getValueType(I->getType());
463
464 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000465 default: assert(0 && "Unhandled argument type!");
466 case MVT::i1:
467 case MVT::i8:
468 case MVT::i16:
469 case MVT::i32:
470 ObjSize = 4;
471 if (!ArgLive) break;
472 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000473 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000474 MF.addLiveIn(GPR[GPR_idx], VReg);
475 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000476 if (ObjectVT != MVT::i32) {
477 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
478 : ISD::AssertZext;
479 argt = DAG.getNode(AssertOp, MVT::i32, argt,
480 DAG.getValueType(ObjectVT));
481 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
482 }
Chris Lattner915fb302005-08-30 00:19:00 +0000483 } else {
484 needsLoad = true;
485 }
486 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000487 case MVT::i64:
488 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000489 if (!ArgLive) break;
490 if (GPR_remaining > 0) {
491 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000492 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000493 MF.addLiveIn(GPR[GPR_idx], VReg);
494 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000495 // If we have two or more remaining argument registers, then both halves
496 // of the i64 can be sourced from there. Otherwise, the lower half will
497 // have to come off the stack. This can happen when an i64 is preceded
498 // by 28 bytes of arguments.
499 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000500 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000501 MF.addLiveIn(GPR[GPR_idx+1], VReg);
502 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000503 } else {
504 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
505 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
506 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
507 DAG.getSrcValue(NULL));
508 }
509 // Build the outgoing arg thingy
510 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
511 newroot = argLo;
512 } else {
513 needsLoad = true;
514 }
515 break;
516 case MVT::f32:
517 case MVT::f64:
518 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000519 if (!ArgLive) {
520 if (FPR_remaining > 0) {
521 --FPR_remaining;
522 ++FPR_idx;
523 }
524 break;
525 }
Chris Lattner915fb302005-08-30 00:19:00 +0000526 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000527 unsigned VReg;
528 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000529 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000530 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000531 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000532 MF.addLiveIn(FPR[FPR_idx], VReg);
533 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000534 --FPR_remaining;
535 ++FPR_idx;
536 } else {
537 needsLoad = true;
538 }
539 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000540 }
541
542 // We need to load the argument to a virtual register if we determined above
543 // that we ran out of physical registers of the appropriate type
544 if (needsLoad) {
545 unsigned SubregOffset = 0;
546 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
547 if (ObjectVT == MVT::i16) SubregOffset = 2;
548 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
549 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
550 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
551 DAG.getConstant(SubregOffset, MVT::i32));
552 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
553 DAG.getSrcValue(NULL));
554 }
555
556 // Every 4 bytes of argument space consumes one of the GPRs available for
557 // argument passing.
558 if (GPR_remaining > 0) {
559 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
560 GPR_remaining -= delta;
561 GPR_idx += delta;
562 }
563 ArgOffset += ObjSize;
564 if (newroot.Val)
565 DAG.setRoot(newroot.getValue(1));
566
567 ArgValues.push_back(argt);
568 }
569
570 // If the function takes variable number of arguments, make a frame index for
571 // the start of the first vararg value... for expansion of llvm.va_start.
572 if (F.isVarArg()) {
573 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
574 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
575 // If this function is vararg, store any remaining integer argument regs
576 // to their spots on the stack so that they may be loaded by deferencing the
577 // result of va_next.
578 std::vector<SDOperand> MemOps;
579 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000580 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000581 MF.addLiveIn(GPR[GPR_idx], VReg);
582 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000583 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
584 Val, FIN, DAG.getSrcValue(NULL));
585 MemOps.push_back(Store);
586 // Increment the address by four for the next argument to store
587 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
588 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
589 }
Chris Lattner80720a92005-11-30 20:40:54 +0000590 if (!MemOps.empty()) {
591 MemOps.push_back(DAG.getRoot());
592 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
593 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000594 }
595
596 // Finally, inform the code generator which regs we return values in.
597 switch (getValueType(F.getReturnType())) {
598 default: assert(0 && "Unknown type!");
599 case MVT::isVoid: break;
600 case MVT::i1:
601 case MVT::i8:
602 case MVT::i16:
603 case MVT::i32:
604 MF.addLiveOut(PPC::R3);
605 break;
606 case MVT::i64:
607 MF.addLiveOut(PPC::R3);
608 MF.addLiveOut(PPC::R4);
609 break;
610 case MVT::f32:
611 case MVT::f64:
612 MF.addLiveOut(PPC::F1);
613 break;
614 }
615
616 return ArgValues;
617}
618
619std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000620PPCTargetLowering::LowerCallTo(SDOperand Chain,
621 const Type *RetTy, bool isVarArg,
622 unsigned CallingConv, bool isTailCall,
623 SDOperand Callee, ArgListTy &Args,
624 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000625 // args_to_use will accumulate outgoing args for the ISD::CALL case in
626 // SelectExpr to use to put the arguments in the appropriate registers.
627 std::vector<SDOperand> args_to_use;
628
629 // Count how many bytes are to be pushed on the stack, including the linkage
630 // area, and parameter passing area.
631 unsigned NumBytes = 24;
632
633 if (Args.empty()) {
634 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
635 DAG.getConstant(NumBytes, getPointerTy()));
636 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000637 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000638 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000639 default: assert(0 && "Unknown value type!");
640 case MVT::i1:
641 case MVT::i8:
642 case MVT::i16:
643 case MVT::i32:
644 case MVT::f32:
645 NumBytes += 4;
646 break;
647 case MVT::i64:
648 case MVT::f64:
649 NumBytes += 8;
650 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000651 }
Chris Lattner915fb302005-08-30 00:19:00 +0000652 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000653
Chris Lattner915fb302005-08-30 00:19:00 +0000654 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
655 // plus 32 bytes of argument space in case any called code gets funky on us.
656 // (Required by ABI to support var arg)
657 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000658
659 // Adjust the stack pointer for the new arguments...
660 // These operations are automatically eliminated by the prolog/epilog pass
661 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
662 DAG.getConstant(NumBytes, getPointerTy()));
663
664 // Set up a copy of the stack pointer for use loading and storing any
665 // arguments that may not fit in the registers available for argument
666 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000667 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000668
669 // Figure out which arguments are going to go in registers, and which in
670 // memory. Also, if this is a vararg function, floating point operations
671 // must be stored to our stack, and loaded into integer regs as well, if
672 // any integer regs are available for argument passing.
673 unsigned ArgOffset = 24;
674 unsigned GPR_remaining = 8;
675 unsigned FPR_remaining = 13;
676
677 std::vector<SDOperand> MemOps;
678 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
679 // PtrOff will be used to store the current argument to the stack if a
680 // register cannot be found for it.
681 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
682 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
683 MVT::ValueType ArgVT = getValueType(Args[i].second);
684
685 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000686 default: assert(0 && "Unexpected ValueType for argument!");
687 case MVT::i1:
688 case MVT::i8:
689 case MVT::i16:
690 // Promote the integer to 32 bits. If the input type is signed use a
691 // sign extend, otherwise use a zero extend.
692 if (Args[i].second->isSigned())
693 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
694 else
695 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
696 // FALL THROUGH
697 case MVT::i32:
698 if (GPR_remaining > 0) {
699 args_to_use.push_back(Args[i].first);
700 --GPR_remaining;
701 } else {
702 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
703 Args[i].first, PtrOff,
704 DAG.getSrcValue(NULL)));
705 }
706 ArgOffset += 4;
707 break;
708 case MVT::i64:
709 // If we have one free GPR left, we can place the upper half of the i64
710 // in it, and store the other half to the stack. If we have two or more
711 // free GPRs, then we can pass both halves of the i64 in registers.
712 if (GPR_remaining > 0) {
713 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
714 Args[i].first, DAG.getConstant(1, MVT::i32));
715 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
716 Args[i].first, DAG.getConstant(0, MVT::i32));
717 args_to_use.push_back(Hi);
718 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000719 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000720 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000721 --GPR_remaining;
722 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000723 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
724 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000725 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000726 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000727 }
Chris Lattner915fb302005-08-30 00:19:00 +0000728 } else {
729 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
730 Args[i].first, PtrOff,
731 DAG.getSrcValue(NULL)));
732 }
733 ArgOffset += 8;
734 break;
735 case MVT::f32:
736 case MVT::f64:
737 if (FPR_remaining > 0) {
738 args_to_use.push_back(Args[i].first);
739 --FPR_remaining;
740 if (isVarArg) {
741 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
742 Args[i].first, PtrOff,
743 DAG.getSrcValue(NULL));
744 MemOps.push_back(Store);
745 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000746 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000747 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
748 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000749 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000750 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000751 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000752 }
753 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000754 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
755 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000756 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
757 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000758 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000759 args_to_use.push_back(Load);
760 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000761 }
762 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000763 // If we have any FPRs remaining, we may also have GPRs remaining.
764 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
765 // GPRs.
766 if (GPR_remaining > 0) {
767 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
768 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000769 }
Chris Lattner915fb302005-08-30 00:19:00 +0000770 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
771 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
772 --GPR_remaining;
773 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000774 }
Chris Lattner915fb302005-08-30 00:19:00 +0000775 } else {
776 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
777 Args[i].first, PtrOff,
778 DAG.getSrcValue(NULL)));
779 }
780 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
781 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000782 }
783 }
784 if (!MemOps.empty())
785 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
786 }
787
788 std::vector<MVT::ValueType> RetVals;
789 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000790 MVT::ValueType ActualRetTyVT = RetTyVT;
791 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
792 ActualRetTyVT = MVT::i32; // Promote result to i32.
793
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000794 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000795 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000796 RetVals.push_back(MVT::Other);
797
Chris Lattner2823b3e2005-11-17 05:56:14 +0000798 // If the callee is a GlobalAddress node (quite common, every direct call is)
799 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
800 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
801 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
802
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000803 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
804 Chain, Callee, args_to_use), 0);
805 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
806 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
807 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000808 SDOperand RetVal = TheCall;
809
810 // If the result is a small value, add a note so that we keep track of the
811 // information about whether it is sign or zero extended.
812 if (RetTyVT != ActualRetTyVT) {
813 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
814 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
815 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
816 }
817
818 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000819}
820
Nate Begeman4a959452005-10-18 23:23:37 +0000821SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
822 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000823 SDOperand Copy;
824 switch (Op.getValueType()) {
825 default: assert(0 && "Unknown type to return!");
826 case MVT::i32:
827 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
828 break;
829 case MVT::f32:
830 case MVT::f64:
831 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
832 break;
833 case MVT::i64:
834 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
835 DAG.getConstant(1, MVT::i32));
836 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
837 DAG.getConstant(0, MVT::i32));
838 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
839 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
840 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000841 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000842 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000843}
844
Nate Begeman21e463b2005-10-16 05:39:50 +0000845SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
846 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000847 // vastart just stores the address of the VarArgsFrameIndex slot into the
848 // memory location argument.
849 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
850 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
851 DAG.getSrcValue(VAListV));
852}
853
854std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000855PPCTargetLowering::LowerVAArg(SDOperand Chain,
856 SDOperand VAListP, Value *VAListV,
857 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000858 MVT::ValueType ArgVT = getValueType(ArgTy);
859
860 SDOperand VAList =
861 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
862 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
863 unsigned Amt;
864 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
865 Amt = 4;
866 else {
867 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
868 "Other types should have been promoted for varargs!");
869 Amt = 8;
870 }
871 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
872 DAG.getConstant(Amt, VAList.getValueType()));
873 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
874 VAList, VAListP, DAG.getSrcValue(VAListV));
875 return std::make_pair(Result, Chain);
876}
877
878
Nate Begeman21e463b2005-10-16 05:39:50 +0000879std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000880LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
881 SelectionDAG &DAG) {
882 assert(0 && "LowerFrameReturnAddress unimplemented");
883 abort();
884}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000885
886MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000887PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
888 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000889 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000890 MI->getOpcode() == PPC::SELECT_CC_F4 ||
891 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000892 "Unexpected instr type to insert");
893
894 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
895 // control-flow pattern. The incoming instruction knows the destination vreg
896 // to set, the condition code register to branch on, the true/false values to
897 // select between, and a branch opcode to use.
898 const BasicBlock *LLVM_BB = BB->getBasicBlock();
899 ilist<MachineBasicBlock>::iterator It = BB;
900 ++It;
901
902 // thisMBB:
903 // ...
904 // TrueVal = ...
905 // cmpTY ccX, r1, r2
906 // bCC copy1MBB
907 // fallthrough --> copy0MBB
908 MachineBasicBlock *thisMBB = BB;
909 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
910 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
911 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
912 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
913 MachineFunction *F = BB->getParent();
914 F->getBasicBlockList().insert(It, copy0MBB);
915 F->getBasicBlockList().insert(It, sinkMBB);
916 // Update machine-CFG edges
917 BB->addSuccessor(copy0MBB);
918 BB->addSuccessor(sinkMBB);
919
920 // copy0MBB:
921 // %FalseValue = ...
922 // # fallthrough to sinkMBB
923 BB = copy0MBB;
924
925 // Update machine-CFG edges
926 BB->addSuccessor(sinkMBB);
927
928 // sinkMBB:
929 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
930 // ...
931 BB = sinkMBB;
932 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
933 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
934 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
935
936 delete MI; // The pseudo instruction is gone now.
937 return BB;
938}
939