blob: 7991821133f1690bd81312a1bc54183c93405ec0 [file] [log] [blame]
Nate Begeman1d9d7422005-10-18 00:28:58 +00001//===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
Chris Lattner7c5a3d32005-08-16 17:14:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Nate Begeman21e463b2005-10-16 05:39:50 +000010// This file implements the PPCISelLowering class.
Chris Lattner7c5a3d32005-08-16 17:14:42 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner16e71f22005-10-14 23:59:06 +000014#include "PPCISelLowering.h"
15#include "PPCTargetMachine.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
17#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner8a2d3ca2005-08-26 21:23:58 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000019#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7b738342005-09-13 19:33:40 +000020#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner0b1e4e52005-08-26 17:36:52 +000021#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022#include "llvm/Function.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000023using namespace llvm;
24
Nate Begeman21e463b2005-10-16 05:39:50 +000025PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000026 : TargetLowering(TM) {
27
28 // Fold away setcc operations if possible.
29 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000030 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000031
Chris Lattnerd145a612005-09-27 22:18:25 +000032 // Use _setjmp/_longjmp instead of setjmp/longjmp.
33 setUseUnderscoreSetJmpLongJmp(true);
34
Chris Lattner7c5a3d32005-08-16 17:14:42 +000035 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000036 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
37 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
38 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000039
40 // PowerPC has no intrinsics for these particular operations
41 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
42 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
43 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
44
45 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
46 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
47 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
48
49 // PowerPC has no SREM/UREM instructions
50 setOperationAction(ISD::SREM, MVT::i32, Expand);
51 setOperationAction(ISD::UREM, MVT::i32, Expand);
52
53 // We don't support sin/cos/sqrt/fmod
54 setOperationAction(ISD::FSIN , MVT::f64, Expand);
55 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000056 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000057 setOperationAction(ISD::FSIN , MVT::f32, Expand);
58 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000059 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000060
61 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000062 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000063 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
64 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
65 }
66
Nate Begemand88fc032006-01-14 03:14:10 +000067 // PowerPC does not have BSWAP, CTPOP or CTTZ
68 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000069 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
70 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
71
Nate Begeman35ef9132006-01-11 21:21:00 +000072 // PowerPC does not have ROTR
73 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
74
Chris Lattner7c5a3d32005-08-16 17:14:42 +000075 // PowerPC does not have Select
76 setOperationAction(ISD::SELECT, MVT::i32, Expand);
77 setOperationAction(ISD::SELECT, MVT::f32, Expand);
78 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000079
Chris Lattner0b1e4e52005-08-26 17:36:52 +000080 // PowerPC wants to turn select_cc of FP into fsel when possible.
81 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
82 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000083
Nate Begeman7cbd5252005-08-16 19:49:35 +000084 // PowerPC does not have BRCOND* which requires SetCC
85 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
86 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000087
Chris Lattnerf7605322005-08-31 21:09:52 +000088 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
89 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000090
Jim Laskeyad23c9d2005-08-17 00:40:22 +000091 // PowerPC does not have [U|S]INT_TO_FP
92 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
93 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
94
Chris Lattner53e88452005-12-23 05:13:35 +000095 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
96 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
97
Chris Lattnere6ec9f22005-09-10 00:21:06 +000098 // PowerPC does not have truncstore for i1.
99 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000100
Jim Laskeyabf6d172006-01-05 01:25:28 +0000101 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000102 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000103 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000104 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000105 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000106 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000107
Nate Begeman28a6b022005-12-10 02:36:00 +0000108 // We want to legalize GlobalAddress and ConstantPool nodes into the
109 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000110 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000111 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000112
113 // Not implemented yet.
114 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
115 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000116
Nate Begemanc09eeec2005-09-06 22:03:27 +0000117 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000118 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000119 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
120 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000121 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
122 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
123 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000124 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000125 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000126 }
127
128 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
129 // 64 bit PowerPC implementations can support i64 types directly
130 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000131 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
132 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000133 } else {
134 // 32 bit PowerPC wants to expand i64 shifts itself.
135 setOperationAction(ISD::SHL, MVT::i64, Custom);
136 setOperationAction(ISD::SRL, MVT::i64, Custom);
137 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000138 }
139
Nate Begeman425a9692005-11-29 08:17:20 +0000140 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000141 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000142 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Nate Begeman425a9692005-11-29 08:17:20 +0000143 }
144
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000145 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000146 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000147
148 computeRegisterProperties();
149}
150
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000151const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
152 switch (Opcode) {
153 default: return 0;
154 case PPCISD::FSEL: return "PPCISD::FSEL";
155 case PPCISD::FCFID: return "PPCISD::FCFID";
156 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
157 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
158 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
159 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
160 case PPCISD::Hi: return "PPCISD::Hi";
161 case PPCISD::Lo: return "PPCISD::Lo";
162 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
163 case PPCISD::SRL: return "PPCISD::SRL";
164 case PPCISD::SRA: return "PPCISD::SRA";
165 case PPCISD::SHL: return "PPCISD::SHL";
166 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
167 }
168}
169
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000170/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
171static bool isFloatingPointZero(SDOperand Op) {
172 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
173 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
174 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
175 // Maybe this has already been legalized into the constant pool?
176 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
177 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
178 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
179 }
180 return false;
181}
182
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000183/// LowerOperation - Provide custom lowering hooks for some operations.
184///
Nate Begeman21e463b2005-10-16 05:39:50 +0000185SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000186 switch (Op.getOpcode()) {
187 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000188 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000189 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000190 SDOperand Src = Op.getOperand(0);
191 if (Src.getValueType() == MVT::f32)
192 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
193
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000194 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000195 switch (Op.getValueType()) {
196 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
197 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000198 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000199 break;
200 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000201 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000202 break;
203 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000204
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000205 // Convert the FP value to an int value through memory.
206 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
207 if (Op.getValueType() == MVT::i32)
208 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
209 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000210 }
211 case ISD::SINT_TO_FP: {
212 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
213 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000214 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
215 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000216 if (MVT::f32 == Op.getValueType())
217 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
218 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000219 }
220 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000221 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000222 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
223 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
224 break;
225
226 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
227
228 // Cannot handle SETEQ/SETNE.
229 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
230
231 MVT::ValueType ResVT = Op.getValueType();
232 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
233 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
234 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000235
Chris Lattnerf7605322005-08-31 21:09:52 +0000236 // If the RHS of the comparison is a 0.0, we don't need to do the
237 // subtraction at all.
238 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000239 switch (CC) {
240 default: assert(0 && "Invalid FSEL condition"); abort();
241 case ISD::SETULT:
242 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000243 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000244 case ISD::SETUGE:
245 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000246 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
247 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000248 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000249 case ISD::SETUGT:
250 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000251 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000252 case ISD::SETULE:
253 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000254 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
255 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000256 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000257 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000258 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000259
Chris Lattnereb255f22005-10-25 20:54:57 +0000260 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000261 switch (CC) {
262 default: assert(0 && "Invalid FSEL condition"); abort();
263 case ISD::SETULT:
264 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000265 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
266 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
267 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
268 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000269 case ISD::SETUGE:
270 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000271 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
272 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
273 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
274 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000275 case ISD::SETUGT:
276 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000277 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
278 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
279 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
280 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000281 case ISD::SETULE:
282 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000283 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
284 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
285 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
286 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000287 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000288 break;
289 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000290 case ISD::SHL: {
291 assert(Op.getValueType() == MVT::i64 &&
292 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
293 // The generic code does a fine job expanding shift by a constant.
294 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
295
296 // Otherwise, expand into a bunch of logical ops. Note that these ops
297 // depend on the PPC behavior for oversized shift amounts.
298 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
299 DAG.getConstant(0, MVT::i32));
300 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
301 DAG.getConstant(1, MVT::i32));
302 SDOperand Amt = Op.getOperand(1);
303
304 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
305 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000306 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
307 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000308 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
309 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
310 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000311 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000312 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000313 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000314 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
315 }
316 case ISD::SRL: {
317 assert(Op.getValueType() == MVT::i64 &&
318 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
319 // The generic code does a fine job expanding shift by a constant.
320 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
321
322 // Otherwise, expand into a bunch of logical ops. Note that these ops
323 // depend on the PPC behavior for oversized shift amounts.
324 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
325 DAG.getConstant(0, MVT::i32));
326 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
327 DAG.getConstant(1, MVT::i32));
328 SDOperand Amt = Op.getOperand(1);
329
330 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
331 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000332 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
333 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000334 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
335 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
336 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000337 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000338 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000339 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000340 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
341 }
342 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000343 assert(Op.getValueType() == MVT::i64 &&
344 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
345 // The generic code does a fine job expanding shift by a constant.
346 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
347
348 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
349 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
350 DAG.getConstant(0, MVT::i32));
351 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
352 DAG.getConstant(1, MVT::i32));
353 SDOperand Amt = Op.getOperand(1);
354
355 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
356 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000357 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
358 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000359 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
360 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
361 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000362 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
363 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000364 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
365 Tmp4, Tmp6, ISD::SETLE);
366 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000367 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000368 case ISD::ConstantPool: {
369 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
370 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
371 SDOperand Zero = DAG.getConstant(0, MVT::i32);
372
373 if (PPCGenerateStaticCode) {
374 // Generate non-pic code that has direct accesses to the constant pool.
375 // The address of the global is just (hi(&g)+lo(&g)).
376 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
377 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
378 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
379 }
380
381 // Only lower ConstantPool on Darwin.
382 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
383 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
384 if (PICEnabled) {
385 // With PIC, the first instruction is actually "GR+hi(&G)".
386 Hi = DAG.getNode(ISD::ADD, MVT::i32,
387 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
388 }
389
390 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
391 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
392 return Lo;
393 }
Chris Lattner860e8862005-11-17 07:30:41 +0000394 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000395 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
396 GlobalValue *GV = GSDN->getGlobal();
397 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000398 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000399
400 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000401 // Generate non-pic code that has direct accesses to globals.
402 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000403 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
404 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
405 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
406 }
Chris Lattner860e8862005-11-17 07:30:41 +0000407
Chris Lattner1d05cb42005-11-17 18:55:48 +0000408 // Only lower GlobalAddress on Darwin.
409 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000410
Chris Lattner860e8862005-11-17 07:30:41 +0000411 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
412 if (PICEnabled) {
413 // With PIC, the first instruction is actually "GR+hi(&G)".
414 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000415 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000416 }
417
418 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
419 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
420
Chris Lattnera35ef632006-01-06 01:04:03 +0000421 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && !GV->isExternal())
Chris Lattner860e8862005-11-17 07:30:41 +0000422 return Lo;
423
424 // If the global is weak or external, we have to go through the lazy
425 // resolution stub.
426 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
427 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000428 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000429 return SDOperand();
430}
431
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000432std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000433PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000434 //
435 // add beautiful description of PPC stack frame format, or at least some docs
436 //
437 MachineFunction &MF = DAG.getMachineFunction();
438 MachineFrameInfo *MFI = MF.getFrameInfo();
439 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000440 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000441 std::vector<SDOperand> ArgValues;
442
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000443 unsigned ArgOffset = 24;
444 unsigned GPR_remaining = 8;
445 unsigned FPR_remaining = 13;
446 unsigned GPR_idx = 0, FPR_idx = 0;
447 static const unsigned GPR[] = {
448 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
449 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
450 };
451 static const unsigned FPR[] = {
452 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
453 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
454 };
455
456 // Add DAG nodes to load the arguments... On entry to a function on PPC,
457 // the arguments start at offset 24, although they are likely to be passed
458 // in registers.
459 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
460 SDOperand newroot, argt;
461 unsigned ObjSize;
462 bool needsLoad = false;
463 bool ArgLive = !I->use_empty();
464 MVT::ValueType ObjectVT = getValueType(I->getType());
465
466 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000467 default: assert(0 && "Unhandled argument type!");
468 case MVT::i1:
469 case MVT::i8:
470 case MVT::i16:
471 case MVT::i32:
472 ObjSize = 4;
473 if (!ArgLive) break;
474 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000475 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000476 MF.addLiveIn(GPR[GPR_idx], VReg);
477 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000478 if (ObjectVT != MVT::i32) {
479 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
480 : ISD::AssertZext;
481 argt = DAG.getNode(AssertOp, MVT::i32, argt,
482 DAG.getValueType(ObjectVT));
483 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
484 }
Chris Lattner915fb302005-08-30 00:19:00 +0000485 } else {
486 needsLoad = true;
487 }
488 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000489 case MVT::i64:
490 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000491 if (!ArgLive) break;
492 if (GPR_remaining > 0) {
493 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000494 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000495 MF.addLiveIn(GPR[GPR_idx], VReg);
496 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000497 // If we have two or more remaining argument registers, then both halves
498 // of the i64 can be sourced from there. Otherwise, the lower half will
499 // have to come off the stack. This can happen when an i64 is preceded
500 // by 28 bytes of arguments.
501 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000502 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000503 MF.addLiveIn(GPR[GPR_idx+1], VReg);
504 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000505 } else {
506 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
507 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
508 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
509 DAG.getSrcValue(NULL));
510 }
511 // Build the outgoing arg thingy
512 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
513 newroot = argLo;
514 } else {
515 needsLoad = true;
516 }
517 break;
518 case MVT::f32:
519 case MVT::f64:
520 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000521 if (!ArgLive) {
522 if (FPR_remaining > 0) {
523 --FPR_remaining;
524 ++FPR_idx;
525 }
526 break;
527 }
Chris Lattner915fb302005-08-30 00:19:00 +0000528 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000529 unsigned VReg;
530 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000531 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000532 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000533 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000534 MF.addLiveIn(FPR[FPR_idx], VReg);
535 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000536 --FPR_remaining;
537 ++FPR_idx;
538 } else {
539 needsLoad = true;
540 }
541 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000542 }
543
544 // We need to load the argument to a virtual register if we determined above
545 // that we ran out of physical registers of the appropriate type
546 if (needsLoad) {
547 unsigned SubregOffset = 0;
548 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
549 if (ObjectVT == MVT::i16) SubregOffset = 2;
550 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
551 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
552 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
553 DAG.getConstant(SubregOffset, MVT::i32));
554 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
555 DAG.getSrcValue(NULL));
556 }
557
558 // Every 4 bytes of argument space consumes one of the GPRs available for
559 // argument passing.
560 if (GPR_remaining > 0) {
561 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
562 GPR_remaining -= delta;
563 GPR_idx += delta;
564 }
565 ArgOffset += ObjSize;
566 if (newroot.Val)
567 DAG.setRoot(newroot.getValue(1));
568
569 ArgValues.push_back(argt);
570 }
571
572 // If the function takes variable number of arguments, make a frame index for
573 // the start of the first vararg value... for expansion of llvm.va_start.
574 if (F.isVarArg()) {
575 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
576 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
577 // If this function is vararg, store any remaining integer argument regs
578 // to their spots on the stack so that they may be loaded by deferencing the
579 // result of va_next.
580 std::vector<SDOperand> MemOps;
581 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000582 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000583 MF.addLiveIn(GPR[GPR_idx], VReg);
584 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000585 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
586 Val, FIN, DAG.getSrcValue(NULL));
587 MemOps.push_back(Store);
588 // Increment the address by four for the next argument to store
589 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
590 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
591 }
Chris Lattner80720a92005-11-30 20:40:54 +0000592 if (!MemOps.empty()) {
593 MemOps.push_back(DAG.getRoot());
594 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
595 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000596 }
597
598 // Finally, inform the code generator which regs we return values in.
599 switch (getValueType(F.getReturnType())) {
600 default: assert(0 && "Unknown type!");
601 case MVT::isVoid: break;
602 case MVT::i1:
603 case MVT::i8:
604 case MVT::i16:
605 case MVT::i32:
606 MF.addLiveOut(PPC::R3);
607 break;
608 case MVT::i64:
609 MF.addLiveOut(PPC::R3);
610 MF.addLiveOut(PPC::R4);
611 break;
612 case MVT::f32:
613 case MVT::f64:
614 MF.addLiveOut(PPC::F1);
615 break;
616 }
617
618 return ArgValues;
619}
620
621std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000622PPCTargetLowering::LowerCallTo(SDOperand Chain,
623 const Type *RetTy, bool isVarArg,
624 unsigned CallingConv, bool isTailCall,
625 SDOperand Callee, ArgListTy &Args,
626 SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000627 // args_to_use will accumulate outgoing args for the ISD::CALL case in
628 // SelectExpr to use to put the arguments in the appropriate registers.
629 std::vector<SDOperand> args_to_use;
630
631 // Count how many bytes are to be pushed on the stack, including the linkage
632 // area, and parameter passing area.
633 unsigned NumBytes = 24;
634
635 if (Args.empty()) {
636 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
637 DAG.getConstant(NumBytes, getPointerTy()));
638 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000639 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000640 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000641 default: assert(0 && "Unknown value type!");
642 case MVT::i1:
643 case MVT::i8:
644 case MVT::i16:
645 case MVT::i32:
646 case MVT::f32:
647 NumBytes += 4;
648 break;
649 case MVT::i64:
650 case MVT::f64:
651 NumBytes += 8;
652 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000653 }
Chris Lattner915fb302005-08-30 00:19:00 +0000654 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000655
Chris Lattner915fb302005-08-30 00:19:00 +0000656 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
657 // plus 32 bytes of argument space in case any called code gets funky on us.
658 // (Required by ABI to support var arg)
659 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000660
661 // Adjust the stack pointer for the new arguments...
662 // These operations are automatically eliminated by the prolog/epilog pass
663 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
664 DAG.getConstant(NumBytes, getPointerTy()));
665
666 // Set up a copy of the stack pointer for use loading and storing any
667 // arguments that may not fit in the registers available for argument
668 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000669 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000670
671 // Figure out which arguments are going to go in registers, and which in
672 // memory. Also, if this is a vararg function, floating point operations
673 // must be stored to our stack, and loaded into integer regs as well, if
674 // any integer regs are available for argument passing.
675 unsigned ArgOffset = 24;
676 unsigned GPR_remaining = 8;
677 unsigned FPR_remaining = 13;
678
679 std::vector<SDOperand> MemOps;
680 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
681 // PtrOff will be used to store the current argument to the stack if a
682 // register cannot be found for it.
683 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
684 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
685 MVT::ValueType ArgVT = getValueType(Args[i].second);
686
687 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000688 default: assert(0 && "Unexpected ValueType for argument!");
689 case MVT::i1:
690 case MVT::i8:
691 case MVT::i16:
692 // Promote the integer to 32 bits. If the input type is signed use a
693 // sign extend, otherwise use a zero extend.
694 if (Args[i].second->isSigned())
695 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
696 else
697 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
698 // FALL THROUGH
699 case MVT::i32:
700 if (GPR_remaining > 0) {
701 args_to_use.push_back(Args[i].first);
702 --GPR_remaining;
703 } else {
704 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
705 Args[i].first, PtrOff,
706 DAG.getSrcValue(NULL)));
707 }
708 ArgOffset += 4;
709 break;
710 case MVT::i64:
711 // If we have one free GPR left, we can place the upper half of the i64
712 // in it, and store the other half to the stack. If we have two or more
713 // free GPRs, then we can pass both halves of the i64 in registers.
714 if (GPR_remaining > 0) {
715 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
716 Args[i].first, DAG.getConstant(1, MVT::i32));
717 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
718 Args[i].first, DAG.getConstant(0, MVT::i32));
719 args_to_use.push_back(Hi);
720 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000721 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000722 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000723 --GPR_remaining;
724 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000725 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
726 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000727 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000728 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000729 }
Chris Lattner915fb302005-08-30 00:19:00 +0000730 } else {
731 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
732 Args[i].first, PtrOff,
733 DAG.getSrcValue(NULL)));
734 }
735 ArgOffset += 8;
736 break;
737 case MVT::f32:
738 case MVT::f64:
739 if (FPR_remaining > 0) {
740 args_to_use.push_back(Args[i].first);
741 --FPR_remaining;
742 if (isVarArg) {
743 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
744 Args[i].first, PtrOff,
745 DAG.getSrcValue(NULL));
746 MemOps.push_back(Store);
747 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000748 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000749 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
750 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000751 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000752 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000753 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000754 }
755 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000756 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
757 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000758 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
759 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000760 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000761 args_to_use.push_back(Load);
762 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000763 }
764 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000765 // If we have any FPRs remaining, we may also have GPRs remaining.
766 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
767 // GPRs.
768 if (GPR_remaining > 0) {
769 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
770 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000771 }
Chris Lattner915fb302005-08-30 00:19:00 +0000772 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
773 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
774 --GPR_remaining;
775 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000776 }
Chris Lattner915fb302005-08-30 00:19:00 +0000777 } else {
778 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
779 Args[i].first, PtrOff,
780 DAG.getSrcValue(NULL)));
781 }
782 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
783 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000784 }
785 }
786 if (!MemOps.empty())
787 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
788 }
789
790 std::vector<MVT::ValueType> RetVals;
791 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000792 MVT::ValueType ActualRetTyVT = RetTyVT;
793 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
794 ActualRetTyVT = MVT::i32; // Promote result to i32.
795
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000796 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000797 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000798 RetVals.push_back(MVT::Other);
799
Chris Lattner2823b3e2005-11-17 05:56:14 +0000800 // If the callee is a GlobalAddress node (quite common, every direct call is)
801 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
802 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
803 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
804
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000805 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
806 Chain, Callee, args_to_use), 0);
807 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
808 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
809 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000810 SDOperand RetVal = TheCall;
811
812 // If the result is a small value, add a note so that we keep track of the
813 // information about whether it is sign or zero extended.
814 if (RetTyVT != ActualRetTyVT) {
815 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
816 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
817 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
818 }
819
820 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000821}
822
Nate Begeman4a959452005-10-18 23:23:37 +0000823SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
824 SelectionDAG &DAG) {
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000825 SDOperand Copy;
826 switch (Op.getValueType()) {
827 default: assert(0 && "Unknown type to return!");
828 case MVT::i32:
829 Copy = DAG.getCopyToReg(Chain, PPC::R3, Op, SDOperand());
830 break;
831 case MVT::f32:
832 case MVT::f64:
833 Copy = DAG.getCopyToReg(Chain, PPC::F1, Op, SDOperand());
834 break;
835 case MVT::i64:
836 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
837 DAG.getConstant(1, MVT::i32));
838 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
839 DAG.getConstant(0, MVT::i32));
840 Copy = DAG.getCopyToReg(Chain, PPC::R3, Hi, SDOperand());
841 Copy = DAG.getCopyToReg(Copy, PPC::R4, Lo, Copy.getValue(1));
842 break;
Nate Begeman4a959452005-10-18 23:23:37 +0000843 }
Nate Begeman9e4dd9d2005-12-20 00:26:01 +0000844 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Nate Begeman4a959452005-10-18 23:23:37 +0000845}
846
Nate Begeman21e463b2005-10-16 05:39:50 +0000847SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
848 Value *VAListV, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000849 // vastart just stores the address of the VarArgsFrameIndex slot into the
850 // memory location argument.
851 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
852 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
853 DAG.getSrcValue(VAListV));
854}
855
856std::pair<SDOperand,SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000857PPCTargetLowering::LowerVAArg(SDOperand Chain,
858 SDOperand VAListP, Value *VAListV,
859 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000860 MVT::ValueType ArgVT = getValueType(ArgTy);
861
862 SDOperand VAList =
863 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
864 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
865 unsigned Amt;
866 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
867 Amt = 4;
868 else {
869 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
870 "Other types should have been promoted for varargs!");
871 Amt = 8;
872 }
873 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
874 DAG.getConstant(Amt, VAList.getValueType()));
875 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
876 VAList, VAListP, DAG.getSrcValue(VAListV));
877 return std::make_pair(Result, Chain);
878}
879
880
Nate Begeman21e463b2005-10-16 05:39:50 +0000881std::pair<SDOperand, SDOperand> PPCTargetLowering::
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000882LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
883 SelectionDAG &DAG) {
884 assert(0 && "LowerFrameReturnAddress unimplemented");
885 abort();
886}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000887
888MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000889PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
890 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000891 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000892 MI->getOpcode() == PPC::SELECT_CC_F4 ||
893 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000894 "Unexpected instr type to insert");
895
896 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
897 // control-flow pattern. The incoming instruction knows the destination vreg
898 // to set, the condition code register to branch on, the true/false values to
899 // select between, and a branch opcode to use.
900 const BasicBlock *LLVM_BB = BB->getBasicBlock();
901 ilist<MachineBasicBlock>::iterator It = BB;
902 ++It;
903
904 // thisMBB:
905 // ...
906 // TrueVal = ...
907 // cmpTY ccX, r1, r2
908 // bCC copy1MBB
909 // fallthrough --> copy0MBB
910 MachineBasicBlock *thisMBB = BB;
911 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
912 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
913 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
914 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
915 MachineFunction *F = BB->getParent();
916 F->getBasicBlockList().insert(It, copy0MBB);
917 F->getBasicBlockList().insert(It, sinkMBB);
918 // Update machine-CFG edges
919 BB->addSuccessor(copy0MBB);
920 BB->addSuccessor(sinkMBB);
921
922 // copy0MBB:
923 // %FalseValue = ...
924 // # fallthrough to sinkMBB
925 BB = copy0MBB;
926
927 // Update machine-CFG edges
928 BB->addSuccessor(sinkMBB);
929
930 // sinkMBB:
931 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
932 // ...
933 BB = sinkMBB;
934 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
935 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
936 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
937
938 delete MI; // The pseudo instruction is gone now.
939 return BB;
940}
941