blob: 3e8214a0574d61ae9ed635eaab3afbddf7755df9 [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);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000086
Nate Begeman7cbd5252005-08-16 19:49:35 +000087 // PowerPC does not have BRCOND* which requires SetCC
88 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
89 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000090
Chris Lattnerf7605322005-08-31 21:09:52 +000091 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
92 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000093
Jim Laskeyad23c9d2005-08-17 00:40:22 +000094 // PowerPC does not have [U|S]INT_TO_FP
95 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
96 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
97
Chris Lattner53e88452005-12-23 05:13:35 +000098 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
99 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
100
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000101 // PowerPC does not have truncstore for i1.
102 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000103
Jim Laskeyabf6d172006-01-05 01:25:28 +0000104 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000105 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000106 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000107 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000108 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000109 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000110
Nate Begeman28a6b022005-12-10 02:36:00 +0000111 // We want to legalize GlobalAddress and ConstantPool nodes into the
112 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000113 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000114 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000115
Nate Begemanee625572006-01-27 21:09:22 +0000116 // RET must be custom lowered, to meet ABI requirements
117 setOperationAction(ISD::RET , MVT::Other, Custom);
118
Nate Begemanacc398c2006-01-25 18:21:52 +0000119 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
120 setOperationAction(ISD::VASTART , MVT::Other, Custom);
121
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000122 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000123 setOperationAction(ISD::VAARG , MVT::Other, Expand);
124 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
125 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000126 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
127 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
128 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000129
Nate Begemanc09eeec2005-09-06 22:03:27 +0000130 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000131 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000132 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
133 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000134 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
135 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
136 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000137 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000138 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000139 }
140
141 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
142 // 64 bit PowerPC implementations can support i64 types directly
143 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000144 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
145 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000146 } else {
147 // 32 bit PowerPC wants to expand i64 shifts itself.
148 setOperationAction(ISD::SHL, MVT::i64, Custom);
149 setOperationAction(ISD::SRL, MVT::i64, Custom);
150 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000151 }
152
Nate Begeman425a9692005-11-29 08:17:20 +0000153 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000154 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000155 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000156
157 // FIXME: We don't support any ConstantVec's yet. We should custom expand
158 // the ones we do!
Chris Lattnerd9b55dd2006-01-29 08:41:37 +0000159 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
160 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000161 }
162
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000163 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000164 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000165
166 computeRegisterProperties();
167}
168
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000169const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
170 switch (Opcode) {
171 default: return 0;
172 case PPCISD::FSEL: return "PPCISD::FSEL";
173 case PPCISD::FCFID: return "PPCISD::FCFID";
174 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
175 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
176 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
177 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
178 case PPCISD::Hi: return "PPCISD::Hi";
179 case PPCISD::Lo: return "PPCISD::Lo";
180 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
181 case PPCISD::SRL: return "PPCISD::SRL";
182 case PPCISD::SRA: return "PPCISD::SRA";
183 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000184 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000185 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
186 }
187}
188
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000189/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
190static bool isFloatingPointZero(SDOperand Op) {
191 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
192 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
193 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
194 // Maybe this has already been legalized into the constant pool?
195 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
196 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
197 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
198 }
199 return false;
200}
201
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000202/// LowerOperation - Provide custom lowering hooks for some operations.
203///
Nate Begeman21e463b2005-10-16 05:39:50 +0000204SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000205 switch (Op.getOpcode()) {
206 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000207 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000208 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000209 SDOperand Src = Op.getOperand(0);
210 if (Src.getValueType() == MVT::f32)
211 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
212
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000213 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000214 switch (Op.getValueType()) {
215 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
216 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000217 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000218 break;
219 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000220 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000221 break;
222 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000223
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000224 // Convert the FP value to an int value through memory.
225 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
226 if (Op.getValueType() == MVT::i32)
227 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
228 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000229 }
230 case ISD::SINT_TO_FP: {
231 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
232 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000233 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
234 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000235 if (MVT::f32 == Op.getValueType())
236 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
237 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000238 }
239 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000240 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000241 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
242 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
243 break;
244
245 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
246
247 // Cannot handle SETEQ/SETNE.
248 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
249
250 MVT::ValueType ResVT = Op.getValueType();
251 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
252 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
253 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000254
Chris Lattnerf7605322005-08-31 21:09:52 +0000255 // If the RHS of the comparison is a 0.0, we don't need to do the
256 // subtraction at all.
257 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000258 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000259 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000260 case ISD::SETULT:
261 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000262 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000263 case ISD::SETUGE:
264 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000265 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
266 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000267 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000268 case ISD::SETUGT:
269 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000270 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000271 case ISD::SETULE:
272 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000273 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
274 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000275 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000276 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000277 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000278
Chris Lattnereb255f22005-10-25 20:54:57 +0000279 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000280 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000281 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000282 case ISD::SETULT:
283 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000284 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
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::SETUGE:
289 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000290 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
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 Lattnerf7605322005-08-31 21:09:52 +0000294 case ISD::SETUGT:
295 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000296 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
297 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
298 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
299 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000300 case ISD::SETULE:
301 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000302 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
303 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
304 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
305 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000306 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000307 break;
308 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000309 case ISD::SHL: {
310 assert(Op.getValueType() == MVT::i64 &&
311 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
312 // The generic code does a fine job expanding shift by a constant.
313 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
314
315 // Otherwise, expand into a bunch of logical ops. Note that these ops
316 // depend on the PPC behavior for oversized shift amounts.
317 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
318 DAG.getConstant(0, MVT::i32));
319 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
320 DAG.getConstant(1, MVT::i32));
321 SDOperand Amt = Op.getOperand(1);
322
323 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
324 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000325 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
326 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000327 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
328 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
329 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000330 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000331 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000332 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000333 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
334 }
335 case ISD::SRL: {
336 assert(Op.getValueType() == MVT::i64 &&
337 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
338 // The generic code does a fine job expanding shift by a constant.
339 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
340
341 // Otherwise, expand into a bunch of logical ops. Note that these ops
342 // depend on the PPC behavior for oversized shift amounts.
343 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
344 DAG.getConstant(0, MVT::i32));
345 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
346 DAG.getConstant(1, MVT::i32));
347 SDOperand Amt = Op.getOperand(1);
348
349 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
350 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000351 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
352 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000353 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
354 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
355 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000356 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000357 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000358 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000359 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
360 }
361 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000362 assert(Op.getValueType() == MVT::i64 &&
363 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
364 // The generic code does a fine job expanding shift by a constant.
365 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
366
367 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
368 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
369 DAG.getConstant(0, MVT::i32));
370 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
371 DAG.getConstant(1, MVT::i32));
372 SDOperand Amt = Op.getOperand(1);
373
374 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
375 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000376 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
377 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000378 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
379 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
380 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000381 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
382 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000383 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
384 Tmp4, Tmp6, ISD::SETLE);
385 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000386 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000387 case ISD::ConstantPool: {
388 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
389 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
390 SDOperand Zero = DAG.getConstant(0, MVT::i32);
391
392 if (PPCGenerateStaticCode) {
393 // Generate non-pic code that has direct accesses to the constant pool.
394 // The address of the global is just (hi(&g)+lo(&g)).
395 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
396 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
397 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
398 }
399
400 // Only lower ConstantPool on Darwin.
401 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
402 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
403 if (PICEnabled) {
404 // With PIC, the first instruction is actually "GR+hi(&G)".
405 Hi = DAG.getNode(ISD::ADD, MVT::i32,
406 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
407 }
408
409 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
410 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
411 return Lo;
412 }
Chris Lattner860e8862005-11-17 07:30:41 +0000413 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000414 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
415 GlobalValue *GV = GSDN->getGlobal();
416 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000417 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000418
419 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000420 // Generate non-pic code that has direct accesses to globals.
421 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000422 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
423 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
424 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
425 }
Chris Lattner860e8862005-11-17 07:30:41 +0000426
Chris Lattner1d05cb42005-11-17 18:55:48 +0000427 // Only lower GlobalAddress on Darwin.
428 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000429
Chris Lattner860e8862005-11-17 07:30:41 +0000430 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
431 if (PICEnabled) {
432 // With PIC, the first instruction is actually "GR+hi(&G)".
433 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000434 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000435 }
436
437 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
438 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
439
Chris Lattner37dd6f12006-01-29 20:49:17 +0000440 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
441 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000442 return Lo;
443
444 // If the global is weak or external, we have to go through the lazy
445 // resolution stub.
446 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
447 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000448 case ISD::VASTART: {
449 // vastart just stores the address of the VarArgsFrameIndex slot into the
450 // memory location argument.
451 // FIXME: Replace MVT::i32 with PointerTy
452 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
453 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
454 Op.getOperand(1), Op.getOperand(2));
455 }
Nate Begemanee625572006-01-27 21:09:22 +0000456 case ISD::RET: {
457 SDOperand Copy;
458
459 switch(Op.getNumOperands()) {
460 default:
461 assert(0 && "Do not know how to return this many arguments!");
462 abort();
463 case 1:
464 return SDOperand(); // ret void is legal
465 case 2: {
466 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
467 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
468 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
469 SDOperand());
470 break;
471 }
472 case 3:
473 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
474 SDOperand());
475 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
476 break;
477 }
478 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
479 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000480 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000481 return SDOperand();
482}
483
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000484std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000485PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000486 //
487 // add beautiful description of PPC stack frame format, or at least some docs
488 //
489 MachineFunction &MF = DAG.getMachineFunction();
490 MachineFrameInfo *MFI = MF.getFrameInfo();
491 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000492 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000493 std::vector<SDOperand> ArgValues;
494
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000495 unsigned ArgOffset = 24;
496 unsigned GPR_remaining = 8;
497 unsigned FPR_remaining = 13;
498 unsigned GPR_idx = 0, FPR_idx = 0;
499 static const unsigned GPR[] = {
500 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
501 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
502 };
503 static const unsigned FPR[] = {
504 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
505 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
506 };
507
508 // Add DAG nodes to load the arguments... On entry to a function on PPC,
509 // the arguments start at offset 24, although they are likely to be passed
510 // in registers.
511 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
512 SDOperand newroot, argt;
513 unsigned ObjSize;
514 bool needsLoad = false;
515 bool ArgLive = !I->use_empty();
516 MVT::ValueType ObjectVT = getValueType(I->getType());
517
518 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000519 default: assert(0 && "Unhandled argument type!");
520 case MVT::i1:
521 case MVT::i8:
522 case MVT::i16:
523 case MVT::i32:
524 ObjSize = 4;
525 if (!ArgLive) break;
526 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000527 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000528 MF.addLiveIn(GPR[GPR_idx], VReg);
529 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000530 if (ObjectVT != MVT::i32) {
531 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
532 : ISD::AssertZext;
533 argt = DAG.getNode(AssertOp, MVT::i32, argt,
534 DAG.getValueType(ObjectVT));
535 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
536 }
Chris Lattner915fb302005-08-30 00:19:00 +0000537 } else {
538 needsLoad = true;
539 }
540 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000541 case MVT::i64:
542 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000543 if (!ArgLive) break;
544 if (GPR_remaining > 0) {
545 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000546 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000547 MF.addLiveIn(GPR[GPR_idx], VReg);
548 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000549 // If we have two or more remaining argument registers, then both halves
550 // of the i64 can be sourced from there. Otherwise, the lower half will
551 // have to come off the stack. This can happen when an i64 is preceded
552 // by 28 bytes of arguments.
553 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000554 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000555 MF.addLiveIn(GPR[GPR_idx+1], VReg);
556 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000557 } else {
558 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
559 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
560 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
561 DAG.getSrcValue(NULL));
562 }
563 // Build the outgoing arg thingy
564 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
565 newroot = argLo;
566 } else {
567 needsLoad = true;
568 }
569 break;
570 case MVT::f32:
571 case MVT::f64:
572 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000573 if (!ArgLive) {
574 if (FPR_remaining > 0) {
575 --FPR_remaining;
576 ++FPR_idx;
577 }
578 break;
579 }
Chris Lattner915fb302005-08-30 00:19:00 +0000580 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000581 unsigned VReg;
582 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000583 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000584 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000585 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000586 MF.addLiveIn(FPR[FPR_idx], VReg);
587 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000588 --FPR_remaining;
589 ++FPR_idx;
590 } else {
591 needsLoad = true;
592 }
593 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000594 }
595
596 // We need to load the argument to a virtual register if we determined above
597 // that we ran out of physical registers of the appropriate type
598 if (needsLoad) {
599 unsigned SubregOffset = 0;
600 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
601 if (ObjectVT == MVT::i16) SubregOffset = 2;
602 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
603 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
604 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
605 DAG.getConstant(SubregOffset, MVT::i32));
606 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
607 DAG.getSrcValue(NULL));
608 }
609
610 // Every 4 bytes of argument space consumes one of the GPRs available for
611 // argument passing.
612 if (GPR_remaining > 0) {
613 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
614 GPR_remaining -= delta;
615 GPR_idx += delta;
616 }
617 ArgOffset += ObjSize;
618 if (newroot.Val)
619 DAG.setRoot(newroot.getValue(1));
620
621 ArgValues.push_back(argt);
622 }
623
624 // If the function takes variable number of arguments, make a frame index for
625 // the start of the first vararg value... for expansion of llvm.va_start.
626 if (F.isVarArg()) {
627 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
628 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
629 // If this function is vararg, store any remaining integer argument regs
630 // to their spots on the stack so that they may be loaded by deferencing the
631 // result of va_next.
632 std::vector<SDOperand> MemOps;
633 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000634 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000635 MF.addLiveIn(GPR[GPR_idx], VReg);
636 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000637 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
638 Val, FIN, DAG.getSrcValue(NULL));
639 MemOps.push_back(Store);
640 // Increment the address by four for the next argument to store
641 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
642 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
643 }
Chris Lattner80720a92005-11-30 20:40:54 +0000644 if (!MemOps.empty()) {
645 MemOps.push_back(DAG.getRoot());
646 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
647 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000648 }
649
650 // Finally, inform the code generator which regs we return values in.
651 switch (getValueType(F.getReturnType())) {
652 default: assert(0 && "Unknown type!");
653 case MVT::isVoid: break;
654 case MVT::i1:
655 case MVT::i8:
656 case MVT::i16:
657 case MVT::i32:
658 MF.addLiveOut(PPC::R3);
659 break;
660 case MVT::i64:
661 MF.addLiveOut(PPC::R3);
662 MF.addLiveOut(PPC::R4);
663 break;
664 case MVT::f32:
665 case MVT::f64:
666 MF.addLiveOut(PPC::F1);
667 break;
668 }
669
670 return ArgValues;
671}
672
673std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000674PPCTargetLowering::LowerCallTo(SDOperand Chain,
675 const Type *RetTy, bool isVarArg,
676 unsigned CallingConv, bool isTailCall,
677 SDOperand Callee, ArgListTy &Args,
678 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000679 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000680 // SelectExpr to use to put the arguments in the appropriate registers.
681 std::vector<SDOperand> args_to_use;
682
683 // Count how many bytes are to be pushed on the stack, including the linkage
684 // area, and parameter passing area.
685 unsigned NumBytes = 24;
686
687 if (Args.empty()) {
688 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
689 DAG.getConstant(NumBytes, getPointerTy()));
690 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000691 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000692 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000693 default: assert(0 && "Unknown value type!");
694 case MVT::i1:
695 case MVT::i8:
696 case MVT::i16:
697 case MVT::i32:
698 case MVT::f32:
699 NumBytes += 4;
700 break;
701 case MVT::i64:
702 case MVT::f64:
703 NumBytes += 8;
704 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000705 }
Chris Lattner915fb302005-08-30 00:19:00 +0000706 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000707
Chris Lattner915fb302005-08-30 00:19:00 +0000708 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
709 // plus 32 bytes of argument space in case any called code gets funky on us.
710 // (Required by ABI to support var arg)
711 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000712
713 // Adjust the stack pointer for the new arguments...
714 // These operations are automatically eliminated by the prolog/epilog pass
715 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
716 DAG.getConstant(NumBytes, getPointerTy()));
717
718 // Set up a copy of the stack pointer for use loading and storing any
719 // arguments that may not fit in the registers available for argument
720 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000721 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000722
723 // Figure out which arguments are going to go in registers, and which in
724 // memory. Also, if this is a vararg function, floating point operations
725 // must be stored to our stack, and loaded into integer regs as well, if
726 // any integer regs are available for argument passing.
727 unsigned ArgOffset = 24;
728 unsigned GPR_remaining = 8;
729 unsigned FPR_remaining = 13;
730
731 std::vector<SDOperand> MemOps;
732 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
733 // PtrOff will be used to store the current argument to the stack if a
734 // register cannot be found for it.
735 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
736 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
737 MVT::ValueType ArgVT = getValueType(Args[i].second);
738
739 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000740 default: assert(0 && "Unexpected ValueType for argument!");
741 case MVT::i1:
742 case MVT::i8:
743 case MVT::i16:
744 // Promote the integer to 32 bits. If the input type is signed use a
745 // sign extend, otherwise use a zero extend.
746 if (Args[i].second->isSigned())
747 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
748 else
749 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
750 // FALL THROUGH
751 case MVT::i32:
752 if (GPR_remaining > 0) {
753 args_to_use.push_back(Args[i].first);
754 --GPR_remaining;
755 } else {
756 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
757 Args[i].first, PtrOff,
758 DAG.getSrcValue(NULL)));
759 }
760 ArgOffset += 4;
761 break;
762 case MVT::i64:
763 // If we have one free GPR left, we can place the upper half of the i64
764 // in it, and store the other half to the stack. If we have two or more
765 // free GPRs, then we can pass both halves of the i64 in registers.
766 if (GPR_remaining > 0) {
767 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
768 Args[i].first, DAG.getConstant(1, MVT::i32));
769 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
770 Args[i].first, DAG.getConstant(0, MVT::i32));
771 args_to_use.push_back(Hi);
772 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000773 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000774 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000775 --GPR_remaining;
776 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000777 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
778 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000779 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000780 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000781 }
Chris Lattner915fb302005-08-30 00:19:00 +0000782 } else {
783 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
784 Args[i].first, PtrOff,
785 DAG.getSrcValue(NULL)));
786 }
787 ArgOffset += 8;
788 break;
789 case MVT::f32:
790 case MVT::f64:
791 if (FPR_remaining > 0) {
792 args_to_use.push_back(Args[i].first);
793 --FPR_remaining;
794 if (isVarArg) {
795 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
796 Args[i].first, PtrOff,
797 DAG.getSrcValue(NULL));
798 MemOps.push_back(Store);
799 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000800 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000801 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
802 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000803 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000804 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000805 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000806 }
807 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000808 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
809 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000810 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
811 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000812 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000813 args_to_use.push_back(Load);
814 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000815 }
816 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000817 // If we have any FPRs remaining, we may also have GPRs remaining.
818 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
819 // GPRs.
820 if (GPR_remaining > 0) {
821 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
822 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000823 }
Chris Lattner915fb302005-08-30 00:19:00 +0000824 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
825 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
826 --GPR_remaining;
827 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000828 }
Chris Lattner915fb302005-08-30 00:19:00 +0000829 } else {
830 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
831 Args[i].first, PtrOff,
832 DAG.getSrcValue(NULL)));
833 }
834 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
835 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000836 }
837 }
838 if (!MemOps.empty())
839 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
840 }
841
842 std::vector<MVT::ValueType> RetVals;
843 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000844 MVT::ValueType ActualRetTyVT = RetTyVT;
845 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
846 ActualRetTyVT = MVT::i32; // Promote result to i32.
847
Chris Lattnere00ebf02006-01-28 07:33:03 +0000848 if (RetTyVT == MVT::i64) {
849 RetVals.push_back(MVT::i32);
850 RetVals.push_back(MVT::i32);
851 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000852 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000853 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000854 RetVals.push_back(MVT::Other);
855
Chris Lattner2823b3e2005-11-17 05:56:14 +0000856 // If the callee is a GlobalAddress node (quite common, every direct call is)
857 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
858 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
859 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
860
Chris Lattner281b55e2006-01-27 23:34:02 +0000861 std::vector<SDOperand> Ops;
862 Ops.push_back(Chain);
863 Ops.push_back(Callee);
864 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
865 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000866 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000867 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
868 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000869 SDOperand RetVal = TheCall;
870
871 // If the result is a small value, add a note so that we keep track of the
872 // information about whether it is sign or zero extended.
873 if (RetTyVT != ActualRetTyVT) {
874 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
875 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
876 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000877 } else if (RetTyVT == MVT::i64) {
878 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000879 }
880
881 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000882}
883
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000884MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000885PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
886 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000887 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000888 MI->getOpcode() == PPC::SELECT_CC_F4 ||
889 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000890 "Unexpected instr type to insert");
891
892 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
893 // control-flow pattern. The incoming instruction knows the destination vreg
894 // to set, the condition code register to branch on, the true/false values to
895 // select between, and a branch opcode to use.
896 const BasicBlock *LLVM_BB = BB->getBasicBlock();
897 ilist<MachineBasicBlock>::iterator It = BB;
898 ++It;
899
900 // thisMBB:
901 // ...
902 // TrueVal = ...
903 // cmpTY ccX, r1, r2
904 // bCC copy1MBB
905 // fallthrough --> copy0MBB
906 MachineBasicBlock *thisMBB = BB;
907 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
908 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
909 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
910 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
911 MachineFunction *F = BB->getParent();
912 F->getBasicBlockList().insert(It, copy0MBB);
913 F->getBasicBlockList().insert(It, sinkMBB);
914 // Update machine-CFG edges
915 BB->addSuccessor(copy0MBB);
916 BB->addSuccessor(sinkMBB);
917
918 // copy0MBB:
919 // %FalseValue = ...
920 // # fallthrough to sinkMBB
921 BB = copy0MBB;
922
923 // Update machine-CFG edges
924 BB->addSuccessor(sinkMBB);
925
926 // sinkMBB:
927 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
928 // ...
929 BB = sinkMBB;
930 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
931 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
932 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
933
934 delete MI; // The pseudo instruction is gone now.
935 return BB;
936}
937