1 //===-- PPC32ISelLowering.cpp - PPC32 DAG Lowering Implementation ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PPC32ISelLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "PPC32ISelLowering.h"
15 #include "PPC32TargetMachine.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Function.h"
24 PPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM)
25 : TargetLowering(TM) {
27 // Fold away setcc operations if possible.
28 setSetCCIsExpensive();
30 // Set up the register classes.
31 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
32 addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass);
33 addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass);
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);
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);
44 // PowerPC has no SREM/UREM instructions
45 setOperationAction(ISD::SREM, MVT::i32, Expand);
46 setOperationAction(ISD::UREM, MVT::i32, Expand);
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);
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);
62 // PowerPC does not have CTPOP or CTTZ
63 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
64 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
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);
71 // 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);
75 // PowerPC wants to expand i64 shifts itself.
76 setOperationAction(ISD::SHL, MVT::i64, Custom);
77 setOperationAction(ISD::SRL, MVT::i64, Custom);
78 setOperationAction(ISD::SRA, MVT::i64, Custom);
80 // PowerPC does not have BRCOND* which requires SetCC
81 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
82 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
84 // PowerPC does not have FP_TO_UINT
85 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
87 // PowerPC does not have [U|S]INT_TO_FP
88 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
89 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
91 setSetCCResultContents(ZeroOrOneSetCCResult);
93 computeRegisterProperties();
96 /// isFloatingPointZero - Return true if this is 0.0 or -0.0.
97 static bool isFloatingPointZero(SDOperand Op) {
98 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
99 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
100 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
101 // Maybe this has already been legalized into the constant pool?
102 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
103 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
104 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
109 /// LowerOperation - Provide custom lowering hooks for some operations.
111 SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
112 switch (Op.getOpcode()) {
113 default: assert(0 && "Wasn't expecting to be able to lower this!");
115 // Turn FP only select_cc's into fsel instructions.
116 if (MVT::isFloatingPoint(Op.getOperand(0).getValueType()) &&
117 MVT::isFloatingPoint(Op.getOperand(2).getValueType())) {
118 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
120 // Cannot handle SETEQ/SETNE.
121 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
123 MVT::ValueType ResVT = Op.getValueType();
124 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
125 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
126 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
128 // If the RHS of the comparison is a 0.0, we don't need to do the
129 // subtraction at all.
130 if (isFloatingPointZero(RHS))
132 default: assert(0 && "Invalid FSEL condition"); abort();
135 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
138 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
141 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
144 return DAG.getNode(PPCISD::FSEL, ResVT,
145 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
149 default: assert(0 && "Invalid FSEL condition"); abort();
152 return DAG.getNode(PPCISD::FSEL, ResVT,
153 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), FV, TV);
156 return DAG.getNode(PPCISD::FSEL, ResVT,
157 DAG.getNode(ISD::SUB, CmpVT, LHS, RHS), TV, FV);
160 return DAG.getNode(PPCISD::FSEL, ResVT,
161 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), FV, TV);
164 return DAG.getNode(PPCISD::FSEL, ResVT,
165 DAG.getNode(ISD::SUB, CmpVT, RHS, LHS), TV, FV);
170 assert(Op.getValueType() == MVT::i64 &&
171 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
172 // The generic code does a fine job expanding shift by a constant.
173 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
175 // Otherwise, expand into a bunch of logical ops. Note that these ops
176 // depend on the PPC behavior for oversized shift amounts.
177 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
178 DAG.getConstant(0, MVT::i32));
179 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
180 DAG.getConstant(1, MVT::i32));
181 SDOperand Amt = Op.getOperand(1);
183 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
184 DAG.getConstant(32, MVT::i32), Amt);
185 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
186 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
187 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
188 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
189 DAG.getConstant(-32U, MVT::i32));
190 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
191 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
192 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
193 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
196 assert(Op.getValueType() == MVT::i64 &&
197 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
198 // The generic code does a fine job expanding shift by a constant.
199 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
201 // Otherwise, expand into a bunch of logical ops. Note that these ops
202 // depend on the PPC behavior for oversized shift amounts.
203 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
204 DAG.getConstant(0, MVT::i32));
205 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
206 DAG.getConstant(1, MVT::i32));
207 SDOperand Amt = Op.getOperand(1);
209 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
210 DAG.getConstant(32, MVT::i32), Amt);
211 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
212 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
213 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
214 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
215 DAG.getConstant(-32U, MVT::i32));
216 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
217 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
218 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
219 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
222 assert(Op.getValueType() == MVT::i64 &&
223 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
224 // The generic code does a fine job expanding shift by a constant.
225 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
227 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
228 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
229 DAG.getConstant(0, MVT::i32));
230 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
231 DAG.getConstant(1, MVT::i32));
232 SDOperand Amt = Op.getOperand(1);
234 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
235 DAG.getConstant(32, MVT::i32), Amt);
236 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
237 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
238 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
239 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
240 DAG.getConstant(-32U, MVT::i32));
241 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
242 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
243 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
244 Tmp4, Tmp6, ISD::SETLE);
245 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
251 std::vector<SDOperand>
252 PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
254 // add beautiful description of PPC stack frame format, or at least some docs
256 MachineFunction &MF = DAG.getMachineFunction();
257 MachineFrameInfo *MFI = MF.getFrameInfo();
258 MachineBasicBlock& BB = MF.front();
259 std::vector<SDOperand> ArgValues;
261 // Due to the rather complicated nature of the PowerPC ABI, rather than a
262 // fixed size array of physical args, for the sake of simplicity let the STL
263 // handle tracking them for us.
264 std::vector<unsigned> argVR, argPR, argOp;
265 unsigned ArgOffset = 24;
266 unsigned GPR_remaining = 8;
267 unsigned FPR_remaining = 13;
268 unsigned GPR_idx = 0, FPR_idx = 0;
269 static const unsigned GPR[] = {
270 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
271 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
273 static const unsigned FPR[] = {
274 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
275 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
278 // Add DAG nodes to load the arguments... On entry to a function on PPC,
279 // the arguments start at offset 24, although they are likely to be passed
281 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
282 SDOperand newroot, argt;
284 bool needsLoad = false;
285 bool ArgLive = !I->use_empty();
286 MVT::ValueType ObjectVT = getValueType(I->getType());
289 default: assert(0 && "Unhandled argument type!");
296 if (GPR_remaining > 0) {
297 MF.addLiveIn(GPR[GPR_idx]);
298 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
299 GPR[GPR_idx], MVT::i32);
300 if (ObjectVT != MVT::i32) {
301 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
303 argt = DAG.getNode(AssertOp, MVT::i32, argt,
304 DAG.getValueType(ObjectVT));
305 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
311 case MVT::i64: ObjSize = 8;
313 if (GPR_remaining > 0) {
314 SDOperand argHi, argLo;
315 MF.addLiveIn(GPR[GPR_idx]);
316 argHi = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
317 // If we have two or more remaining argument registers, then both halves
318 // of the i64 can be sourced from there. Otherwise, the lower half will
319 // have to come off the stack. This can happen when an i64 is preceded
320 // by 28 bytes of arguments.
321 if (GPR_remaining > 1) {
322 MF.addLiveIn(GPR[GPR_idx+1]);
323 argLo = DAG.getCopyFromReg(argHi, GPR[GPR_idx+1], MVT::i32);
325 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
326 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
327 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
328 DAG.getSrcValue(NULL));
330 // Build the outgoing arg thingy
331 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
339 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
341 if (FPR_remaining > 0) {
342 MF.addLiveIn(FPR[FPR_idx]);
343 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(),
344 FPR[FPR_idx], ObjectVT);
353 // We need to load the argument to a virtual register if we determined above
354 // that we ran out of physical registers of the appropriate type
356 unsigned SubregOffset = 0;
357 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
358 if (ObjectVT == MVT::i16) SubregOffset = 2;
359 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
360 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
361 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
362 DAG.getConstant(SubregOffset, MVT::i32));
363 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
364 DAG.getSrcValue(NULL));
367 // Every 4 bytes of argument space consumes one of the GPRs available for
369 if (GPR_remaining > 0) {
370 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
371 GPR_remaining -= delta;
374 ArgOffset += ObjSize;
376 DAG.setRoot(newroot.getValue(1));
378 ArgValues.push_back(argt);
381 // If the function takes variable number of arguments, make a frame index for
382 // the start of the first vararg value... for expansion of llvm.va_start.
384 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
385 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
386 // If this function is vararg, store any remaining integer argument regs
387 // to their spots on the stack so that they may be loaded by deferencing the
388 // result of va_next.
389 std::vector<SDOperand> MemOps;
390 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
391 MF.addLiveIn(GPR[GPR_idx]);
392 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), GPR[GPR_idx], MVT::i32);
393 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
394 Val, FIN, DAG.getSrcValue(NULL));
395 MemOps.push_back(Store);
396 // Increment the address by four for the next argument to store
397 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
398 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
400 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
403 // Finally, inform the code generator which regs we return values in.
404 switch (getValueType(F.getReturnType())) {
405 default: assert(0 && "Unknown type!");
406 case MVT::isVoid: break;
411 MF.addLiveOut(PPC::R3);
414 MF.addLiveOut(PPC::R3);
415 MF.addLiveOut(PPC::R4);
419 MF.addLiveOut(PPC::F1);
426 std::pair<SDOperand, SDOperand>
427 PPC32TargetLowering::LowerCallTo(SDOperand Chain,
428 const Type *RetTy, bool isVarArg,
429 unsigned CallingConv, bool isTailCall,
430 SDOperand Callee, ArgListTy &Args,
432 // args_to_use will accumulate outgoing args for the ISD::CALL case in
433 // SelectExpr to use to put the arguments in the appropriate registers.
434 std::vector<SDOperand> args_to_use;
436 // Count how many bytes are to be pushed on the stack, including the linkage
437 // area, and parameter passing area.
438 unsigned NumBytes = 24;
441 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
442 DAG.getConstant(NumBytes, getPointerTy()));
444 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
445 switch (getValueType(Args[i].second)) {
446 default: assert(0 && "Unknown value type!");
461 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
462 // plus 32 bytes of argument space in case any called code gets funky on us.
463 // (Required by ABI to support var arg)
464 if (NumBytes < 56) NumBytes = 56;
466 // Adjust the stack pointer for the new arguments...
467 // These operations are automatically eliminated by the prolog/epilog pass
468 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
469 DAG.getConstant(NumBytes, getPointerTy()));
471 // Set up a copy of the stack pointer for use loading and storing any
472 // arguments that may not fit in the registers available for argument
474 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
477 // Figure out which arguments are going to go in registers, and which in
478 // memory. Also, if this is a vararg function, floating point operations
479 // must be stored to our stack, and loaded into integer regs as well, if
480 // any integer regs are available for argument passing.
481 unsigned ArgOffset = 24;
482 unsigned GPR_remaining = 8;
483 unsigned FPR_remaining = 13;
485 std::vector<SDOperand> MemOps;
486 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
487 // PtrOff will be used to store the current argument to the stack if a
488 // register cannot be found for it.
489 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
490 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
491 MVT::ValueType ArgVT = getValueType(Args[i].second);
494 default: assert(0 && "Unexpected ValueType for argument!");
498 // Promote the integer to 32 bits. If the input type is signed use a
499 // sign extend, otherwise use a zero extend.
500 if (Args[i].second->isSigned())
501 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
503 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
506 if (GPR_remaining > 0) {
507 args_to_use.push_back(Args[i].first);
510 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
511 Args[i].first, PtrOff,
512 DAG.getSrcValue(NULL)));
517 // If we have one free GPR left, we can place the upper half of the i64
518 // in it, and store the other half to the stack. If we have two or more
519 // free GPRs, then we can pass both halves of the i64 in registers.
520 if (GPR_remaining > 0) {
521 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
522 Args[i].first, DAG.getConstant(1, MVT::i32));
523 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
524 Args[i].first, DAG.getConstant(0, MVT::i32));
525 args_to_use.push_back(Hi);
527 if (GPR_remaining > 0) {
528 args_to_use.push_back(Lo);
531 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
532 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
533 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
534 Lo, PtrOff, DAG.getSrcValue(NULL)));
537 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
538 Args[i].first, PtrOff,
539 DAG.getSrcValue(NULL)));
545 if (FPR_remaining > 0) {
546 args_to_use.push_back(Args[i].first);
549 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
550 Args[i].first, PtrOff,
551 DAG.getSrcValue(NULL));
552 MemOps.push_back(Store);
553 // Float varargs are always shadowed in available integer registers
554 if (GPR_remaining > 0) {
555 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
556 DAG.getSrcValue(NULL));
557 MemOps.push_back(Load);
558 args_to_use.push_back(Load);
561 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
562 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
563 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
564 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
565 DAG.getSrcValue(NULL));
566 MemOps.push_back(Load);
567 args_to_use.push_back(Load);
571 // If we have any FPRs remaining, we may also have GPRs remaining.
572 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
574 if (GPR_remaining > 0) {
575 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
578 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
579 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
584 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
585 Args[i].first, PtrOff,
586 DAG.getSrcValue(NULL)));
588 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
593 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
596 std::vector<MVT::ValueType> RetVals;
597 MVT::ValueType RetTyVT = getValueType(RetTy);
598 if (RetTyVT != MVT::isVoid)
599 RetVals.push_back(RetTyVT);
600 RetVals.push_back(MVT::Other);
602 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
603 Chain, Callee, args_to_use), 0);
604 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
605 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
606 DAG.getConstant(NumBytes, getPointerTy()));
607 return std::make_pair(TheCall, Chain);
610 SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
611 Value *VAListV, SelectionDAG &DAG) {
612 // vastart just stores the address of the VarArgsFrameIndex slot into the
613 // memory location argument.
614 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
615 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
616 DAG.getSrcValue(VAListV));
619 std::pair<SDOperand,SDOperand>
620 PPC32TargetLowering::LowerVAArg(SDOperand Chain,
621 SDOperand VAListP, Value *VAListV,
622 const Type *ArgTy, SelectionDAG &DAG) {
623 MVT::ValueType ArgVT = getValueType(ArgTy);
626 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
627 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
629 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
632 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
633 "Other types should have been promoted for varargs!");
636 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
637 DAG.getConstant(Amt, VAList.getValueType()));
638 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
639 VAList, VAListP, DAG.getSrcValue(VAListV));
640 return std::make_pair(Result, Chain);
644 std::pair<SDOperand, SDOperand> PPC32TargetLowering::
645 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
647 assert(0 && "LowerFrameReturnAddress unimplemented");
652 PPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
653 MachineBasicBlock *BB) {
654 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
655 MI->getOpcode() == PPC::SELECT_CC_FP) &&
656 "Unexpected instr type to insert");
658 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
659 // control-flow pattern. The incoming instruction knows the destination vreg
660 // to set, the condition code register to branch on, the true/false values to
661 // select between, and a branch opcode to use.
662 const BasicBlock *LLVM_BB = BB->getBasicBlock();
663 ilist<MachineBasicBlock>::iterator It = BB;
671 // fallthrough --> copy0MBB
672 MachineBasicBlock *thisMBB = BB;
673 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
674 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
675 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
676 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
677 MachineFunction *F = BB->getParent();
678 F->getBasicBlockList().insert(It, copy0MBB);
679 F->getBasicBlockList().insert(It, sinkMBB);
680 // Update machine-CFG edges
681 BB->addSuccessor(copy0MBB);
682 BB->addSuccessor(sinkMBB);
686 // # fallthrough to sinkMBB
689 // Update machine-CFG edges
690 BB->addSuccessor(sinkMBB);
693 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
696 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
697 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
698 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
700 delete MI; // The pseudo instruction is gone now.