blob: df69a84acbcfb6ea4e6b5a2191c2539b26420646 [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 Begemanacc398c2006-01-25 18:21:52 +0000113 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
114 setOperationAction(ISD::VASTART , MVT::Other, Custom);
115
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000116 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000117 setOperationAction(ISD::VAARG , MVT::Other, Expand);
118 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
119 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000120 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
121 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
122 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000123
Nate Begemanc09eeec2005-09-06 22:03:27 +0000124 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000125 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000126 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
127 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000128 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
129 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
130 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000131 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000132 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000133 }
134
135 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
136 // 64 bit PowerPC implementations can support i64 types directly
137 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000138 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
139 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000140 } else {
141 // 32 bit PowerPC wants to expand i64 shifts itself.
142 setOperationAction(ISD::SHL, MVT::i64, Custom);
143 setOperationAction(ISD::SRL, MVT::i64, Custom);
144 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000145 }
146
Nate Begeman425a9692005-11-29 08:17:20 +0000147 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000148 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000149 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000150 }
151
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000152 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng7558b0e2006-01-25 09:15:54 +0000153 setSchedulingPreference(SchedulingForLatency);
Chris Lattnercadd7422006-01-13 17:52:03 +0000154 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000155
156 computeRegisterProperties();
157}
158
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000159const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
160 switch (Opcode) {
161 default: return 0;
162 case PPCISD::FSEL: return "PPCISD::FSEL";
163 case PPCISD::FCFID: return "PPCISD::FCFID";
164 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
165 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
166 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
167 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
168 case PPCISD::Hi: return "PPCISD::Hi";
169 case PPCISD::Lo: return "PPCISD::Lo";
170 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
171 case PPCISD::SRL: return "PPCISD::SRL";
172 case PPCISD::SRA: return "PPCISD::SRA";
173 case PPCISD::SHL: return "PPCISD::SHL";
174 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
175 }
176}
177
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000178/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
179static bool isFloatingPointZero(SDOperand Op) {
180 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
181 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
182 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
183 // Maybe this has already been legalized into the constant pool?
184 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
185 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
186 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
187 }
188 return false;
189}
190
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000191/// LowerOperation - Provide custom lowering hooks for some operations.
192///
Nate Begeman21e463b2005-10-16 05:39:50 +0000193SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000194 switch (Op.getOpcode()) {
195 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000196 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000197 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000198 SDOperand Src = Op.getOperand(0);
199 if (Src.getValueType() == MVT::f32)
200 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
201
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000202 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000203 switch (Op.getValueType()) {
204 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
205 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000206 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000207 break;
208 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000209 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000210 break;
211 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000212
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000213 // Convert the FP value to an int value through memory.
214 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
215 if (Op.getValueType() == MVT::i32)
216 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
217 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000218 }
219 case ISD::SINT_TO_FP: {
220 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
221 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000222 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
223 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000224 if (MVT::f32 == Op.getValueType())
225 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
226 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000227 }
228 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000229 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000230 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
231 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
232 break;
233
234 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
235
236 // Cannot handle SETEQ/SETNE.
237 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
238
239 MVT::ValueType ResVT = Op.getValueType();
240 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
241 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
242 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000243
Chris Lattnerf7605322005-08-31 21:09:52 +0000244 // If the RHS of the comparison is a 0.0, we don't need to do the
245 // subtraction at all.
246 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000247 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000248 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000249 case ISD::SETULT:
250 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000251 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000252 case ISD::SETUGE:
253 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000254 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
255 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000256 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000257 case ISD::SETUGT:
258 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000259 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000260 case ISD::SETULE:
261 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000262 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
263 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000264 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000265 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000266 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000267
Chris Lattnereb255f22005-10-25 20:54:57 +0000268 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000269 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000270 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000271 case ISD::SETULT:
272 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000273 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
274 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
275 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
276 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000277 case ISD::SETUGE:
278 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000279 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
280 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
281 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
282 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000283 case ISD::SETUGT:
284 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000285 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
286 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
287 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
288 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000289 case ISD::SETULE:
290 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000291 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
292 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
293 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
294 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000295 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000296 break;
297 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000298 case ISD::SHL: {
299 assert(Op.getValueType() == MVT::i64 &&
300 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
301 // The generic code does a fine job expanding shift by a constant.
302 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
303
304 // Otherwise, expand into a bunch of logical ops. Note that these ops
305 // depend on the PPC behavior for oversized shift amounts.
306 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
307 DAG.getConstant(0, MVT::i32));
308 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
309 DAG.getConstant(1, MVT::i32));
310 SDOperand Amt = Op.getOperand(1);
311
312 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
313 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000314 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
315 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000316 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
317 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
318 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000319 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000320 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000321 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000322 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
323 }
324 case ISD::SRL: {
325 assert(Op.getValueType() == MVT::i64 &&
326 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
327 // The generic code does a fine job expanding shift by a constant.
328 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
329
330 // Otherwise, expand into a bunch of logical ops. Note that these ops
331 // depend on the PPC behavior for oversized shift amounts.
332 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
333 DAG.getConstant(0, MVT::i32));
334 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
335 DAG.getConstant(1, MVT::i32));
336 SDOperand Amt = Op.getOperand(1);
337
338 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
339 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000340 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
341 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000342 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
343 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
344 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000345 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000346 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000347 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000348 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
349 }
350 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000351 assert(Op.getValueType() == MVT::i64 &&
352 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
353 // The generic code does a fine job expanding shift by a constant.
354 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
355
356 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
357 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
358 DAG.getConstant(0, MVT::i32));
359 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
360 DAG.getConstant(1, MVT::i32));
361 SDOperand Amt = Op.getOperand(1);
362
363 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
364 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000365 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
366 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000367 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
368 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
369 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000370 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
371 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000372 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
373 Tmp4, Tmp6, ISD::SETLE);
374 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000375 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000376 case ISD::ConstantPool: {
377 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
378 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
379 SDOperand Zero = DAG.getConstant(0, MVT::i32);
380
381 if (PPCGenerateStaticCode) {
382 // Generate non-pic code that has direct accesses to the constant pool.
383 // The address of the global is just (hi(&g)+lo(&g)).
384 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
385 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
386 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
387 }
388
389 // Only lower ConstantPool on Darwin.
390 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
391 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
392 if (PICEnabled) {
393 // With PIC, the first instruction is actually "GR+hi(&G)".
394 Hi = DAG.getNode(ISD::ADD, MVT::i32,
395 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
396 }
397
398 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
399 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
400 return Lo;
401 }
Chris Lattner860e8862005-11-17 07:30:41 +0000402 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000403 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
404 GlobalValue *GV = GSDN->getGlobal();
405 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000406 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000407
408 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000409 // Generate non-pic code that has direct accesses to globals.
410 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000411 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
412 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
413 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
414 }
Chris Lattner860e8862005-11-17 07:30:41 +0000415
Chris Lattner1d05cb42005-11-17 18:55:48 +0000416 // Only lower GlobalAddress on Darwin.
417 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000418
Chris Lattner860e8862005-11-17 07:30:41 +0000419 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
420 if (PICEnabled) {
421 // With PIC, the first instruction is actually "GR+hi(&G)".
422 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000423 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000424 }
425
426 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
427 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
428
Chris Lattnera35ef632006-01-06 01:04:03 +0000429 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000430 return Lo;
431
432 // If the global is weak or external, we have to go through the lazy
433 // resolution stub.
434 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
435 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000436 case ISD::VASTART: {
437 // vastart just stores the address of the VarArgsFrameIndex slot into the
438 // memory location argument.
439 // FIXME: Replace MVT::i32 with PointerTy
440 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
441 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
442 Op.getOperand(1), Op.getOperand(2));
443 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000444 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000445 return SDOperand();
446}
447
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000448std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000449PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000450 //
451 // add beautiful description of PPC stack frame format, or at least some docs
452 //
453 MachineFunction &MF = DAG.getMachineFunction();
454 MachineFrameInfo *MFI = MF.getFrameInfo();
455 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000456 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000457 std::vector<SDOperand> ArgValues;
458
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000459 unsigned ArgOffset = 24;
460 unsigned GPR_remaining = 8;
461 unsigned FPR_remaining = 13;
462 unsigned GPR_idx = 0, FPR_idx = 0;
463 static const unsigned GPR[] = {
464 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
465 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
466 };
467 static const unsigned FPR[] = {
468 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
469 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
470 };
471
472 // Add DAG nodes to load the arguments... On entry to a function on PPC,
473 // the arguments start at offset 24, although they are likely to be passed
474 // in registers.
475 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
476 SDOperand newroot, argt;
477 unsigned ObjSize;
478 bool needsLoad = false;
479 bool ArgLive = !I->use_empty();
480 MVT::ValueType ObjectVT = getValueType(I->getType());
481
482 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000483 default: assert(0 && "Unhandled argument type!");
484 case MVT::i1:
485 case MVT::i8:
486 case MVT::i16:
487 case MVT::i32:
488 ObjSize = 4;
489 if (!ArgLive) break;
490 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000491 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000492 MF.addLiveIn(GPR[GPR_idx], VReg);
493 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000494 if (ObjectVT != MVT::i32) {
495 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
496 : ISD::AssertZext;
497 argt = DAG.getNode(AssertOp, MVT::i32, argt,
498 DAG.getValueType(ObjectVT));
499 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
500 }
Chris Lattner915fb302005-08-30 00:19:00 +0000501 } else {
502 needsLoad = true;
503 }
504 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000505 case MVT::i64:
506 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000507 if (!ArgLive) break;
508 if (GPR_remaining > 0) {
509 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000510 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000511 MF.addLiveIn(GPR[GPR_idx], VReg);
512 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000513 // If we have two or more remaining argument registers, then both halves
514 // of the i64 can be sourced from there. Otherwise, the lower half will
515 // have to come off the stack. This can happen when an i64 is preceded
516 // by 28 bytes of arguments.
517 if (GPR_remaining > 1) {
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+1], VReg);
520 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000521 } else {
522 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
523 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
524 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
525 DAG.getSrcValue(NULL));
526 }
527 // Build the outgoing arg thingy
528 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
529 newroot = argLo;
530 } else {
531 needsLoad = true;
532 }
533 break;
534 case MVT::f32:
535 case MVT::f64:
536 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000537 if (!ArgLive) {
538 if (FPR_remaining > 0) {
539 --FPR_remaining;
540 ++FPR_idx;
541 }
542 break;
543 }
Chris Lattner915fb302005-08-30 00:19:00 +0000544 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000545 unsigned VReg;
546 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000547 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000548 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000549 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000550 MF.addLiveIn(FPR[FPR_idx], VReg);
551 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000552 --FPR_remaining;
553 ++FPR_idx;
554 } else {
555 needsLoad = true;
556 }
557 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000558 }
559
560 // We need to load the argument to a virtual register if we determined above
561 // that we ran out of physical registers of the appropriate type
562 if (needsLoad) {
563 unsigned SubregOffset = 0;
564 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
565 if (ObjectVT == MVT::i16) SubregOffset = 2;
566 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
567 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
568 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
569 DAG.getConstant(SubregOffset, MVT::i32));
570 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
571 DAG.getSrcValue(NULL));
572 }
573
574 // Every 4 bytes of argument space consumes one of the GPRs available for
575 // argument passing.
576 if (GPR_remaining > 0) {
577 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
578 GPR_remaining -= delta;
579 GPR_idx += delta;
580 }
581 ArgOffset += ObjSize;
582 if (newroot.Val)
583 DAG.setRoot(newroot.getValue(1));
584
585 ArgValues.push_back(argt);
586 }
587
588 // If the function takes variable number of arguments, make a frame index for
589 // the start of the first vararg value... for expansion of llvm.va_start.
590 if (F.isVarArg()) {
591 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
592 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
593 // If this function is vararg, store any remaining integer argument regs
594 // to their spots on the stack so that they may be loaded by deferencing the
595 // result of va_next.
596 std::vector<SDOperand> MemOps;
597 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000598 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000599 MF.addLiveIn(GPR[GPR_idx], VReg);
600 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000601 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
602 Val, FIN, DAG.getSrcValue(NULL));
603 MemOps.push_back(Store);
604 // Increment the address by four for the next argument to store
605 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
606 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
607 }
Chris Lattner80720a92005-11-30 20:40:54 +0000608 if (!MemOps.empty()) {
609 MemOps.push_back(DAG.getRoot());
610 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
611 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000612 }
613
614 // Finally, inform the code generator which regs we return values in.
615 switch (getValueType(F.getReturnType())) {
616 default: assert(0 && "Unknown type!");
617 case MVT::isVoid: break;
618 case MVT::i1:
619 case MVT::i8:
620 case MVT::i16:
621 case MVT::i32:
622 MF.addLiveOut(PPC::R3);
623 break;
624 case MVT::i64:
625 MF.addLiveOut(PPC::R3);
626 MF.addLiveOut(PPC::R4);
627 break;
628 case MVT::f32:
629 case MVT::f64:
630 MF.addLiveOut(PPC::F1);
631 break;
632 }
633
634 return ArgValues;
635}
636
637std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000638PPCTargetLowering::LowerCallTo(SDOperand Chain,
639 const Type *RetTy, bool isVarArg,
640 unsigned CallingConv, bool isTailCall,
641 SDOperand Callee, ArgListTy &Args,
642 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000643 // args_to_use will accumulate outgoing args for the ISD::CALL case in
644 // SelectExpr to use to put the arguments in the appropriate registers.
645 std::vector<SDOperand> args_to_use;
646
647 // Count how many bytes are to be pushed on the stack, including the linkage
648 // area, and parameter passing area.
649 unsigned NumBytes = 24;
650
651 if (Args.empty()) {
652 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
653 DAG.getConstant(NumBytes, getPointerTy()));
654 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000655 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000656 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000657 default: assert(0 && "Unknown value type!");
658 case MVT::i1:
659 case MVT::i8:
660 case MVT::i16:
661 case MVT::i32:
662 case MVT::f32:
663 NumBytes += 4;
664 break;
665 case MVT::i64:
666 case MVT::f64:
667 NumBytes += 8;
668 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000669 }
Chris Lattner915fb302005-08-30 00:19:00 +0000670 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000671
Chris Lattner915fb302005-08-30 00:19:00 +0000672 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
673 // plus 32 bytes of argument space in case any called code gets funky on us.
674 // (Required by ABI to support var arg)
675 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000676
677 // Adjust the stack pointer for the new arguments...
678 // These operations are automatically eliminated by the prolog/epilog pass
679 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
680 DAG.getConstant(NumBytes, getPointerTy()));
681
682 // Set up a copy of the stack pointer for use loading and storing any
683 // arguments that may not fit in the registers available for argument
684 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000685 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000686
687 // Figure out which arguments are going to go in registers, and which in
688 // memory. Also, if this is a vararg function, floating point operations
689 // must be stored to our stack, and loaded into integer regs as well, if
690 // any integer regs are available for argument passing.
691 unsigned ArgOffset = 24;
692 unsigned GPR_remaining = 8;
693 unsigned FPR_remaining = 13;
694
695 std::vector<SDOperand> MemOps;
696 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
697 // PtrOff will be used to store the current argument to the stack if a
698 // register cannot be found for it.
699 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
700 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
701 MVT::ValueType ArgVT = getValueType(Args[i].second);
702
703 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000704 default: assert(0 && "Unexpected ValueType for argument!");
705 case MVT::i1:
706 case MVT::i8:
707 case MVT::i16:
708 // Promote the integer to 32 bits. If the input type is signed use a
709 // sign extend, otherwise use a zero extend.
710 if (Args[i].second->isSigned())
711 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
712 else
713 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
714 // FALL THROUGH
715 case MVT::i32:
716 if (GPR_remaining > 0) {
717 args_to_use.push_back(Args[i].first);
718 --GPR_remaining;
719 } else {
720 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
721 Args[i].first, PtrOff,
722 DAG.getSrcValue(NULL)));
723 }
724 ArgOffset += 4;
725 break;
726 case MVT::i64:
727 // If we have one free GPR left, we can place the upper half of the i64
728 // in it, and store the other half to the stack. If we have two or more
729 // free GPRs, then we can pass both halves of the i64 in registers.
730 if (GPR_remaining > 0) {
731 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
732 Args[i].first, DAG.getConstant(1, MVT::i32));
733 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
734 Args[i].first, DAG.getConstant(0, MVT::i32));
735 args_to_use.push_back(Hi);
736 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000737 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000738 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000739 --GPR_remaining;
740 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000741 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
742 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000743 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000744 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000745 }
Chris Lattner915fb302005-08-30 00:19:00 +0000746 } else {
747 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
748 Args[i].first, PtrOff,
749 DAG.getSrcValue(NULL)));
750 }
751 ArgOffset += 8;
752 break;
753 case MVT::f32:
754 case MVT::f64:
755 if (FPR_remaining > 0) {
756 args_to_use.push_back(Args[i].first);
757 --FPR_remaining;
758 if (isVarArg) {
759 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
760 Args[i].first, PtrOff,
761 DAG.getSrcValue(NULL));
762 MemOps.push_back(Store);
763 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000764 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000765 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
766 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000767 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000768 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000769 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000770 }
771 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000772 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
773 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000774 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
775 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000776 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000777 args_to_use.push_back(Load);
778 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000779 }
780 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000781 // If we have any FPRs remaining, we may also have GPRs remaining.
782 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
783 // GPRs.
784 if (GPR_remaining > 0) {
785 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
786 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000787 }
Chris Lattner915fb302005-08-30 00:19:00 +0000788 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
789 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
790 --GPR_remaining;
791 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000792 }
Chris Lattner915fb302005-08-30 00:19:00 +0000793 } else {
794 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
795 Args[i].first, PtrOff,
796 DAG.getSrcValue(NULL)));
797 }
798 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
799 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000800 }
801 }
802 if (!MemOps.empty())
803 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
804 }
805
806 std::vector<MVT::ValueType> RetVals;
807 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000808 MVT::ValueType ActualRetTyVT = RetTyVT;
809 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
810 ActualRetTyVT = MVT::i32; // Promote result to i32.
811
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000812 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000813 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000814 RetVals.push_back(MVT::Other);
815
Chris Lattner2823b3e2005-11-17 05:56:14 +0000816 // If the callee is a GlobalAddress node (quite common, every direct call is)
817 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
818 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
819 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
820
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000821 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
822 Chain, Callee, args_to_use), 0);
823 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
824 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
825 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000826 SDOperand RetVal = TheCall;
827
828 // If the result is a small value, add a note so that we keep track of the
829 // information about whether it is sign or zero extended.
830 if (RetTyVT != ActualRetTyVT) {
831 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
832 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
833 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
834 }
835
836 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000837}
838
Nate Begeman4a959452005-10-18 23:23:37 +0000839SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
840 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000841 SDOperand Copy;
842 switch (Op.getValueType()) {
843 default: assert(0 && "Unknown type to return!");
844 case MVT::i32:
845 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
846 break;
847 case MVT::f32:
848 case MVT::f64:
849 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
850 break;
851 case MVT::i64:
852 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
853 DAG.getConstant(1, MVT::i32));
854 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
855 DAG.getConstant(0, MVT::i32));
856 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
857 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
858 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000859 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000860 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000861}
862
Nate Begeman21e463b2005-10-16 05:39:50 +0000863std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000864LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
865 SelectionDAG &DAG) {
866 assert(0 && "LowerFrameReturnAddress unimplemented");
867 abort();
868}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000869
870MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000871PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
872 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000873 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000874 MI->getOpcode() == PPC::SELECT_CC_F4 ||
875 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000876 "Unexpected instr type to insert");
877
878 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
879 // control-flow pattern. The incoming instruction knows the destination vreg
880 // to set, the condition code register to branch on, the true/false values to
881 // select between, and a branch opcode to use.
882 const BasicBlock *LLVM_BB = BB->getBasicBlock();
883 ilist<MachineBasicBlock>::iterator It = BB;
884 ++It;
885
886 // thisMBB:
887 // ...
888 // TrueVal = ...
889 // cmpTY ccX, r1, r2
890 // bCC copy1MBB
891 // fallthrough --> copy0MBB
892 MachineBasicBlock *thisMBB = BB;
893 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
894 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
895 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
896 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
897 MachineFunction *F = BB->getParent();
898 F->getBasicBlockList().insert(It, copy0MBB);
899 F->getBasicBlockList().insert(It, sinkMBB);
900 // Update machine-CFG edges
901 BB->addSuccessor(copy0MBB);
902 BB->addSuccessor(sinkMBB);
903
904 // copy0MBB:
905 // %FalseValue = ...
906 // # fallthrough to sinkMBB
907 BB = copy0MBB;
908
909 // Update machine-CFG edges
910 BB->addSuccessor(sinkMBB);
911
912 // sinkMBB:
913 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
914 // ...
915 BB = sinkMBB;
916 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
917 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
918 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
919
920 delete MI; // The pseudo instruction is gone now.
921 return BB;
922}
923