blob: c6511e8f5cd42e3cbb6faf97563f52c8b11ff0d4 [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
57 if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) {
58 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
75 // PowerPC wants to expand SRA_PARTS into SELECT_CC and stuff.
76 setOperationAction(ISD::SRA, MVT::i64, Custom);
Nate Begeman7cbd5252005-08-16 19:49:35 +000077
78 // PowerPC does not have BRCOND* which requires SetCC
79 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
80 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000081
82 // PowerPC does not have FP_TO_UINT
83 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
84
Jim Laskeyad23c9d2005-08-17 00:40:22 +000085 // PowerPC does not have [U|S]INT_TO_FP
86 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
87 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
88
Chris Lattner7c5a3d32005-08-16 17:14:42 +000089 setSetCCResultContents(ZeroOrOneSetCCResult);
Chris Lattner7c5a3d32005-08-16 17:14:42 +000090
91 computeRegisterProperties();
92}
93
Chris Lattner0b1e4e52005-08-26 17:36:52 +000094/// isFloatingPointZero - Return true if this is 0.0 or -0.0.
95static bool isFloatingPointZero(SDOperand Op) {
96 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
97 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
98 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
99 // Maybe this has already been legalized into the constant pool?
100 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
101 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
102 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
103 }
104 return false;
105}
106
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000107/// LowerOperation - Provide custom lowering hooks for some operations.
108///
109SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
110 switch (Op.getOpcode()) {
111 default: assert(0 && "Wasn't expecting to be able to lower this!");
112 case ISD::SELECT_CC:
113 // Turn FP only select_cc's into fsel instructions.
114 if (MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
115 MVT::isFloatingPoint(Op.getOperand(2).getValueType())) {
116 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Chris Lattner6de08f42005-08-30 00:45:18 +0000117
118 // Cannot handle SETEQ/SETNE.
119 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
120
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000121 MVT::ValueType ResVT = Op.getValueType();
122 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
123 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
124 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
125
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000126 // If the RHS of the comparison is a 0.0, we don't need to do the
127 // subtraction at all.
128 if (isFloatingPointZero(RHS))
129 switch (CC) {
130 default: assert(0 && "Invalid FSEL condition"); abort();
131 case ISD::SETULT:
132 case ISD::SETLT:
133 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
134 case ISD::SETUGE:
135 case ISD::SETGE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000136 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000137 case ISD::SETUGT:
138 case ISD::SETGT:
139 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
140 case ISD::SETULE:
141 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000142 return DAG.getNode(PPCISD::FSEL, ResVT,
143 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
Chris Lattner0b1e4e52005-08-26 17:36:52 +0000144 }
145
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000146 switch (CC) {
147 default: assert(0 && "Invalid FSEL condition"); abort();
148 case ISD::SETULT:
149 case ISD::SETLT:
Chris Lattner0bbea952005-08-26 20:25:03 +0000150 return DAG.getNode(PPCISD::FSEL, ResVT,
151 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000152 case ISD::SETUGE:
153 case ISD::SETGE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000154 return DAG.getNode(PPCISD::FSEL, ResVT,
155 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000156 case ISD::SETUGT:
157 case ISD::SETGT:
Chris Lattner0bbea952005-08-26 20:25:03 +0000158 return DAG.getNode(PPCISD::FSEL, ResVT,
159 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000160 case ISD::SETULE:
161 case ISD::SETLE:
Chris Lattner0bbea952005-08-26 20:25:03 +0000162 return DAG.getNode(PPCISD::FSEL, ResVT,
163 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000164 }
165 }
166 break;
Chris Lattnereb9b62e2005-08-31 19:09:57 +0000167 case ISD::SRA:
168 assert(Op.getValueType() == MVT::i64 &&
169 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
170 // The generic code does a fine job expanding shift by a constant.
171 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
172
173 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
174 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
175 DAG.getConstant(0, MVT::i32));
176 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
177 DAG.getConstant(1, MVT::i32));
178 SDOperand Amt = Op.getOperand(1);
179
180 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
181 DAG.getConstant(32, MVT::i32), Amt);
182 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
183 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
184 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
185 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
186 DAG.getConstant(-32U, MVT::i32));
187 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
188 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
189 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
190 Tmp4, Tmp6, ISD::SETLE);
191 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
Chris Lattnere4bc9ea2005-08-26 00:52:45 +0000192 }
193 return SDOperand();
194}
195
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000196std::vector<SDOperand>
197PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
198 //
199 // add beautiful description of PPC stack frame format, or at least some docs
200 //
201 MachineFunction &MF = DAG.getMachineFunction();
202 MachineFrameInfo *MFI = MF.getFrameInfo();
203 MachineBasicBlock& BB = MF.front();
204 std::vector<SDOperand> ArgValues;
205
206 // Due to the rather complicated nature of the PowerPC ABI, rather than a
207 // fixed size array of physical args, for the sake of simplicity let the STL
208 // handle tracking them for us.
209 std::vector<unsigned> argVR, argPR, argOp;
210 unsigned ArgOffset = 24;
211 unsigned GPR_remaining = 8;
212 unsigned FPR_remaining = 13;
213 unsigned GPR_idx = 0, FPR_idx = 0;
214 static const unsigned GPR[] = {
215 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
216 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
217 };
218 static const unsigned FPR[] = {
219 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
220 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
221 };
222
223 // Add DAG nodes to load the arguments... On entry to a function on PPC,
224 // the arguments start at offset 24, although they are likely to be passed
225 // in registers.
226 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
227 SDOperand newroot, argt;
228 unsigned ObjSize;
229 bool needsLoad = false;
230 bool ArgLive = !I->use_empty();
231 MVT::ValueType ObjectVT = getValueType(I->getType());
232
233 switch (ObjectVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000234 default: assert(0 && "Unhandled argument type!");
235 case MVT::i1:
236 case MVT::i8:
237 case MVT::i16:
238 case MVT::i32:
239 ObjSize = 4;
240 if (!ArgLive) break;
241 if (GPR_remaining > 0) {
242 MF.addLiveIn(GPR[GPR_idx]);
243 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
244 GPR[GPR_idx], MVT::i32);
Nate Begeman49296f12005-08-31 01:58:39 +0000245 if (ObjectVT != MVT::i32) {
246 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
247 : ISD::AssertZext;
248 argt = DAG.getNode(AssertOp, MVT::i32, argt,
249 DAG.getValueType(ObjectVT));
250 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
251 }
Chris Lattner915fb302005-08-30 00:19:00 +0000252 } else {
253 needsLoad = true;
254 }
255 break;
256 case MVT::i64: ObjSize = 8;
257 if (!ArgLive) break;
258 if (GPR_remaining > 0) {
259 SDOperand argHi, argLo;
260 MF.addLiveIn(GPR[GPR_idx]);
261 argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
262 // If we have two or more remaining argument registers, then both halves
263 // of the i64 can be sourced from there. Otherwise, the lower half will
264 // have to come off the stack. This can happen when an i64 is preceded
265 // by 28 bytes of arguments.
266 if (GPR_remaining > 1) {
267 MF.addLiveIn(GPR[GPR_idx+1]);
268 argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32);
269 } else {
270 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
271 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
272 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
273 DAG.getSrcValue(NULL));
274 }
275 // Build the outgoing arg thingy
276 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
277 newroot = argLo;
278 } else {
279 needsLoad = true;
280 }
281 break;
282 case MVT::f32:
283 case MVT::f64:
284 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
285 if (!ArgLive) break;
286 if (FPR_remaining > 0) {
287 MF.addLiveIn(FPR[FPR_idx]);
288 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
289 FPR[FPR_idx], ObjectVT);
290 --FPR_remaining;
291 ++FPR_idx;
292 } else {
293 needsLoad = true;
294 }
295 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000296 }
297
298 // We need to load the argument to a virtual register if we determined above
299 // that we ran out of physical registers of the appropriate type
300 if (needsLoad) {
301 unsigned SubregOffset = 0;
302 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
303 if (ObjectVT == MVT::i16) SubregOffset = 2;
304 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
305 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
306 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
307 DAG.getConstant(SubregOffset, MVT::i32));
308 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
309 DAG.getSrcValue(NULL));
310 }
311
312 // Every 4 bytes of argument space consumes one of the GPRs available for
313 // argument passing.
314 if (GPR_remaining > 0) {
315 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
316 GPR_remaining -= delta;
317 GPR_idx += delta;
318 }
319 ArgOffset += ObjSize;
320 if (newroot.Val)
321 DAG.setRoot(newroot.getValue(1));
322
323 ArgValues.push_back(argt);
324 }
325
326 // If the function takes variable number of arguments, make a frame index for
327 // the start of the first vararg value... for expansion of llvm.va_start.
328 if (F.isVarArg()) {
329 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
330 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
331 // If this function is vararg, store any remaining integer argument regs
332 // to their spots on the stack so that they may be loaded by deferencing the
333 // result of va_next.
334 std::vector<SDOperand> MemOps;
335 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
336 MF.addLiveIn(GPR[GPR_idx]);
Chris Lattnera8cd0152005-08-16 21:58:15 +0000337 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000338 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
339 Val, FIN, DAG.getSrcValue(NULL));
340 MemOps.push_back(Store);
341 // Increment the address by four for the next argument to store
342 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
343 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
344 }
345 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
346 }
347
348 // Finally, inform the code generator which regs we return values in.
349 switch (getValueType(F.getReturnType())) {
350 default: assert(0 && "Unknown type!");
351 case MVT::isVoid: break;
352 case MVT::i1:
353 case MVT::i8:
354 case MVT::i16:
355 case MVT::i32:
356 MF.addLiveOut(PPC::R3);
357 break;
358 case MVT::i64:
359 MF.addLiveOut(PPC::R3);
360 MF.addLiveOut(PPC::R4);
361 break;
362 case MVT::f32:
363 case MVT::f64:
364 MF.addLiveOut(PPC::F1);
365 break;
366 }
367
368 return ArgValues;
369}
370
371std::pair<SDOperand, SDOperand>
372PPC32TargetLowering::LowerCallTo(SDOperand Chain,
373 const Type *RetTy, bool isVarArg,
374 unsigned CallingConv, bool isTailCall,
375 SDOperand Callee, ArgListTy &Args,
376 SelectionDAG &DAG) {
377 // args_to_use will accumulate outgoing args for the ISD::CALL case in
378 // SelectExpr to use to put the arguments in the appropriate registers.
379 std::vector<SDOperand> args_to_use;
380
381 // Count how many bytes are to be pushed on the stack, including the linkage
382 // area, and parameter passing area.
383 unsigned NumBytes = 24;
384
385 if (Args.empty()) {
386 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
387 DAG.getConstant(NumBytes, getPointerTy()));
388 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000389 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000390 switch (getValueType(Args[i].second)) {
Chris Lattner915fb302005-08-30 00:19:00 +0000391 default: assert(0 && "Unknown value type!");
392 case MVT::i1:
393 case MVT::i8:
394 case MVT::i16:
395 case MVT::i32:
396 case MVT::f32:
397 NumBytes += 4;
398 break;
399 case MVT::i64:
400 case MVT::f64:
401 NumBytes += 8;
402 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000403 }
Chris Lattner915fb302005-08-30 00:19:00 +0000404 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000405
Chris Lattner915fb302005-08-30 00:19:00 +0000406 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
407 // plus 32 bytes of argument space in case any called code gets funky on us.
408 // (Required by ABI to support var arg)
409 if (NumBytes < 56) NumBytes = 56;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000410
411 // Adjust the stack pointer for the new arguments...
412 // These operations are automatically eliminated by the prolog/epilog pass
413 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
414 DAG.getConstant(NumBytes, getPointerTy()));
415
416 // Set up a copy of the stack pointer for use loading and storing any
417 // arguments that may not fit in the registers available for argument
418 // passing.
Chris Lattnera8cd0152005-08-16 21:58:15 +0000419 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
420 PPC::R1, MVT::i32);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000421
422 // Figure out which arguments are going to go in registers, and which in
423 // memory. Also, if this is a vararg function, floating point operations
424 // must be stored to our stack, and loaded into integer regs as well, if
425 // any integer regs are available for argument passing.
426 unsigned ArgOffset = 24;
427 unsigned GPR_remaining = 8;
428 unsigned FPR_remaining = 13;
429
430 std::vector<SDOperand> MemOps;
431 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
432 // PtrOff will be used to store the current argument to the stack if a
433 // register cannot be found for it.
434 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
435 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
436 MVT::ValueType ArgVT = getValueType(Args[i].second);
437
438 switch (ArgVT) {
Chris Lattner915fb302005-08-30 00:19:00 +0000439 default: assert(0 && "Unexpected ValueType for argument!");
440 case MVT::i1:
441 case MVT::i8:
442 case MVT::i16:
443 // Promote the integer to 32 bits. If the input type is signed use a
444 // sign extend, otherwise use a zero extend.
445 if (Args[i].second->isSigned())
446 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
447 else
448 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
449 // FALL THROUGH
450 case MVT::i32:
451 if (GPR_remaining > 0) {
452 args_to_use.push_back(Args[i].first);
453 --GPR_remaining;
454 } else {
455 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
456 Args[i].first, PtrOff,
457 DAG.getSrcValue(NULL)));
458 }
459 ArgOffset += 4;
460 break;
461 case MVT::i64:
462 // If we have one free GPR left, we can place the upper half of the i64
463 // in it, and store the other half to the stack. If we have two or more
464 // free GPRs, then we can pass both halves of the i64 in registers.
465 if (GPR_remaining > 0) {
466 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
467 Args[i].first, DAG.getConstant(1, MVT::i32));
468 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
469 Args[i].first, DAG.getConstant(0, MVT::i32));
470 args_to_use.push_back(Hi);
471 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000472 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000473 args_to_use.push_back(Lo);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000474 --GPR_remaining;
475 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000476 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
477 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000478 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
Chris Lattner915fb302005-08-30 00:19:00 +0000479 Lo, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000480 }
Chris Lattner915fb302005-08-30 00:19:00 +0000481 } else {
482 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
483 Args[i].first, PtrOff,
484 DAG.getSrcValue(NULL)));
485 }
486 ArgOffset += 8;
487 break;
488 case MVT::f32:
489 case MVT::f64:
490 if (FPR_remaining > 0) {
491 args_to_use.push_back(Args[i].first);
492 --FPR_remaining;
493 if (isVarArg) {
494 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
495 Args[i].first, PtrOff,
496 DAG.getSrcValue(NULL));
497 MemOps.push_back(Store);
498 // Float varargs are always shadowed in available integer registers
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000499 if (GPR_remaining > 0) {
Chris Lattner915fb302005-08-30 00:19:00 +0000500 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
501 DAG.getSrcValue(NULL));
502 MemOps.push_back(Load);
503 args_to_use.push_back(Load);
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000504 --GPR_remaining;
Chris Lattner915fb302005-08-30 00:19:00 +0000505 }
506 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000507 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
508 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
Chris Lattner915fb302005-08-30 00:19:00 +0000509 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
510 DAG.getSrcValue(NULL));
511 MemOps.push_back(Load);
512 args_to_use.push_back(Load);
513 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000514 }
515 } else {
Chris Lattner915fb302005-08-30 00:19:00 +0000516 // If we have any FPRs remaining, we may also have GPRs remaining.
517 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
518 // GPRs.
519 if (GPR_remaining > 0) {
520 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
521 --GPR_remaining;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000522 }
Chris Lattner915fb302005-08-30 00:19:00 +0000523 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
524 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
525 --GPR_remaining;
526 }
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000527 }
Chris Lattner915fb302005-08-30 00:19:00 +0000528 } else {
529 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
530 Args[i].first, PtrOff,
531 DAG.getSrcValue(NULL)));
532 }
533 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
534 break;
Chris Lattner7c5a3d32005-08-16 17:14:42 +0000535 }
536 }
537 if (!MemOps.empty())
538 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
539 }
540
541 std::vector<MVT::ValueType> RetVals;
542 MVT::ValueType RetTyVT = getValueType(RetTy);
543 if (RetTyVT != MVT::isVoid)
544 RetVals.push_back(RetTyVT);
545 RetVals.push_back(MVT::Other);
546
547 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
548 Chain, Callee, args_to_use), 0);
549 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
550 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
551 DAG.getConstant(NumBytes, getPointerTy()));
552 return std::make_pair(TheCall, Chain);
553}
554
555SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
556 Value *VAListV, SelectionDAG &DAG) {
557 // vastart just stores the address of the VarArgsFrameIndex slot into the
558 // memory location argument.
559 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
560 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
561 DAG.getSrcValue(VAListV));
562}
563
564std::pair<SDOperand,SDOperand>
565PPC32TargetLowering::LowerVAArg(SDOperand Chain,
566 SDOperand VAListP, Value *VAListV,
567 const Type *ArgTy, SelectionDAG &DAG) {
568 MVT::ValueType ArgVT = getValueType(ArgTy);
569
570 SDOperand VAList =
571 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
572 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
573 unsigned Amt;
574 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
575 Amt = 4;
576 else {
577 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
578 "Other types should have been promoted for varargs!");
579 Amt = 8;
580 }
581 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
582 DAG.getConstant(Amt, VAList.getValueType()));
583 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
584 VAList, VAListP, DAG.getSrcValue(VAListV));
585 return std::make_pair(Result, Chain);
586}
587
588
589std::pair<SDOperand, SDOperand> PPC32TargetLowering::
590LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
591 SelectionDAG &DAG) {
592 assert(0 && "LowerFrameReturnAddress unimplemented");
593 abort();
594}
Chris Lattner8a2d3ca2005-08-26 21:23:58 +0000595
596MachineBasicBlock *
597PPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
598 MachineBasicBlock *BB) {
599 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
600 MI->getOpcode() == PPC::SELECT_CC_FP) &&
601 "Unexpected instr type to insert");
602
603 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
604 // control-flow pattern. The incoming instruction knows the destination vreg
605 // to set, the condition code register to branch on, the true/false values to
606 // select between, and a branch opcode to use.
607 const BasicBlock *LLVM_BB = BB->getBasicBlock();
608 ilist<MachineBasicBlock>::iterator It = BB;
609 ++It;
610
611 // thisMBB:
612 // ...
613 // TrueVal = ...
614 // cmpTY ccX, r1, r2
615 // bCC copy1MBB
616 // fallthrough --> copy0MBB
617 MachineBasicBlock *thisMBB = BB;
618 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
619 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
620 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
621 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
622 MachineFunction *F = BB->getParent();
623 F->getBasicBlockList().insert(It, copy0MBB);
624 F->getBasicBlockList().insert(It, sinkMBB);
625 // Update machine-CFG edges
626 BB->addSuccessor(copy0MBB);
627 BB->addSuccessor(sinkMBB);
628
629 // copy0MBB:
630 // %FalseValue = ...
631 // # fallthrough to sinkMBB
632 BB = copy0MBB;
633
634 // Update machine-CFG edges
635 BB->addSuccessor(sinkMBB);
636
637 // sinkMBB:
638 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
639 // ...
640 BB = sinkMBB;
641 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
642 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
643 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
644
645 delete MI; // The pseudo instruction is gone now.
646 return BB;
647}
648