blob: c4f6be81af215077818cd3b017c1283429226391 [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
Chris Lattnera54aa942006-01-29 06:26:08 +000040 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
41 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
42
Chris Lattner7c5a3d32005-08-16 17:14:42 +000043 // PowerPC has no intrinsics for these particular operations
44 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
45 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
46 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
47
48 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
49 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
50 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
51
52 // PowerPC has no SREM/UREM instructions
53 setOperationAction(ISD::SREM, MVT::i32, Expand);
54 setOperationAction(ISD::UREM, MVT::i32, Expand);
55
56 // We don't support sin/cos/sqrt/fmod
57 setOperationAction(ISD::FSIN , MVT::f64, Expand);
58 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000059 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000060 setOperationAction(ISD::FSIN , MVT::f32, Expand);
61 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000062 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000063
64 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000065 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000066 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
67 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
68 }
69
Nate Begemand88fc032006-01-14 03:14:10 +000070 // PowerPC does not have BSWAP, CTPOP or CTTZ
71 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000072 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
73 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
74
Nate Begeman35ef9132006-01-11 21:21:00 +000075 // PowerPC does not have ROTR
76 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
77
Chris Lattner7c5a3d32005-08-16 17:14:42 +000078 // PowerPC does not have Select
79 setOperationAction(ISD::SELECT, MVT::i32, Expand);
80 setOperationAction(ISD::SELECT, MVT::f32, Expand);
81 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000082
Chris Lattner0b1e4e52005-08-26 17:36:52 +000083 // PowerPC wants to turn select_cc of FP into fsel when possible.
84 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
85 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000086
87 // PowerPC wants to optimize setcc i32, imm a bit.
88 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000089
Nate Begeman7cbd5252005-08-16 19:49:35 +000090 // PowerPC does not have BRCOND* which requires SetCC
91 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
92 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000093
Chris Lattnerf7605322005-08-31 21:09:52 +000094 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
95 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000096
Jim Laskeyad23c9d2005-08-17 00:40:22 +000097 // PowerPC does not have [U|S]INT_TO_FP
98 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
99 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
100
Chris Lattner53e88452005-12-23 05:13:35 +0000101 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
102 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
103
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000104 // PowerPC does not have truncstore for i1.
105 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000106
Jim Laskeyabf6d172006-01-05 01:25:28 +0000107 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000108 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000109 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000110 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000111 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000112 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000113
Nate Begeman28a6b022005-12-10 02:36:00 +0000114 // We want to legalize GlobalAddress and ConstantPool nodes into the
115 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000116 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000117 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000118
Nate Begemanee625572006-01-27 21:09:22 +0000119 // RET must be custom lowered, to meet ABI requirements
120 setOperationAction(ISD::RET , MVT::Other, Custom);
121
Nate Begemanacc398c2006-01-25 18:21:52 +0000122 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
123 setOperationAction(ISD::VASTART , MVT::Other, Custom);
124
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000125 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000126 setOperationAction(ISD::VAARG , MVT::Other, Expand);
127 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
128 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000129 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
130 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
131 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000132
Nate Begemanc09eeec2005-09-06 22:03:27 +0000133 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000134 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000135 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
136 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000137 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
138 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
139 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000140 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000141 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000142 }
143
144 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
145 // 64 bit PowerPC implementations can support i64 types directly
146 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000147 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
148 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000149 } else {
150 // 32 bit PowerPC wants to expand i64 shifts itself.
151 setOperationAction(ISD::SHL, MVT::i64, Custom);
152 setOperationAction(ISD::SRL, MVT::i64, Custom);
153 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000154 }
155
Nate Begeman425a9692005-11-29 08:17:20 +0000156 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000157 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000158 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000159
160 // FIXME: We don't support any ConstantVec's yet. We should custom expand
161 // the ones we do!
Chris Lattnerd9b55dd2006-01-29 08:41:37 +0000162 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
163 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000164 }
165
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000166 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000167 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000168
169 computeRegisterProperties();
170}
171
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000172const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
173 switch (Opcode) {
174 default: return 0;
175 case PPCISD::FSEL: return "PPCISD::FSEL";
176 case PPCISD::FCFID: return "PPCISD::FCFID";
177 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
178 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
179 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
180 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
181 case PPCISD::Hi: return "PPCISD::Hi";
182 case PPCISD::Lo: return "PPCISD::Lo";
183 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
184 case PPCISD::SRL: return "PPCISD::SRL";
185 case PPCISD::SRA: return "PPCISD::SRA";
186 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000187 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000188 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
189 }
190}
191
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000192/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
193static bool isFloatingPointZero(SDOperand Op) {
194 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
195 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
196 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
197 // Maybe this has already been legalized into the constant pool?
198 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
199 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
200 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
201 }
202 return false;
203}
204
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000205/// LowerOperation - Provide custom lowering hooks for some operations.
206///
Nate Begeman21e463b2005-10-16 05:39:50 +0000207SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000208 switch (Op.getOpcode()) {
209 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000210 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000211 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000212 SDOperand Src = Op.getOperand(0);
213 if (Src.getValueType() == MVT::f32)
214 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
215
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000216 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000217 switch (Op.getValueType()) {
218 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
219 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000220 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000221 break;
222 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000223 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000224 break;
225 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000226
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000227 // Convert the FP value to an int value through memory.
228 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
229 if (Op.getValueType() == MVT::i32)
230 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
231 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000232 }
233 case ISD::SINT_TO_FP: {
234 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
235 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000236 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
237 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000238 if (MVT::f32 == Op.getValueType())
239 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
240 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000241 }
242 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000243 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000244 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
245 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
246 break;
247
248 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
249
250 // Cannot handle SETEQ/SETNE.
251 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
252
253 MVT::ValueType ResVT = Op.getValueType();
254 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
255 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
256 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000257
Chris Lattnerf7605322005-08-31 21:09:52 +0000258 // If the RHS of the comparison is a 0.0, we don't need to do the
259 // subtraction at all.
260 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000261 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000262 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000263 case ISD::SETULT:
264 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000265 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000266 case ISD::SETUGE:
267 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000268 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
269 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000270 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000271 case ISD::SETUGT:
272 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000273 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000274 case ISD::SETULE:
275 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000276 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
277 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000278 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000279 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000280 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000281
Chris Lattnereb255f22005-10-25 20:54:57 +0000282 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000283 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000284 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000285 case ISD::SETULT:
286 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000287 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
288 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
289 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
290 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000291 case ISD::SETUGE:
292 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000293 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
294 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
295 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
296 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000297 case ISD::SETUGT:
298 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000299 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
300 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
301 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
302 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000303 case ISD::SETULE:
304 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000305 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
306 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
307 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
308 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000309 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000310 break;
311 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000312 case ISD::SHL: {
313 assert(Op.getValueType() == MVT::i64 &&
314 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
315 // The generic code does a fine job expanding shift by a constant.
316 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
317
318 // Otherwise, expand into a bunch of logical ops. Note that these ops
319 // depend on the PPC behavior for oversized shift amounts.
320 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
321 DAG.getConstant(0, MVT::i32));
322 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
323 DAG.getConstant(1, MVT::i32));
324 SDOperand Amt = Op.getOperand(1);
325
326 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
327 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000328 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
329 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000330 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
331 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
332 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000333 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000334 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000335 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000336 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
337 }
338 case ISD::SRL: {
339 assert(Op.getValueType() == MVT::i64 &&
340 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
341 // The generic code does a fine job expanding shift by a constant.
342 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
343
344 // Otherwise, expand into a bunch of logical ops. Note that these ops
345 // depend on the PPC behavior for oversized shift amounts.
346 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
347 DAG.getConstant(0, MVT::i32));
348 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
349 DAG.getConstant(1, MVT::i32));
350 SDOperand Amt = Op.getOperand(1);
351
352 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
353 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000354 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
355 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000356 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
357 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
358 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000359 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000360 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000361 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000362 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
363 }
364 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000365 assert(Op.getValueType() == MVT::i64 &&
366 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
367 // The generic code does a fine job expanding shift by a constant.
368 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
369
370 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
371 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
372 DAG.getConstant(0, MVT::i32));
373 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
374 DAG.getConstant(1, MVT::i32));
375 SDOperand Amt = Op.getOperand(1);
376
377 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
378 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000379 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
380 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000381 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
382 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
383 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000384 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
385 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000386 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
387 Tmp4, Tmp6, ISD::SETLE);
388 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000389 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000390 case ISD::ConstantPool: {
391 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
392 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
393 SDOperand Zero = DAG.getConstant(0, MVT::i32);
394
395 if (PPCGenerateStaticCode) {
396 // Generate non-pic code that has direct accesses to the constant pool.
397 // The address of the global is just (hi(&g)+lo(&g)).
398 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
399 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
400 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
401 }
402
403 // Only lower ConstantPool on Darwin.
404 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
405 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
406 if (PICEnabled) {
407 // With PIC, the first instruction is actually "GR+hi(&G)".
408 Hi = DAG.getNode(ISD::ADD, MVT::i32,
409 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
410 }
411
412 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
413 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
414 return Lo;
415 }
Chris Lattner860e8862005-11-17 07:30:41 +0000416 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000417 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
418 GlobalValue *GV = GSDN->getGlobal();
419 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000420 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000421
422 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000423 // Generate non-pic code that has direct accesses to globals.
424 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000425 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
426 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
427 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
428 }
Chris Lattner860e8862005-11-17 07:30:41 +0000429
Chris Lattner1d05cb42005-11-17 18:55:48 +0000430 // Only lower GlobalAddress on Darwin.
431 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000432
Chris Lattner860e8862005-11-17 07:30:41 +0000433 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
434 if (PICEnabled) {
435 // With PIC, the first instruction is actually "GR+hi(&G)".
436 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000437 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000438 }
439
440 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
441 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
442
Chris Lattner37dd6f12006-01-29 20:49:17 +0000443 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
444 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000445 return Lo;
446
447 // If the global is weak or external, we have to go through the lazy
448 // resolution stub.
449 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
450 }
Nate Begeman44775902006-01-31 08:17:29 +0000451 case ISD::SETCC: {
452 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
454 if (C->getValue() && !C->isAllOnesValue())
455 if (CC == ISD::SETEQ || CC == ISD::SETNE ||
456 CC == ISD::SETLT || CC == ISD::SETGT) {
457 MVT::ValueType VT = Op.getValueType();
458 SDOperand SUB = DAG.getNode(ISD::SUB, Op.getOperand(0).getValueType(),
459 Op.getOperand(0), Op.getOperand(1));
460 return DAG.getSetCC(VT, SUB, DAG.getConstant(0, VT), CC);
461 }
462 break;
463 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000464 case ISD::VASTART: {
465 // vastart just stores the address of the VarArgsFrameIndex slot into the
466 // memory location argument.
467 // FIXME: Replace MVT::i32 with PointerTy
468 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
469 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
470 Op.getOperand(1), Op.getOperand(2));
471 }
Nate Begemanee625572006-01-27 21:09:22 +0000472 case ISD::RET: {
473 SDOperand Copy;
474
475 switch(Op.getNumOperands()) {
476 default:
477 assert(0 && "Do not know how to return this many arguments!");
478 abort();
479 case 1:
480 return SDOperand(); // ret void is legal
481 case 2: {
482 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
483 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
484 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
485 SDOperand());
486 break;
487 }
488 case 3:
489 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
490 SDOperand());
491 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
492 break;
493 }
494 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
495 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000496 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000497 return SDOperand();
498}
499
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000500std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000501PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000502 //
503 // add beautiful description of PPC stack frame format, or at least some docs
504 //
505 MachineFunction &MF = DAG.getMachineFunction();
506 MachineFrameInfo *MFI = MF.getFrameInfo();
507 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000508 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000509 std::vector<SDOperand> ArgValues;
510
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000511 unsigned ArgOffset = 24;
512 unsigned GPR_remaining = 8;
513 unsigned FPR_remaining = 13;
514 unsigned GPR_idx = 0, FPR_idx = 0;
515 static const unsigned GPR[] = {
516 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
517 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
518 };
519 static const unsigned FPR[] = {
520 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
521 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
522 };
523
524 // Add DAG nodes to load the arguments... On entry to a function on PPC,
525 // the arguments start at offset 24, although they are likely to be passed
526 // in registers.
527 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
528 SDOperand newroot, argt;
529 unsigned ObjSize;
530 bool needsLoad = false;
531 bool ArgLive = !I->use_empty();
532 MVT::ValueType ObjectVT = getValueType(I->getType());
533
534 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000535 default: assert(0 && "Unhandled argument type!");
536 case MVT::i1:
537 case MVT::i8:
538 case MVT::i16:
539 case MVT::i32:
540 ObjSize = 4;
541 if (!ArgLive) break;
542 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000543 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000544 MF.addLiveIn(GPR[GPR_idx], VReg);
545 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000546 if (ObjectVT != MVT::i32) {
547 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
548 : ISD::AssertZext;
549 argt = DAG.getNode(AssertOp, MVT::i32, argt,
550 DAG.getValueType(ObjectVT));
551 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
552 }
Chris Lattner915fb302005-08-30 00:19:00 +0000553 } else {
554 needsLoad = true;
555 }
556 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000557 case MVT::i64:
558 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000559 if (!ArgLive) break;
560 if (GPR_remaining > 0) {
561 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000562 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000563 MF.addLiveIn(GPR[GPR_idx], VReg);
564 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000565 // If we have two or more remaining argument registers, then both halves
566 // of the i64 can be sourced from there. Otherwise, the lower half will
567 // have to come off the stack. This can happen when an i64 is preceded
568 // by 28 bytes of arguments.
569 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000570 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000571 MF.addLiveIn(GPR[GPR_idx+1], VReg);
572 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000573 } else {
574 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
575 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
576 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
577 DAG.getSrcValue(NULL));
578 }
579 // Build the outgoing arg thingy
580 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
581 newroot = argLo;
582 } else {
583 needsLoad = true;
584 }
585 break;
586 case MVT::f32:
587 case MVT::f64:
588 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000589 if (!ArgLive) {
590 if (FPR_remaining > 0) {
591 --FPR_remaining;
592 ++FPR_idx;
593 }
594 break;
595 }
Chris Lattner915fb302005-08-30 00:19:00 +0000596 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000597 unsigned VReg;
598 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000599 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000600 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000601 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000602 MF.addLiveIn(FPR[FPR_idx], VReg);
603 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000604 --FPR_remaining;
605 ++FPR_idx;
606 } else {
607 needsLoad = true;
608 }
609 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000610 }
611
612 // We need to load the argument to a virtual register if we determined above
613 // that we ran out of physical registers of the appropriate type
614 if (needsLoad) {
615 unsigned SubregOffset = 0;
616 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
617 if (ObjectVT == MVT::i16) SubregOffset = 2;
618 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
619 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
620 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
621 DAG.getConstant(SubregOffset, MVT::i32));
622 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
623 DAG.getSrcValue(NULL));
624 }
625
626 // Every 4 bytes of argument space consumes one of the GPRs available for
627 // argument passing.
628 if (GPR_remaining > 0) {
629 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
630 GPR_remaining -= delta;
631 GPR_idx += delta;
632 }
633 ArgOffset += ObjSize;
634 if (newroot.Val)
635 DAG.setRoot(newroot.getValue(1));
636
637 ArgValues.push_back(argt);
638 }
639
640 // If the function takes variable number of arguments, make a frame index for
641 // the start of the first vararg value... for expansion of llvm.va_start.
642 if (F.isVarArg()) {
643 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
644 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
645 // If this function is vararg, store any remaining integer argument regs
646 // to their spots on the stack so that they may be loaded by deferencing the
647 // result of va_next.
648 std::vector<SDOperand> MemOps;
649 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000650 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000651 MF.addLiveIn(GPR[GPR_idx], VReg);
652 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000653 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
654 Val, FIN, DAG.getSrcValue(NULL));
655 MemOps.push_back(Store);
656 // Increment the address by four for the next argument to store
657 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
658 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
659 }
Chris Lattner80720a92005-11-30 20:40:54 +0000660 if (!MemOps.empty()) {
661 MemOps.push_back(DAG.getRoot());
662 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
663 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000664 }
665
666 // Finally, inform the code generator which regs we return values in.
667 switch (getValueType(F.getReturnType())) {
668 default: assert(0 && "Unknown type!");
669 case MVT::isVoid: break;
670 case MVT::i1:
671 case MVT::i8:
672 case MVT::i16:
673 case MVT::i32:
674 MF.addLiveOut(PPC::R3);
675 break;
676 case MVT::i64:
677 MF.addLiveOut(PPC::R3);
678 MF.addLiveOut(PPC::R4);
679 break;
680 case MVT::f32:
681 case MVT::f64:
682 MF.addLiveOut(PPC::F1);
683 break;
684 }
685
686 return ArgValues;
687}
688
689std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000690PPCTargetLowering::LowerCallTo(SDOperand Chain,
691 const Type *RetTy, bool isVarArg,
692 unsigned CallingConv, bool isTailCall,
693 SDOperand Callee, ArgListTy &Args,
694 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000695 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000696 // SelectExpr to use to put the arguments in the appropriate registers.
697 std::vector<SDOperand> args_to_use;
698
699 // Count how many bytes are to be pushed on the stack, including the linkage
700 // area, and parameter passing area.
701 unsigned NumBytes = 24;
702
703 if (Args.empty()) {
704 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
705 DAG.getConstant(NumBytes, getPointerTy()));
706 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000707 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000708 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000709 default: assert(0 && "Unknown value type!");
710 case MVT::i1:
711 case MVT::i8:
712 case MVT::i16:
713 case MVT::i32:
714 case MVT::f32:
715 NumBytes += 4;
716 break;
717 case MVT::i64:
718 case MVT::f64:
719 NumBytes += 8;
720 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000721 }
Chris Lattner915fb302005-08-30 00:19:00 +0000722 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000723
Chris Lattner915fb302005-08-30 00:19:00 +0000724 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
725 // plus 32 bytes of argument space in case any called code gets funky on us.
726 // (Required by ABI to support var arg)
727 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000728
729 // Adjust the stack pointer for the new arguments...
730 // These operations are automatically eliminated by the prolog/epilog pass
731 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
732 DAG.getConstant(NumBytes, getPointerTy()));
733
734 // Set up a copy of the stack pointer for use loading and storing any
735 // arguments that may not fit in the registers available for argument
736 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000737 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000738
739 // Figure out which arguments are going to go in registers, and which in
740 // memory. Also, if this is a vararg function, floating point operations
741 // must be stored to our stack, and loaded into integer regs as well, if
742 // any integer regs are available for argument passing.
743 unsigned ArgOffset = 24;
744 unsigned GPR_remaining = 8;
745 unsigned FPR_remaining = 13;
746
747 std::vector<SDOperand> MemOps;
748 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
749 // PtrOff will be used to store the current argument to the stack if a
750 // register cannot be found for it.
751 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
752 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
753 MVT::ValueType ArgVT = getValueType(Args[i].second);
754
755 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000756 default: assert(0 && "Unexpected ValueType for argument!");
757 case MVT::i1:
758 case MVT::i8:
759 case MVT::i16:
760 // Promote the integer to 32 bits. If the input type is signed use a
761 // sign extend, otherwise use a zero extend.
762 if (Args[i].second->isSigned())
763 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
764 else
765 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
766 // FALL THROUGH
767 case MVT::i32:
768 if (GPR_remaining > 0) {
769 args_to_use.push_back(Args[i].first);
770 --GPR_remaining;
771 } else {
772 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
773 Args[i].first, PtrOff,
774 DAG.getSrcValue(NULL)));
775 }
776 ArgOffset += 4;
777 break;
778 case MVT::i64:
779 // If we have one free GPR left, we can place the upper half of the i64
780 // in it, and store the other half to the stack. If we have two or more
781 // free GPRs, then we can pass both halves of the i64 in registers.
782 if (GPR_remaining > 0) {
783 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
784 Args[i].first, DAG.getConstant(1, MVT::i32));
785 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
786 Args[i].first, DAG.getConstant(0, MVT::i32));
787 args_to_use.push_back(Hi);
788 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000789 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000790 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000791 --GPR_remaining;
792 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000793 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
794 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000795 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000796 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000797 }
Chris Lattner915fb302005-08-30 00:19:00 +0000798 } else {
799 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
800 Args[i].first, PtrOff,
801 DAG.getSrcValue(NULL)));
802 }
803 ArgOffset += 8;
804 break;
805 case MVT::f32:
806 case MVT::f64:
807 if (FPR_remaining > 0) {
808 args_to_use.push_back(Args[i].first);
809 --FPR_remaining;
810 if (isVarArg) {
811 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
812 Args[i].first, PtrOff,
813 DAG.getSrcValue(NULL));
814 MemOps.push_back(Store);
815 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000816 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000817 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
818 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000819 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000820 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000821 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000822 }
823 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000824 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
825 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000826 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
827 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000828 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000829 args_to_use.push_back(Load);
830 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000831 }
832 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000833 // If we have any FPRs remaining, we may also have GPRs remaining.
834 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
835 // GPRs.
836 if (GPR_remaining > 0) {
837 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
838 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000839 }
Chris Lattner915fb302005-08-30 00:19:00 +0000840 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
841 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
842 --GPR_remaining;
843 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000844 }
Chris Lattner915fb302005-08-30 00:19:00 +0000845 } else {
846 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
847 Args[i].first, PtrOff,
848 DAG.getSrcValue(NULL)));
849 }
850 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
851 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000852 }
853 }
854 if (!MemOps.empty())
855 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
856 }
857
858 std::vector<MVT::ValueType> RetVals;
859 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000860 MVT::ValueType ActualRetTyVT = RetTyVT;
861 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
862 ActualRetTyVT = MVT::i32; // Promote result to i32.
863
Chris Lattnere00ebf02006-01-28 07:33:03 +0000864 if (RetTyVT == MVT::i64) {
865 RetVals.push_back(MVT::i32);
866 RetVals.push_back(MVT::i32);
867 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000868 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000869 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000870 RetVals.push_back(MVT::Other);
871
Chris Lattner2823b3e2005-11-17 05:56:14 +0000872 // If the callee is a GlobalAddress node (quite common, every direct call is)
873 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
874 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
875 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
876
Chris Lattner281b55e2006-01-27 23:34:02 +0000877 std::vector<SDOperand> Ops;
878 Ops.push_back(Chain);
879 Ops.push_back(Callee);
880 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
881 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000882 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000883 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
884 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000885 SDOperand RetVal = TheCall;
886
887 // If the result is a small value, add a note so that we keep track of the
888 // information about whether it is sign or zero extended.
889 if (RetTyVT != ActualRetTyVT) {
890 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
891 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
892 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000893 } else if (RetTyVT == MVT::i64) {
894 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000895 }
896
897 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000898}
899
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000900MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000901PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
902 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000903 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000904 MI->getOpcode() == PPC::SELECT_CC_F4 ||
905 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000906 "Unexpected instr type to insert");
907
908 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
909 // control-flow pattern. The incoming instruction knows the destination vreg
910 // to set, the condition code register to branch on, the true/false values to
911 // select between, and a branch opcode to use.
912 const BasicBlock *LLVM_BB = BB->getBasicBlock();
913 ilist<MachineBasicBlock>::iterator It = BB;
914 ++It;
915
916 // thisMBB:
917 // ...
918 // TrueVal = ...
919 // cmpTY ccX, r1, r2
920 // bCC copy1MBB
921 // fallthrough --> copy0MBB
922 MachineBasicBlock *thisMBB = BB;
923 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
924 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
925 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
926 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
927 MachineFunction *F = BB->getParent();
928 F->getBasicBlockList().insert(It, copy0MBB);
929 F->getBasicBlockList().insert(It, sinkMBB);
930 // Update machine-CFG edges
931 BB->addSuccessor(copy0MBB);
932 BB->addSuccessor(sinkMBB);
933
934 // copy0MBB:
935 // %FalseValue = ...
936 // # fallthrough to sinkMBB
937 BB = copy0MBB;
938
939 // Update machine-CFG edges
940 BB->addSuccessor(sinkMBB);
941
942 // sinkMBB:
943 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
944 // ...
945 BB = sinkMBB;
946 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
947 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
948 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
949
950 delete MI; // The pseudo instruction is gone now.
951 return BB;
952}
953