blob: 8b5546d62cdc01451732f3b4c93a693ff819ac59 [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 Lattnerddc787d2006-01-31 19:20:21 +000023#include "llvm/ADT/VectorExtras.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000024using namespace llvm;
25
Nate Begeman21e463b2005-10-16 05:39:50 +000026PPCTargetLowering::PPCTargetLowering(TargetMachine &TM)
Chris Lattner7c5a3d32005-08-16 17:14:42 +000027 : TargetLowering(TM) {
28
29 // Fold away setcc operations if possible.
30 setSetCCIsExpensive();
Nate Begeman405e3ec2005-10-21 00:02:42 +000031 setPow2DivIsCheap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +000032
Chris Lattnerd145a612005-09-27 22:18:25 +000033 // Use _setjmp/_longjmp instead of setjmp/longjmp.
34 setUseUnderscoreSetJmpLongJmp(true);
35
Chris Lattner7c5a3d32005-08-16 17:14:42 +000036 // Set up the register classes.
Nate Begeman1d9d7422005-10-18 00:28:58 +000037 addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
38 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
39 addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000040
Chris Lattnera54aa942006-01-29 06:26:08 +000041 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
42 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
43
Chris Lattner7c5a3d32005-08-16 17:14:42 +000044 // PowerPC has no intrinsics for these particular operations
45 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
46 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
47 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
48
49 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
50 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
51 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
52
53 // PowerPC has no SREM/UREM instructions
54 setOperationAction(ISD::SREM, MVT::i32, Expand);
55 setOperationAction(ISD::UREM, MVT::i32, Expand);
56
57 // We don't support sin/cos/sqrt/fmod
58 setOperationAction(ISD::FSIN , MVT::f64, Expand);
59 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000060 setOperationAction(ISD::FREM , MVT::f64, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000061 setOperationAction(ISD::FSIN , MVT::f32, Expand);
62 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner615c2d02005-09-28 22:29:58 +000063 setOperationAction(ISD::FREM , MVT::f32, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000064
65 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000066 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000067 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
68 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
69 }
70
Nate Begemand88fc032006-01-14 03:14:10 +000071 // PowerPC does not have BSWAP, CTPOP or CTTZ
72 setOperationAction(ISD::BSWAP, MVT::i32 , Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000073 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
74 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
75
Nate Begeman35ef9132006-01-11 21:21:00 +000076 // PowerPC does not have ROTR
77 setOperationAction(ISD::ROTR, MVT::i32 , Expand);
78
Chris Lattner7c5a3d32005-08-16 17:14:42 +000079 // PowerPC does not have Select
80 setOperationAction(ISD::SELECT, MVT::i32, Expand);
81 setOperationAction(ISD::SELECT, MVT::f32, Expand);
82 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000083
Chris Lattner0b1e4e52005-08-26 17:36:52 +000084 // PowerPC wants to turn select_cc of FP into fsel when possible.
85 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
86 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Nate Begeman44775902006-01-31 08:17:29 +000087
88 // PowerPC wants to optimize setcc i32, imm a bit.
89 setOperationAction(ISD::SETCC, MVT::i32, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000090
Nate Begeman7cbd5252005-08-16 19:49:35 +000091 // PowerPC does not have BRCOND* which requires SetCC
92 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
93 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000094
Chris Lattnerf7605322005-08-31 21:09:52 +000095 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
96 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000097
Jim Laskeyad23c9d2005-08-17 00:40:22 +000098 // PowerPC does not have [U|S]INT_TO_FP
99 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
100 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
101
Chris Lattner53e88452005-12-23 05:13:35 +0000102 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
103 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
104
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000105 // PowerPC does not have truncstore for i1.
106 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000107
Jim Laskeyabf6d172006-01-05 01:25:28 +0000108 // Support label based line numbers.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000109 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000110 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Jim Laskeyabf6d172006-01-05 01:25:28 +0000111 // FIXME - use subtarget debug flags
Jim Laskeye0bce712006-01-05 01:47:43 +0000112 if (!TM.getSubtarget<PPCSubtarget>().isDarwin())
Jim Laskeyabf6d172006-01-05 01:25:28 +0000113 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnere6ec9f22005-09-10 00:21:06 +0000114
Nate Begeman28a6b022005-12-10 02:36:00 +0000115 // We want to legalize GlobalAddress and ConstantPool nodes into the
116 // appropriate instructions to materialize the address.
Chris Lattner3eef4e32005-11-17 18:26:56 +0000117 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Nate Begeman28a6b022005-12-10 02:36:00 +0000118 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000119
Nate Begemanee625572006-01-27 21:09:22 +0000120 // RET must be custom lowered, to meet ABI requirements
121 setOperationAction(ISD::RET , MVT::Other, Custom);
122
Nate Begemanacc398c2006-01-25 18:21:52 +0000123 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
124 setOperationAction(ISD::VASTART , MVT::Other, Custom);
125
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000126 // Use the default implementation.
Nate Begemanacc398c2006-01-25 18:21:52 +0000127 setOperationAction(ISD::VAARG , MVT::Other, Expand);
128 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
129 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnerb22c08b2006-01-15 09:02:48 +0000130 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
131 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
132 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner860e8862005-11-17 07:30:41 +0000133
Nate Begemanc09eeec2005-09-06 22:03:27 +0000134 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000135 // They also have instructions for converting between i64 and fp.
Nate Begemanc09eeec2005-09-06 22:03:27 +0000136 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
137 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Nate Begemanae749a92005-10-25 23:48:36 +0000138 // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
139 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
140 } else {
Chris Lattner860e8862005-11-17 07:30:41 +0000141 // PowerPC does not have FP_TO_UINT on 32-bit implementations.
Nate Begemanae749a92005-10-25 23:48:36 +0000142 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
Nate Begeman9d2b8172005-10-18 00:56:42 +0000143 }
144
145 if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
146 // 64 bit PowerPC implementations can support i64 types directly
147 addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000148 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
149 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
Nate Begeman1d9d7422005-10-18 00:28:58 +0000150 } else {
151 // 32 bit PowerPC wants to expand i64 shifts itself.
152 setOperationAction(ISD::SHL, MVT::i64, Custom);
153 setOperationAction(ISD::SRL, MVT::i64, Custom);
154 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000155 }
156
Nate Begeman425a9692005-11-29 08:17:20 +0000157 if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
Nate Begeman425a9692005-11-29 08:17:20 +0000158 addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000159 addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
Chris Lattnerec4a0c72006-01-29 06:32:58 +0000160
161 // FIXME: We don't support any ConstantVec's yet. We should custom expand
162 // the ones we do!
Chris Lattnerd9b55dd2006-01-29 08:41:37 +0000163 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
164 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
Nate Begeman425a9692005-11-29 08:17:20 +0000165 }
166
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000167 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattnercadd7422006-01-13 17:52:03 +0000168 setStackPointerRegisterToSaveRestore(PPC::R1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000169
170 computeRegisterProperties();
171}
172
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000173const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
174 switch (Opcode) {
175 default: return 0;
176 case PPCISD::FSEL: return "PPCISD::FSEL";
177 case PPCISD::FCFID: return "PPCISD::FCFID";
178 case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ";
179 case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ";
180 case PPCISD::VMADDFP: return "PPCISD::VMADDFP";
181 case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP";
182 case PPCISD::Hi: return "PPCISD::Hi";
183 case PPCISD::Lo: return "PPCISD::Lo";
184 case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg";
185 case PPCISD::SRL: return "PPCISD::SRL";
186 case PPCISD::SRA: return "PPCISD::SRA";
187 case PPCISD::SHL: return "PPCISD::SHL";
Chris Lattnere00ebf02006-01-28 07:33:03 +0000188 case PPCISD::CALL: return "PPCISD::CALL";
Chris Lattnerda6d20f2006-01-09 23:52:17 +0000189 case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG";
190 }
191}
192
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000193/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
194static bool isFloatingPointZero(SDOperand Op) {
195 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
196 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
197 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
198 // Maybe this has already been legalized into the constant pool?
199 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
200 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
201 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
202 }
203 return false;
204}
205
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000206/// LowerOperation - Provide custom lowering hooks for some operations.
207///
Nate Begeman21e463b2005-10-16 05:39:50 +0000208SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000209 switch (Op.getOpcode()) {
210 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000211 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000212 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
Chris Lattner7c0d6642005-10-02 06:37:13 +0000213 SDOperand Src = Op.getOperand(0);
214 if (Src.getValueType() == MVT::f32)
215 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
216
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000217 SDOperand Tmp;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000218 switch (Op.getValueType()) {
219 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
220 case MVT::i32:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000221 Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000222 break;
223 case MVT::i64:
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000224 Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000225 break;
226 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000227
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000228 // Convert the FP value to an int value through memory.
229 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp);
230 if (Op.getValueType() == MVT::i32)
231 Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits);
232 return Bits;
Nate Begemanc09eeec2005-09-06 22:03:27 +0000233 }
234 case ISD::SINT_TO_FP: {
235 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
236 "Unhandled SINT_TO_FP type in custom expander!");
Chris Lattner1b95e0b2005-12-23 00:59:59 +0000237 SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0));
238 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits);
Nate Begemanc09eeec2005-09-06 22:03:27 +0000239 if (MVT::f32 == Op.getValueType())
240 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
241 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000242 }
243 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000244 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000245 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
246 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
247 break;
248
249 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
250
251 // Cannot handle SETEQ/SETNE.
252 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
253
254 MVT::ValueType ResVT = Op.getValueType();
255 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
256 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
257 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000258
Chris Lattnerf7605322005-08-31 21:09:52 +0000259 // If the RHS of the comparison is a 0.0, we don't need to do the
260 // subtraction at all.
261 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000262 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000263 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000264 case ISD::SETULT:
265 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000266 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000267 case ISD::SETUGE:
268 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000269 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
270 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattnerf7605322005-08-31 21:09:52 +0000271 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000272 case ISD::SETUGT:
273 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000274 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000275 case ISD::SETULE:
276 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000277 if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits
278 LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
Chris Lattner0bbea952005-08-26 20:25:03 +0000279 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattner85fd97d2005-10-26 18:01:11 +0000280 DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000281 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000282
Chris Lattnereb255f22005-10-25 20:54:57 +0000283 SDOperand Cmp;
Chris Lattnerf7605322005-08-31 21:09:52 +0000284 switch (CC) {
Chris Lattnerbc38dbf2006-01-18 19:42:35 +0000285 default: break; // SETUO etc aren't handled by fsel.
Chris Lattnerf7605322005-08-31 21:09:52 +0000286 case ISD::SETULT:
287 case ISD::SETLT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000288 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
289 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
290 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
291 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000292 case ISD::SETUGE:
293 case ISD::SETGE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000294 Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
295 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
296 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
297 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000298 case ISD::SETUGT:
299 case ISD::SETGT:
Chris Lattnereb255f22005-10-25 20:54:57 +0000300 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
301 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
302 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
303 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
Chris Lattnerf7605322005-08-31 21:09:52 +0000304 case ISD::SETULE:
305 case ISD::SETLE:
Chris Lattnereb255f22005-10-25 20:54:57 +0000306 Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
307 if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits
308 Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
309 return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000310 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000311 break;
312 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000313 case ISD::SHL: {
314 assert(Op.getValueType() == MVT::i64 &&
315 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
316 // The generic code does a fine job expanding shift by a constant.
317 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
318
319 // Otherwise, expand into a bunch of logical ops. Note that these ops
320 // depend on the PPC behavior for oversized shift amounts.
321 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
322 DAG.getConstant(0, MVT::i32));
323 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
324 DAG.getConstant(1, MVT::i32));
325 SDOperand Amt = Op.getOperand(1);
326
327 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
328 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000329 SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt);
330 SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000331 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
332 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
333 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000334 SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000335 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000336 SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000337 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
338 }
339 case ISD::SRL: {
340 assert(Op.getValueType() == MVT::i64 &&
341 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
342 // The generic code does a fine job expanding shift by a constant.
343 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
344
345 // Otherwise, expand into a bunch of logical ops. Note that these ops
346 // depend on the PPC behavior for oversized shift amounts.
347 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
348 DAG.getConstant(0, MVT::i32));
349 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
350 DAG.getConstant(1, MVT::i32));
351 SDOperand Amt = Op.getOperand(1);
352
353 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
354 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000355 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
356 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000357 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
358 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
359 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000360 SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000361 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
Chris Lattner4172b102005-12-06 02:10:38 +0000362 SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt);
Chris Lattnerbc11c342005-08-31 20:23:54 +0000363 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
364 }
365 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000366 assert(Op.getValueType() == MVT::i64 &&
367 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
368 // The generic code does a fine job expanding shift by a constant.
369 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
370
371 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
372 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
373 DAG.getConstant(0, MVT::i32));
374 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
375 DAG.getConstant(1, MVT::i32));
376 SDOperand Amt = Op.getOperand(1);
377
378 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
379 DAG.getConstant(32, MVT::i32), Amt);
Chris Lattner4172b102005-12-06 02:10:38 +0000380 SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt);
381 SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000382 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
383 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
384 DAG.getConstant(-32U, MVT::i32));
Chris Lattner4172b102005-12-06 02:10:38 +0000385 SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5);
386 SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt);
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000387 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
388 Tmp4, Tmp6, ISD::SETLE);
389 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000390 }
Nate Begeman28a6b022005-12-10 02:36:00 +0000391 case ISD::ConstantPool: {
392 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
393 SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32);
394 SDOperand Zero = DAG.getConstant(0, MVT::i32);
395
396 if (PPCGenerateStaticCode) {
397 // Generate non-pic code that has direct accesses to the constant pool.
398 // The address of the global is just (hi(&g)+lo(&g)).
399 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
400 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
401 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
402 }
403
404 // Only lower ConstantPool on Darwin.
405 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
406 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero);
407 if (PICEnabled) {
408 // With PIC, the first instruction is actually "GR+hi(&G)".
409 Hi = DAG.getNode(ISD::ADD, MVT::i32,
410 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
411 }
412
413 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero);
414 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
415 return Lo;
416 }
Chris Lattner860e8862005-11-17 07:30:41 +0000417 case ISD::GlobalAddress: {
Nate Begeman50fb3c42005-12-24 01:00:15 +0000418 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
419 GlobalValue *GV = GSDN->getGlobal();
420 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset());
Chris Lattner860e8862005-11-17 07:30:41 +0000421 SDOperand Zero = DAG.getConstant(0, MVT::i32);
Chris Lattner1d05cb42005-11-17 18:55:48 +0000422
423 if (PPCGenerateStaticCode) {
Nate Begeman28a6b022005-12-10 02:36:00 +0000424 // Generate non-pic code that has direct accesses to globals.
425 // The address of the global is just (hi(&g)+lo(&g)).
Chris Lattner1d05cb42005-11-17 18:55:48 +0000426 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
427 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
428 return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
429 }
Chris Lattner860e8862005-11-17 07:30:41 +0000430
Chris Lattner1d05cb42005-11-17 18:55:48 +0000431 // Only lower GlobalAddress on Darwin.
432 if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break;
Chris Lattnera35ef632006-01-06 01:04:03 +0000433
Chris Lattner860e8862005-11-17 07:30:41 +0000434 SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero);
435 if (PICEnabled) {
436 // With PIC, the first instruction is actually "GR+hi(&G)".
437 Hi = DAG.getNode(ISD::ADD, MVT::i32,
Chris Lattner15666132005-11-17 17:51:38 +0000438 DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi);
Chris Lattner860e8862005-11-17 07:30:41 +0000439 }
440
441 SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero);
442 Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo);
443
Chris Lattner37dd6f12006-01-29 20:49:17 +0000444 if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() &&
445 (!GV->isExternal() || GV->hasNotBeenReadFromBytecode()))
Chris Lattner860e8862005-11-17 07:30:41 +0000446 return Lo;
447
448 // If the global is weak or external, we have to go through the lazy
449 // resolution stub.
450 return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0));
451 }
Nate Begeman44775902006-01-31 08:17:29 +0000452 case ISD::SETCC: {
453 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
455 if (C->getValue() && !C->isAllOnesValue())
456 if (CC == ISD::SETEQ || CC == ISD::SETNE ||
457 CC == ISD::SETLT || CC == ISD::SETGT) {
458 MVT::ValueType VT = Op.getValueType();
459 SDOperand SUB = DAG.getNode(ISD::SUB, Op.getOperand(0).getValueType(),
460 Op.getOperand(0), Op.getOperand(1));
461 return DAG.getSetCC(VT, SUB, DAG.getConstant(0, VT), CC);
462 }
463 break;
464 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000465 case ISD::VASTART: {
466 // vastart just stores the address of the VarArgsFrameIndex slot into the
467 // memory location argument.
468 // FIXME: Replace MVT::i32 with PointerTy
469 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
470 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
471 Op.getOperand(1), Op.getOperand(2));
472 }
Nate Begemanee625572006-01-27 21:09:22 +0000473 case ISD::RET: {
474 SDOperand Copy;
475
476 switch(Op.getNumOperands()) {
477 default:
478 assert(0 && "Do not know how to return this many arguments!");
479 abort();
480 case 1:
481 return SDOperand(); // ret void is legal
482 case 2: {
483 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
484 unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1;
485 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
486 SDOperand());
487 break;
488 }
489 case 3:
490 Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2),
491 SDOperand());
492 Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1));
493 break;
494 }
495 return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
496 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000497 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000498 return SDOperand();
499}
500
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000501std::vector<SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000502PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000503 //
504 // add beautiful description of PPC stack frame format, or at least some docs
505 //
506 MachineFunction &MF = DAG.getMachineFunction();
507 MachineFrameInfo *MFI = MF.getFrameInfo();
508 MachineBasicBlock& BB = MF.front();
Chris Lattner7b738342005-09-13 19:33:40 +0000509 SSARegMap *RegMap = MF.getSSARegMap();
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000510 std::vector<SDOperand> ArgValues;
511
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000512 unsigned ArgOffset = 24;
513 unsigned GPR_remaining = 8;
514 unsigned FPR_remaining = 13;
515 unsigned GPR_idx = 0, FPR_idx = 0;
516 static const unsigned GPR[] = {
517 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
518 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
519 };
520 static const unsigned FPR[] = {
521 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
522 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
523 };
524
525 // Add DAG nodes to load the arguments... On entry to a function on PPC,
526 // the arguments start at offset 24, although they are likely to be passed
527 // in registers.
528 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
529 SDOperand newroot, argt;
530 unsigned ObjSize;
531 bool needsLoad = false;
532 bool ArgLive = !I->use_empty();
533 MVT::ValueType ObjectVT = getValueType(I->getType());
534
535 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000536 default: assert(0 && "Unhandled argument type!");
537 case MVT::i1:
538 case MVT::i8:
539 case MVT::i16:
540 case MVT::i32:
541 ObjSize = 4;
542 if (!ArgLive) break;
543 if (GPR_remaining > 0) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000544 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000545 MF.addLiveIn(GPR[GPR_idx], VReg);
546 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000547 if (ObjectVT != MVT::i32) {
548 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
549 : ISD::AssertZext;
550 argt = DAG.getNode(AssertOp, MVT::i32, argt,
551 DAG.getValueType(ObjectVT));
552 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
553 }
Chris Lattner915fb302005-08-30 00:19:00 +0000554 } else {
555 needsLoad = true;
556 }
557 break;
Chris Lattner80720a92005-11-30 20:40:54 +0000558 case MVT::i64:
559 ObjSize = 8;
Chris Lattner915fb302005-08-30 00:19:00 +0000560 if (!ArgLive) break;
561 if (GPR_remaining > 0) {
562 SDOperand argHi, argLo;
Nate Begeman1d9d7422005-10-18 00:28:58 +0000563 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000564 MF.addLiveIn(GPR[GPR_idx], VReg);
565 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000566 // If we have two or more remaining argument registers, then both halves
567 // of the i64 can be sourced from there. Otherwise, the lower half will
568 // have to come off the stack. This can happen when an i64 is preceded
569 // by 28 bytes of arguments.
570 if (GPR_remaining > 1) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000571 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000572 MF.addLiveIn(GPR[GPR_idx+1], VReg);
573 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
Chris Lattner915fb302005-08-30 00:19:00 +0000574 } else {
575 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
576 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
577 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
578 DAG.getSrcValue(NULL));
579 }
580 // Build the outgoing arg thingy
581 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
582 newroot = argLo;
583 } else {
584 needsLoad = true;
585 }
586 break;
587 case MVT::f32:
588 case MVT::f64:
589 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
Chris Lattner413b9792006-01-11 18:21:25 +0000590 if (!ArgLive) {
591 if (FPR_remaining > 0) {
592 --FPR_remaining;
593 ++FPR_idx;
594 }
595 break;
596 }
Chris Lattner915fb302005-08-30 00:19:00 +0000597 if (FPR_remaining > 0) {
Chris Lattner919c0322005-10-01 01:35:02 +0000598 unsigned VReg;
599 if (ObjectVT == MVT::f32)
Nate Begeman1d9d7422005-10-18 00:28:58 +0000600 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
Chris Lattner919c0322005-10-01 01:35:02 +0000601 else
Nate Begeman1d9d7422005-10-18 00:28:58 +0000602 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000603 MF.addLiveIn(FPR[FPR_idx], VReg);
604 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
Chris Lattner915fb302005-08-30 00:19:00 +0000605 --FPR_remaining;
606 ++FPR_idx;
607 } else {
608 needsLoad = true;
609 }
610 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000611 }
612
613 // We need to load the argument to a virtual register if we determined above
614 // that we ran out of physical registers of the appropriate type
615 if (needsLoad) {
616 unsigned SubregOffset = 0;
617 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
618 if (ObjectVT == MVT::i16) SubregOffset = 2;
619 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
620 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
621 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
622 DAG.getConstant(SubregOffset, MVT::i32));
623 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
624 DAG.getSrcValue(NULL));
625 }
626
627 // Every 4 bytes of argument space consumes one of the GPRs available for
628 // argument passing.
629 if (GPR_remaining > 0) {
630 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
631 GPR_remaining -= delta;
632 GPR_idx += delta;
633 }
634 ArgOffset += ObjSize;
635 if (newroot.Val)
636 DAG.setRoot(newroot.getValue(1));
637
638 ArgValues.push_back(argt);
639 }
640
641 // If the function takes variable number of arguments, make a frame index for
642 // the start of the first vararg value... for expansion of llvm.va_start.
643 if (F.isVarArg()) {
644 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
645 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
646 // If this function is vararg, store any remaining integer argument regs
647 // to their spots on the stack so that they may be loaded by deferencing the
648 // result of va_next.
649 std::vector<SDOperand> MemOps;
650 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
Nate Begeman1d9d7422005-10-18 00:28:58 +0000651 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
Chris Lattner7b738342005-09-13 19:33:40 +0000652 MF.addLiveIn(GPR[GPR_idx], VReg);
653 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000654 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
655 Val, FIN, DAG.getSrcValue(NULL));
656 MemOps.push_back(Store);
657 // Increment the address by four for the next argument to store
658 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
659 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
660 }
Chris Lattner80720a92005-11-30 20:40:54 +0000661 if (!MemOps.empty()) {
662 MemOps.push_back(DAG.getRoot());
663 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
664 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000665 }
666
667 // Finally, inform the code generator which regs we return values in.
668 switch (getValueType(F.getReturnType())) {
669 default: assert(0 && "Unknown type!");
670 case MVT::isVoid: break;
671 case MVT::i1:
672 case MVT::i8:
673 case MVT::i16:
674 case MVT::i32:
675 MF.addLiveOut(PPC::R3);
676 break;
677 case MVT::i64:
678 MF.addLiveOut(PPC::R3);
679 MF.addLiveOut(PPC::R4);
680 break;
681 case MVT::f32:
682 case MVT::f64:
683 MF.addLiveOut(PPC::F1);
684 break;
685 }
686
687 return ArgValues;
688}
689
690std::pair<SDOperand, SDOperand>
Nate Begeman21e463b2005-10-16 05:39:50 +0000691PPCTargetLowering::LowerCallTo(SDOperand Chain,
692 const Type *RetTy, bool isVarArg,
693 unsigned CallingConv, bool isTailCall,
694 SDOperand Callee, ArgListTy &Args,
695 SelectionDAG &DAG) {
Chris Lattner281b55e2006-01-27 23:34:02 +0000696 // args_to_use will accumulate outgoing args for the PPCISD::CALL case in
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000697 // SelectExpr to use to put the arguments in the appropriate registers.
698 std::vector<SDOperand> args_to_use;
699
700 // Count how many bytes are to be pushed on the stack, including the linkage
701 // area, and parameter passing area.
702 unsigned NumBytes = 24;
703
704 if (Args.empty()) {
705 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
706 DAG.getConstant(NumBytes, getPointerTy()));
707 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000708 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000709 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000710 default: assert(0 && "Unknown value type!");
711 case MVT::i1:
712 case MVT::i8:
713 case MVT::i16:
714 case MVT::i32:
715 case MVT::f32:
716 NumBytes += 4;
717 break;
718 case MVT::i64:
719 case MVT::f64:
720 NumBytes += 8;
721 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000722 }
Chris Lattner915fb302005-08-30 00:19:00 +0000723 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000724
Chris Lattner915fb302005-08-30 00:19:00 +0000725 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
726 // plus 32 bytes of argument space in case any called code gets funky on us.
727 // (Required by ABI to support var arg)
728 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000729
730 // Adjust the stack pointer for the new arguments...
731 // These operations are automatically eliminated by the prolog/epilog pass
732 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
733 DAG.getConstant(NumBytes, getPointerTy()));
734
735 // Set up a copy of the stack pointer for use loading and storing any
736 // arguments that may not fit in the registers available for argument
737 // passing.
Chris Lattnera243db82006-01-11 19:55:07 +0000738 SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000739
740 // Figure out which arguments are going to go in registers, and which in
741 // memory. Also, if this is a vararg function, floating point operations
742 // must be stored to our stack, and loaded into integer regs as well, if
743 // any integer regs are available for argument passing.
744 unsigned ArgOffset = 24;
745 unsigned GPR_remaining = 8;
746 unsigned FPR_remaining = 13;
747
748 std::vector<SDOperand> MemOps;
749 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
750 // PtrOff will be used to store the current argument to the stack if a
751 // register cannot be found for it.
752 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
753 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
754 MVT::ValueType ArgVT = getValueType(Args[i].second);
755
756 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000757 default: assert(0 && "Unexpected ValueType for argument!");
758 case MVT::i1:
759 case MVT::i8:
760 case MVT::i16:
761 // Promote the integer to 32 bits. If the input type is signed use a
762 // sign extend, otherwise use a zero extend.
763 if (Args[i].second->isSigned())
764 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
765 else
766 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
767 // FALL THROUGH
768 case MVT::i32:
769 if (GPR_remaining > 0) {
770 args_to_use.push_back(Args[i].first);
771 --GPR_remaining;
772 } else {
773 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
774 Args[i].first, PtrOff,
775 DAG.getSrcValue(NULL)));
776 }
777 ArgOffset += 4;
778 break;
779 case MVT::i64:
780 // If we have one free GPR left, we can place the upper half of the i64
781 // in it, and store the other half to the stack. If we have two or more
782 // free GPRs, then we can pass both halves of the i64 in registers.
783 if (GPR_remaining > 0) {
784 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
785 Args[i].first, DAG.getConstant(1, MVT::i32));
786 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
787 Args[i].first, DAG.getConstant(0, MVT::i32));
788 args_to_use.push_back(Hi);
789 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000790 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000791 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000792 --GPR_remaining;
793 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000794 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
795 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000796 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000797 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000798 }
Chris Lattner915fb302005-08-30 00:19:00 +0000799 } else {
800 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
801 Args[i].first, PtrOff,
802 DAG.getSrcValue(NULL)));
803 }
804 ArgOffset += 8;
805 break;
806 case MVT::f32:
807 case MVT::f64:
808 if (FPR_remaining > 0) {
809 args_to_use.push_back(Args[i].first);
810 --FPR_remaining;
811 if (isVarArg) {
812 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
813 Args[i].first, PtrOff,
814 DAG.getSrcValue(NULL));
815 MemOps.push_back(Store);
816 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000817 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000818 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
819 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000820 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000821 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000822 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000823 }
824 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000825 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
826 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000827 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
828 DAG.getSrcValue(NULL));
Chris Lattner1df74782005-11-17 18:30:17 +0000829 MemOps.push_back(Load.getValue(1));
Chris Lattner915fb302005-08-30 00:19:00 +0000830 args_to_use.push_back(Load);
831 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000832 }
833 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000834 // If we have any FPRs remaining, we may also have GPRs remaining.
835 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
836 // GPRs.
837 if (GPR_remaining > 0) {
838 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
839 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000840 }
Chris Lattner915fb302005-08-30 00:19:00 +0000841 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
842 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
843 --GPR_remaining;
844 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000845 }
Chris Lattner915fb302005-08-30 00:19:00 +0000846 } else {
847 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
848 Args[i].first, PtrOff,
849 DAG.getSrcValue(NULL)));
850 }
851 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
852 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000853 }
854 }
855 if (!MemOps.empty())
856 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
857 }
858
859 std::vector<MVT::ValueType> RetVals;
860 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000861 MVT::ValueType ActualRetTyVT = RetTyVT;
862 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
863 ActualRetTyVT = MVT::i32; // Promote result to i32.
864
Chris Lattnere00ebf02006-01-28 07:33:03 +0000865 if (RetTyVT == MVT::i64) {
866 RetVals.push_back(MVT::i32);
867 RetVals.push_back(MVT::i32);
868 } else if (RetTyVT != MVT::isVoid) {
Chris Lattnerf5059492005-09-02 01:24:55 +0000869 RetVals.push_back(ActualRetTyVT);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000870 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000871 RetVals.push_back(MVT::Other);
872
Chris Lattner2823b3e2005-11-17 05:56:14 +0000873 // If the callee is a GlobalAddress node (quite common, every direct call is)
874 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
875 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
876 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
877
Chris Lattner281b55e2006-01-27 23:34:02 +0000878 std::vector<SDOperand> Ops;
879 Ops.push_back(Chain);
880 Ops.push_back(Callee);
881 Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end());
882 SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000883 Chain = TheCall.getValue(TheCall.Val->getNumValues()-1);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000884 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
885 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000886 SDOperand RetVal = TheCall;
887
888 // If the result is a small value, add a note so that we keep track of the
889 // information about whether it is sign or zero extended.
890 if (RetTyVT != ActualRetTyVT) {
891 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
892 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
893 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
Chris Lattnere00ebf02006-01-28 07:33:03 +0000894 } else if (RetTyVT == MVT::i64) {
895 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1));
Chris Lattnerf5059492005-09-02 01:24:55 +0000896 }
897
898 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000899}
900
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000901MachineBasicBlock *
Nate Begeman21e463b2005-10-16 05:39:50 +0000902PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
903 MachineBasicBlock *BB) {
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000904 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
Chris Lattner919c0322005-10-01 01:35:02 +0000905 MI->getOpcode() == PPC::SELECT_CC_F4 ||
906 MI->getOpcode() == PPC::SELECT_CC_F8) &&
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000907 "Unexpected instr type to insert");
908
909 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
910 // control-flow pattern. The incoming instruction knows the destination vreg
911 // to set, the condition code register to branch on, the true/false values to
912 // select between, and a branch opcode to use.
913 const BasicBlock *LLVM_BB = BB->getBasicBlock();
914 ilist<MachineBasicBlock>::iterator It = BB;
915 ++It;
916
917 // thisMBB:
918 // ...
919 // TrueVal = ...
920 // cmpTY ccX, r1, r2
921 // bCC copy1MBB
922 // fallthrough --> copy0MBB
923 MachineBasicBlock *thisMBB = BB;
924 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
925 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
926 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
927 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
928 MachineFunction *F = BB->getParent();
929 F->getBasicBlockList().insert(It, copy0MBB);
930 F->getBasicBlockList().insert(It, sinkMBB);
931 // Update machine-CFG edges
932 BB->addSuccessor(copy0MBB);
933 BB->addSuccessor(sinkMBB);
934
935 // copy0MBB:
936 // %FalseValue = ...
937 // # fallthrough to sinkMBB
938 BB = copy0MBB;
939
940 // Update machine-CFG edges
941 BB->addSuccessor(sinkMBB);
942
943 // sinkMBB:
944 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
945 // ...
946 BB = sinkMBB;
947 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
948 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
949 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
950
951 delete MI; // The pseudo instruction is gone now.
952 return BB;
953}
954
Chris Lattnerddc787d2006-01-31 19:20:21 +0000955std::vector<unsigned> PPCTargetLowering::
956getRegForInlineAsmConstraint(const std::string &Constraint) const {
957 if (Constraint.size() == 1) {
958 switch (Constraint[0]) { // GCC RS6000 Constraint Letters
959 default: break; // Unknown constriant letter
960 case 'b':
961 return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 ,
962 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
963 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
964 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
965 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
966 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
967 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
968 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
969 0);
970 case 'r':
971 return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 ,
972 PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 ,
973 PPC::R8 , PPC::R9 , PPC::R10, PPC::R11,
974 PPC::R12, PPC::R13, PPC::R14, PPC::R15,
975 PPC::R16, PPC::R17, PPC::R18, PPC::R19,
976 PPC::R20, PPC::R21, PPC::R22, PPC::R23,
977 PPC::R24, PPC::R25, PPC::R26, PPC::R27,
978 PPC::R28, PPC::R29, PPC::R30, PPC::R31,
979 0);
980 case 'f':
981 return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 ,
982 PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 ,
983 PPC::F8 , PPC::F9 , PPC::F10, PPC::F11,
984 PPC::F12, PPC::F13, PPC::F14, PPC::F15,
985 PPC::F16, PPC::F17, PPC::F18, PPC::F19,
986 PPC::F20, PPC::F21, PPC::F22, PPC::F23,
987 PPC::F24, PPC::F25, PPC::F26, PPC::F27,
988 PPC::F28, PPC::F29, PPC::F30, PPC::F31,
989 0);
990 case 'v':
991 return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 ,
992 PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 ,
993 PPC::V8 , PPC::V9 , PPC::V10, PPC::V11,
994 PPC::V12, PPC::V13, PPC::V14, PPC::V15,
995 PPC::V16, PPC::V17, PPC::V18, PPC::V19,
996 PPC::V20, PPC::V21, PPC::V22, PPC::V23,
997 PPC::V24, PPC::V25, PPC::V26, PPC::V27,
998 PPC::V28, PPC::V29, PPC::V30, PPC::V31,
999 0);
1000 case 'y':
1001 return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
1002 PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7,
1003 0);
1004 }
1005 }
1006
1007 // Handle explicit register names.
1008 return TargetLowering::getRegForInlineAsmConstraint(Constraint);
1009}