blob: 8eb4ac303a7d68ae465b9f57375d742d6fe459c2 [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 Laskeyabf6d172006-01-05 01:25:28 +000099 // FIXME - use subtarget debug flags
100 if (TM.getSubtarget<PPCSubtarget>().isDarwin())
101 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000102
Nate Begeman28a6b022005-12-10 02:36:00 +0000103 // We want to legalize GlobalAddress and ConstantPool nodes into the
104 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000105 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000106 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattner860e8862005-11-17 07:30:41 +0000107
Nate Begemanc09eeec2005-09-06 22:03:27 +0000108 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000109 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000110 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
111 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000112 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
113 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
114 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000115 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000116 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000117 }
118
119 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
120 // 64 bit PowerPC implementations can support i64 types directly
121 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000122 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
123 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000124 } else {
125 // 32 bit PowerPC wants to expand i64 shifts itself.
126 setOperationAction(ISD::SHL, MVT::i64, Custom);
127 setOperationAction(ISD::SRL, MVT::i64, Custom);
128 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000129 }
130
Nate Begeman425a9692005-11-29 08:17:20 +0000131 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000132 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000133 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000134 }
135
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000136 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000137
138 computeRegisterProperties();
139}
140
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000141/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
142static bool isFloatingPointZero(SDOperand Op) {
143 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
144 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
145 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
146 // Maybe this has already been legalized into the constant pool?
147 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
148 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
149 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
150 }
151 return false;
152}
153
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000154/// LowerOperation - Provide custom lowering hooks for some operations.
155///
Nate Begeman21e463b2005-10-16 05:39:50 +0000156SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000157 switch (Op.getOpcode()) {
158 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000159 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000160 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000161 SDOperand Src = Op.getOperand(0);
162 if (Src.getValueType() == MVT::f32)
163 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
164
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000165 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000166 switch (Op.getValueType()) {
167 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
168 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000169 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000170 break;
171 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000172 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000173 break;
174 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000175
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000176 // Convert the FP value to an int value through memory.
177 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
178 if (Op.getValueType() == MVT::i32)
179 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
180 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000181 }
182 case ISD::SINT_TO_FP: {
183 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
184 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000185 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
186 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000187 if (MVT::f32 == Op.getValueType())
188 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
189 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000190 }
191 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000192 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000193 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
194 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
195 break;
196
197 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
198
199 // Cannot handle SETEQ/SETNE.
200 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
201
202 MVT::ValueType ResVT = Op.getValueType();
203 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
204 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
205 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000206
Chris Lattnerf7605322005-08-31 21:09:52 +0000207 // If the RHS of the comparison is a 0.0, we don't need to do the
208 // subtraction at all.
209 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000210 switch (CC) {
211 default: assert(0 && "Invalid FSEL condition"); abort();
212 case ISD::SETULT:
213 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000214 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000215 case ISD::SETUGE:
216 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000217 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
218 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000219 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000220 case ISD::SETUGT:
221 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000222 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000223 case ISD::SETULE:
224 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000225 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
226 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000227 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000228 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000229 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000230
Chris Lattnereb255f22005-10-25 20:54:57 +0000231 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000232 switch (CC) {
233 default: assert(0 && "Invalid FSEL condition"); abort();
234 case ISD::SETULT:
235 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000236 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
237 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
238 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
239 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000240 case ISD::SETUGE:
241 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000242 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
243 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
244 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
245 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000246 case ISD::SETUGT:
247 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000248 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
249 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
250 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
251 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000252 case ISD::SETULE:
253 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000254 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
255 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
256 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
257 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000258 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000259 break;
260 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000261 case ISD::SHL: {
262 assert(Op.getValueType() == MVT::i64 &&
263 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
264 // The generic code does a fine job expanding shift by a constant.
265 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
266
267 // Otherwise, expand into a bunch of logical ops. Note that these ops
268 // depend on the PPC behavior for oversized shift amounts.
269 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
270 DAG.getConstant(0, MVT::i32));
271 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
272 DAG.getConstant(1, MVT::i32));
273 SDOperand Amt = Op.getOperand(1);
274
275 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
276 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000277 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
278 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000279 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
280 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
281 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000282 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000283 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000284 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000285 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
286 }
287 case ISD::SRL: {
288 assert(Op.getValueType() == MVT::i64 &&
289 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
290 // The generic code does a fine job expanding shift by a constant.
291 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
292
293 // Otherwise, expand into a bunch of logical ops. Note that these ops
294 // depend on the PPC behavior for oversized shift amounts.
295 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
296 DAG.getConstant(0, MVT::i32));
297 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
298 DAG.getConstant(1, MVT::i32));
299 SDOperand Amt = Op.getOperand(1);
300
301 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
302 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000303 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
304 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000305 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
306 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
307 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000308 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000309 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000310 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000311 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
312 }
313 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000314 assert(Op.getValueType() == MVT::i64 &&
315 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
316 // The generic code does a fine job expanding shift by a constant.
317 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
318
319 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
320 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
321 DAG.getConstant(0, MVT::i32));
322 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
323 DAG.getConstant(1, MVT::i32));
324 SDOperand Amt = Op.getOperand(1);
325
326 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
327 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000328 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
329 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000330 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
331 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
332 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000333 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
334 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000335 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
336 Tmp4, Tmp6, ISD::SETLE);
337 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000338 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000339 case ISD::ConstantPool: {
340 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
341 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
342 SDOperand Zero = DAG.getConstant(0, MVT::i32);
343
344 if (PPCGenerateStaticCode) {
345 // Generate non-pic code that has direct accesses to the constant pool.
346 // The address of the global is just (hi(&g)+lo(&g)).
347 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
348 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
349 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
350 }
351
352 // Only lower ConstantPool on Darwin.
353 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
354 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
355 if (PICEnabled) {
356 // With PIC, the first instruction is actually "GR+hi(&G)".
357 Hi = DAG.getNode(ISD::ADD, MVT::i32,
358 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
359 }
360
361 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
362 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
363 return Lo;
364 }
Chris Lattner860e8862005-11-17 07:30:41 +0000365 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000366 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
367 GlobalValue *GV = GSDN->getGlobal();
368 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000369 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000370
371 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000372 // Generate non-pic code that has direct accesses to globals.
373 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000374 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
375 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
376 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
377 }
Chris Lattner860e8862005-11-17 07:30:41 +0000378
Chris Lattner1d05cb42005-11-17 18:55:48 +0000379 // Only lower GlobalAddress on Darwin.
380 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000381 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
382 if (PICEnabled) {
383 // With PIC, the first instruction is actually "GR+hi(&G)".
384 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000385 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000386 }
387
388 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
389 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
390
391 if (!GV->hasWeakLinkage() && !GV->isExternal())
392 return Lo;
393
394 // If the global is weak or external, we have to go through the lazy
395 // resolution stub.
396 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
397 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000398 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000399 return SDOperand();
400}
401
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000402std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000403PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000404 //
405 // add beautiful description of PPC stack frame format, or at least some docs
406 //
407 MachineFunction &MF = DAG.getMachineFunction();
408 MachineFrameInfo *MFI = MF.getFrameInfo();
409 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000410 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000411 std::vector<SDOperand> ArgValues;
412
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000413 unsigned ArgOffset = 24;
414 unsigned GPR_remaining = 8;
415 unsigned FPR_remaining = 13;
416 unsigned GPR_idx = 0, FPR_idx = 0;
417 static const unsigned GPR[] = {
418 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
419 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
420 };
421 static const unsigned FPR[] = {
422 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
423 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
424 };
425
426 // Add DAG nodes to load the arguments... On entry to a function on PPC,
427 // the arguments start at offset 24, although they are likely to be passed
428 // in registers.
429 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
430 SDOperand newroot, argt;
431 unsigned ObjSize;
432 bool needsLoad = false;
433 bool ArgLive = !I->use_empty();
434 MVT::ValueType ObjectVT = getValueType(I->getType());
435
436 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000437 default: assert(0 && "Unhandled argument type!");
438 case MVT::i1:
439 case MVT::i8:
440 case MVT::i16:
441 case MVT::i32:
442 ObjSize = 4;
443 if (!ArgLive) break;
444 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000445 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000446 MF.addLiveIn(GPR[GPR_idx], VReg);
447 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000448 if (ObjectVT != MVT::i32) {
449 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
450 : ISD::AssertZext;
451 argt = DAG.getNode(AssertOp, MVT::i32, argt,
452 DAG.getValueType(ObjectVT));
453 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
454 }
Chris Lattner915fb302005-08-30 00:19:00 +0000455 } else {
456 needsLoad = true;
457 }
458 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000459 case MVT::i64:
460 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000461 if (!ArgLive) break;
462 if (GPR_remaining > 0) {
463 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000464 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000465 MF.addLiveIn(GPR[GPR_idx], VReg);
466 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000467 // If we have two or more remaining argument registers, then both halves
468 // of the i64 can be sourced from there. Otherwise, the lower half will
469 // have to come off the stack. This can happen when an i64 is preceded
470 // by 28 bytes of arguments.
471 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000472 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000473 MF.addLiveIn(GPR[GPR_idx+1], VReg);
474 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000475 } else {
476 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
477 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
478 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
479 DAG.getSrcValue(NULL));
480 }
481 // Build the outgoing arg thingy
482 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
483 newroot = argLo;
484 } else {
485 needsLoad = true;
486 }
487 break;
488 case MVT::f32:
489 case MVT::f64:
490 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
491 if (!ArgLive) break;
492 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000493 unsigned VReg;
494 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000495 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000496 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000497 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000498 MF.addLiveIn(FPR[FPR_idx], VReg);
499 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000500 --FPR_remaining;
501 ++FPR_idx;
502 } else {
503 needsLoad = true;
504 }
505 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000506 }
507
508 // We need to load the argument to a virtual register if we determined above
509 // that we ran out of physical registers of the appropriate type
510 if (needsLoad) {
511 unsigned SubregOffset = 0;
512 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
513 if (ObjectVT == MVT::i16) SubregOffset = 2;
514 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
515 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
516 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
517 DAG.getConstant(SubregOffset, MVT::i32));
518 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
519 DAG.getSrcValue(NULL));
520 }
521
522 // Every 4 bytes of argument space consumes one of the GPRs available for
523 // argument passing.
524 if (GPR_remaining > 0) {
525 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
526 GPR_remaining -= delta;
527 GPR_idx += delta;
528 }
529 ArgOffset += ObjSize;
530 if (newroot.Val)
531 DAG.setRoot(newroot.getValue(1));
532
533 ArgValues.push_back(argt);
534 }
535
536 // If the function takes variable number of arguments, make a frame index for
537 // the start of the first vararg value... for expansion of llvm.va_start.
538 if (F.isVarArg()) {
539 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
540 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
541 // If this function is vararg, store any remaining integer argument regs
542 // to their spots on the stack so that they may be loaded by deferencing the
543 // result of va_next.
544 std::vector<SDOperand> MemOps;
545 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000546 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000547 MF.addLiveIn(GPR[GPR_idx], VReg);
548 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000549 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
550 Val, FIN, DAG.getSrcValue(NULL));
551 MemOps.push_back(Store);
552 // Increment the address by four for the next argument to store
553 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
554 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
555 }
Chris Lattner80720a92005-11-30 20:40:54 +0000556 if (!MemOps.empty()) {
557 MemOps.push_back(DAG.getRoot());
558 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
559 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000560 }
561
562 // Finally, inform the code generator which regs we return values in.
563 switch (getValueType(F.getReturnType())) {
564 default: assert(0 && "Unknown type!");
565 case MVT::isVoid: break;
566 case MVT::i1:
567 case MVT::i8:
568 case MVT::i16:
569 case MVT::i32:
570 MF.addLiveOut(PPC::R3);
571 break;
572 case MVT::i64:
573 MF.addLiveOut(PPC::R3);
574 MF.addLiveOut(PPC::R4);
575 break;
576 case MVT::f32:
577 case MVT::f64:
578 MF.addLiveOut(PPC::F1);
579 break;
580 }
581
582 return ArgValues;
583}
584
585std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000586PPCTargetLowering::LowerCallTo(SDOperand Chain,
587 const Type *RetTy, bool isVarArg,
588 unsigned CallingConv, bool isTailCall,
589 SDOperand Callee, ArgListTy &Args,
590 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000591 // args_to_use will accumulate outgoing args for the ISD::CALL case in
592 // SelectExpr to use to put the arguments in the appropriate registers.
593 std::vector<SDOperand> args_to_use;
594
595 // Count how many bytes are to be pushed on the stack, including the linkage
596 // area, and parameter passing area.
597 unsigned NumBytes = 24;
598
599 if (Args.empty()) {
600 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
601 DAG.getConstant(NumBytes, getPointerTy()));
602 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000603 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000604 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000605 default: assert(0 && "Unknown value type!");
606 case MVT::i1:
607 case MVT::i8:
608 case MVT::i16:
609 case MVT::i32:
610 case MVT::f32:
611 NumBytes += 4;
612 break;
613 case MVT::i64:
614 case MVT::f64:
615 NumBytes += 8;
616 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000617 }
Chris Lattner915fb302005-08-30 00:19:00 +0000618 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000619
Chris Lattner915fb302005-08-30 00:19:00 +0000620 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
621 // plus 32 bytes of argument space in case any called code gets funky on us.
622 // (Required by ABI to support var arg)
623 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000624
625 // Adjust the stack pointer for the new arguments...
626 // These operations are automatically eliminated by the prolog/epilog pass
627 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
628 DAG.getConstant(NumBytes, getPointerTy()));
629
630 // Set up a copy of the stack pointer for use loading and storing any
631 // arguments that may not fit in the registers available for argument
632 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000633 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
634 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000635
636 // Figure out which arguments are going to go in registers, and which in
637 // memory. Also, if this is a vararg function, floating point operations
638 // must be stored to our stack, and loaded into integer regs as well, if
639 // any integer regs are available for argument passing.
640 unsigned ArgOffset = 24;
641 unsigned GPR_remaining = 8;
642 unsigned FPR_remaining = 13;
643
644 std::vector<SDOperand> MemOps;
645 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
646 // PtrOff will be used to store the current argument to the stack if a
647 // register cannot be found for it.
648 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
649 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
650 MVT::ValueType ArgVT = getValueType(Args[i].second);
651
652 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000653 default: assert(0 && "Unexpected ValueType for argument!");
654 case MVT::i1:
655 case MVT::i8:
656 case MVT::i16:
657 // Promote the integer to 32 bits. If the input type is signed use a
658 // sign extend, otherwise use a zero extend.
659 if (Args[i].second->isSigned())
660 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
661 else
662 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
663 // FALL THROUGH
664 case MVT::i32:
665 if (GPR_remaining > 0) {
666 args_to_use.push_back(Args[i].first);
667 --GPR_remaining;
668 } else {
669 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
670 Args[i].first, PtrOff,
671 DAG.getSrcValue(NULL)));
672 }
673 ArgOffset += 4;
674 break;
675 case MVT::i64:
676 // If we have one free GPR left, we can place the upper half of the i64
677 // in it, and store the other half to the stack. If we have two or more
678 // free GPRs, then we can pass both halves of the i64 in registers.
679 if (GPR_remaining > 0) {
680 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
681 Args[i].first, DAG.getConstant(1, MVT::i32));
682 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
683 Args[i].first, DAG.getConstant(0, MVT::i32));
684 args_to_use.push_back(Hi);
685 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000686 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000687 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000688 --GPR_remaining;
689 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000690 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
691 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000692 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000693 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000694 }
Chris Lattner915fb302005-08-30 00:19:00 +0000695 } else {
696 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
697 Args[i].first, PtrOff,
698 DAG.getSrcValue(NULL)));
699 }
700 ArgOffset += 8;
701 break;
702 case MVT::f32:
703 case MVT::f64:
704 if (FPR_remaining > 0) {
705 args_to_use.push_back(Args[i].first);
706 --FPR_remaining;
707 if (isVarArg) {
708 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
709 Args[i].first, PtrOff,
710 DAG.getSrcValue(NULL));
711 MemOps.push_back(Store);
712 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000713 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000714 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
715 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000716 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000717 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000718 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000719 }
720 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000721 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
722 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000723 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
724 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000725 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000726 args_to_use.push_back(Load);
727 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000728 }
729 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000730 // If we have any FPRs remaining, we may also have GPRs remaining.
731 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
732 // GPRs.
733 if (GPR_remaining > 0) {
734 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
735 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000736 }
Chris Lattner915fb302005-08-30 00:19:00 +0000737 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
738 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
739 --GPR_remaining;
740 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000741 }
Chris Lattner915fb302005-08-30 00:19:00 +0000742 } else {
743 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
744 Args[i].first, PtrOff,
745 DAG.getSrcValue(NULL)));
746 }
747 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
748 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000749 }
750 }
751 if (!MemOps.empty())
752 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
753 }
754
755 std::vector<MVT::ValueType> RetVals;
756 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000757 MVT::ValueType ActualRetTyVT = RetTyVT;
758 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
759 ActualRetTyVT = MVT::i32; // Promote result to i32.
760
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000761 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000762 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000763 RetVals.push_back(MVT::Other);
764
Chris Lattner2823b3e2005-11-17 05:56:14 +0000765 // If the callee is a GlobalAddress node (quite common, every direct call is)
766 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
767 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
768 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
769
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000770 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
771 Chain, Callee, args_to_use), 0);
772 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
773 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
774 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000775 SDOperand RetVal = TheCall;
776
777 // If the result is a small value, add a note so that we keep track of the
778 // information about whether it is sign or zero extended.
779 if (RetTyVT != ActualRetTyVT) {
780 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
781 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
782 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
783 }
784
785 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000786}
787
Nate Begeman4a959452005-10-18 23:23:37 +0000788SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
789 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000790 SDOperand Copy;
791 switch (Op.getValueType()) {
792 default: assert(0 && "Unknown type to return!");
793 case MVT::i32:
794 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
795 break;
796 case MVT::f32:
797 case MVT::f64:
798 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
799 break;
800 case MVT::i64:
801 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
802 DAG.getConstant(1, MVT::i32));
803 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
804 DAG.getConstant(0, MVT::i32));
805 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
806 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
807 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000808 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000809 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000810}
811
Nate Begeman21e463b2005-10-16 05:39:50 +0000812SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
813 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000814 // vastart just stores the address of the VarArgsFrameIndex slot into the
815 // memory location argument.
816 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
817 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
818 DAG.getSrcValue(VAListV));
819}
820
821std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000822PPCTargetLowering::LowerVAArg(SDOperand Chain,
823 SDOperand VAListP, Value *VAListV,
824 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000825 MVT::ValueType ArgVT = getValueType(ArgTy);
826
827 SDOperand VAList =
828 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
829 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
830 unsigned Amt;
831 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
832 Amt = 4;
833 else {
834 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
835 "Other types should have been promoted for varargs!");
836 Amt = 8;
837 }
838 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
839 DAG.getConstant(Amt, VAList.getValueType()));
840 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
841 VAList, VAListP, DAG.getSrcValue(VAListV));
842 return std::make_pair(Result, Chain);
843}
844
845
Nate Begeman21e463b2005-10-16 05:39:50 +0000846std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000847LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
848 SelectionDAG &DAG) {
849 assert(0 && "LowerFrameReturnAddress unimplemented");
850 abort();
851}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000852
853MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000854PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
855 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000856 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000857 MI->getOpcode() == PPC::SELECT_CC_F4 ||
858 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000859 "Unexpected instr type to insert");
860
861 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
862 // control-flow pattern. The incoming instruction knows the destination vreg
863 // to set, the condition code register to branch on, the true/false values to
864 // select between, and a branch opcode to use.
865 const BasicBlock *LLVM_BB = BB->getBasicBlock();
866 ilist<MachineBasicBlock>::iterator It = BB;
867 ++It;
868
869 // thisMBB:
870 // ...
871 // TrueVal = ...
872 // cmpTY ccX, r1, r2
873 // bCC copy1MBB
874 // fallthrough --> copy0MBB
875 MachineBasicBlock *thisMBB = BB;
876 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
877 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
878 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
879 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
880 MachineFunction *F = BB->getParent();
881 F->getBasicBlockList().insert(It, copy0MBB);
882 F->getBasicBlockList().insert(It, sinkMBB);
883 // Update machine-CFG edges
884 BB->addSuccessor(copy0MBB);
885 BB->addSuccessor(sinkMBB);
886
887 // copy0MBB:
888 // %FalseValue = ...
889 // # fallthrough to sinkMBB
890 BB = copy0MBB;
891
892 // Update machine-CFG edges
893 BB->addSuccessor(sinkMBB);
894
895 // sinkMBB:
896 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
897 // ...
898 BB = sinkMBB;
899 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
900 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
901 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
902
903 delete MI; // The pseudo instruction is gone now.
904 return BB;
905}
906