blob: decc6a388aaa0253765412269224eb66f96f9a61 [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
Nate Begemand88fc032006-01-14 03:14:10 +000067 // PowerPC does not have BSWAP, CTPOP or CTTZ
68 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000069 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
70 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
71
Nate Begeman35ef9132006-01-11 21:21:00 +000072 // PowerPC does not have ROTR
73 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
74
Chris Lattner7c5a3d32005-08-16 17:14:42 +000075 // PowerPC does not have Select
76 setOperationAction(ISD::SELECT, MVT::i32, Expand);
77 setOperationAction(ISD::SELECT, MVT::f32, Expand);
78 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000079
Chris Lattner0b1e4e52005-08-26 17:36:52 +000080 // PowerPC wants to turn select_cc of FP into fsel when possible.
81 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
82 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000083
Nate Begeman7cbd5252005-08-16 19:49:35 +000084 // PowerPC does not have BRCOND* which requires SetCC
85 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
86 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000087
Chris Lattnerf7605322005-08-31 21:09:52 +000088 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
89 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000090
Jim Laskeyad23c9d2005-08-17 00:40:22 +000091 // PowerPC does not have [U|S]INT_TO_FP
92 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
93 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
94
Chris Lattner53e88452005-12-23 05:13:35 +000095 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
96 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
97
Chris Lattnere6ec9f22005-09-10 00:21:06 +000098 // PowerPC does not have truncstore for i1.
99 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000100
Jim Laskeyabf6d172006-01-05 01:25:28 +0000101 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000102 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000103 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000104 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000105 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000106 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000107
Nate Begeman28a6b022005-12-10 02:36:00 +0000108 // We want to legalize GlobalAddress and ConstantPool nodes into the
109 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000110 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000111 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000112
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000113 // Use the default implementation.
114 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
115 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
116 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000117
Nate Begemanc09eeec2005-09-06 22:03:27 +0000118 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000119 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000120 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
121 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000122 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
123 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
124 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000125 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000126 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000127 }
128
129 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
130 // 64 bit PowerPC implementations can support i64 types directly
131 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000132 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
133 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000134 } else {
135 // 32 bit PowerPC wants to expand i64 shifts itself.
136 setOperationAction(ISD::SHL, MVT::i64, Custom);
137 setOperationAction(ISD::SRL, MVT::i64, Custom);
138 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000139 }
140
Nate Begeman425a9692005-11-29 08:17:20 +0000141 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000142 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000143 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000144 }
145
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000146 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000147 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000148
149 computeRegisterProperties();
150}
151
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000152const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
153 switch (Opcode) {
154 default: return 0;
155 case PPCISD::FSEL: return "PPCISD::FSEL";
156 case PPCISD::FCFID: return "PPCISD::FCFID";
157 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
158 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
159 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
160 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
161 case PPCISD::Hi: return "PPCISD::Hi";
162 case PPCISD::Lo: return "PPCISD::Lo";
163 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
164 case PPCISD::SRL: return "PPCISD::SRL";
165 case PPCISD::SRA: return "PPCISD::SRA";
166 case PPCISD::SHL: return "PPCISD::SHL";
167 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
168 }
169}
170
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000171/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
172static bool isFloatingPointZero(SDOperand Op) {
173 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
174 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
175 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
176 // Maybe this has already been legalized into the constant pool?
177 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
178 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
179 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
180 }
181 return false;
182}
183
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000184/// LowerOperation - Provide custom lowering hooks for some operations.
185///
Nate Begeman21e463b2005-10-16 05:39:50 +0000186SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000187 switch (Op.getOpcode()) {
188 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000189 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000190 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000191 SDOperand Src = Op.getOperand(0);
192 if (Src.getValueType() == MVT::f32)
193 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
194
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000195 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000196 switch (Op.getValueType()) {
197 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
198 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000199 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000200 break;
201 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000202 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000203 break;
204 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000205
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000206 // Convert the FP value to an int value through memory.
207 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
208 if (Op.getValueType() == MVT::i32)
209 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
210 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000211 }
212 case ISD::SINT_TO_FP: {
213 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
214 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000215 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
216 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000217 if (MVT::f32 == Op.getValueType())
218 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
219 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000220 }
221 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000222 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000223 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
224 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
225 break;
226
227 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
228
229 // Cannot handle SETEQ/SETNE.
230 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
231
232 MVT::ValueType ResVT = Op.getValueType();
233 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
234 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
235 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000236
Chris Lattnerf7605322005-08-31 21:09:52 +0000237 // If the RHS of the comparison is a 0.0, we don't need to do the
238 // subtraction at all.
239 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000240 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000241 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000242 case ISD::SETULT:
243 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000244 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000245 case ISD::SETUGE:
246 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000247 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
248 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000249 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000250 case ISD::SETUGT:
251 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000252 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000253 case ISD::SETULE:
254 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000255 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
256 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000257 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000258 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000259 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000260
Chris Lattnereb255f22005-10-25 20:54:57 +0000261 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000262 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000263 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000264 case ISD::SETULT:
265 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000266 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
267 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
268 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
269 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000270 case ISD::SETUGE:
271 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000272 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
273 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
274 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
275 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000276 case ISD::SETUGT:
277 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000278 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
279 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
280 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
281 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000282 case ISD::SETULE:
283 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000284 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
285 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
286 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
287 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000288 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000289 break;
290 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000291 case ISD::SHL: {
292 assert(Op.getValueType() == MVT::i64 &&
293 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
294 // The generic code does a fine job expanding shift by a constant.
295 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
296
297 // Otherwise, expand into a bunch of logical ops. Note that these ops
298 // depend on the PPC behavior for oversized shift amounts.
299 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
300 DAG.getConstant(0, MVT::i32));
301 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
302 DAG.getConstant(1, MVT::i32));
303 SDOperand Amt = Op.getOperand(1);
304
305 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
306 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000307 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
308 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000309 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
310 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
311 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000312 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000313 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000314 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000315 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
316 }
317 case ISD::SRL: {
318 assert(Op.getValueType() == MVT::i64 &&
319 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
320 // The generic code does a fine job expanding shift by a constant.
321 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
322
323 // Otherwise, expand into a bunch of logical ops. Note that these ops
324 // depend on the PPC behavior for oversized shift amounts.
325 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
326 DAG.getConstant(0, MVT::i32));
327 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
328 DAG.getConstant(1, MVT::i32));
329 SDOperand Amt = Op.getOperand(1);
330
331 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
332 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000333 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
334 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000335 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
336 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
337 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000338 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000339 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000340 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000341 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
342 }
343 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000344 assert(Op.getValueType() == MVT::i64 &&
345 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
346 // The generic code does a fine job expanding shift by a constant.
347 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
348
349 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
350 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
351 DAG.getConstant(0, MVT::i32));
352 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
353 DAG.getConstant(1, MVT::i32));
354 SDOperand Amt = Op.getOperand(1);
355
356 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
357 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000358 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
359 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000360 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
361 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
362 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000363 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
364 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000365 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
366 Tmp4, Tmp6, ISD::SETLE);
367 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000368 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000369 case ISD::ConstantPool: {
370 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
371 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
372 SDOperand Zero = DAG.getConstant(0, MVT::i32);
373
374 if (PPCGenerateStaticCode) {
375 // Generate non-pic code that has direct accesses to the constant pool.
376 // The address of the global is just (hi(&g)+lo(&g)).
377 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
378 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
379 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
380 }
381
382 // Only lower ConstantPool on Darwin.
383 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
384 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
385 if (PICEnabled) {
386 // With PIC, the first instruction is actually "GR+hi(&G)".
387 Hi = DAG.getNode(ISD::ADD, MVT::i32,
388 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
389 }
390
391 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
392 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
393 return Lo;
394 }
Chris Lattner860e8862005-11-17 07:30:41 +0000395 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000396 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
397 GlobalValue *GV = GSDN->getGlobal();
398 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000399 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000400
401 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000402 // Generate non-pic code that has direct accesses to globals.
403 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000404 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
405 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
406 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
407 }
Chris Lattner860e8862005-11-17 07:30:41 +0000408
Chris Lattner1d05cb42005-11-17 18:55:48 +0000409 // Only lower GlobalAddress on Darwin.
410 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000411
Chris Lattner860e8862005-11-17 07:30:41 +0000412 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
413 if (PICEnabled) {
414 // With PIC, the first instruction is actually "GR+hi(&G)".
415 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000416 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000417 }
418
419 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
420 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
421
Chris Lattnera35ef632006-01-06 01:04:03 +0000422 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000423 return Lo;
424
425 // If the global is weak or external, we have to go through the lazy
426 // resolution stub.
427 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
428 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000429 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000430 return SDOperand();
431}
432
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000433std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000434PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000435 //
436 // add beautiful description of PPC stack frame format, or at least some docs
437 //
438 MachineFunction &MF = DAG.getMachineFunction();
439 MachineFrameInfo *MFI = MF.getFrameInfo();
440 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000441 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000442 std::vector<SDOperand> ArgValues;
443
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000444 unsigned ArgOffset = 24;
445 unsigned GPR_remaining = 8;
446 unsigned FPR_remaining = 13;
447 unsigned GPR_idx = 0, FPR_idx = 0;
448 static const unsigned GPR[] = {
449 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
450 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
451 };
452 static const unsigned FPR[] = {
453 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
454 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
455 };
456
457 // Add DAG nodes to load the arguments... On entry to a function on PPC,
458 // the arguments start at offset 24, although they are likely to be passed
459 // in registers.
460 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
461 SDOperand newroot, argt;
462 unsigned ObjSize;
463 bool needsLoad = false;
464 bool ArgLive = !I->use_empty();
465 MVT::ValueType ObjectVT = getValueType(I->getType());
466
467 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000468 default: assert(0 && "Unhandled argument type!");
469 case MVT::i1:
470 case MVT::i8:
471 case MVT::i16:
472 case MVT::i32:
473 ObjSize = 4;
474 if (!ArgLive) break;
475 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000476 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000477 MF.addLiveIn(GPR[GPR_idx], VReg);
478 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000479 if (ObjectVT != MVT::i32) {
480 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
481 : ISD::AssertZext;
482 argt = DAG.getNode(AssertOp, MVT::i32, argt,
483 DAG.getValueType(ObjectVT));
484 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
485 }
Chris Lattner915fb302005-08-30 00:19:00 +0000486 } else {
487 needsLoad = true;
488 }
489 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000490 case MVT::i64:
491 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000492 if (!ArgLive) break;
493 if (GPR_remaining > 0) {
494 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000495 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000496 MF.addLiveIn(GPR[GPR_idx], VReg);
497 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000498 // If we have two or more remaining argument registers, then both halves
499 // of the i64 can be sourced from there. Otherwise, the lower half will
500 // have to come off the stack. This can happen when an i64 is preceded
501 // by 28 bytes of arguments.
502 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000503 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000504 MF.addLiveIn(GPR[GPR_idx+1], VReg);
505 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000506 } else {
507 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
508 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
509 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
510 DAG.getSrcValue(NULL));
511 }
512 // Build the outgoing arg thingy
513 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
514 newroot = argLo;
515 } else {
516 needsLoad = true;
517 }
518 break;
519 case MVT::f32:
520 case MVT::f64:
521 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000522 if (!ArgLive) {
523 if (FPR_remaining > 0) {
524 --FPR_remaining;
525 ++FPR_idx;
526 }
527 break;
528 }
Chris Lattner915fb302005-08-30 00:19:00 +0000529 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000530 unsigned VReg;
531 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000532 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000533 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000534 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000535 MF.addLiveIn(FPR[FPR_idx], VReg);
536 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000537 --FPR_remaining;
538 ++FPR_idx;
539 } else {
540 needsLoad = true;
541 }
542 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000543 }
544
545 // We need to load the argument to a virtual register if we determined above
546 // that we ran out of physical registers of the appropriate type
547 if (needsLoad) {
548 unsigned SubregOffset = 0;
549 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
550 if (ObjectVT == MVT::i16) SubregOffset = 2;
551 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
552 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
553 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
554 DAG.getConstant(SubregOffset, MVT::i32));
555 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
556 DAG.getSrcValue(NULL));
557 }
558
559 // Every 4 bytes of argument space consumes one of the GPRs available for
560 // argument passing.
561 if (GPR_remaining > 0) {
562 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
563 GPR_remaining -= delta;
564 GPR_idx += delta;
565 }
566 ArgOffset += ObjSize;
567 if (newroot.Val)
568 DAG.setRoot(newroot.getValue(1));
569
570 ArgValues.push_back(argt);
571 }
572
573 // If the function takes variable number of arguments, make a frame index for
574 // the start of the first vararg value... for expansion of llvm.va_start.
575 if (F.isVarArg()) {
576 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
577 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
578 // If this function is vararg, store any remaining integer argument regs
579 // to their spots on the stack so that they may be loaded by deferencing the
580 // result of va_next.
581 std::vector<SDOperand> MemOps;
582 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000583 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000584 MF.addLiveIn(GPR[GPR_idx], VReg);
585 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000586 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
587 Val, FIN, DAG.getSrcValue(NULL));
588 MemOps.push_back(Store);
589 // Increment the address by four for the next argument to store
590 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
591 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
592 }
Chris Lattner80720a92005-11-30 20:40:54 +0000593 if (!MemOps.empty()) {
594 MemOps.push_back(DAG.getRoot());
595 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
596 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000597 }
598
599 // Finally, inform the code generator which regs we return values in.
600 switch (getValueType(F.getReturnType())) {
601 default: assert(0 && "Unknown type!");
602 case MVT::isVoid: break;
603 case MVT::i1:
604 case MVT::i8:
605 case MVT::i16:
606 case MVT::i32:
607 MF.addLiveOut(PPC::R3);
608 break;
609 case MVT::i64:
610 MF.addLiveOut(PPC::R3);
611 MF.addLiveOut(PPC::R4);
612 break;
613 case MVT::f32:
614 case MVT::f64:
615 MF.addLiveOut(PPC::F1);
616 break;
617 }
618
619 return ArgValues;
620}
621
622std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000623PPCTargetLowering::LowerCallTo(SDOperand Chain,
624 const Type *RetTy, bool isVarArg,
625 unsigned CallingConv, bool isTailCall,
626 SDOperand Callee, ArgListTy &Args,
627 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000628 // args_to_use will accumulate outgoing args for the ISD::CALL case in
629 // SelectExpr to use to put the arguments in the appropriate registers.
630 std::vector<SDOperand> args_to_use;
631
632 // Count how many bytes are to be pushed on the stack, including the linkage
633 // area, and parameter passing area.
634 unsigned NumBytes = 24;
635
636 if (Args.empty()) {
637 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
638 DAG.getConstant(NumBytes, getPointerTy()));
639 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000640 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000641 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000642 default: assert(0 && "Unknown value type!");
643 case MVT::i1:
644 case MVT::i8:
645 case MVT::i16:
646 case MVT::i32:
647 case MVT::f32:
648 NumBytes += 4;
649 break;
650 case MVT::i64:
651 case MVT::f64:
652 NumBytes += 8;
653 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000654 }
Chris Lattner915fb302005-08-30 00:19:00 +0000655 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000656
Chris Lattner915fb302005-08-30 00:19:00 +0000657 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
658 // plus 32 bytes of argument space in case any called code gets funky on us.
659 // (Required by ABI to support var arg)
660 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000661
662 // Adjust the stack pointer for the new arguments...
663 // These operations are automatically eliminated by the prolog/epilog pass
664 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
665 DAG.getConstant(NumBytes, getPointerTy()));
666
667 // Set up a copy of the stack pointer for use loading and storing any
668 // arguments that may not fit in the registers available for argument
669 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000670 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000671
672 // Figure out which arguments are going to go in registers, and which in
673 // memory. Also, if this is a vararg function, floating point operations
674 // must be stored to our stack, and loaded into integer regs as well, if
675 // any integer regs are available for argument passing.
676 unsigned ArgOffset = 24;
677 unsigned GPR_remaining = 8;
678 unsigned FPR_remaining = 13;
679
680 std::vector<SDOperand> MemOps;
681 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
682 // PtrOff will be used to store the current argument to the stack if a
683 // register cannot be found for it.
684 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
685 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
686 MVT::ValueType ArgVT = getValueType(Args[i].second);
687
688 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000689 default: assert(0 && "Unexpected ValueType for argument!");
690 case MVT::i1:
691 case MVT::i8:
692 case MVT::i16:
693 // Promote the integer to 32 bits. If the input type is signed use a
694 // sign extend, otherwise use a zero extend.
695 if (Args[i].second->isSigned())
696 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
697 else
698 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
699 // FALL THROUGH
700 case MVT::i32:
701 if (GPR_remaining > 0) {
702 args_to_use.push_back(Args[i].first);
703 --GPR_remaining;
704 } else {
705 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
706 Args[i].first, PtrOff,
707 DAG.getSrcValue(NULL)));
708 }
709 ArgOffset += 4;
710 break;
711 case MVT::i64:
712 // If we have one free GPR left, we can place the upper half of the i64
713 // in it, and store the other half to the stack. If we have two or more
714 // free GPRs, then we can pass both halves of the i64 in registers.
715 if (GPR_remaining > 0) {
716 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
717 Args[i].first, DAG.getConstant(1, MVT::i32));
718 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
719 Args[i].first, DAG.getConstant(0, MVT::i32));
720 args_to_use.push_back(Hi);
721 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000722 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000723 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000724 --GPR_remaining;
725 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000726 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
727 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000728 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000729 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000730 }
Chris Lattner915fb302005-08-30 00:19:00 +0000731 } else {
732 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
733 Args[i].first, PtrOff,
734 DAG.getSrcValue(NULL)));
735 }
736 ArgOffset += 8;
737 break;
738 case MVT::f32:
739 case MVT::f64:
740 if (FPR_remaining > 0) {
741 args_to_use.push_back(Args[i].first);
742 --FPR_remaining;
743 if (isVarArg) {
744 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
745 Args[i].first, PtrOff,
746 DAG.getSrcValue(NULL));
747 MemOps.push_back(Store);
748 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000749 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000750 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
751 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000752 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000753 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000754 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000755 }
756 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000757 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
758 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000759 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
760 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000761 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000762 args_to_use.push_back(Load);
763 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000764 }
765 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000766 // If we have any FPRs remaining, we may also have GPRs remaining.
767 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
768 // GPRs.
769 if (GPR_remaining > 0) {
770 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
771 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000772 }
Chris Lattner915fb302005-08-30 00:19:00 +0000773 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
774 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
775 --GPR_remaining;
776 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000777 }
Chris Lattner915fb302005-08-30 00:19:00 +0000778 } else {
779 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
780 Args[i].first, PtrOff,
781 DAG.getSrcValue(NULL)));
782 }
783 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
784 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000785 }
786 }
787 if (!MemOps.empty())
788 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
789 }
790
791 std::vector<MVT::ValueType> RetVals;
792 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000793 MVT::ValueType ActualRetTyVT = RetTyVT;
794 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
795 ActualRetTyVT = MVT::i32; // Promote result to i32.
796
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000797 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000798 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000799 RetVals.push_back(MVT::Other);
800
Chris Lattner2823b3e2005-11-17 05:56:14 +0000801 // If the callee is a GlobalAddress node (quite common, every direct call is)
802 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
803 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
804 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
805
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000806 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
807 Chain, Callee, args_to_use), 0);
808 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
809 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
810 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000811 SDOperand RetVal = TheCall;
812
813 // If the result is a small value, add a note so that we keep track of the
814 // information about whether it is sign or zero extended.
815 if (RetTyVT != ActualRetTyVT) {
816 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
817 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
818 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
819 }
820
821 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000822}
823
Nate Begeman4a959452005-10-18 23:23:37 +0000824SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
825 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000826 SDOperand Copy;
827 switch (Op.getValueType()) {
828 default: assert(0 && "Unknown type to return!");
829 case MVT::i32:
830 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
831 break;
832 case MVT::f32:
833 case MVT::f64:
834 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
835 break;
836 case MVT::i64:
837 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
838 DAG.getConstant(1, MVT::i32));
839 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
840 DAG.getConstant(0, MVT::i32));
841 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
842 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
843 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000844 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000845 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000846}
847
Nate Begeman21e463b2005-10-16 05:39:50 +0000848SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
849 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000850 // vastart just stores the address of the VarArgsFrameIndex slot into the
851 // memory location argument.
852 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
853 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
854 DAG.getSrcValue(VAListV));
855}
856
857std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000858PPCTargetLowering::LowerVAArg(SDOperand Chain,
859 SDOperand VAListP, Value *VAListV,
860 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000861 MVT::ValueType ArgVT = getValueType(ArgTy);
862
863 SDOperand VAList =
864 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
865 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
866 unsigned Amt;
867 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
868 Amt = 4;
869 else {
870 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
871 "Other types should have been promoted for varargs!");
872 Amt = 8;
873 }
874 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
875 DAG.getConstant(Amt, VAList.getValueType()));
876 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
877 VAList, VAListP, DAG.getSrcValue(VAListV));
878 return std::make_pair(Result, Chain);
879}
880
881
Nate Begeman21e463b2005-10-16 05:39:50 +0000882std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000883LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
884 SelectionDAG &DAG) {
885 assert(0 && "LowerFrameReturnAddress unimplemented");
886 abort();
887}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000888
889MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000890PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
891 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000892 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000893 MI->getOpcode() == PPC::SELECT_CC_F4 ||
894 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000895 "Unexpected instr type to insert");
896
897 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
898 // control-flow pattern. The incoming instruction knows the destination vreg
899 // to set, the condition code register to branch on, the true/false values to
900 // select between, and a branch opcode to use.
901 const BasicBlock *LLVM_BB = BB->getBasicBlock();
902 ilist<MachineBasicBlock>::iterator It = BB;
903 ++It;
904
905 // thisMBB:
906 // ...
907 // TrueVal = ...
908 // cmpTY ccX, r1, r2
909 // bCC copy1MBB
910 // fallthrough --> copy0MBB
911 MachineBasicBlock *thisMBB = BB;
912 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
913 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
914 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
915 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
916 MachineFunction *F = BB->getParent();
917 F->getBasicBlockList().insert(It, copy0MBB);
918 F->getBasicBlockList().insert(It, sinkMBB);
919 // Update machine-CFG edges
920 BB->addSuccessor(copy0MBB);
921 BB->addSuccessor(sinkMBB);
922
923 // copy0MBB:
924 // %FalseValue = ...
925 // # fallthrough to sinkMBB
926 BB = copy0MBB;
927
928 // Update machine-CFG edges
929 BB->addSuccessor(sinkMBB);
930
931 // sinkMBB:
932 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
933 // ...
934 BB = sinkMBB;
935 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
936 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
937 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
938
939 delete MI; // The pseudo instruction is gone now.
940 return BB;
941}
942