blob: 48249982b0112586b0ac7b9649ca416fe9610531 [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
Nate Begemanee625572006-01-27 21:09:22 +0000113 // RET must be custom lowered, to meet ABI requirements
114 setOperationAction(ISD::RET , MVT::Other, Custom);
115
Nate Begemanacc398c2006-01-25 18:21:52 +0000116 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
117 setOperationAction(ISD::VASTART , MVT::Other, Custom);
118
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000119 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000120 setOperationAction(ISD::VAARG , MVT::Other, Expand);
121 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
122 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000123 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
124 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
125 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000126
Nate Begemanc09eeec2005-09-06 22:03:27 +0000127 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000128 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000129 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
130 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000131 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
132 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
133 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000134 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000135 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000136 }
137
138 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
139 // 64 bit PowerPC implementations can support i64 types directly
140 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000141 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
142 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000143 } else {
144 // 32 bit PowerPC wants to expand i64 shifts itself.
145 setOperationAction(ISD::SHL, MVT::i64, Custom);
146 setOperationAction(ISD::SRL, MVT::i64, Custom);
147 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000148 }
149
Nate Begeman425a9692005-11-29 08:17:20 +0000150 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000151 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000152 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000153 }
154
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000155 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000156 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000157
158 computeRegisterProperties();
159}
160
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000161const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
162 switch (Opcode) {
163 default: return 0;
164 case PPCISD::FSEL: return "PPCISD::FSEL";
165 case PPCISD::FCFID: return "PPCISD::FCFID";
166 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
167 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
168 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
169 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
170 case PPCISD::Hi: return "PPCISD::Hi";
171 case PPCISD::Lo: return "PPCISD::Lo";
172 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
173 case PPCISD::SRL: return "PPCISD::SRL";
174 case PPCISD::SRA: return "PPCISD::SRA";
175 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000176 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000177 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
178 }
179}
180
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000181/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
182static bool isFloatingPointZero(SDOperand Op) {
183 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
184 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
185 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
186 // Maybe this has already been legalized into the constant pool?
187 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
188 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
189 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
190 }
191 return false;
192}
193
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000194/// LowerOperation - Provide custom lowering hooks for some operations.
195///
Nate Begeman21e463b2005-10-16 05:39:50 +0000196SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000197 switch (Op.getOpcode()) {
198 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000199 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000200 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000201 SDOperand Src = Op.getOperand(0);
202 if (Src.getValueType() == MVT::f32)
203 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
204
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000205 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000206 switch (Op.getValueType()) {
207 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
208 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000209 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000210 break;
211 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000212 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000213 break;
214 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000215
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000216 // Convert the FP value to an int value through memory.
217 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
218 if (Op.getValueType() == MVT::i32)
219 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
220 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000221 }
222 case ISD::SINT_TO_FP: {
223 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
224 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000225 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
226 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000227 if (MVT::f32 == Op.getValueType())
228 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
229 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000230 }
231 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000232 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000233 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
234 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
235 break;
236
237 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
238
239 // Cannot handle SETEQ/SETNE.
240 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
241
242 MVT::ValueType ResVT = Op.getValueType();
243 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
244 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
245 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000246
Chris Lattnerf7605322005-08-31 21:09:52 +0000247 // If the RHS of the comparison is a 0.0, we don't need to do the
248 // subtraction at all.
249 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000250 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000251 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000252 case ISD::SETULT:
253 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000254 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000255 case ISD::SETUGE:
256 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000257 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
258 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000259 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000260 case ISD::SETUGT:
261 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000262 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000263 case ISD::SETULE:
264 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000265 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
266 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000267 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000268 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000269 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000270
Chris Lattnereb255f22005-10-25 20:54:57 +0000271 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000272 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000273 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000274 case ISD::SETULT:
275 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000276 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
277 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
278 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
279 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000280 case ISD::SETUGE:
281 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000282 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
283 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
284 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
285 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000286 case ISD::SETUGT:
287 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000288 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
289 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
290 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
291 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000292 case ISD::SETULE:
293 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000294 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
295 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
296 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
297 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000298 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000299 break;
300 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000301 case ISD::SHL: {
302 assert(Op.getValueType() == MVT::i64 &&
303 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
304 // The generic code does a fine job expanding shift by a constant.
305 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
306
307 // Otherwise, expand into a bunch of logical ops. Note that these ops
308 // depend on the PPC behavior for oversized shift amounts.
309 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
310 DAG.getConstant(0, MVT::i32));
311 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
312 DAG.getConstant(1, MVT::i32));
313 SDOperand Amt = Op.getOperand(1);
314
315 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
316 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000317 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
318 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000319 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
320 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
321 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000322 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000323 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000324 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000325 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
326 }
327 case ISD::SRL: {
328 assert(Op.getValueType() == MVT::i64 &&
329 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
330 // The generic code does a fine job expanding shift by a constant.
331 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
332
333 // Otherwise, expand into a bunch of logical ops. Note that these ops
334 // depend on the PPC behavior for oversized shift amounts.
335 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
336 DAG.getConstant(0, MVT::i32));
337 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
338 DAG.getConstant(1, MVT::i32));
339 SDOperand Amt = Op.getOperand(1);
340
341 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
342 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000343 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
344 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000345 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
346 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
347 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000348 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000349 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000350 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000351 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
352 }
353 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000354 assert(Op.getValueType() == MVT::i64 &&
355 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
356 // The generic code does a fine job expanding shift by a constant.
357 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
358
359 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
360 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
361 DAG.getConstant(0, MVT::i32));
362 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
363 DAG.getConstant(1, MVT::i32));
364 SDOperand Amt = Op.getOperand(1);
365
366 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
367 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000368 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
369 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000370 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
371 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
372 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000373 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
374 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000375 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
376 Tmp4, Tmp6, ISD::SETLE);
377 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000378 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000379 case ISD::ConstantPool: {
380 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
381 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
382 SDOperand Zero = DAG.getConstant(0, MVT::i32);
383
384 if (PPCGenerateStaticCode) {
385 // Generate non-pic code that has direct accesses to the constant pool.
386 // The address of the global is just (hi(&g)+lo(&g)).
387 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
388 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
389 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
390 }
391
392 // Only lower ConstantPool on Darwin.
393 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
394 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
395 if (PICEnabled) {
396 // With PIC, the first instruction is actually "GR+hi(&G)".
397 Hi = DAG.getNode(ISD::ADD, MVT::i32,
398 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
399 }
400
401 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
402 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
403 return Lo;
404 }
Chris Lattner860e8862005-11-17 07:30:41 +0000405 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000406 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
407 GlobalValue *GV = GSDN->getGlobal();
408 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000409 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000410
411 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000412 // Generate non-pic code that has direct accesses to globals.
413 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000414 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
415 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
416 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
417 }
Chris Lattner860e8862005-11-17 07:30:41 +0000418
Chris Lattner1d05cb42005-11-17 18:55:48 +0000419 // Only lower GlobalAddress on Darwin.
420 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000421
Chris Lattner860e8862005-11-17 07:30:41 +0000422 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
423 if (PICEnabled) {
424 // With PIC, the first instruction is actually "GR+hi(&G)".
425 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000426 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000427 }
428
429 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
430 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
431
Chris Lattnera35ef632006-01-06 01:04:03 +0000432 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000433 return Lo;
434
435 // If the global is weak or external, we have to go through the lazy
436 // resolution stub.
437 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
438 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000439 case ISD::VASTART: {
440 // vastart just stores the address of the VarArgsFrameIndex slot into the
441 // memory location argument.
442 // FIXME: Replace MVT::i32 with PointerTy
443 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
444 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
445 Op.getOperand(1), Op.getOperand(2));
446 }
Nate Begemanee625572006-01-27 21:09:22 +0000447 case ISD::RET: {
448 SDOperand Copy;
449
450 switch(Op.getNumOperands()) {
451 default:
452 assert(0 && "Do not know how to return this many arguments!");
453 abort();
454 case 1:
455 return SDOperand(); // ret void is legal
456 case 2: {
457 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
458 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
459 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
460 SDOperand());
461 break;
462 }
463 case 3:
464 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
465 SDOperand());
466 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
467 break;
468 }
469 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
470 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000471 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000472 return SDOperand();
473}
474
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000475std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000476PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000477 //
478 // add beautiful description of PPC stack frame format, or at least some docs
479 //
480 MachineFunction &MF = DAG.getMachineFunction();
481 MachineFrameInfo *MFI = MF.getFrameInfo();
482 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000483 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000484 std::vector<SDOperand> ArgValues;
485
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000486 unsigned ArgOffset = 24;
487 unsigned GPR_remaining = 8;
488 unsigned FPR_remaining = 13;
489 unsigned GPR_idx = 0, FPR_idx = 0;
490 static const unsigned GPR[] = {
491 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
492 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
493 };
494 static const unsigned FPR[] = {
495 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
496 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
497 };
498
499 // Add DAG nodes to load the arguments... On entry to a function on PPC,
500 // the arguments start at offset 24, although they are likely to be passed
501 // in registers.
502 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
503 SDOperand newroot, argt;
504 unsigned ObjSize;
505 bool needsLoad = false;
506 bool ArgLive = !I->use_empty();
507 MVT::ValueType ObjectVT = getValueType(I->getType());
508
509 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000510 default: assert(0 && "Unhandled argument type!");
511 case MVT::i1:
512 case MVT::i8:
513 case MVT::i16:
514 case MVT::i32:
515 ObjSize = 4;
516 if (!ArgLive) break;
517 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000518 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000519 MF.addLiveIn(GPR[GPR_idx], VReg);
520 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000521 if (ObjectVT != MVT::i32) {
522 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
523 : ISD::AssertZext;
524 argt = DAG.getNode(AssertOp, MVT::i32, argt,
525 DAG.getValueType(ObjectVT));
526 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
527 }
Chris Lattner915fb302005-08-30 00:19:00 +0000528 } else {
529 needsLoad = true;
530 }
531 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000532 case MVT::i64:
533 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000534 if (!ArgLive) break;
535 if (GPR_remaining > 0) {
536 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000537 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000538 MF.addLiveIn(GPR[GPR_idx], VReg);
539 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000540 // If we have two or more remaining argument registers, then both halves
541 // of the i64 can be sourced from there. Otherwise, the lower half will
542 // have to come off the stack. This can happen when an i64 is preceded
543 // by 28 bytes of arguments.
544 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000545 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000546 MF.addLiveIn(GPR[GPR_idx+1], VReg);
547 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000548 } else {
549 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
550 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
551 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
552 DAG.getSrcValue(NULL));
553 }
554 // Build the outgoing arg thingy
555 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
556 newroot = argLo;
557 } else {
558 needsLoad = true;
559 }
560 break;
561 case MVT::f32:
562 case MVT::f64:
563 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000564 if (!ArgLive) {
565 if (FPR_remaining > 0) {
566 --FPR_remaining;
567 ++FPR_idx;
568 }
569 break;
570 }
Chris Lattner915fb302005-08-30 00:19:00 +0000571 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000572 unsigned VReg;
573 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000574 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000575 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000576 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000577 MF.addLiveIn(FPR[FPR_idx], VReg);
578 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000579 --FPR_remaining;
580 ++FPR_idx;
581 } else {
582 needsLoad = true;
583 }
584 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000585 }
586
587 // We need to load the argument to a virtual register if we determined above
588 // that we ran out of physical registers of the appropriate type
589 if (needsLoad) {
590 unsigned SubregOffset = 0;
591 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
592 if (ObjectVT == MVT::i16) SubregOffset = 2;
593 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
594 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
595 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
596 DAG.getConstant(SubregOffset, MVT::i32));
597 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
598 DAG.getSrcValue(NULL));
599 }
600
601 // Every 4 bytes of argument space consumes one of the GPRs available for
602 // argument passing.
603 if (GPR_remaining > 0) {
604 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
605 GPR_remaining -= delta;
606 GPR_idx += delta;
607 }
608 ArgOffset += ObjSize;
609 if (newroot.Val)
610 DAG.setRoot(newroot.getValue(1));
611
612 ArgValues.push_back(argt);
613 }
614
615 // If the function takes variable number of arguments, make a frame index for
616 // the start of the first vararg value... for expansion of llvm.va_start.
617 if (F.isVarArg()) {
618 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
619 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
620 // If this function is vararg, store any remaining integer argument regs
621 // to their spots on the stack so that they may be loaded by deferencing the
622 // result of va_next.
623 std::vector<SDOperand> MemOps;
624 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000625 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000626 MF.addLiveIn(GPR[GPR_idx], VReg);
627 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000628 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
629 Val, FIN, DAG.getSrcValue(NULL));
630 MemOps.push_back(Store);
631 // Increment the address by four for the next argument to store
632 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
633 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
634 }
Chris Lattner80720a92005-11-30 20:40:54 +0000635 if (!MemOps.empty()) {
636 MemOps.push_back(DAG.getRoot());
637 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
638 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000639 }
640
641 // Finally, inform the code generator which regs we return values in.
642 switch (getValueType(F.getReturnType())) {
643 default: assert(0 && "Unknown type!");
644 case MVT::isVoid: break;
645 case MVT::i1:
646 case MVT::i8:
647 case MVT::i16:
648 case MVT::i32:
649 MF.addLiveOut(PPC::R3);
650 break;
651 case MVT::i64:
652 MF.addLiveOut(PPC::R3);
653 MF.addLiveOut(PPC::R4);
654 break;
655 case MVT::f32:
656 case MVT::f64:
657 MF.addLiveOut(PPC::F1);
658 break;
659 }
660
661 return ArgValues;
662}
663
664std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000665PPCTargetLowering::LowerCallTo(SDOperand Chain,
666 const Type *RetTy, bool isVarArg,
667 unsigned CallingConv, bool isTailCall,
668 SDOperand Callee, ArgListTy &Args,
669 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000670 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000671 // SelectExpr to use to put the arguments in the appropriate registers.
672 std::vector<SDOperand> args_to_use;
673
674 // Count how many bytes are to be pushed on the stack, including the linkage
675 // area, and parameter passing area.
676 unsigned NumBytes = 24;
677
678 if (Args.empty()) {
679 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
680 DAG.getConstant(NumBytes, getPointerTy()));
681 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000682 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000683 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000684 default: assert(0 && "Unknown value type!");
685 case MVT::i1:
686 case MVT::i8:
687 case MVT::i16:
688 case MVT::i32:
689 case MVT::f32:
690 NumBytes += 4;
691 break;
692 case MVT::i64:
693 case MVT::f64:
694 NumBytes += 8;
695 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000696 }
Chris Lattner915fb302005-08-30 00:19:00 +0000697 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000698
Chris Lattner915fb302005-08-30 00:19:00 +0000699 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
700 // plus 32 bytes of argument space in case any called code gets funky on us.
701 // (Required by ABI to support var arg)
702 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000703
704 // Adjust the stack pointer for the new arguments...
705 // These operations are automatically eliminated by the prolog/epilog pass
706 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
707 DAG.getConstant(NumBytes, getPointerTy()));
708
709 // Set up a copy of the stack pointer for use loading and storing any
710 // arguments that may not fit in the registers available for argument
711 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000712 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000713
714 // Figure out which arguments are going to go in registers, and which in
715 // memory. Also, if this is a vararg function, floating point operations
716 // must be stored to our stack, and loaded into integer regs as well, if
717 // any integer regs are available for argument passing.
718 unsigned ArgOffset = 24;
719 unsigned GPR_remaining = 8;
720 unsigned FPR_remaining = 13;
721
722 std::vector<SDOperand> MemOps;
723 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
724 // PtrOff will be used to store the current argument to the stack if a
725 // register cannot be found for it.
726 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
727 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
728 MVT::ValueType ArgVT = getValueType(Args[i].second);
729
730 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000731 default: assert(0 && "Unexpected ValueType for argument!");
732 case MVT::i1:
733 case MVT::i8:
734 case MVT::i16:
735 // Promote the integer to 32 bits. If the input type is signed use a
736 // sign extend, otherwise use a zero extend.
737 if (Args[i].second->isSigned())
738 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
739 else
740 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
741 // FALL THROUGH
742 case MVT::i32:
743 if (GPR_remaining > 0) {
744 args_to_use.push_back(Args[i].first);
745 --GPR_remaining;
746 } else {
747 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
748 Args[i].first, PtrOff,
749 DAG.getSrcValue(NULL)));
750 }
751 ArgOffset += 4;
752 break;
753 case MVT::i64:
754 // If we have one free GPR left, we can place the upper half of the i64
755 // in it, and store the other half to the stack. If we have two or more
756 // free GPRs, then we can pass both halves of the i64 in registers.
757 if (GPR_remaining > 0) {
758 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
759 Args[i].first, DAG.getConstant(1, MVT::i32));
760 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
761 Args[i].first, DAG.getConstant(0, MVT::i32));
762 args_to_use.push_back(Hi);
763 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000764 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000765 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000766 --GPR_remaining;
767 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000768 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
769 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000770 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000771 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000772 }
Chris Lattner915fb302005-08-30 00:19:00 +0000773 } else {
774 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
775 Args[i].first, PtrOff,
776 DAG.getSrcValue(NULL)));
777 }
778 ArgOffset += 8;
779 break;
780 case MVT::f32:
781 case MVT::f64:
782 if (FPR_remaining > 0) {
783 args_to_use.push_back(Args[i].first);
784 --FPR_remaining;
785 if (isVarArg) {
786 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
787 Args[i].first, PtrOff,
788 DAG.getSrcValue(NULL));
789 MemOps.push_back(Store);
790 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000791 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000792 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
793 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000794 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000795 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000796 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000797 }
798 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000799 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
800 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000801 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
802 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000803 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000804 args_to_use.push_back(Load);
805 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000806 }
807 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000808 // If we have any FPRs remaining, we may also have GPRs remaining.
809 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
810 // GPRs.
811 if (GPR_remaining > 0) {
812 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
813 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000814 }
Chris Lattner915fb302005-08-30 00:19:00 +0000815 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
816 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
817 --GPR_remaining;
818 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000819 }
Chris Lattner915fb302005-08-30 00:19:00 +0000820 } else {
821 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
822 Args[i].first, PtrOff,
823 DAG.getSrcValue(NULL)));
824 }
825 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
826 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000827 }
828 }
829 if (!MemOps.empty())
830 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
831 }
832
833 std::vector<MVT::ValueType> RetVals;
834 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000835 MVT::ValueType ActualRetTyVT = RetTyVT;
836 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
837 ActualRetTyVT = MVT::i32; // Promote result to i32.
838
Chris Lattnere00ebf02006-01-28 07:33:03 +0000839 if (RetTyVT == MVT::i64) {
840 RetVals.push_back(MVT::i32);
841 RetVals.push_back(MVT::i32);
842 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000843 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000844 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000845 RetVals.push_back(MVT::Other);
846
Chris Lattner2823b3e2005-11-17 05:56:14 +0000847 // If the callee is a GlobalAddress node (quite common, every direct call is)
848 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
849 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
850 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
851
Chris Lattner281b55e2006-01-27 23:34:02 +0000852 std::vector<SDOperand> Ops;
853 Ops.push_back(Chain);
854 Ops.push_back(Callee);
855 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
856 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000857 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000858 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
859 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000860 SDOperand RetVal = TheCall;
861
862 // If the result is a small value, add a note so that we keep track of the
863 // information about whether it is sign or zero extended.
864 if (RetTyVT != ActualRetTyVT) {
865 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
866 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
867 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000868 } else if (RetTyVT == MVT::i64) {
869 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000870 }
871
872 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000873}
874
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000875MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000876PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
877 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000878 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000879 MI->getOpcode() == PPC::SELECT_CC_F4 ||
880 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000881 "Unexpected instr type to insert");
882
883 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
884 // control-flow pattern. The incoming instruction knows the destination vreg
885 // to set, the condition code register to branch on, the true/false values to
886 // select between, and a branch opcode to use.
887 const BasicBlock *LLVM_BB = BB->getBasicBlock();
888 ilist<MachineBasicBlock>::iterator It = BB;
889 ++It;
890
891 // thisMBB:
892 // ...
893 // TrueVal = ...
894 // cmpTY ccX, r1, r2
895 // bCC copy1MBB
896 // fallthrough --> copy0MBB
897 MachineBasicBlock *thisMBB = BB;
898 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
899 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
900 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
901 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
902 MachineFunction *F = BB->getParent();
903 F->getBasicBlockList().insert(It, copy0MBB);
904 F->getBasicBlockList().insert(It, sinkMBB);
905 // Update machine-CFG edges
906 BB->addSuccessor(copy0MBB);
907 BB->addSuccessor(sinkMBB);
908
909 // copy0MBB:
910 // %FalseValue = ...
911 // # fallthrough to sinkMBB
912 BB = copy0MBB;
913
914 // Update machine-CFG edges
915 BB->addSuccessor(sinkMBB);
916
917 // sinkMBB:
918 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
919 // ...
920 BB = sinkMBB;
921 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
922 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
923 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
924
925 delete MI; // The pseudo instruction is gone now.
926 return BB;
927}
928