blob: 629b7cabf8c4b1159496a35028a63f1f756e8287 [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
15#include "PPCTargetMachine.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000019#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000021#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022#include "llvm/Function.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000023using namespace llvm;
24
Nate Begeman21e463b2005-10-16 05:39:50 +000025PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026 : TargetLowering(TM) {
27
28 // Fold away setcc operations if possible.
29 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000030 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000031
Chris Lattnerd145a612005-09-27 22:18:25 +000032 // Use _setjmp/_longjmp instead of setjmp/longjmp.
33 setUseUnderscoreSetJmpLongJmp(true);
34
Chris Lattner7c5a3d32005-08-16 17:14:42 +000035 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000036 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
37 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
38 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000039
40 // PowerPC has no intrinsics for these particular operations
41 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
42 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
43 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
44
45 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
46 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
47 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
48
49 // PowerPC has no SREM/UREM instructions
50 setOperationAction(ISD::SREM, MVT::i32, Expand);
51 setOperationAction(ISD::UREM, MVT::i32, Expand);
52
53 // We don't support sin/cos/sqrt/fmod
54 setOperationAction(ISD::FSIN , MVT::f64, Expand);
55 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000056 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000057 setOperationAction(ISD::FSIN , MVT::f32, Expand);
58 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000059 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000060
61 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000062 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000063 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
64 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
65 }
66
Nate Begemand88fc032006-01-14 03:14:10 +000067 // PowerPC does not have BSWAP, CTPOP or CTTZ
68 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000069 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
70 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
71
Nate Begeman35ef9132006-01-11 21:21:00 +000072 // PowerPC does not have ROTR
73 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
74
Chris Lattner7c5a3d32005-08-16 17:14:42 +000075 // PowerPC does not have Select
76 setOperationAction(ISD::SELECT, MVT::i32, Expand);
77 setOperationAction(ISD::SELECT, MVT::f32, Expand);
78 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000079
Chris Lattner0b1e4e52005-08-26 17:36:52 +000080 // PowerPC wants to turn select_cc of FP into fsel when possible.
81 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
82 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000083
Nate Begeman7cbd5252005-08-16 19:49:35 +000084 // PowerPC does not have BRCOND* which requires SetCC
85 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
86 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000087
Chris Lattnerf7605322005-08-31 21:09:52 +000088 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
89 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000090
Jim Laskeyad23c9d2005-08-17 00:40:22 +000091 // PowerPC does not have [U|S]INT_TO_FP
92 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
93 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
94
Chris Lattner53e88452005-12-23 05:13:35 +000095 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
96 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
97
Chris Lattnere6ec9f22005-09-10 00:21:06 +000098 // PowerPC does not have truncstore for i1.
99 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000100
Jim Laskeyabf6d172006-01-05 01:25:28 +0000101 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000102 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000103 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000104 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000105 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000106 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000107
Nate Begeman28a6b022005-12-10 02:36:00 +0000108 // We want to legalize GlobalAddress and ConstantPool nodes into the
109 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000110 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000111 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000112
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000113 // Use the default implementation.
114 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
115 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
116 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000117
Nate Begemanc09eeec2005-09-06 22:03:27 +0000118 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000119 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000120 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
121 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000122 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
123 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
124 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000125 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000126 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000127 }
128
129 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
130 // 64 bit PowerPC implementations can support i64 types directly
131 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000132 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
133 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000134 } else {
135 // 32 bit PowerPC wants to expand i64 shifts itself.
136 setOperationAction(ISD::SHL, MVT::i64, Custom);
137 setOperationAction(ISD::SRL, MVT::i64, Custom);
138 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000139 }
140
Nate Begeman425a9692005-11-29 08:17:20 +0000141 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000142 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000143 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000144 }
145
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000146 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng7558b0e2006-01-25 09:15:54 +0000147 setSchedulingPreference(SchedulingForLatency);
Chris Lattnercadd7422006-01-13 17:52:03 +0000148 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000149
150 computeRegisterProperties();
151}
152
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000153const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
154 switch (Opcode) {
155 default: return 0;
156 case PPCISD::FSEL: return "PPCISD::FSEL";
157 case PPCISD::FCFID: return "PPCISD::FCFID";
158 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
159 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
160 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
161 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
162 case PPCISD::Hi: return "PPCISD::Hi";
163 case PPCISD::Lo: return "PPCISD::Lo";
164 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
165 case PPCISD::SRL: return "PPCISD::SRL";
166 case PPCISD::SRA: return "PPCISD::SRA";
167 case PPCISD::SHL: return "PPCISD::SHL";
168 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
169 }
170}
171
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000172/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
173static bool isFloatingPointZero(SDOperand Op) {
174 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
175 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
176 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
177 // Maybe this has already been legalized into the constant pool?
178 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
179 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
180 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
181 }
182 return false;
183}
184
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000185/// LowerOperation - Provide custom lowering hooks for some operations.
186///
Nate Begeman21e463b2005-10-16 05:39:50 +0000187SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000188 switch (Op.getOpcode()) {
189 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000190 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000191 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000192 SDOperand Src = Op.getOperand(0);
193 if (Src.getValueType() == MVT::f32)
194 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
195
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000196 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000197 switch (Op.getValueType()) {
198 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
199 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000200 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000201 break;
202 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000203 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000204 break;
205 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000206
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000207 // Convert the FP value to an int value through memory.
208 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
209 if (Op.getValueType() == MVT::i32)
210 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
211 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000212 }
213 case ISD::SINT_TO_FP: {
214 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
215 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000216 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
217 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000218 if (MVT::f32 == Op.getValueType())
219 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
220 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000221 }
222 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000223 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000224 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
225 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
226 break;
227
228 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
229
230 // Cannot handle SETEQ/SETNE.
231 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
232
233 MVT::ValueType ResVT = Op.getValueType();
234 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
235 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
236 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000237
Chris Lattnerf7605322005-08-31 21:09:52 +0000238 // If the RHS of the comparison is a 0.0, we don't need to do the
239 // subtraction at all.
240 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000241 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000242 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000243 case ISD::SETULT:
244 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000245 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000246 case ISD::SETUGE:
247 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000248 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
249 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000250 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000251 case ISD::SETUGT:
252 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000253 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000254 case ISD::SETULE:
255 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000256 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
257 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000258 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000259 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000260 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000261
Chris Lattnereb255f22005-10-25 20:54:57 +0000262 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000263 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000264 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000265 case ISD::SETULT:
266 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000267 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
268 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
269 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
270 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000271 case ISD::SETUGE:
272 case ISD::SETGE:
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, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000277 case ISD::SETUGT:
278 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000279 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
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, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000283 case ISD::SETULE:
284 case ISD::SETLE:
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, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000289 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000290 break;
291 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000292 case ISD::SHL: {
293 assert(Op.getValueType() == MVT::i64 &&
294 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
295 // The generic code does a fine job expanding shift by a constant.
296 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
297
298 // Otherwise, expand into a bunch of logical ops. Note that these ops
299 // depend on the PPC behavior for oversized shift amounts.
300 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
301 DAG.getConstant(0, MVT::i32));
302 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
303 DAG.getConstant(1, MVT::i32));
304 SDOperand Amt = Op.getOperand(1);
305
306 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
307 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000308 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
309 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000310 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
311 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
312 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000313 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000314 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000315 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000316 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
317 }
318 case ISD::SRL: {
319 assert(Op.getValueType() == MVT::i64 &&
320 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
321 // The generic code does a fine job expanding shift by a constant.
322 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
323
324 // Otherwise, expand into a bunch of logical ops. Note that these ops
325 // depend on the PPC behavior for oversized shift amounts.
326 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
327 DAG.getConstant(0, MVT::i32));
328 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
329 DAG.getConstant(1, MVT::i32));
330 SDOperand Amt = Op.getOperand(1);
331
332 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
333 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000334 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
335 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000336 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
337 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
338 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000339 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000340 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000341 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000342 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
343 }
344 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000345 assert(Op.getValueType() == MVT::i64 &&
346 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
347 // The generic code does a fine job expanding shift by a constant.
348 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
349
350 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
351 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
352 DAG.getConstant(0, MVT::i32));
353 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
354 DAG.getConstant(1, MVT::i32));
355 SDOperand Amt = Op.getOperand(1);
356
357 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
358 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000359 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
360 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000361 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
362 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
363 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000364 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
365 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000366 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
367 Tmp4, Tmp6, ISD::SETLE);
368 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000369 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000370 case ISD::ConstantPool: {
371 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
372 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
373 SDOperand Zero = DAG.getConstant(0, MVT::i32);
374
375 if (PPCGenerateStaticCode) {
376 // Generate non-pic code that has direct accesses to the constant pool.
377 // The address of the global is just (hi(&g)+lo(&g)).
378 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
379 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
380 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
381 }
382
383 // Only lower ConstantPool on Darwin.
384 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
385 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
386 if (PICEnabled) {
387 // With PIC, the first instruction is actually "GR+hi(&G)".
388 Hi = DAG.getNode(ISD::ADD, MVT::i32,
389 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
390 }
391
392 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
393 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
394 return Lo;
395 }
Chris Lattner860e8862005-11-17 07:30:41 +0000396 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000397 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
398 GlobalValue *GV = GSDN->getGlobal();
399 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000400 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000401
402 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000403 // Generate non-pic code that has direct accesses to globals.
404 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000405 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
406 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
407 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
408 }
Chris Lattner860e8862005-11-17 07:30:41 +0000409
Chris Lattner1d05cb42005-11-17 18:55:48 +0000410 // Only lower GlobalAddress on Darwin.
411 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000412
Chris Lattner860e8862005-11-17 07:30:41 +0000413 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
414 if (PICEnabled) {
415 // With PIC, the first instruction is actually "GR+hi(&G)".
416 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000417 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000418 }
419
420 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
421 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
422
Chris Lattnera35ef632006-01-06 01:04:03 +0000423 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000424 return Lo;
425
426 // If the global is weak or external, we have to go through the lazy
427 // resolution stub.
428 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
429 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000430 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000431 return SDOperand();
432}
433
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000434std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000435PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000436 //
437 // add beautiful description of PPC stack frame format, or at least some docs
438 //
439 MachineFunction &MF = DAG.getMachineFunction();
440 MachineFrameInfo *MFI = MF.getFrameInfo();
441 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000442 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000443 std::vector<SDOperand> ArgValues;
444
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000445 unsigned ArgOffset = 24;
446 unsigned GPR_remaining = 8;
447 unsigned FPR_remaining = 13;
448 unsigned GPR_idx = 0, FPR_idx = 0;
449 static const unsigned GPR[] = {
450 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
451 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
452 };
453 static const unsigned FPR[] = {
454 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
455 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
456 };
457
458 // Add DAG nodes to load the arguments... On entry to a function on PPC,
459 // the arguments start at offset 24, although they are likely to be passed
460 // in registers.
461 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
462 SDOperand newroot, argt;
463 unsigned ObjSize;
464 bool needsLoad = false;
465 bool ArgLive = !I->use_empty();
466 MVT::ValueType ObjectVT = getValueType(I->getType());
467
468 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000469 default: assert(0 && "Unhandled argument type!");
470 case MVT::i1:
471 case MVT::i8:
472 case MVT::i16:
473 case MVT::i32:
474 ObjSize = 4;
475 if (!ArgLive) break;
476 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000477 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000478 MF.addLiveIn(GPR[GPR_idx], VReg);
479 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000480 if (ObjectVT != MVT::i32) {
481 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
482 : ISD::AssertZext;
483 argt = DAG.getNode(AssertOp, MVT::i32, argt,
484 DAG.getValueType(ObjectVT));
485 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
486 }
Chris Lattner915fb302005-08-30 00:19:00 +0000487 } else {
488 needsLoad = true;
489 }
490 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000491 case MVT::i64:
492 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000493 if (!ArgLive) break;
494 if (GPR_remaining > 0) {
495 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000496 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000497 MF.addLiveIn(GPR[GPR_idx], VReg);
498 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000499 // If we have two or more remaining argument registers, then both halves
500 // of the i64 can be sourced from there. Otherwise, the lower half will
501 // have to come off the stack. This can happen when an i64 is preceded
502 // by 28 bytes of arguments.
503 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000504 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000505 MF.addLiveIn(GPR[GPR_idx+1], VReg);
506 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000507 } else {
508 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
509 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
510 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
511 DAG.getSrcValue(NULL));
512 }
513 // Build the outgoing arg thingy
514 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
515 newroot = argLo;
516 } else {
517 needsLoad = true;
518 }
519 break;
520 case MVT::f32:
521 case MVT::f64:
522 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000523 if (!ArgLive) {
524 if (FPR_remaining > 0) {
525 --FPR_remaining;
526 ++FPR_idx;
527 }
528 break;
529 }
Chris Lattner915fb302005-08-30 00:19:00 +0000530 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000531 unsigned VReg;
532 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000533 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000534 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000535 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000536 MF.addLiveIn(FPR[FPR_idx], VReg);
537 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000538 --FPR_remaining;
539 ++FPR_idx;
540 } else {
541 needsLoad = true;
542 }
543 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000544 }
545
546 // We need to load the argument to a virtual register if we determined above
547 // that we ran out of physical registers of the appropriate type
548 if (needsLoad) {
549 unsigned SubregOffset = 0;
550 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
551 if (ObjectVT == MVT::i16) SubregOffset = 2;
552 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
553 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
554 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
555 DAG.getConstant(SubregOffset, MVT::i32));
556 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
557 DAG.getSrcValue(NULL));
558 }
559
560 // Every 4 bytes of argument space consumes one of the GPRs available for
561 // argument passing.
562 if (GPR_remaining > 0) {
563 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
564 GPR_remaining -= delta;
565 GPR_idx += delta;
566 }
567 ArgOffset += ObjSize;
568 if (newroot.Val)
569 DAG.setRoot(newroot.getValue(1));
570
571 ArgValues.push_back(argt);
572 }
573
574 // If the function takes variable number of arguments, make a frame index for
575 // the start of the first vararg value... for expansion of llvm.va_start.
576 if (F.isVarArg()) {
577 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
578 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
579 // If this function is vararg, store any remaining integer argument regs
580 // to their spots on the stack so that they may be loaded by deferencing the
581 // result of va_next.
582 std::vector<SDOperand> MemOps;
583 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000584 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000585 MF.addLiveIn(GPR[GPR_idx], VReg);
586 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000587 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
588 Val, FIN, DAG.getSrcValue(NULL));
589 MemOps.push_back(Store);
590 // Increment the address by four for the next argument to store
591 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
592 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
593 }
Chris Lattner80720a92005-11-30 20:40:54 +0000594 if (!MemOps.empty()) {
595 MemOps.push_back(DAG.getRoot());
596 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
597 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000598 }
599
600 // Finally, inform the code generator which regs we return values in.
601 switch (getValueType(F.getReturnType())) {
602 default: assert(0 && "Unknown type!");
603 case MVT::isVoid: break;
604 case MVT::i1:
605 case MVT::i8:
606 case MVT::i16:
607 case MVT::i32:
608 MF.addLiveOut(PPC::R3);
609 break;
610 case MVT::i64:
611 MF.addLiveOut(PPC::R3);
612 MF.addLiveOut(PPC::R4);
613 break;
614 case MVT::f32:
615 case MVT::f64:
616 MF.addLiveOut(PPC::F1);
617 break;
618 }
619
620 return ArgValues;
621}
622
623std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000624PPCTargetLowering::LowerCallTo(SDOperand Chain,
625 const Type *RetTy, bool isVarArg,
626 unsigned CallingConv, bool isTailCall,
627 SDOperand Callee, ArgListTy &Args,
628 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000629 // args_to_use will accumulate outgoing args for the ISD::CALL case in
630 // SelectExpr to use to put the arguments in the appropriate registers.
631 std::vector<SDOperand> args_to_use;
632
633 // Count how many bytes are to be pushed on the stack, including the linkage
634 // area, and parameter passing area.
635 unsigned NumBytes = 24;
636
637 if (Args.empty()) {
638 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
639 DAG.getConstant(NumBytes, getPointerTy()));
640 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000641 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000642 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000643 default: assert(0 && "Unknown value type!");
644 case MVT::i1:
645 case MVT::i8:
646 case MVT::i16:
647 case MVT::i32:
648 case MVT::f32:
649 NumBytes += 4;
650 break;
651 case MVT::i64:
652 case MVT::f64:
653 NumBytes += 8;
654 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000655 }
Chris Lattner915fb302005-08-30 00:19:00 +0000656 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000657
Chris Lattner915fb302005-08-30 00:19:00 +0000658 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
659 // plus 32 bytes of argument space in case any called code gets funky on us.
660 // (Required by ABI to support var arg)
661 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000662
663 // Adjust the stack pointer for the new arguments...
664 // These operations are automatically eliminated by the prolog/epilog pass
665 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
666 DAG.getConstant(NumBytes, getPointerTy()));
667
668 // Set up a copy of the stack pointer for use loading and storing any
669 // arguments that may not fit in the registers available for argument
670 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000671 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000672
673 // Figure out which arguments are going to go in registers, and which in
674 // memory. Also, if this is a vararg function, floating point operations
675 // must be stored to our stack, and loaded into integer regs as well, if
676 // any integer regs are available for argument passing.
677 unsigned ArgOffset = 24;
678 unsigned GPR_remaining = 8;
679 unsigned FPR_remaining = 13;
680
681 std::vector<SDOperand> MemOps;
682 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
683 // PtrOff will be used to store the current argument to the stack if a
684 // register cannot be found for it.
685 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
686 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
687 MVT::ValueType ArgVT = getValueType(Args[i].second);
688
689 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000690 default: assert(0 && "Unexpected ValueType for argument!");
691 case MVT::i1:
692 case MVT::i8:
693 case MVT::i16:
694 // Promote the integer to 32 bits. If the input type is signed use a
695 // sign extend, otherwise use a zero extend.
696 if (Args[i].second->isSigned())
697 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
698 else
699 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
700 // FALL THROUGH
701 case MVT::i32:
702 if (GPR_remaining > 0) {
703 args_to_use.push_back(Args[i].first);
704 --GPR_remaining;
705 } else {
706 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
707 Args[i].first, PtrOff,
708 DAG.getSrcValue(NULL)));
709 }
710 ArgOffset += 4;
711 break;
712 case MVT::i64:
713 // If we have one free GPR left, we can place the upper half of the i64
714 // in it, and store the other half to the stack. If we have two or more
715 // free GPRs, then we can pass both halves of the i64 in registers.
716 if (GPR_remaining > 0) {
717 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
718 Args[i].first, DAG.getConstant(1, MVT::i32));
719 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
720 Args[i].first, DAG.getConstant(0, MVT::i32));
721 args_to_use.push_back(Hi);
722 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000723 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000724 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000725 --GPR_remaining;
726 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000727 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
728 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000729 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000730 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000731 }
Chris Lattner915fb302005-08-30 00:19:00 +0000732 } else {
733 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
734 Args[i].first, PtrOff,
735 DAG.getSrcValue(NULL)));
736 }
737 ArgOffset += 8;
738 break;
739 case MVT::f32:
740 case MVT::f64:
741 if (FPR_remaining > 0) {
742 args_to_use.push_back(Args[i].first);
743 --FPR_remaining;
744 if (isVarArg) {
745 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
746 Args[i].first, PtrOff,
747 DAG.getSrcValue(NULL));
748 MemOps.push_back(Store);
749 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000750 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000751 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
752 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000753 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000754 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000755 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000756 }
757 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000758 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
759 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000760 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
761 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000762 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000763 args_to_use.push_back(Load);
764 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000765 }
766 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000767 // If we have any FPRs remaining, we may also have GPRs remaining.
768 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
769 // GPRs.
770 if (GPR_remaining > 0) {
771 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
772 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000773 }
Chris Lattner915fb302005-08-30 00:19:00 +0000774 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
775 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
776 --GPR_remaining;
777 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000778 }
Chris Lattner915fb302005-08-30 00:19:00 +0000779 } else {
780 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
781 Args[i].first, PtrOff,
782 DAG.getSrcValue(NULL)));
783 }
784 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
785 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000786 }
787 }
788 if (!MemOps.empty())
789 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
790 }
791
792 std::vector<MVT::ValueType> RetVals;
793 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000794 MVT::ValueType ActualRetTyVT = RetTyVT;
795 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
796 ActualRetTyVT = MVT::i32; // Promote result to i32.
797
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000798 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000799 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000800 RetVals.push_back(MVT::Other);
801
Chris Lattner2823b3e2005-11-17 05:56:14 +0000802 // If the callee is a GlobalAddress node (quite common, every direct call is)
803 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
804 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
805 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
806
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000807 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
808 Chain, Callee, args_to_use), 0);
809 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
810 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
811 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000812 SDOperand RetVal = TheCall;
813
814 // If the result is a small value, add a note so that we keep track of the
815 // information about whether it is sign or zero extended.
816 if (RetTyVT != ActualRetTyVT) {
817 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
818 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
819 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
820 }
821
822 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000823}
824
Nate Begeman4a959452005-10-18 23:23:37 +0000825SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
826 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000827 SDOperand Copy;
828 switch (Op.getValueType()) {
829 default: assert(0 && "Unknown type to return!");
830 case MVT::i32:
831 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
832 break;
833 case MVT::f32:
834 case MVT::f64:
835 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
836 break;
837 case MVT::i64:
838 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
839 DAG.getConstant(1, MVT::i32));
840 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
841 DAG.getConstant(0, MVT::i32));
842 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
843 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
844 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000845 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000846 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000847}
848
Nate Begeman21e463b2005-10-16 05:39:50 +0000849SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
850 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000851 // vastart just stores the address of the VarArgsFrameIndex slot into the
852 // memory location argument.
853 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
854 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
855 DAG.getSrcValue(VAListV));
856}
857
858std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000859PPCTargetLowering::LowerVAArg(SDOperand Chain,
860 SDOperand VAListP, Value *VAListV,
861 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000862 MVT::ValueType ArgVT = getValueType(ArgTy);
863
864 SDOperand VAList =
865 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
866 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
867 unsigned Amt;
868 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
869 Amt = 4;
870 else {
871 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
872 "Other types should have been promoted for varargs!");
873 Amt = 8;
874 }
875 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
876 DAG.getConstant(Amt, VAList.getValueType()));
877 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
878 VAList, VAListP, DAG.getSrcValue(VAListV));
879 return std::make_pair(Result, Chain);
880}
881
882
Nate Begeman21e463b2005-10-16 05:39:50 +0000883std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000884LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
885 SelectionDAG &DAG) {
886 assert(0 && "LowerFrameReturnAddress unimplemented");
887 abort();
888}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000889
890MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000891PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
892 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000893 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000894 MI->getOpcode() == PPC::SELECT_CC_F4 ||
895 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000896 "Unexpected instr type to insert");
897
898 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
899 // control-flow pattern. The incoming instruction knows the destination vreg
900 // to set, the condition code register to branch on, the true/false values to
901 // select between, and a branch opcode to use.
902 const BasicBlock *LLVM_BB = BB->getBasicBlock();
903 ilist<MachineBasicBlock>::iterator It = BB;
904 ++It;
905
906 // thisMBB:
907 // ...
908 // TrueVal = ...
909 // cmpTY ccX, r1, r2
910 // bCC copy1MBB
911 // fallthrough --> copy0MBB
912 MachineBasicBlock *thisMBB = BB;
913 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
914 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
915 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
916 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
917 MachineFunction *F = BB->getParent();
918 F->getBasicBlockList().insert(It, copy0MBB);
919 F->getBasicBlockList().insert(It, sinkMBB);
920 // Update machine-CFG edges
921 BB->addSuccessor(copy0MBB);
922 BB->addSuccessor(sinkMBB);
923
924 // copy0MBB:
925 // %FalseValue = ...
926 // # fallthrough to sinkMBB
927 BB = copy0MBB;
928
929 // Update machine-CFG edges
930 BB->addSuccessor(sinkMBB);
931
932 // sinkMBB:
933 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
934 // ...
935 BB = sinkMBB;
936 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
937 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
938 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
939
940 delete MI; // The pseudo instruction is gone now.
941 return BB;
942}
943