blob: 83297bada58851591cce111f9a4eb37f185184fb [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);
Nate Begeman425a9692005-11-29 08:17:20 +0000156 }
157
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000158 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000159 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000160
161 computeRegisterProperties();
162}
163
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000164const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
165 switch (Opcode) {
166 default: return 0;
167 case PPCISD::FSEL: return "PPCISD::FSEL";
168 case PPCISD::FCFID: return "PPCISD::FCFID";
169 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
170 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
171 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
172 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
173 case PPCISD::Hi: return "PPCISD::Hi";
174 case PPCISD::Lo: return "PPCISD::Lo";
175 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
176 case PPCISD::SRL: return "PPCISD::SRL";
177 case PPCISD::SRA: return "PPCISD::SRA";
178 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000179 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000180 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
181 }
182}
183
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000184/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
185static bool isFloatingPointZero(SDOperand Op) {
186 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
187 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
188 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
189 // Maybe this has already been legalized into the constant pool?
190 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
191 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
192 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
193 }
194 return false;
195}
196
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000197/// LowerOperation - Provide custom lowering hooks for some operations.
198///
Nate Begeman21e463b2005-10-16 05:39:50 +0000199SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000200 switch (Op.getOpcode()) {
201 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000202 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000203 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000204 SDOperand Src = Op.getOperand(0);
205 if (Src.getValueType() == MVT::f32)
206 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
207
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000208 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000209 switch (Op.getValueType()) {
210 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
211 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000212 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000213 break;
214 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000215 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000216 break;
217 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000218
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000219 // Convert the FP value to an int value through memory.
220 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
221 if (Op.getValueType() == MVT::i32)
222 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
223 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000224 }
225 case ISD::SINT_TO_FP: {
226 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
227 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000228 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
229 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000230 if (MVT::f32 == Op.getValueType())
231 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
232 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000233 }
234 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000235 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000236 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
237 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
238 break;
239
240 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
241
242 // Cannot handle SETEQ/SETNE.
243 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
244
245 MVT::ValueType ResVT = Op.getValueType();
246 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
247 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
248 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000249
Chris Lattnerf7605322005-08-31 21:09:52 +0000250 // If the RHS of the comparison is a 0.0, we don't need to do the
251 // subtraction at all.
252 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000253 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000254 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000255 case ISD::SETULT:
256 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000257 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000258 case ISD::SETUGE:
259 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000260 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
261 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000262 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000263 case ISD::SETUGT:
264 case ISD::SETGT:
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::SETULE:
267 case ISD::SETLE:
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 Lattner0bbea952005-08-26 20:25:03 +0000270 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000271 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000272 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000273
Chris Lattnereb255f22005-10-25 20:54:57 +0000274 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000275 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000276 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000277 case ISD::SETULT:
278 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000279 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
280 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
281 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
282 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000283 case ISD::SETUGE:
284 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000285 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
286 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
287 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
288 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000289 case ISD::SETUGT:
290 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000291 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
292 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
293 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
294 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000295 case ISD::SETULE:
296 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000297 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
298 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
299 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
300 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000301 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000302 break;
303 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000304 case ISD::SHL: {
305 assert(Op.getValueType() == MVT::i64 &&
306 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
307 // The generic code does a fine job expanding shift by a constant.
308 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
309
310 // Otherwise, expand into a bunch of logical ops. Note that these ops
311 // depend on the PPC behavior for oversized shift amounts.
312 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
313 DAG.getConstant(0, MVT::i32));
314 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
315 DAG.getConstant(1, MVT::i32));
316 SDOperand Amt = Op.getOperand(1);
317
318 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
319 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000320 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
321 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000322 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
323 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
324 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000325 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000326 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000327 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000328 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
329 }
330 case ISD::SRL: {
331 assert(Op.getValueType() == MVT::i64 &&
332 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
333 // The generic code does a fine job expanding shift by a constant.
334 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
335
336 // Otherwise, expand into a bunch of logical ops. Note that these ops
337 // depend on the PPC behavior for oversized shift amounts.
338 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
339 DAG.getConstant(0, MVT::i32));
340 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
341 DAG.getConstant(1, MVT::i32));
342 SDOperand Amt = Op.getOperand(1);
343
344 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
345 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000346 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
347 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000348 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
349 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
350 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000351 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000352 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000353 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000354 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
355 }
356 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000357 assert(Op.getValueType() == MVT::i64 &&
358 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
359 // The generic code does a fine job expanding shift by a constant.
360 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
361
362 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
363 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
364 DAG.getConstant(0, MVT::i32));
365 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
366 DAG.getConstant(1, MVT::i32));
367 SDOperand Amt = Op.getOperand(1);
368
369 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
370 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000371 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
372 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000373 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
374 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
375 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000376 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
377 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000378 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
379 Tmp4, Tmp6, ISD::SETLE);
380 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000381 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000382 case ISD::ConstantPool: {
383 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
384 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
385 SDOperand Zero = DAG.getConstant(0, MVT::i32);
386
387 if (PPCGenerateStaticCode) {
388 // Generate non-pic code that has direct accesses to the constant pool.
389 // The address of the global is just (hi(&g)+lo(&g)).
390 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
391 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
392 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
393 }
394
395 // Only lower ConstantPool on Darwin.
396 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
397 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
398 if (PICEnabled) {
399 // With PIC, the first instruction is actually "GR+hi(&G)".
400 Hi = DAG.getNode(ISD::ADD, MVT::i32,
401 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
402 }
403
404 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
405 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
406 return Lo;
407 }
Chris Lattner860e8862005-11-17 07:30:41 +0000408 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000409 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
410 GlobalValue *GV = GSDN->getGlobal();
411 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000412 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000413
414 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000415 // Generate non-pic code that has direct accesses to globals.
416 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000417 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
418 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
419 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
420 }
Chris Lattner860e8862005-11-17 07:30:41 +0000421
Chris Lattner1d05cb42005-11-17 18:55:48 +0000422 // Only lower GlobalAddress on Darwin.
423 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000424
Chris Lattner860e8862005-11-17 07:30:41 +0000425 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
426 if (PICEnabled) {
427 // With PIC, the first instruction is actually "GR+hi(&G)".
428 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000429 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000430 }
431
432 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
433 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
434
Chris Lattnera35ef632006-01-06 01:04:03 +0000435 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000436 return Lo;
437
438 // If the global is weak or external, we have to go through the lazy
439 // resolution stub.
440 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
441 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000442 case ISD::VASTART: {
443 // vastart just stores the address of the VarArgsFrameIndex slot into the
444 // memory location argument.
445 // FIXME: Replace MVT::i32 with PointerTy
446 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
447 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
448 Op.getOperand(1), Op.getOperand(2));
449 }
Nate Begemanee625572006-01-27 21:09:22 +0000450 case ISD::RET: {
451 SDOperand Copy;
452
453 switch(Op.getNumOperands()) {
454 default:
455 assert(0 && "Do not know how to return this many arguments!");
456 abort();
457 case 1:
458 return SDOperand(); // ret void is legal
459 case 2: {
460 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
461 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
462 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
463 SDOperand());
464 break;
465 }
466 case 3:
467 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
468 SDOperand());
469 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
470 break;
471 }
472 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
473 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000474 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000475 return SDOperand();
476}
477
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000478std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000479PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000480 //
481 // add beautiful description of PPC stack frame format, or at least some docs
482 //
483 MachineFunction &MF = DAG.getMachineFunction();
484 MachineFrameInfo *MFI = MF.getFrameInfo();
485 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000486 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000487 std::vector<SDOperand> ArgValues;
488
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000489 unsigned ArgOffset = 24;
490 unsigned GPR_remaining = 8;
491 unsigned FPR_remaining = 13;
492 unsigned GPR_idx = 0, FPR_idx = 0;
493 static const unsigned GPR[] = {
494 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
495 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
496 };
497 static const unsigned FPR[] = {
498 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
499 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
500 };
501
502 // Add DAG nodes to load the arguments... On entry to a function on PPC,
503 // the arguments start at offset 24, although they are likely to be passed
504 // in registers.
505 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
506 SDOperand newroot, argt;
507 unsigned ObjSize;
508 bool needsLoad = false;
509 bool ArgLive = !I->use_empty();
510 MVT::ValueType ObjectVT = getValueType(I->getType());
511
512 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000513 default: assert(0 && "Unhandled argument type!");
514 case MVT::i1:
515 case MVT::i8:
516 case MVT::i16:
517 case MVT::i32:
518 ObjSize = 4;
519 if (!ArgLive) break;
520 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000521 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000522 MF.addLiveIn(GPR[GPR_idx], VReg);
523 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000524 if (ObjectVT != MVT::i32) {
525 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
526 : ISD::AssertZext;
527 argt = DAG.getNode(AssertOp, MVT::i32, argt,
528 DAG.getValueType(ObjectVT));
529 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
530 }
Chris Lattner915fb302005-08-30 00:19:00 +0000531 } else {
532 needsLoad = true;
533 }
534 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000535 case MVT::i64:
536 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000537 if (!ArgLive) break;
538 if (GPR_remaining > 0) {
539 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000540 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000541 MF.addLiveIn(GPR[GPR_idx], VReg);
542 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000543 // If we have two or more remaining argument registers, then both halves
544 // of the i64 can be sourced from there. Otherwise, the lower half will
545 // have to come off the stack. This can happen when an i64 is preceded
546 // by 28 bytes of arguments.
547 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000548 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000549 MF.addLiveIn(GPR[GPR_idx+1], VReg);
550 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000551 } else {
552 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
553 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
554 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
555 DAG.getSrcValue(NULL));
556 }
557 // Build the outgoing arg thingy
558 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
559 newroot = argLo;
560 } else {
561 needsLoad = true;
562 }
563 break;
564 case MVT::f32:
565 case MVT::f64:
566 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000567 if (!ArgLive) {
568 if (FPR_remaining > 0) {
569 --FPR_remaining;
570 ++FPR_idx;
571 }
572 break;
573 }
Chris Lattner915fb302005-08-30 00:19:00 +0000574 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000575 unsigned VReg;
576 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000577 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000578 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000579 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000580 MF.addLiveIn(FPR[FPR_idx], VReg);
581 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000582 --FPR_remaining;
583 ++FPR_idx;
584 } else {
585 needsLoad = true;
586 }
587 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000588 }
589
590 // We need to load the argument to a virtual register if we determined above
591 // that we ran out of physical registers of the appropriate type
592 if (needsLoad) {
593 unsigned SubregOffset = 0;
594 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
595 if (ObjectVT == MVT::i16) SubregOffset = 2;
596 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
597 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
598 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
599 DAG.getConstant(SubregOffset, MVT::i32));
600 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
601 DAG.getSrcValue(NULL));
602 }
603
604 // Every 4 bytes of argument space consumes one of the GPRs available for
605 // argument passing.
606 if (GPR_remaining > 0) {
607 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
608 GPR_remaining -= delta;
609 GPR_idx += delta;
610 }
611 ArgOffset += ObjSize;
612 if (newroot.Val)
613 DAG.setRoot(newroot.getValue(1));
614
615 ArgValues.push_back(argt);
616 }
617
618 // If the function takes variable number of arguments, make a frame index for
619 // the start of the first vararg value... for expansion of llvm.va_start.
620 if (F.isVarArg()) {
621 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
622 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
623 // If this function is vararg, store any remaining integer argument regs
624 // to their spots on the stack so that they may be loaded by deferencing the
625 // result of va_next.
626 std::vector<SDOperand> MemOps;
627 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000628 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000629 MF.addLiveIn(GPR[GPR_idx], VReg);
630 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000631 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
632 Val, FIN, DAG.getSrcValue(NULL));
633 MemOps.push_back(Store);
634 // Increment the address by four for the next argument to store
635 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
636 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
637 }
Chris Lattner80720a92005-11-30 20:40:54 +0000638 if (!MemOps.empty()) {
639 MemOps.push_back(DAG.getRoot());
640 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
641 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000642 }
643
644 // Finally, inform the code generator which regs we return values in.
645 switch (getValueType(F.getReturnType())) {
646 default: assert(0 && "Unknown type!");
647 case MVT::isVoid: break;
648 case MVT::i1:
649 case MVT::i8:
650 case MVT::i16:
651 case MVT::i32:
652 MF.addLiveOut(PPC::R3);
653 break;
654 case MVT::i64:
655 MF.addLiveOut(PPC::R3);
656 MF.addLiveOut(PPC::R4);
657 break;
658 case MVT::f32:
659 case MVT::f64:
660 MF.addLiveOut(PPC::F1);
661 break;
662 }
663
664 return ArgValues;
665}
666
667std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000668PPCTargetLowering::LowerCallTo(SDOperand Chain,
669 const Type *RetTy, bool isVarArg,
670 unsigned CallingConv, bool isTailCall,
671 SDOperand Callee, ArgListTy &Args,
672 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000673 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000674 // SelectExpr to use to put the arguments in the appropriate registers.
675 std::vector<SDOperand> args_to_use;
676
677 // Count how many bytes are to be pushed on the stack, including the linkage
678 // area, and parameter passing area.
679 unsigned NumBytes = 24;
680
681 if (Args.empty()) {
682 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
683 DAG.getConstant(NumBytes, getPointerTy()));
684 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000685 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000686 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000687 default: assert(0 && "Unknown value type!");
688 case MVT::i1:
689 case MVT::i8:
690 case MVT::i16:
691 case MVT::i32:
692 case MVT::f32:
693 NumBytes += 4;
694 break;
695 case MVT::i64:
696 case MVT::f64:
697 NumBytes += 8;
698 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000699 }
Chris Lattner915fb302005-08-30 00:19:00 +0000700 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000701
Chris Lattner915fb302005-08-30 00:19:00 +0000702 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
703 // plus 32 bytes of argument space in case any called code gets funky on us.
704 // (Required by ABI to support var arg)
705 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000706
707 // Adjust the stack pointer for the new arguments...
708 // These operations are automatically eliminated by the prolog/epilog pass
709 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
710 DAG.getConstant(NumBytes, getPointerTy()));
711
712 // Set up a copy of the stack pointer for use loading and storing any
713 // arguments that may not fit in the registers available for argument
714 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000715 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000716
717 // Figure out which arguments are going to go in registers, and which in
718 // memory. Also, if this is a vararg function, floating point operations
719 // must be stored to our stack, and loaded into integer regs as well, if
720 // any integer regs are available for argument passing.
721 unsigned ArgOffset = 24;
722 unsigned GPR_remaining = 8;
723 unsigned FPR_remaining = 13;
724
725 std::vector<SDOperand> MemOps;
726 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
727 // PtrOff will be used to store the current argument to the stack if a
728 // register cannot be found for it.
729 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
730 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
731 MVT::ValueType ArgVT = getValueType(Args[i].second);
732
733 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000734 default: assert(0 && "Unexpected ValueType for argument!");
735 case MVT::i1:
736 case MVT::i8:
737 case MVT::i16:
738 // Promote the integer to 32 bits. If the input type is signed use a
739 // sign extend, otherwise use a zero extend.
740 if (Args[i].second->isSigned())
741 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
742 else
743 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
744 // FALL THROUGH
745 case MVT::i32:
746 if (GPR_remaining > 0) {
747 args_to_use.push_back(Args[i].first);
748 --GPR_remaining;
749 } else {
750 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
751 Args[i].first, PtrOff,
752 DAG.getSrcValue(NULL)));
753 }
754 ArgOffset += 4;
755 break;
756 case MVT::i64:
757 // If we have one free GPR left, we can place the upper half of the i64
758 // in it, and store the other half to the stack. If we have two or more
759 // free GPRs, then we can pass both halves of the i64 in registers.
760 if (GPR_remaining > 0) {
761 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
762 Args[i].first, DAG.getConstant(1, MVT::i32));
763 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
764 Args[i].first, DAG.getConstant(0, MVT::i32));
765 args_to_use.push_back(Hi);
766 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000767 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000768 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000769 --GPR_remaining;
770 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000771 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
772 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000773 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000774 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000775 }
Chris Lattner915fb302005-08-30 00:19:00 +0000776 } else {
777 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
778 Args[i].first, PtrOff,
779 DAG.getSrcValue(NULL)));
780 }
781 ArgOffset += 8;
782 break;
783 case MVT::f32:
784 case MVT::f64:
785 if (FPR_remaining > 0) {
786 args_to_use.push_back(Args[i].first);
787 --FPR_remaining;
788 if (isVarArg) {
789 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
790 Args[i].first, PtrOff,
791 DAG.getSrcValue(NULL));
792 MemOps.push_back(Store);
793 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000794 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000795 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
796 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000797 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000798 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000799 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000800 }
801 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000802 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
803 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000804 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
805 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000806 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000807 args_to_use.push_back(Load);
808 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000809 }
810 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000811 // If we have any FPRs remaining, we may also have GPRs remaining.
812 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
813 // GPRs.
814 if (GPR_remaining > 0) {
815 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
816 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000817 }
Chris Lattner915fb302005-08-30 00:19:00 +0000818 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
819 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
820 --GPR_remaining;
821 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000822 }
Chris Lattner915fb302005-08-30 00:19:00 +0000823 } else {
824 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
825 Args[i].first, PtrOff,
826 DAG.getSrcValue(NULL)));
827 }
828 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
829 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000830 }
831 }
832 if (!MemOps.empty())
833 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
834 }
835
836 std::vector<MVT::ValueType> RetVals;
837 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000838 MVT::ValueType ActualRetTyVT = RetTyVT;
839 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
840 ActualRetTyVT = MVT::i32; // Promote result to i32.
841
Chris Lattnere00ebf02006-01-28 07:33:03 +0000842 if (RetTyVT == MVT::i64) {
843 RetVals.push_back(MVT::i32);
844 RetVals.push_back(MVT::i32);
845 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000846 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000847 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000848 RetVals.push_back(MVT::Other);
849
Chris Lattner2823b3e2005-11-17 05:56:14 +0000850 // If the callee is a GlobalAddress node (quite common, every direct call is)
851 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
852 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
853 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
854
Chris Lattner281b55e2006-01-27 23:34:02 +0000855 std::vector<SDOperand> Ops;
856 Ops.push_back(Chain);
857 Ops.push_back(Callee);
858 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
859 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000860 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000861 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
862 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000863 SDOperand RetVal = TheCall;
864
865 // If the result is a small value, add a note so that we keep track of the
866 // information about whether it is sign or zero extended.
867 if (RetTyVT != ActualRetTyVT) {
868 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
869 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
870 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000871 } else if (RetTyVT == MVT::i64) {
872 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000873 }
874
875 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000876}
877
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000878MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000879PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
880 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000881 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000882 MI->getOpcode() == PPC::SELECT_CC_F4 ||
883 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000884 "Unexpected instr type to insert");
885
886 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
887 // control-flow pattern. The incoming instruction knows the destination vreg
888 // to set, the condition code register to branch on, the true/false values to
889 // select between, and a branch opcode to use.
890 const BasicBlock *LLVM_BB = BB->getBasicBlock();
891 ilist<MachineBasicBlock>::iterator It = BB;
892 ++It;
893
894 // thisMBB:
895 // ...
896 // TrueVal = ...
897 // cmpTY ccX, r1, r2
898 // bCC copy1MBB
899 // fallthrough --> copy0MBB
900 MachineBasicBlock *thisMBB = BB;
901 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
902 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
903 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
904 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
905 MachineFunction *F = BB->getParent();
906 F->getBasicBlockList().insert(It, copy0MBB);
907 F->getBasicBlockList().insert(It, sinkMBB);
908 // Update machine-CFG edges
909 BB->addSuccessor(copy0MBB);
910 BB->addSuccessor(sinkMBB);
911
912 // copy0MBB:
913 // %FalseValue = ...
914 // # fallthrough to sinkMBB
915 BB = copy0MBB;
916
917 // Update machine-CFG edges
918 BB->addSuccessor(sinkMBB);
919
920 // sinkMBB:
921 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
922 // ...
923 BB = sinkMBB;
924 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
925 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
926 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
927
928 delete MI; // The pseudo instruction is gone now.
929 return BB;
930}
931