blob: 0edcfe2e4971689de2c4c269bd96fa2c30f00c1b [file] [log] [blame]
Chris Lattner7c5a3d32005-08-16 17:14:42 +00001//===-- PPC32ISelLowering.cpp - PPC32 DAG Lowering Implementation ---------===//
2//
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//
10// This file implements the PPC32ISelLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPC32ISelLowering.h"
15#include "PPC32TargetMachine.h"
16#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 Lattner0b1e4e52005-08-26 17:36:52 +000020#include "llvm/Constants.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000021#include "llvm/Function.h"
Chris Lattner7c5a3d32005-08-16 17:14:42 +000022using namespace llvm;
23
24PPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM)
25 : TargetLowering(TM) {
26
27 // Fold away setcc operations if possible.
28 setSetCCIsExpensive();
29
30 // Set up the register classes.
31 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
32 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
33 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
34
35 // PowerPC has no intrinsics for these particular operations
36 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
37 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
38 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
39
40 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
41 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
42 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
43
44 // PowerPC has no SREM/UREM instructions
45 setOperationAction(ISD::SREM, MVT::i32, Expand);
46 setOperationAction(ISD::UREM, MVT::i32, Expand);
47
48 // We don't support sin/cos/sqrt/fmod
49 setOperationAction(ISD::FSIN , MVT::f64, Expand);
50 setOperationAction(ISD::FCOS , MVT::f64, Expand);
51 setOperationAction(ISD::SREM , MVT::f64, Expand);
52 setOperationAction(ISD::FSIN , MVT::f32, Expand);
53 setOperationAction(ISD::FCOS , MVT::f32, Expand);
54 setOperationAction(ISD::SREM , MVT::f32, Expand);
55
56 // If we're enabling GP optimizations, use hardware square root
Chris Lattner1e9de3e2005-09-02 18:33:05 +000057 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +000058 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
59 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
60 }
61
62 // PowerPC does not have CTPOP or CTTZ
63 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
64 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
65
66 // PowerPC does not have Select
67 setOperationAction(ISD::SELECT, MVT::i32, Expand);
68 setOperationAction(ISD::SELECT, MVT::f32, Expand);
69 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +000070
Chris Lattner0b1e4e52005-08-26 17:36:52 +000071 // PowerPC wants to turn select_cc of FP into fsel when possible.
72 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
73 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000074
Chris Lattnerbc11c342005-08-31 20:23:54 +000075 // PowerPC wants to expand i64 shifts itself.
76 setOperationAction(ISD::SHL, MVT::i64, Custom);
77 setOperationAction(ISD::SRL, MVT::i64, Custom);
Chris Lattnereb9b62e2005-08-31 19:09:57 +000078 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begeman7cbd5252005-08-16 19:49:35 +000079
80 // PowerPC does not have BRCOND* which requires SetCC
81 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
82 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000083
84 // PowerPC does not have FP_TO_UINT
85 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
86
Chris Lattnerf7605322005-08-31 21:09:52 +000087 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
88 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
Nate Begemanc09eeec2005-09-06 22:03:27 +000089
Jim Laskeyad23c9d2005-08-17 00:40:22 +000090 // PowerPC does not have [U|S]INT_TO_FP
91 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
92 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
93
Chris Lattnere6ec9f22005-09-10 00:21:06 +000094 // PowerPC does not have truncstore for i1.
95 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
96
Nate Begemanc09eeec2005-09-06 22:03:27 +000097 // 64 bit PowerPC implementations have instructions to facilitate conversion
98 // between i64 and fp.
99 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
100 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
101 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
102 }
103
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000104 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000105
106 computeRegisterProperties();
107}
108
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000109/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
110static bool isFloatingPointZero(SDOperand Op) {
111 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
112 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
113 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
114 // Maybe this has already been legalized into the constant pool?
115 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
116 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
117 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
118 }
119 return false;
120}
121
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000122/// LowerOperation - Provide custom lowering hooks for some operations.
123///
124SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
125 switch (Op.getOpcode()) {
126 default: assert(0 && "Wasn't expecting to be able to lower this!");
Chris Lattnerf7605322005-08-31 21:09:52 +0000127 case ISD::FP_TO_SINT: {
Nate Begemanc09eeec2005-09-06 22:03:27 +0000128 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
129 switch (Op.getValueType()) {
130 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
131 case MVT::i32:
132 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Op.getOperand(0));
133 break;
134 case MVT::i64:
135 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Op.getOperand(0));
136 break;
137 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000138
139 int FrameIdx =
140 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
141 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
142 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
143 Op, FI, DAG.getSrcValue(0));
Nate Begemanc09eeec2005-09-06 22:03:27 +0000144 if (Op.getOpcode() == PPCISD::FCTIDZ) {
145 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
146 } else {
147 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
148 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
149 }
150 return Op;
151 }
152 case ISD::SINT_TO_FP: {
153 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
154 "Unhandled SINT_TO_FP type in custom expander!");
155 int FrameIdx =
156 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
157 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
158 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
159 Op.getOperand(0), FI, DAG.getSrcValue(0));
160 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
161 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
162 if (MVT::f32 == Op.getValueType())
163 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
164 return FP;
Chris Lattnerf7605322005-08-31 21:09:52 +0000165 }
166 case ISD::SELECT_CC: {
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000167 // Turn FP only select_cc's into fsel instructions.
Chris Lattnerf7605322005-08-31 21:09:52 +0000168 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
169 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
170 break;
171
172 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
173
174 // Cannot handle SETEQ/SETNE.
175 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
176
177 MVT::ValueType ResVT = Op.getValueType();
178 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
179 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
180 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000181
Chris Lattnerf7605322005-08-31 21:09:52 +0000182 // If the RHS of the comparison is a 0.0, we don't need to do the
183 // subtraction at all.
184 if (isFloatingPointZero(RHS))
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000185 switch (CC) {
186 default: assert(0 && "Invalid FSEL condition"); abort();
187 case ISD::SETULT:
188 case ISD::SETLT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000189 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000190 case ISD::SETUGE:
191 case ISD::SETGE:
Chris Lattnerf7605322005-08-31 21:09:52 +0000192 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000193 case ISD::SETUGT:
194 case ISD::SETGT:
Chris Lattnerf7605322005-08-31 21:09:52 +0000195 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000196 case ISD::SETULE:
197 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000198 return DAG.getNode(PPCISD::FSEL, ResVT,
Chris Lattnerf7605322005-08-31 21:09:52 +0000199 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000200 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000201
202 switch (CC) {
203 default: assert(0 && "Invalid FSEL condition"); abort();
204 case ISD::SETULT:
205 case ISD::SETLT:
206 return DAG.getNode(PPCISD::FSEL, ResVT,
207 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV);
208 case ISD::SETUGE:
209 case ISD::SETGE:
210 return DAG.getNode(PPCISD::FSEL, ResVT,
211 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV);
212 case ISD::SETUGT:
213 case ISD::SETGT:
214 return DAG.getNode(PPCISD::FSEL, ResVT,
215 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV);
216 case ISD::SETULE:
217 case ISD::SETLE:
218 return DAG.getNode(PPCISD::FSEL, ResVT,
219 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000220 }
Chris Lattnerf7605322005-08-31 21:09:52 +0000221 break;
222 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000223 case ISD::SHL: {
224 assert(Op.getValueType() == MVT::i64 &&
225 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
226 // The generic code does a fine job expanding shift by a constant.
227 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
228
229 // Otherwise, expand into a bunch of logical ops. Note that these ops
230 // depend on the PPC behavior for oversized shift amounts.
231 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
232 DAG.getConstant(0, MVT::i32));
233 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
234 DAG.getConstant(1, MVT::i32));
235 SDOperand Amt = Op.getOperand(1);
236
237 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
238 DAG.getConstant(32, MVT::i32), Amt);
239 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
240 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
241 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
242 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
243 DAG.getConstant(-32U, MVT::i32));
244 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
245 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
246 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
247 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
248 }
249 case ISD::SRL: {
250 assert(Op.getValueType() == MVT::i64 &&
251 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
252 // The generic code does a fine job expanding shift by a constant.
253 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
254
255 // Otherwise, expand into a bunch of logical ops. Note that these ops
256 // depend on the PPC behavior for oversized shift amounts.
257 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
258 DAG.getConstant(0, MVT::i32));
259 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
260 DAG.getConstant(1, MVT::i32));
261 SDOperand Amt = Op.getOperand(1);
262
263 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
264 DAG.getConstant(32, MVT::i32), Amt);
265 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
266 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
267 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
268 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
269 DAG.getConstant(-32U, MVT::i32));
270 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
271 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
272 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
273 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
274 }
275 case ISD::SRA: {
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000276 assert(Op.getValueType() == MVT::i64 &&
277 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
278 // The generic code does a fine job expanding shift by a constant.
279 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
280
281 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
282 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
283 DAG.getConstant(0, MVT::i32));
284 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
285 DAG.getConstant(1, MVT::i32));
286 SDOperand Amt = Op.getOperand(1);
287
288 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
289 DAG.getConstant(32, MVT::i32), Amt);
290 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
291 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
292 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
293 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
294 DAG.getConstant(-32U, MVT::i32));
295 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
296 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
297 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
298 Tmp4, Tmp6, ISD::SETLE);
299 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000300 }
Chris Lattnerbc11c342005-08-31 20:23:54 +0000301 }
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000302 return SDOperand();
303}
304
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000305std::vector<SDOperand>
306PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
307 //
308 // add beautiful description of PPC stack frame format, or at least some docs
309 //
310 MachineFunction &MF = DAG.getMachineFunction();
311 MachineFrameInfo *MFI = MF.getFrameInfo();
312 MachineBasicBlock& BB = MF.front();
313 std::vector<SDOperand> ArgValues;
314
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000315 unsigned ArgOffset = 24;
316 unsigned GPR_remaining = 8;
317 unsigned FPR_remaining = 13;
318 unsigned GPR_idx = 0, FPR_idx = 0;
319 static const unsigned GPR[] = {
320 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
321 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
322 };
323 static const unsigned FPR[] = {
324 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
325 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
326 };
327
328 // Add DAG nodes to load the arguments... On entry to a function on PPC,
329 // the arguments start at offset 24, although they are likely to be passed
330 // in registers.
331 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
332 SDOperand newroot, argt;
333 unsigned ObjSize;
334 bool needsLoad = false;
335 bool ArgLive = !I->use_empty();
336 MVT::ValueType ObjectVT = getValueType(I->getType());
337
338 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000339 default: assert(0 && "Unhandled argument type!");
340 case MVT::i1:
341 case MVT::i8:
342 case MVT::i16:
343 case MVT::i32:
344 ObjSize = 4;
345 if (!ArgLive) break;
346 if (GPR_remaining > 0) {
347 MF.addLiveIn(GPR[GPR_idx]);
348 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
349 GPR[GPR_idx], MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000350 if (ObjectVT != MVT::i32) {
351 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
352 : ISD::AssertZext;
353 argt = DAG.getNode(AssertOp, MVT::i32, argt,
354 DAG.getValueType(ObjectVT));
355 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
356 }
Chris Lattner915fb302005-08-30 00:19:00 +0000357 } else {
358 needsLoad = true;
359 }
360 break;
361 case MVT::i64: ObjSize = 8;
362 if (!ArgLive) break;
363 if (GPR_remaining > 0) {
364 SDOperand argHi, argLo;
365 MF.addLiveIn(GPR[GPR_idx]);
366 argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
367 // If we have two or more remaining argument registers, then both halves
368 // of the i64 can be sourced from there. Otherwise, the lower half will
369 // have to come off the stack. This can happen when an i64 is preceded
370 // by 28 bytes of arguments.
371 if (GPR_remaining > 1) {
372 MF.addLiveIn(GPR[GPR_idx+1]);
373 argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32);
374 } else {
375 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
376 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
377 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
378 DAG.getSrcValue(NULL));
379 }
380 // Build the outgoing arg thingy
381 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
382 newroot = argLo;
383 } else {
384 needsLoad = true;
385 }
386 break;
387 case MVT::f32:
388 case MVT::f64:
389 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
390 if (!ArgLive) break;
391 if (FPR_remaining > 0) {
392 MF.addLiveIn(FPR[FPR_idx]);
393 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
394 FPR[FPR_idx], ObjectVT);
395 --FPR_remaining;
396 ++FPR_idx;
397 } else {
398 needsLoad = true;
399 }
400 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000401 }
402
403 // We need to load the argument to a virtual register if we determined above
404 // that we ran out of physical registers of the appropriate type
405 if (needsLoad) {
406 unsigned SubregOffset = 0;
407 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
408 if (ObjectVT == MVT::i16) SubregOffset = 2;
409 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
410 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
411 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
412 DAG.getConstant(SubregOffset, MVT::i32));
413 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
414 DAG.getSrcValue(NULL));
415 }
416
417 // Every 4 bytes of argument space consumes one of the GPRs available for
418 // argument passing.
419 if (GPR_remaining > 0) {
420 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
421 GPR_remaining -= delta;
422 GPR_idx += delta;
423 }
424 ArgOffset += ObjSize;
425 if (newroot.Val)
426 DAG.setRoot(newroot.getValue(1));
427
428 ArgValues.push_back(argt);
429 }
430
431 // If the function takes variable number of arguments, make a frame index for
432 // the start of the first vararg value... for expansion of llvm.va_start.
433 if (F.isVarArg()) {
434 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
435 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
436 // If this function is vararg, store any remaining integer argument regs
437 // to their spots on the stack so that they may be loaded by deferencing the
438 // result of va_next.
439 std::vector<SDOperand> MemOps;
440 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
441 MF.addLiveIn(GPR[GPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000442 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000443 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
444 Val, FIN, DAG.getSrcValue(NULL));
445 MemOps.push_back(Store);
446 // Increment the address by four for the next argument to store
447 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
448 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
449 }
450 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
451 }
452
453 // Finally, inform the code generator which regs we return values in.
454 switch (getValueType(F.getReturnType())) {
455 default: assert(0 && "Unknown type!");
456 case MVT::isVoid: break;
457 case MVT::i1:
458 case MVT::i8:
459 case MVT::i16:
460 case MVT::i32:
461 MF.addLiveOut(PPC::R3);
462 break;
463 case MVT::i64:
464 MF.addLiveOut(PPC::R3);
465 MF.addLiveOut(PPC::R4);
466 break;
467 case MVT::f32:
468 case MVT::f64:
469 MF.addLiveOut(PPC::F1);
470 break;
471 }
472
473 return ArgValues;
474}
475
476std::pair<SDOperand, SDOperand>
477PPC32TargetLowering::LowerCallTo(SDOperand Chain,
478 const Type *RetTy, bool isVarArg,
479 unsigned CallingConv, bool isTailCall,
480 SDOperand Callee, ArgListTy &Args,
481 SelectionDAG &DAG) {
482 // args_to_use will accumulate outgoing args for the ISD::CALL case in
483 // SelectExpr to use to put the arguments in the appropriate registers.
484 std::vector<SDOperand> args_to_use;
485
486 // Count how many bytes are to be pushed on the stack, including the linkage
487 // area, and parameter passing area.
488 unsigned NumBytes = 24;
489
490 if (Args.empty()) {
491 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
492 DAG.getConstant(NumBytes, getPointerTy()));
493 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000494 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000495 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000496 default: assert(0 && "Unknown value type!");
497 case MVT::i1:
498 case MVT::i8:
499 case MVT::i16:
500 case MVT::i32:
501 case MVT::f32:
502 NumBytes += 4;
503 break;
504 case MVT::i64:
505 case MVT::f64:
506 NumBytes += 8;
507 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000508 }
Chris Lattner915fb302005-08-30 00:19:00 +0000509 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000510
Chris Lattner915fb302005-08-30 00:19:00 +0000511 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
512 // plus 32 bytes of argument space in case any called code gets funky on us.
513 // (Required by ABI to support var arg)
514 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000515
516 // Adjust the stack pointer for the new arguments...
517 // These operations are automatically eliminated by the prolog/epilog pass
518 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
519 DAG.getConstant(NumBytes, getPointerTy()));
520
521 // Set up a copy of the stack pointer for use loading and storing any
522 // arguments that may not fit in the registers available for argument
523 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000524 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
525 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000526
527 // Figure out which arguments are going to go in registers, and which in
528 // memory. Also, if this is a vararg function, floating point operations
529 // must be stored to our stack, and loaded into integer regs as well, if
530 // any integer regs are available for argument passing.
531 unsigned ArgOffset = 24;
532 unsigned GPR_remaining = 8;
533 unsigned FPR_remaining = 13;
534
535 std::vector<SDOperand> MemOps;
536 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
537 // PtrOff will be used to store the current argument to the stack if a
538 // register cannot be found for it.
539 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
540 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
541 MVT::ValueType ArgVT = getValueType(Args[i].second);
542
543 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000544 default: assert(0 && "Unexpected ValueType for argument!");
545 case MVT::i1:
546 case MVT::i8:
547 case MVT::i16:
548 // Promote the integer to 32 bits. If the input type is signed use a
549 // sign extend, otherwise use a zero extend.
550 if (Args[i].second->isSigned())
551 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
552 else
553 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
554 // FALL THROUGH
555 case MVT::i32:
556 if (GPR_remaining > 0) {
557 args_to_use.push_back(Args[i].first);
558 --GPR_remaining;
559 } else {
560 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
561 Args[i].first, PtrOff,
562 DAG.getSrcValue(NULL)));
563 }
564 ArgOffset += 4;
565 break;
566 case MVT::i64:
567 // If we have one free GPR left, we can place the upper half of the i64
568 // in it, and store the other half to the stack. If we have two or more
569 // free GPRs, then we can pass both halves of the i64 in registers.
570 if (GPR_remaining > 0) {
571 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
572 Args[i].first, DAG.getConstant(1, MVT::i32));
573 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
574 Args[i].first, DAG.getConstant(0, MVT::i32));
575 args_to_use.push_back(Hi);
576 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000577 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000578 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000579 --GPR_remaining;
580 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000581 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
582 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000583 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000584 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000585 }
Chris Lattner915fb302005-08-30 00:19:00 +0000586 } else {
587 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
588 Args[i].first, PtrOff,
589 DAG.getSrcValue(NULL)));
590 }
591 ArgOffset += 8;
592 break;
593 case MVT::f32:
594 case MVT::f64:
595 if (FPR_remaining > 0) {
596 args_to_use.push_back(Args[i].first);
597 --FPR_remaining;
598 if (isVarArg) {
599 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
600 Args[i].first, PtrOff,
601 DAG.getSrcValue(NULL));
602 MemOps.push_back(Store);
603 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000604 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000605 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
606 DAG.getSrcValue(NULL));
607 MemOps.push_back(Load);
608 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000609 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000610 }
611 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000612 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
613 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000614 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
615 DAG.getSrcValue(NULL));
616 MemOps.push_back(Load);
617 args_to_use.push_back(Load);
618 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000619 }
620 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000621 // If we have any FPRs remaining, we may also have GPRs remaining.
622 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
623 // GPRs.
624 if (GPR_remaining > 0) {
625 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
626 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000627 }
Chris Lattner915fb302005-08-30 00:19:00 +0000628 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
629 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
630 --GPR_remaining;
631 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000632 }
Chris Lattner915fb302005-08-30 00:19:00 +0000633 } else {
634 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
635 Args[i].first, PtrOff,
636 DAG.getSrcValue(NULL)));
637 }
638 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
639 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000640 }
641 }
642 if (!MemOps.empty())
643 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
644 }
645
646 std::vector<MVT::ValueType> RetVals;
647 MVT::ValueType RetTyVT = getValueType(RetTy);
Chris Lattnerf5059492005-09-02 01:24:55 +0000648 MVT::ValueType ActualRetTyVT = RetTyVT;
649 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
650 ActualRetTyVT = MVT::i32; // Promote result to i32.
651
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000652 if (RetTyVT != MVT::isVoid)
Chris Lattnerf5059492005-09-02 01:24:55 +0000653 RetVals.push_back(ActualRetTyVT);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000654 RetVals.push_back(MVT::Other);
655
656 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
657 Chain, Callee, args_to_use), 0);
658 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
659 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
660 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5059492005-09-02 01:24:55 +0000661 SDOperand RetVal = TheCall;
662
663 // If the result is a small value, add a note so that we keep track of the
664 // information about whether it is sign or zero extended.
665 if (RetTyVT != ActualRetTyVT) {
666 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
667 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
668 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
669 }
670
671 return std::make_pair(RetVal, Chain);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000672}
673
674SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
675 Value *VAListV, SelectionDAG &DAG) {
676 // vastart just stores the address of the VarArgsFrameIndex slot into the
677 // memory location argument.
678 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
679 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
680 DAG.getSrcValue(VAListV));
681}
682
683std::pair<SDOperand,SDOperand>
684PPC32TargetLowering::LowerVAArg(SDOperand Chain,
685 SDOperand VAListP, Value *VAListV,
686 const Type *ArgTy, SelectionDAG &DAG) {
687 MVT::ValueType ArgVT = getValueType(ArgTy);
688
689 SDOperand VAList =
690 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
691 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
692 unsigned Amt;
693 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
694 Amt = 4;
695 else {
696 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
697 "Other types should have been promoted for varargs!");
698 Amt = 8;
699 }
700 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
701 DAG.getConstant(Amt, VAList.getValueType()));
702 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
703 VAList, VAListP, DAG.getSrcValue(VAListV));
704 return std::make_pair(Result, Chain);
705}
706
707
708std::pair<SDOperand, SDOperand> PPC32TargetLowering::
709LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
710 SelectionDAG &DAG) {
711 assert(0 && "LowerFrameReturnAddress unimplemented");
712 abort();
713}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000714
715MachineBasicBlock *
716PPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
717 MachineBasicBlock *BB) {
718 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
719 MI->getOpcode() == PPC::SELECT_CC_FP) &&
720 "Unexpected instr type to insert");
721
722 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
723 // control-flow pattern. The incoming instruction knows the destination vreg
724 // to set, the condition code register to branch on, the true/false values to
725 // select between, and a branch opcode to use.
726 const BasicBlock *LLVM_BB = BB->getBasicBlock();
727 ilist<MachineBasicBlock>::iterator It = BB;
728 ++It;
729
730 // thisMBB:
731 // ...
732 // TrueVal = ...
733 // cmpTY ccX, r1, r2
734 // bCC copy1MBB
735 // fallthrough --> copy0MBB
736 MachineBasicBlock *thisMBB = BB;
737 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
738 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
739 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
740 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
741 MachineFunction *F = BB->getParent();
742 F->getBasicBlockList().insert(It, copy0MBB);
743 F->getBasicBlockList().insert(It, sinkMBB);
744 // Update machine-CFG edges
745 BB->addSuccessor(copy0MBB);
746 BB->addSuccessor(sinkMBB);
747
748 // copy0MBB:
749 // %FalseValue = ...
750 // # fallthrough to sinkMBB
751 BB = copy0MBB;
752
753 // Update machine-CFG edges
754 BB->addSuccessor(sinkMBB);
755
756 // sinkMBB:
757 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
758 // ...
759 BB = sinkMBB;
760 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
761 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
762 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
763
764 delete MI; // The pseudo instruction is gone now.
765 return BB;
766}
767