blob: b9949590a297479b55550faf95daa0ae7bc6251a [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);
Chris Lattnercadd7422006-01-13 17:52:03 +0000153 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000154
155 computeRegisterProperties();
156}
157
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000158const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
159 switch (Opcode) {
160 default: return 0;
161 case PPCISD::FSEL: return "PPCISD::FSEL";
162 case PPCISD::FCFID: return "PPCISD::FCFID";
163 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
164 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
165 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
166 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
167 case PPCISD::Hi: return "PPCISD::Hi";
168 case PPCISD::Lo: return "PPCISD::Lo";
169 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
170 case PPCISD::SRL: return "PPCISD::SRL";
171 case PPCISD::SRA: return "PPCISD::SRA";
172 case PPCISD::SHL: return "PPCISD::SHL";
173 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
174 }
175}
176
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000177/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
178static bool isFloatingPointZero(SDOperand Op) {
179 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
180 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
181 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
182 // Maybe this has already been legalized into the constant pool?
183 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
184 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
185 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
186 }
187 return false;
188}
189
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000190/// LowerOperation - Provide custom lowering hooks for some operations.
191///
Nate Begeman21e463b2005-10-16 05:39:50 +0000192SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000193 switch (Op.getOpcode()) {
194 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000195 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000196 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000197 SDOperand Src = Op.getOperand(0);
198 if (Src.getValueType() == MVT::f32)
199 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
200
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000201 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000202 switch (Op.getValueType()) {
203 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
204 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000205 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000206 break;
207 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000208 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000209 break;
210 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000211
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000212 // Convert the FP value to an int value through memory.
213 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
214 if (Op.getValueType() == MVT::i32)
215 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
216 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000217 }
218 case ISD::SINT_TO_FP: {
219 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
220 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000221 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
222 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000223 if (MVT::f32 == Op.getValueType())
224 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
225 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000226 }
227 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000228 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000229 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
230 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
231 break;
232
233 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
234
235 // Cannot handle SETEQ/SETNE.
236 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
237
238 MVT::ValueType ResVT = Op.getValueType();
239 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
240 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
241 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000242
Chris Lattnerf7605322005-08-31 21:09:52 +0000243 // If the RHS of the comparison is a 0.0, we don't need to do the
244 // subtraction at all.
245 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000246 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000247 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000248 case ISD::SETULT:
249 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000250 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000251 case ISD::SETUGE:
252 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000253 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
254 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000255 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000256 case ISD::SETUGT:
257 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000258 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000259 case ISD::SETULE:
260 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000261 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
262 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000263 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000264 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000265 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000266
Chris Lattnereb255f22005-10-25 20:54:57 +0000267 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000268 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000269 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000270 case ISD::SETULT:
271 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000272 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
273 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
274 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
275 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000276 case ISD::SETUGE:
277 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000278 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
279 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
280 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
281 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000282 case ISD::SETUGT:
283 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000284 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
285 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
286 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
287 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000288 case ISD::SETULE:
289 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000290 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
291 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
292 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
293 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000294 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000295 break;
296 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000297 case ISD::SHL: {
298 assert(Op.getValueType() == MVT::i64 &&
299 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
300 // The generic code does a fine job expanding shift by a constant.
301 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
302
303 // Otherwise, expand into a bunch of logical ops. Note that these ops
304 // depend on the PPC behavior for oversized shift amounts.
305 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
306 DAG.getConstant(0, MVT::i32));
307 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
308 DAG.getConstant(1, MVT::i32));
309 SDOperand Amt = Op.getOperand(1);
310
311 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
312 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000313 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
314 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000315 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
316 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
317 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000318 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000319 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000320 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000321 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
322 }
323 case ISD::SRL: {
324 assert(Op.getValueType() == MVT::i64 &&
325 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
326 // The generic code does a fine job expanding shift by a constant.
327 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
328
329 // Otherwise, expand into a bunch of logical ops. Note that these ops
330 // depend on the PPC behavior for oversized shift amounts.
331 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
332 DAG.getConstant(0, MVT::i32));
333 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
334 DAG.getConstant(1, MVT::i32));
335 SDOperand Amt = Op.getOperand(1);
336
337 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
338 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000339 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
340 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000341 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
342 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
343 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000344 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000345 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000346 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000347 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
348 }
349 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000350 assert(Op.getValueType() == MVT::i64 &&
351 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
352 // The generic code does a fine job expanding shift by a constant.
353 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
354
355 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
356 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
357 DAG.getConstant(0, MVT::i32));
358 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
359 DAG.getConstant(1, MVT::i32));
360 SDOperand Amt = Op.getOperand(1);
361
362 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
363 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000364 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
365 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000366 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
367 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
368 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000369 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
370 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000371 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
372 Tmp4, Tmp6, ISD::SETLE);
373 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000374 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000375 case ISD::ConstantPool: {
376 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
377 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
378 SDOperand Zero = DAG.getConstant(0, MVT::i32);
379
380 if (PPCGenerateStaticCode) {
381 // Generate non-pic code that has direct accesses to the constant pool.
382 // The address of the global is just (hi(&g)+lo(&g)).
383 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
384 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
385 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
386 }
387
388 // Only lower ConstantPool on Darwin.
389 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
390 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
391 if (PICEnabled) {
392 // With PIC, the first instruction is actually "GR+hi(&G)".
393 Hi = DAG.getNode(ISD::ADD, MVT::i32,
394 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
395 }
396
397 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
398 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
399 return Lo;
400 }
Chris Lattner860e8862005-11-17 07:30:41 +0000401 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000402 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
403 GlobalValue *GV = GSDN->getGlobal();
404 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000405 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000406
407 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000408 // Generate non-pic code that has direct accesses to globals.
409 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000410 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
411 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
412 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
413 }
Chris Lattner860e8862005-11-17 07:30:41 +0000414
Chris Lattner1d05cb42005-11-17 18:55:48 +0000415 // Only lower GlobalAddress on Darwin.
416 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000417
Chris Lattner860e8862005-11-17 07:30:41 +0000418 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
419 if (PICEnabled) {
420 // With PIC, the first instruction is actually "GR+hi(&G)".
421 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000422 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000423 }
424
425 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
426 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
427
Chris Lattnera35ef632006-01-06 01:04:03 +0000428 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000429 return Lo;
430
431 // If the global is weak or external, we have to go through the lazy
432 // resolution stub.
433 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
434 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000435 case ISD::VASTART: {
436 // vastart just stores the address of the VarArgsFrameIndex slot into the
437 // memory location argument.
438 // FIXME: Replace MVT::i32 with PointerTy
439 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
440 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
441 Op.getOperand(1), Op.getOperand(2));
442 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000443 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000444 return SDOperand();
445}
446
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000447std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000448PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000449 //
450 // add beautiful description of PPC stack frame format, or at least some docs
451 //
452 MachineFunction &MF = DAG.getMachineFunction();
453 MachineFrameInfo *MFI = MF.getFrameInfo();
454 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000455 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000456 std::vector<SDOperand> ArgValues;
457
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000458 unsigned ArgOffset = 24;
459 unsigned GPR_remaining = 8;
460 unsigned FPR_remaining = 13;
461 unsigned GPR_idx = 0, FPR_idx = 0;
462 static const unsigned GPR[] = {
463 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
464 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
465 };
466 static const unsigned FPR[] = {
467 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
468 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
469 };
470
471 // Add DAG nodes to load the arguments... On entry to a function on PPC,
472 // the arguments start at offset 24, although they are likely to be passed
473 // in registers.
474 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
475 SDOperand newroot, argt;
476 unsigned ObjSize;
477 bool needsLoad = false;
478 bool ArgLive = !I->use_empty();
479 MVT::ValueType ObjectVT = getValueType(I->getType());
480
481 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000482 default: assert(0 && "Unhandled argument type!");
483 case MVT::i1:
484 case MVT::i8:
485 case MVT::i16:
486 case MVT::i32:
487 ObjSize = 4;
488 if (!ArgLive) break;
489 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000490 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000491 MF.addLiveIn(GPR[GPR_idx], VReg);
492 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000493 if (ObjectVT != MVT::i32) {
494 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
495 : ISD::AssertZext;
496 argt = DAG.getNode(AssertOp, MVT::i32, argt,
497 DAG.getValueType(ObjectVT));
498 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
499 }
Chris Lattner915fb302005-08-30 00:19:00 +0000500 } else {
501 needsLoad = true;
502 }
503 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000504 case MVT::i64:
505 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000506 if (!ArgLive) break;
507 if (GPR_remaining > 0) {
508 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000509 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000510 MF.addLiveIn(GPR[GPR_idx], VReg);
511 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000512 // If we have two or more remaining argument registers, then both halves
513 // of the i64 can be sourced from there. Otherwise, the lower half will
514 // have to come off the stack. This can happen when an i64 is preceded
515 // by 28 bytes of arguments.
516 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000517 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000518 MF.addLiveIn(GPR[GPR_idx+1], VReg);
519 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000520 } else {
521 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
522 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
523 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
524 DAG.getSrcValue(NULL));
525 }
526 // Build the outgoing arg thingy
527 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
528 newroot = argLo;
529 } else {
530 needsLoad = true;
531 }
532 break;
533 case MVT::f32:
534 case MVT::f64:
535 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000536 if (!ArgLive) {
537 if (FPR_remaining > 0) {
538 --FPR_remaining;
539 ++FPR_idx;
540 }
541 break;
542 }
Chris Lattner915fb302005-08-30 00:19:00 +0000543 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000544 unsigned VReg;
545 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000546 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000547 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000548 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000549 MF.addLiveIn(FPR[FPR_idx], VReg);
550 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000551 --FPR_remaining;
552 ++FPR_idx;
553 } else {
554 needsLoad = true;
555 }
556 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000557 }
558
559 // We need to load the argument to a virtual register if we determined above
560 // that we ran out of physical registers of the appropriate type
561 if (needsLoad) {
562 unsigned SubregOffset = 0;
563 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
564 if (ObjectVT == MVT::i16) SubregOffset = 2;
565 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
566 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
567 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
568 DAG.getConstant(SubregOffset, MVT::i32));
569 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
570 DAG.getSrcValue(NULL));
571 }
572
573 // Every 4 bytes of argument space consumes one of the GPRs available for
574 // argument passing.
575 if (GPR_remaining > 0) {
576 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
577 GPR_remaining -= delta;
578 GPR_idx += delta;
579 }
580 ArgOffset += ObjSize;
581 if (newroot.Val)
582 DAG.setRoot(newroot.getValue(1));
583
584 ArgValues.push_back(argt);
585 }
586
587 // If the function takes variable number of arguments, make a frame index for
588 // the start of the first vararg value... for expansion of llvm.va_start.
589 if (F.isVarArg()) {
590 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
591 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
592 // If this function is vararg, store any remaining integer argument regs
593 // to their spots on the stack so that they may be loaded by deferencing the
594 // result of va_next.
595 std::vector<SDOperand> MemOps;
596 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000597 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000598 MF.addLiveIn(GPR[GPR_idx], VReg);
599 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000600 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
601 Val, FIN, DAG.getSrcValue(NULL));
602 MemOps.push_back(Store);
603 // Increment the address by four for the next argument to store
604 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
605 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
606 }
Chris Lattner80720a92005-11-30 20:40:54 +0000607 if (!MemOps.empty()) {
608 MemOps.push_back(DAG.getRoot());
609 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
610 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000611 }
612
613 // Finally, inform the code generator which regs we return values in.
614 switch (getValueType(F.getReturnType())) {
615 default: assert(0 && "Unknown type!");
616 case MVT::isVoid: break;
617 case MVT::i1:
618 case MVT::i8:
619 case MVT::i16:
620 case MVT::i32:
621 MF.addLiveOut(PPC::R3);
622 break;
623 case MVT::i64:
624 MF.addLiveOut(PPC::R3);
625 MF.addLiveOut(PPC::R4);
626 break;
627 case MVT::f32:
628 case MVT::f64:
629 MF.addLiveOut(PPC::F1);
630 break;
631 }
632
633 return ArgValues;
634}
635
636std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000637PPCTargetLowering::LowerCallTo(SDOperand Chain,
638 const Type *RetTy, bool isVarArg,
639 unsigned CallingConv, bool isTailCall,
640 SDOperand Callee, ArgListTy &Args,
641 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000642 // args_to_use will accumulate outgoing args for the ISD::CALL case in
643 // SelectExpr to use to put the arguments in the appropriate registers.
644 std::vector<SDOperand> args_to_use;
645
646 // Count how many bytes are to be pushed on the stack, including the linkage
647 // area, and parameter passing area.
648 unsigned NumBytes = 24;
649
650 if (Args.empty()) {
651 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
652 DAG.getConstant(NumBytes, getPointerTy()));
653 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000654 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000655 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000656 default: assert(0 && "Unknown value type!");
657 case MVT::i1:
658 case MVT::i8:
659 case MVT::i16:
660 case MVT::i32:
661 case MVT::f32:
662 NumBytes += 4;
663 break;
664 case MVT::i64:
665 case MVT::f64:
666 NumBytes += 8;
667 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000668 }
Chris Lattner915fb302005-08-30 00:19:00 +0000669 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000670
Chris Lattner915fb302005-08-30 00:19:00 +0000671 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
672 // plus 32 bytes of argument space in case any called code gets funky on us.
673 // (Required by ABI to support var arg)
674 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000675
676 // Adjust the stack pointer for the new arguments...
677 // These operations are automatically eliminated by the prolog/epilog pass
678 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
679 DAG.getConstant(NumBytes, getPointerTy()));
680
681 // Set up a copy of the stack pointer for use loading and storing any
682 // arguments that may not fit in the registers available for argument
683 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000684 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000685
686 // Figure out which arguments are going to go in registers, and which in
687 // memory. Also, if this is a vararg function, floating point operations
688 // must be stored to our stack, and loaded into integer regs as well, if
689 // any integer regs are available for argument passing.
690 unsigned ArgOffset = 24;
691 unsigned GPR_remaining = 8;
692 unsigned FPR_remaining = 13;
693
694 std::vector<SDOperand> MemOps;
695 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
696 // PtrOff will be used to store the current argument to the stack if a
697 // register cannot be found for it.
698 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
699 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
700 MVT::ValueType ArgVT = getValueType(Args[i].second);
701
702 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000703 default: assert(0 && "Unexpected ValueType for argument!");
704 case MVT::i1:
705 case MVT::i8:
706 case MVT::i16:
707 // Promote the integer to 32 bits. If the input type is signed use a
708 // sign extend, otherwise use a zero extend.
709 if (Args[i].second->isSigned())
710 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
711 else
712 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
713 // FALL THROUGH
714 case MVT::i32:
715 if (GPR_remaining > 0) {
716 args_to_use.push_back(Args[i].first);
717 --GPR_remaining;
718 } else {
719 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
720 Args[i].first, PtrOff,
721 DAG.getSrcValue(NULL)));
722 }
723 ArgOffset += 4;
724 break;
725 case MVT::i64:
726 // If we have one free GPR left, we can place the upper half of the i64
727 // in it, and store the other half to the stack. If we have two or more
728 // free GPRs, then we can pass both halves of the i64 in registers.
729 if (GPR_remaining > 0) {
730 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
731 Args[i].first, DAG.getConstant(1, MVT::i32));
732 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
733 Args[i].first, DAG.getConstant(0, MVT::i32));
734 args_to_use.push_back(Hi);
735 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000736 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000737 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000738 --GPR_remaining;
739 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000740 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
741 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000742 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000743 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000744 }
Chris Lattner915fb302005-08-30 00:19:00 +0000745 } else {
746 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
747 Args[i].first, PtrOff,
748 DAG.getSrcValue(NULL)));
749 }
750 ArgOffset += 8;
751 break;
752 case MVT::f32:
753 case MVT::f64:
754 if (FPR_remaining > 0) {
755 args_to_use.push_back(Args[i].first);
756 --FPR_remaining;
757 if (isVarArg) {
758 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
759 Args[i].first, PtrOff,
760 DAG.getSrcValue(NULL));
761 MemOps.push_back(Store);
762 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000763 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000764 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
765 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000766 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000767 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000768 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000769 }
770 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000771 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
772 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000773 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
774 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000775 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000776 args_to_use.push_back(Load);
777 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000778 }
779 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000780 // If we have any FPRs remaining, we may also have GPRs remaining.
781 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
782 // GPRs.
783 if (GPR_remaining > 0) {
784 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
785 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000786 }
Chris Lattner915fb302005-08-30 00:19:00 +0000787 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
788 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
789 --GPR_remaining;
790 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000791 }
Chris Lattner915fb302005-08-30 00:19:00 +0000792 } else {
793 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
794 Args[i].first, PtrOff,
795 DAG.getSrcValue(NULL)));
796 }
797 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
798 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000799 }
800 }
801 if (!MemOps.empty())
802 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
803 }
804
805 std::vector<MVT::ValueType> RetVals;
806 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000807 MVT::ValueType ActualRetTyVT = RetTyVT;
808 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
809 ActualRetTyVT = MVT::i32; // Promote result to i32.
810
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000811 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000812 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000813 RetVals.push_back(MVT::Other);
814
Chris Lattner2823b3e2005-11-17 05:56:14 +0000815 // If the callee is a GlobalAddress node (quite common, every direct call is)
816 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
817 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
818 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
819
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000820 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
821 Chain, Callee, args_to_use), 0);
822 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
823 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
824 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000825 SDOperand RetVal = TheCall;
826
827 // If the result is a small value, add a note so that we keep track of the
828 // information about whether it is sign or zero extended.
829 if (RetTyVT != ActualRetTyVT) {
830 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
831 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
832 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
833 }
834
835 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000836}
837
Nate Begeman4a959452005-10-18 23:23:37 +0000838SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
839 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000840 SDOperand Copy;
841 switch (Op.getValueType()) {
842 default: assert(0 && "Unknown type to return!");
843 case MVT::i32:
844 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
845 break;
846 case MVT::f32:
847 case MVT::f64:
848 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
849 break;
850 case MVT::i64:
851 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
852 DAG.getConstant(1, MVT::i32));
853 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
854 DAG.getConstant(0, MVT::i32));
855 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
856 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
857 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000858 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000859 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000860}
861
Nate Begeman21e463b2005-10-16 05:39:50 +0000862std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000863LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
864 SelectionDAG &DAG) {
865 assert(0 && "LowerFrameReturnAddress unimplemented");
866 abort();
867}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000868
869MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000870PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
871 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000872 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000873 MI->getOpcode() == PPC::SELECT_CC_F4 ||
874 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000875 "Unexpected instr type to insert");
876
877 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
878 // control-flow pattern. The incoming instruction knows the destination vreg
879 // to set, the condition code register to branch on, the true/false values to
880 // select between, and a branch opcode to use.
881 const BasicBlock *LLVM_BB = BB->getBasicBlock();
882 ilist<MachineBasicBlock>::iterator It = BB;
883 ++It;
884
885 // thisMBB:
886 // ...
887 // TrueVal = ...
888 // cmpTY ccX, r1, r2
889 // bCC copy1MBB
890 // fallthrough --> copy0MBB
891 MachineBasicBlock *thisMBB = BB;
892 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
893 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
894 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
895 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
896 MachineFunction *F = BB->getParent();
897 F->getBasicBlockList().insert(It, copy0MBB);
898 F->getBasicBlockList().insert(It, sinkMBB);
899 // Update machine-CFG edges
900 BB->addSuccessor(copy0MBB);
901 BB->addSuccessor(sinkMBB);
902
903 // copy0MBB:
904 // %FalseValue = ...
905 // # fallthrough to sinkMBB
906 BB = copy0MBB;
907
908 // Update machine-CFG edges
909 BB->addSuccessor(sinkMBB);
910
911 // sinkMBB:
912 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
913 // ...
914 BB = sinkMBB;
915 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
916 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
917 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
918
919 delete MI; // The pseudo instruction is gone now.
920 return BB;
921}
922