blob: 01d8f075b7bb1cb96fa0d4f3b126f4adedaedfa2 [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
97 // PowerPC doesn't have line number support yet.
98 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +000099
Nate Begeman28a6b022005-12-10 02:36:00 +0000100 // We want to legalize GlobalAddress and ConstantPool nodes into the
101 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000102 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000103 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattner860e8862005-11-17 07:30:41 +0000104
Nate Begemanc09eeec2005-09-06 22:03:27 +0000105 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000106 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000107 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
108 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000109 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
110 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
111 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000112 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000113 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000114 }
115
116 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
117 // 64 bit PowerPC implementations can support i64 types directly
118 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000119 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
120 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000121 } else {
122 // 32 bit PowerPC wants to expand i64 shifts itself.
123 setOperationAction(ISD::SHL, MVT::i64, Custom);
124 setOperationAction(ISD::SRL, MVT::i64, Custom);
125 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000126 }
127
Nate Begeman425a9692005-11-29 08:17:20 +0000128 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000129 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000130 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000131 }
132
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000133 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000134
135 computeRegisterProperties();
136}
137
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000138/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
139static bool isFloatingPointZero(SDOperand Op) {
140 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
141 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
142 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
143 // Maybe this has already been legalized into the constant pool?
144 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
145 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
146 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
147 }
148 return false;
149}
150
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000151/// LowerOperation - Provide custom lowering hooks for some operations.
152///
Nate Begeman21e463b2005-10-16 05:39:50 +0000153SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000154 switch (Op.getOpcode()) {
155 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000156 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000157 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000158 SDOperand Src = Op.getOperand(0);
159 if (Src.getValueType() == MVT::f32)
160 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
161
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000162 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000163 switch (Op.getValueType()) {
164 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
165 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000166 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000167 break;
168 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000169 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000170 break;
171 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000172
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000173 // Convert the FP value to an int value through memory.
174 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
175 if (Op.getValueType() == MVT::i32)
176 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
177 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000178 }
179 case ISD::SINT_TO_FP: {
180 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
181 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000182 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
183 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000184 if (MVT::f32 == Op.getValueType())
185 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
186 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000187 }
188 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000189 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000190 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
191 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
192 break;
193
194 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
195
196 // Cannot handle SETEQ/SETNE.
197 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
198
199 MVT::ValueType ResVT = Op.getValueType();
200 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
201 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
202 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000203
Chris Lattnerf7605322005-08-31 21:09:52 +0000204 // If the RHS of the comparison is a 0.0, we don't need to do the
205 // subtraction at all.
206 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000207 switch (CC) {
208 default: assert(0 && "Invalid FSEL condition"); abort();
209 case ISD::SETULT:
210 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000211 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000212 case ISD::SETUGE:
213 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000214 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
215 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000216 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000217 case ISD::SETUGT:
218 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000219 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000220 case ISD::SETULE:
221 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000222 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
223 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000224 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000225 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000226 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000227
Chris Lattnereb255f22005-10-25 20:54:57 +0000228 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000229 switch (CC) {
230 default: assert(0 && "Invalid FSEL condition"); abort();
231 case ISD::SETULT:
232 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000233 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
234 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
235 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
236 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000237 case ISD::SETUGE:
238 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000239 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
240 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
241 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
242 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000243 case ISD::SETUGT:
244 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000245 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
246 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
247 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
248 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000249 case ISD::SETULE:
250 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000251 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
252 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
253 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
254 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000255 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000256 break;
257 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000258 case ISD::SHL: {
259 assert(Op.getValueType() == MVT::i64 &&
260 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
261 // The generic code does a fine job expanding shift by a constant.
262 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
263
264 // Otherwise, expand into a bunch of logical ops. Note that these ops
265 // depend on the PPC behavior for oversized shift amounts.
266 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
267 DAG.getConstant(0, MVT::i32));
268 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
269 DAG.getConstant(1, MVT::i32));
270 SDOperand Amt = Op.getOperand(1);
271
272 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
273 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000274 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
275 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000276 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
277 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
278 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000279 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000280 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000281 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000282 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
283 }
284 case ISD::SRL: {
285 assert(Op.getValueType() == MVT::i64 &&
286 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
287 // The generic code does a fine job expanding shift by a constant.
288 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
289
290 // Otherwise, expand into a bunch of logical ops. Note that these ops
291 // depend on the PPC behavior for oversized shift amounts.
292 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
293 DAG.getConstant(0, MVT::i32));
294 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
295 DAG.getConstant(1, MVT::i32));
296 SDOperand Amt = Op.getOperand(1);
297
298 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
299 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000300 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
301 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000302 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
303 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
304 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000305 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000306 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000307 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000308 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
309 }
310 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000311 assert(Op.getValueType() == MVT::i64 &&
312 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
313 // The generic code does a fine job expanding shift by a constant.
314 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
315
316 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
317 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
318 DAG.getConstant(0, MVT::i32));
319 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
320 DAG.getConstant(1, MVT::i32));
321 SDOperand Amt = Op.getOperand(1);
322
323 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
324 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000325 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
326 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000327 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
328 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
329 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000330 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
331 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000332 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
333 Tmp4, Tmp6, ISD::SETLE);
334 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000335 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000336 case ISD::ConstantPool: {
337 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
338 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
339 SDOperand Zero = DAG.getConstant(0, MVT::i32);
340
341 if (PPCGenerateStaticCode) {
342 // Generate non-pic code that has direct accesses to the constant pool.
343 // The address of the global is just (hi(&g)+lo(&g)).
344 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
345 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
346 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
347 }
348
349 // Only lower ConstantPool on Darwin.
350 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
351 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
352 if (PICEnabled) {
353 // With PIC, the first instruction is actually "GR+hi(&G)".
354 Hi = DAG.getNode(ISD::ADD, MVT::i32,
355 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
356 }
357
358 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
359 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
360 return Lo;
361 }
Chris Lattner860e8862005-11-17 07:30:41 +0000362 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000363 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
364 GlobalValue *GV = GSDN->getGlobal();
365 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000366 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000367
368 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000369 // Generate non-pic code that has direct accesses to globals.
370 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000371 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
372 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
373 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
374 }
Chris Lattner860e8862005-11-17 07:30:41 +0000375
Chris Lattner1d05cb42005-11-17 18:55:48 +0000376 // Only lower GlobalAddress on Darwin.
377 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattner860e8862005-11-17 07:30:41 +0000378 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
379 if (PICEnabled) {
380 // With PIC, the first instruction is actually "GR+hi(&G)".
381 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000382 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000383 }
384
385 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
386 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
387
388 if (!GV->hasWeakLinkage() && !GV->isExternal())
389 return Lo;
390
391 // If the global is weak or external, we have to go through the lazy
392 // resolution stub.
393 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
394 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000395 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000396 return SDOperand();
397}
398
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000399std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000400PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000401 //
402 // add beautiful description of PPC stack frame format, or at least some docs
403 //
404 MachineFunction &MF = DAG.getMachineFunction();
405 MachineFrameInfo *MFI = MF.getFrameInfo();
406 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000407 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000408 std::vector<SDOperand> ArgValues;
409
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000410 unsigned ArgOffset = 24;
411 unsigned GPR_remaining = 8;
412 unsigned FPR_remaining = 13;
413 unsigned GPR_idx = 0, FPR_idx = 0;
414 static const unsigned GPR[] = {
415 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
416 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
417 };
418 static const unsigned FPR[] = {
419 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
420 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
421 };
422
423 // Add DAG nodes to load the arguments... On entry to a function on PPC,
424 // the arguments start at offset 24, although they are likely to be passed
425 // in registers.
426 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
427 SDOperand newroot, argt;
428 unsigned ObjSize;
429 bool needsLoad = false;
430 bool ArgLive = !I->use_empty();
431 MVT::ValueType ObjectVT = getValueType(I->getType());
432
433 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000434 default: assert(0 && "Unhandled argument type!");
435 case MVT::i1:
436 case MVT::i8:
437 case MVT::i16:
438 case MVT::i32:
439 ObjSize = 4;
440 if (!ArgLive) break;
441 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000442 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000443 MF.addLiveIn(GPR[GPR_idx], VReg);
444 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000445 if (ObjectVT != MVT::i32) {
446 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
447 : ISD::AssertZext;
448 argt = DAG.getNode(AssertOp, MVT::i32, argt,
449 DAG.getValueType(ObjectVT));
450 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
451 }
Chris Lattner915fb302005-08-30 00:19:00 +0000452 } else {
453 needsLoad = true;
454 }
455 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000456 case MVT::i64:
457 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000458 if (!ArgLive) break;
459 if (GPR_remaining > 0) {
460 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000461 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000462 MF.addLiveIn(GPR[GPR_idx], VReg);
463 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000464 // If we have two or more remaining argument registers, then both halves
465 // of the i64 can be sourced from there. Otherwise, the lower half will
466 // have to come off the stack. This can happen when an i64 is preceded
467 // by 28 bytes of arguments.
468 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000469 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000470 MF.addLiveIn(GPR[GPR_idx+1], VReg);
471 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000472 } else {
473 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
474 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
475 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
476 DAG.getSrcValue(NULL));
477 }
478 // Build the outgoing arg thingy
479 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
480 newroot = argLo;
481 } else {
482 needsLoad = true;
483 }
484 break;
485 case MVT::f32:
486 case MVT::f64:
487 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
488 if (!ArgLive) break;
489 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000490 unsigned VReg;
491 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000492 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000493 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000494 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000495 MF.addLiveIn(FPR[FPR_idx], VReg);
496 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000497 --FPR_remaining;
498 ++FPR_idx;
499 } else {
500 needsLoad = true;
501 }
502 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000503 }
504
505 // We need to load the argument to a virtual register if we determined above
506 // that we ran out of physical registers of the appropriate type
507 if (needsLoad) {
508 unsigned SubregOffset = 0;
509 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
510 if (ObjectVT == MVT::i16) SubregOffset = 2;
511 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
512 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
513 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
514 DAG.getConstant(SubregOffset, MVT::i32));
515 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
516 DAG.getSrcValue(NULL));
517 }
518
519 // Every 4 bytes of argument space consumes one of the GPRs available for
520 // argument passing.
521 if (GPR_remaining > 0) {
522 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
523 GPR_remaining -= delta;
524 GPR_idx += delta;
525 }
526 ArgOffset += ObjSize;
527 if (newroot.Val)
528 DAG.setRoot(newroot.getValue(1));
529
530 ArgValues.push_back(argt);
531 }
532
533 // If the function takes variable number of arguments, make a frame index for
534 // the start of the first vararg value... for expansion of llvm.va_start.
535 if (F.isVarArg()) {
536 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
537 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
538 // If this function is vararg, store any remaining integer argument regs
539 // to their spots on the stack so that they may be loaded by deferencing the
540 // result of va_next.
541 std::vector<SDOperand> MemOps;
542 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000543 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000544 MF.addLiveIn(GPR[GPR_idx], VReg);
545 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000546 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
547 Val, FIN, DAG.getSrcValue(NULL));
548 MemOps.push_back(Store);
549 // Increment the address by four for the next argument to store
550 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
551 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
552 }
Chris Lattner80720a92005-11-30 20:40:54 +0000553 if (!MemOps.empty()) {
554 MemOps.push_back(DAG.getRoot());
555 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
556 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000557 }
558
559 // Finally, inform the code generator which regs we return values in.
560 switch (getValueType(F.getReturnType())) {
561 default: assert(0 && "Unknown type!");
562 case MVT::isVoid: break;
563 case MVT::i1:
564 case MVT::i8:
565 case MVT::i16:
566 case MVT::i32:
567 MF.addLiveOut(PPC::R3);
568 break;
569 case MVT::i64:
570 MF.addLiveOut(PPC::R3);
571 MF.addLiveOut(PPC::R4);
572 break;
573 case MVT::f32:
574 case MVT::f64:
575 MF.addLiveOut(PPC::F1);
576 break;
577 }
578
579 return ArgValues;
580}
581
582std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000583PPCTargetLowering::LowerCallTo(SDOperand Chain,
584 const Type *RetTy, bool isVarArg,
585 unsigned CallingConv, bool isTailCall,
586 SDOperand Callee, ArgListTy &Args,
587 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000588 // args_to_use will accumulate outgoing args for the ISD::CALL case in
589 // SelectExpr to use to put the arguments in the appropriate registers.
590 std::vector<SDOperand> args_to_use;
591
592 // Count how many bytes are to be pushed on the stack, including the linkage
593 // area, and parameter passing area.
594 unsigned NumBytes = 24;
595
596 if (Args.empty()) {
597 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
598 DAG.getConstant(NumBytes, getPointerTy()));
599 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000600 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000601 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000602 default: assert(0 && "Unknown value type!");
603 case MVT::i1:
604 case MVT::i8:
605 case MVT::i16:
606 case MVT::i32:
607 case MVT::f32:
608 NumBytes += 4;
609 break;
610 case MVT::i64:
611 case MVT::f64:
612 NumBytes += 8;
613 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000614 }
Chris Lattner915fb302005-08-30 00:19:00 +0000615 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000616
Chris Lattner915fb302005-08-30 00:19:00 +0000617 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
618 // plus 32 bytes of argument space in case any called code gets funky on us.
619 // (Required by ABI to support var arg)
620 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000621
622 // Adjust the stack pointer for the new arguments...
623 // These operations are automatically eliminated by the prolog/epilog pass
624 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
625 DAG.getConstant(NumBytes, getPointerTy()));
626
627 // Set up a copy of the stack pointer for use loading and storing any
628 // arguments that may not fit in the registers available for argument
629 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000630 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
631 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000632
633 // Figure out which arguments are going to go in registers, and which in
634 // memory. Also, if this is a vararg function, floating point operations
635 // must be stored to our stack, and loaded into integer regs as well, if
636 // any integer regs are available for argument passing.
637 unsigned ArgOffset = 24;
638 unsigned GPR_remaining = 8;
639 unsigned FPR_remaining = 13;
640
641 std::vector<SDOperand> MemOps;
642 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
643 // PtrOff will be used to store the current argument to the stack if a
644 // register cannot be found for it.
645 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
646 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
647 MVT::ValueType ArgVT = getValueType(Args[i].second);
648
649 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000650 default: assert(0 && "Unexpected ValueType for argument!");
651 case MVT::i1:
652 case MVT::i8:
653 case MVT::i16:
654 // Promote the integer to 32 bits. If the input type is signed use a
655 // sign extend, otherwise use a zero extend.
656 if (Args[i].second->isSigned())
657 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
658 else
659 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
660 // FALL THROUGH
661 case MVT::i32:
662 if (GPR_remaining > 0) {
663 args_to_use.push_back(Args[i].first);
664 --GPR_remaining;
665 } else {
666 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
667 Args[i].first, PtrOff,
668 DAG.getSrcValue(NULL)));
669 }
670 ArgOffset += 4;
671 break;
672 case MVT::i64:
673 // If we have one free GPR left, we can place the upper half of the i64
674 // in it, and store the other half to the stack. If we have two or more
675 // free GPRs, then we can pass both halves of the i64 in registers.
676 if (GPR_remaining > 0) {
677 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
678 Args[i].first, DAG.getConstant(1, MVT::i32));
679 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
680 Args[i].first, DAG.getConstant(0, MVT::i32));
681 args_to_use.push_back(Hi);
682 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000683 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000684 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000685 --GPR_remaining;
686 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000687 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
688 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000689 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000690 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000691 }
Chris Lattner915fb302005-08-30 00:19:00 +0000692 } else {
693 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
694 Args[i].first, PtrOff,
695 DAG.getSrcValue(NULL)));
696 }
697 ArgOffset += 8;
698 break;
699 case MVT::f32:
700 case MVT::f64:
701 if (FPR_remaining > 0) {
702 args_to_use.push_back(Args[i].first);
703 --FPR_remaining;
704 if (isVarArg) {
705 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
706 Args[i].first, PtrOff,
707 DAG.getSrcValue(NULL));
708 MemOps.push_back(Store);
709 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000710 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000711 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
712 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000713 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000714 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000715 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000716 }
717 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000718 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
719 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000720 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
721 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000722 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000723 args_to_use.push_back(Load);
724 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000725 }
726 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000727 // If we have any FPRs remaining, we may also have GPRs remaining.
728 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
729 // GPRs.
730 if (GPR_remaining > 0) {
731 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
732 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000733 }
Chris Lattner915fb302005-08-30 00:19:00 +0000734 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
735 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
736 --GPR_remaining;
737 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000738 }
Chris Lattner915fb302005-08-30 00:19:00 +0000739 } else {
740 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
741 Args[i].first, PtrOff,
742 DAG.getSrcValue(NULL)));
743 }
744 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
745 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000746 }
747 }
748 if (!MemOps.empty())
749 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
750 }
751
752 std::vector<MVT::ValueType> RetVals;
753 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000754 MVT::ValueType ActualRetTyVT = RetTyVT;
755 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
756 ActualRetTyVT = MVT::i32; // Promote result to i32.
757
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000758 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000759 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000760 RetVals.push_back(MVT::Other);
761
Chris Lattner2823b3e2005-11-17 05:56:14 +0000762 // If the callee is a GlobalAddress node (quite common, every direct call is)
763 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
764 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
765 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
766
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000767 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
768 Chain, Callee, args_to_use), 0);
769 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
770 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
771 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000772 SDOperand RetVal = TheCall;
773
774 // If the result is a small value, add a note so that we keep track of the
775 // information about whether it is sign or zero extended.
776 if (RetTyVT != ActualRetTyVT) {
777 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
778 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
779 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
780 }
781
782 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000783}
784
Nate Begeman4a959452005-10-18 23:23:37 +0000785SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
786 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000787 SDOperand Copy;
788 switch (Op.getValueType()) {
789 default: assert(0 && "Unknown type to return!");
790 case MVT::i32:
791 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
792 break;
793 case MVT::f32:
794 case MVT::f64:
795 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
796 break;
797 case MVT::i64:
798 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
799 DAG.getConstant(1, MVT::i32));
800 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
801 DAG.getConstant(0, MVT::i32));
802 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
803 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
804 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000805 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000806 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000807}
808
Nate Begeman21e463b2005-10-16 05:39:50 +0000809SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
810 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000811 // vastart just stores the address of the VarArgsFrameIndex slot into the
812 // memory location argument.
813 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
814 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
815 DAG.getSrcValue(VAListV));
816}
817
818std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000819PPCTargetLowering::LowerVAArg(SDOperand Chain,
820 SDOperand VAListP, Value *VAListV,
821 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000822 MVT::ValueType ArgVT = getValueType(ArgTy);
823
824 SDOperand VAList =
825 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
826 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
827 unsigned Amt;
828 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
829 Amt = 4;
830 else {
831 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
832 "Other types should have been promoted for varargs!");
833 Amt = 8;
834 }
835 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
836 DAG.getConstant(Amt, VAList.getValueType()));
837 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
838 VAList, VAListP, DAG.getSrcValue(VAListV));
839 return std::make_pair(Result, Chain);
840}
841
842
Nate Begeman21e463b2005-10-16 05:39:50 +0000843std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000844LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
845 SelectionDAG &DAG) {
846 assert(0 && "LowerFrameReturnAddress unimplemented");
847 abort();
848}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000849
850MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000851PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
852 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000853 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000854 MI->getOpcode() == PPC::SELECT_CC_F4 ||
855 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000856 "Unexpected instr type to insert");
857
858 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
859 // control-flow pattern. The incoming instruction knows the destination vreg
860 // to set, the condition code register to branch on, the true/false values to
861 // select between, and a branch opcode to use.
862 const BasicBlock *LLVM_BB = BB->getBasicBlock();
863 ilist<MachineBasicBlock>::iterator It = BB;
864 ++It;
865
866 // thisMBB:
867 // ...
868 // TrueVal = ...
869 // cmpTY ccX, r1, r2
870 // bCC copy1MBB
871 // fallthrough --> copy0MBB
872 MachineBasicBlock *thisMBB = BB;
873 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
874 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
875 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
876 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
877 MachineFunction *F = BB->getParent();
878 F->getBasicBlockList().insert(It, copy0MBB);
879 F->getBasicBlockList().insert(It, sinkMBB);
880 // Update machine-CFG edges
881 BB->addSuccessor(copy0MBB);
882 BB->addSuccessor(sinkMBB);
883
884 // copy0MBB:
885 // %FalseValue = ...
886 // # fallthrough to sinkMBB
887 BB = copy0MBB;
888
889 // Update machine-CFG edges
890 BB->addSuccessor(sinkMBB);
891
892 // sinkMBB:
893 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
894 // ...
895 BB = sinkMBB;
896 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
897 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
898 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
899
900 delete MI; // The pseudo instruction is gone now.
901 return BB;
902}
903