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/CodeGen/SSARegMap.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Function.h"
25 PPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM)
26 : TargetLowering(TM) {
28 // Fold away setcc operations if possible.
29 setSetCCIsExpensive();
31 // Use _setjmp/_longjmp instead of setjmp/longjmp.
32 setUseUnderscoreSetJmpLongJmp(true);
34 // Set up the register classes.
35 addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
36 addRegisterClass(MVT::f32, PPC32::F4RCRegisterClass);
37 addRegisterClass(MVT::f64, PPC32::F8RCRegisterClass);
39 // PowerPC has no intrinsics for these particular operations
40 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
41 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
42 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
44 // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
45 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
46 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
48 // PowerPC has no SREM/UREM instructions
49 setOperationAction(ISD::SREM, MVT::i32, Expand);
50 setOperationAction(ISD::UREM, MVT::i32, Expand);
52 // We don't support sin/cos/sqrt/fmod
53 setOperationAction(ISD::FSIN , MVT::f64, Expand);
54 setOperationAction(ISD::FCOS , MVT::f64, Expand);
55 setOperationAction(ISD::FREM , MVT::f64, Expand);
56 setOperationAction(ISD::FSIN , MVT::f32, Expand);
57 setOperationAction(ISD::FCOS , MVT::f32, Expand);
58 setOperationAction(ISD::FREM , MVT::f32, Expand);
60 // If we're enabling GP optimizations, use hardware square root
61 if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
62 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
63 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
66 // PowerPC does not have CTPOP or CTTZ
67 setOperationAction(ISD::CTPOP, MVT::i32 , Expand);
68 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
70 // PowerPC does not have Select
71 setOperationAction(ISD::SELECT, MVT::i32, Expand);
72 setOperationAction(ISD::SELECT, MVT::f32, Expand);
73 setOperationAction(ISD::SELECT, MVT::f64, Expand);
75 // PowerPC wants to turn select_cc of FP into fsel when possible.
76 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
77 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
79 // PowerPC wants to expand i64 shifts itself.
80 setOperationAction(ISD::SHL, MVT::i64, Custom);
81 setOperationAction(ISD::SRL, MVT::i64, Custom);
82 setOperationAction(ISD::SRA, MVT::i64, Custom);
84 // PowerPC does not have BRCOND* which requires SetCC
85 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
86 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
88 // PowerPC does not have FP_TO_UINT
89 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
91 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
92 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
94 // PowerPC does not have [U|S]INT_TO_FP
95 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
96 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
98 // PowerPC does not have truncstore for i1.
99 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
101 // 64 bit PowerPC implementations have instructions to facilitate conversion
102 // between i64 and fp.
103 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
104 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
105 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
108 setSetCCResultContents(ZeroOrOneSetCCResult);
110 computeRegisterProperties();
113 /// isFloatingPointZero - Return true if this is 0.0 or -0.0.
114 static bool isFloatingPointZero(SDOperand Op) {
115 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
116 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
117 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
118 // Maybe this has already been legalized into the constant pool?
119 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
120 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
121 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
126 /// LowerOperation - Provide custom lowering hooks for some operations.
128 SDOperand PPC32TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
129 switch (Op.getOpcode()) {
130 default: assert(0 && "Wasn't expecting to be able to lower this!");
131 case ISD::FP_TO_SINT: {
132 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
133 switch (Op.getValueType()) {
134 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
136 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Op.getOperand(0));
139 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Op.getOperand(0));
144 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
145 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
146 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
147 Op, FI, DAG.getSrcValue(0));
148 if (Op.getOpcode() == PPCISD::FCTIDZ) {
149 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
151 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
152 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
156 case ISD::SINT_TO_FP: {
157 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
158 "Unhandled SINT_TO_FP type in custom expander!");
160 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
161 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
162 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
163 Op.getOperand(0), FI, DAG.getSrcValue(0));
164 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
165 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
166 if (MVT::f32 == Op.getValueType())
167 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
170 case ISD::SELECT_CC: {
171 // Turn FP only select_cc's into fsel instructions.
172 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
173 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
176 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
178 // Cannot handle SETEQ/SETNE.
179 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
181 MVT::ValueType ResVT = Op.getValueType();
182 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
183 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
184 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
186 // If the RHS of the comparison is a 0.0, we don't need to do the
187 // subtraction at all.
188 if (isFloatingPointZero(RHS))
190 default: assert(0 && "Invalid FSEL condition"); abort();
193 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
196 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
199 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
202 return DAG.getNode(PPCISD::FSEL, ResVT,
203 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
207 default: assert(0 && "Invalid FSEL condition"); abort();
210 return DAG.getNode(PPCISD::FSEL, ResVT,
211 DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), FV, TV);
214 return DAG.getNode(PPCISD::FSEL, ResVT,
215 DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), TV, FV);
218 return DAG.getNode(PPCISD::FSEL, ResVT,
219 DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), FV, TV);
222 return DAG.getNode(PPCISD::FSEL, ResVT,
223 DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), TV, FV);
228 assert(Op.getValueType() == MVT::i64 &&
229 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
230 // The generic code does a fine job expanding shift by a constant.
231 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
233 // Otherwise, expand into a bunch of logical ops. Note that these ops
234 // depend on the PPC behavior for oversized shift amounts.
235 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
236 DAG.getConstant(0, MVT::i32));
237 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
238 DAG.getConstant(1, MVT::i32));
239 SDOperand Amt = Op.getOperand(1);
241 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
242 DAG.getConstant(32, MVT::i32), Amt);
243 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
244 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
245 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
246 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
247 DAG.getConstant(-32U, MVT::i32));
248 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
249 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
250 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
251 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
254 assert(Op.getValueType() == MVT::i64 &&
255 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
256 // The generic code does a fine job expanding shift by a constant.
257 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
259 // Otherwise, expand into a bunch of logical ops. Note that these ops
260 // depend on the PPC behavior for oversized shift amounts.
261 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
262 DAG.getConstant(0, MVT::i32));
263 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
264 DAG.getConstant(1, MVT::i32));
265 SDOperand Amt = Op.getOperand(1);
267 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
268 DAG.getConstant(32, MVT::i32), Amt);
269 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
270 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
271 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
272 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
273 DAG.getConstant(-32U, MVT::i32));
274 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
275 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
276 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
277 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
280 assert(Op.getValueType() == MVT::i64 &&
281 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
282 // The generic code does a fine job expanding shift by a constant.
283 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
285 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
286 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
287 DAG.getConstant(0, MVT::i32));
288 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
289 DAG.getConstant(1, MVT::i32));
290 SDOperand Amt = Op.getOperand(1);
292 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
293 DAG.getConstant(32, MVT::i32), Amt);
294 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
295 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
296 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
297 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
298 DAG.getConstant(-32U, MVT::i32));
299 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
300 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
301 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
302 Tmp4, Tmp6, ISD::SETLE);
303 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
309 std::vector<SDOperand>
310 PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
312 // add beautiful description of PPC stack frame format, or at least some docs
314 MachineFunction &MF = DAG.getMachineFunction();
315 MachineFrameInfo *MFI = MF.getFrameInfo();
316 MachineBasicBlock& BB = MF.front();
317 SSARegMap *RegMap = MF.getSSARegMap();
318 std::vector<SDOperand> ArgValues;
320 unsigned ArgOffset = 24;
321 unsigned GPR_remaining = 8;
322 unsigned FPR_remaining = 13;
323 unsigned GPR_idx = 0, FPR_idx = 0;
324 static const unsigned GPR[] = {
325 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
326 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
328 static const unsigned FPR[] = {
329 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
330 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
333 // Add DAG nodes to load the arguments... On entry to a function on PPC,
334 // the arguments start at offset 24, although they are likely to be passed
336 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
337 SDOperand newroot, argt;
339 bool needsLoad = false;
340 bool ArgLive = !I->use_empty();
341 MVT::ValueType ObjectVT = getValueType(I->getType());
344 default: assert(0 && "Unhandled argument type!");
351 if (GPR_remaining > 0) {
352 unsigned VReg = RegMap->createVirtualRegister(&PPC32::GPRCRegClass);
353 MF.addLiveIn(GPR[GPR_idx], VReg);
354 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
355 if (ObjectVT != MVT::i32) {
356 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
358 argt = DAG.getNode(AssertOp, MVT::i32, argt,
359 DAG.getValueType(ObjectVT));
360 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
366 case MVT::i64: ObjSize = 8;
368 if (GPR_remaining > 0) {
369 SDOperand argHi, argLo;
370 unsigned VReg = RegMap->createVirtualRegister(&PPC32::GPRCRegClass);
371 MF.addLiveIn(GPR[GPR_idx], VReg);
372 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
373 // If we have two or more remaining argument registers, then both halves
374 // of the i64 can be sourced from there. Otherwise, the lower half will
375 // have to come off the stack. This can happen when an i64 is preceded
376 // by 28 bytes of arguments.
377 if (GPR_remaining > 1) {
378 unsigned VReg = RegMap->createVirtualRegister(&PPC32::GPRCRegClass);
379 MF.addLiveIn(GPR[GPR_idx+1], VReg);
380 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
382 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
383 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
384 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
385 DAG.getSrcValue(NULL));
387 // Build the outgoing arg thingy
388 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
396 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
398 if (FPR_remaining > 0) {
400 if (ObjectVT == MVT::f32)
401 VReg = RegMap->createVirtualRegister(&PPC32::F4RCRegClass);
403 VReg = RegMap->createVirtualRegister(&PPC32::F8RCRegClass);
404 MF.addLiveIn(FPR[FPR_idx], VReg);
405 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
414 // We need to load the argument to a virtual register if we determined above
415 // that we ran out of physical registers of the appropriate type
417 unsigned SubregOffset = 0;
418 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
419 if (ObjectVT == MVT::i16) SubregOffset = 2;
420 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
421 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
422 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
423 DAG.getConstant(SubregOffset, MVT::i32));
424 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
425 DAG.getSrcValue(NULL));
428 // Every 4 bytes of argument space consumes one of the GPRs available for
430 if (GPR_remaining > 0) {
431 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
432 GPR_remaining -= delta;
435 ArgOffset += ObjSize;
437 DAG.setRoot(newroot.getValue(1));
439 ArgValues.push_back(argt);
442 // If the function takes variable number of arguments, make a frame index for
443 // the start of the first vararg value... for expansion of llvm.va_start.
445 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
446 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
447 // If this function is vararg, store any remaining integer argument regs
448 // to their spots on the stack so that they may be loaded by deferencing the
449 // result of va_next.
450 std::vector<SDOperand> MemOps;
451 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
452 unsigned VReg = RegMap->createVirtualRegister(&PPC32::GPRCRegClass);
453 MF.addLiveIn(GPR[GPR_idx], VReg);
454 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
455 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
456 Val, FIN, DAG.getSrcValue(NULL));
457 MemOps.push_back(Store);
458 // Increment the address by four for the next argument to store
459 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
460 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
462 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
465 // Finally, inform the code generator which regs we return values in.
466 switch (getValueType(F.getReturnType())) {
467 default: assert(0 && "Unknown type!");
468 case MVT::isVoid: break;
473 MF.addLiveOut(PPC::R3);
476 MF.addLiveOut(PPC::R3);
477 MF.addLiveOut(PPC::R4);
481 MF.addLiveOut(PPC::F1);
488 std::pair<SDOperand, SDOperand>
489 PPC32TargetLowering::LowerCallTo(SDOperand Chain,
490 const Type *RetTy, bool isVarArg,
491 unsigned CallingConv, bool isTailCall,
492 SDOperand Callee, ArgListTy &Args,
494 // args_to_use will accumulate outgoing args for the ISD::CALL case in
495 // SelectExpr to use to put the arguments in the appropriate registers.
496 std::vector<SDOperand> args_to_use;
498 // Count how many bytes are to be pushed on the stack, including the linkage
499 // area, and parameter passing area.
500 unsigned NumBytes = 24;
503 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
504 DAG.getConstant(NumBytes, getPointerTy()));
506 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
507 switch (getValueType(Args[i].second)) {
508 default: assert(0 && "Unknown value type!");
523 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
524 // plus 32 bytes of argument space in case any called code gets funky on us.
525 // (Required by ABI to support var arg)
526 if (NumBytes < 56) NumBytes = 56;
528 // Adjust the stack pointer for the new arguments...
529 // These operations are automatically eliminated by the prolog/epilog pass
530 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
531 DAG.getConstant(NumBytes, getPointerTy()));
533 // Set up a copy of the stack pointer for use loading and storing any
534 // arguments that may not fit in the registers available for argument
536 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
539 // Figure out which arguments are going to go in registers, and which in
540 // memory. Also, if this is a vararg function, floating point operations
541 // must be stored to our stack, and loaded into integer regs as well, if
542 // any integer regs are available for argument passing.
543 unsigned ArgOffset = 24;
544 unsigned GPR_remaining = 8;
545 unsigned FPR_remaining = 13;
547 std::vector<SDOperand> MemOps;
548 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
549 // PtrOff will be used to store the current argument to the stack if a
550 // register cannot be found for it.
551 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
552 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
553 MVT::ValueType ArgVT = getValueType(Args[i].second);
556 default: assert(0 && "Unexpected ValueType for argument!");
560 // Promote the integer to 32 bits. If the input type is signed use a
561 // sign extend, otherwise use a zero extend.
562 if (Args[i].second->isSigned())
563 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
565 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
568 if (GPR_remaining > 0) {
569 args_to_use.push_back(Args[i].first);
572 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
573 Args[i].first, PtrOff,
574 DAG.getSrcValue(NULL)));
579 // If we have one free GPR left, we can place the upper half of the i64
580 // in it, and store the other half to the stack. If we have two or more
581 // free GPRs, then we can pass both halves of the i64 in registers.
582 if (GPR_remaining > 0) {
583 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
584 Args[i].first, DAG.getConstant(1, MVT::i32));
585 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
586 Args[i].first, DAG.getConstant(0, MVT::i32));
587 args_to_use.push_back(Hi);
589 if (GPR_remaining > 0) {
590 args_to_use.push_back(Lo);
593 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
594 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
595 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
596 Lo, PtrOff, DAG.getSrcValue(NULL)));
599 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
600 Args[i].first, PtrOff,
601 DAG.getSrcValue(NULL)));
607 if (FPR_remaining > 0) {
608 args_to_use.push_back(Args[i].first);
611 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
612 Args[i].first, PtrOff,
613 DAG.getSrcValue(NULL));
614 MemOps.push_back(Store);
615 // Float varargs are always shadowed in available integer registers
616 if (GPR_remaining > 0) {
617 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
618 DAG.getSrcValue(NULL));
619 MemOps.push_back(Load);
620 args_to_use.push_back(Load);
623 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
624 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
625 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
626 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
627 DAG.getSrcValue(NULL));
628 MemOps.push_back(Load);
629 args_to_use.push_back(Load);
633 // If we have any FPRs remaining, we may also have GPRs remaining.
634 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
636 if (GPR_remaining > 0) {
637 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
640 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
641 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
646 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
647 Args[i].first, PtrOff,
648 DAG.getSrcValue(NULL)));
650 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
655 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
658 std::vector<MVT::ValueType> RetVals;
659 MVT::ValueType RetTyVT = getValueType(RetTy);
660 MVT::ValueType ActualRetTyVT = RetTyVT;
661 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
662 ActualRetTyVT = MVT::i32; // Promote result to i32.
664 if (RetTyVT != MVT::isVoid)
665 RetVals.push_back(ActualRetTyVT);
666 RetVals.push_back(MVT::Other);
668 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
669 Chain, Callee, args_to_use), 0);
670 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
671 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
672 DAG.getConstant(NumBytes, getPointerTy()));
673 SDOperand RetVal = TheCall;
675 // If the result is a small value, add a note so that we keep track of the
676 // information about whether it is sign or zero extended.
677 if (RetTyVT != ActualRetTyVT) {
678 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
679 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
680 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
683 return std::make_pair(RetVal, Chain);
686 SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
687 Value *VAListV, SelectionDAG &DAG) {
688 // vastart just stores the address of the VarArgsFrameIndex slot into the
689 // memory location argument.
690 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
691 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
692 DAG.getSrcValue(VAListV));
695 std::pair<SDOperand,SDOperand>
696 PPC32TargetLowering::LowerVAArg(SDOperand Chain,
697 SDOperand VAListP, Value *VAListV,
698 const Type *ArgTy, SelectionDAG &DAG) {
699 MVT::ValueType ArgVT = getValueType(ArgTy);
702 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
703 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
705 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
708 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
709 "Other types should have been promoted for varargs!");
712 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
713 DAG.getConstant(Amt, VAList.getValueType()));
714 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
715 VAList, VAListP, DAG.getSrcValue(VAListV));
716 return std::make_pair(Result, Chain);
720 std::pair<SDOperand, SDOperand> PPC32TargetLowering::
721 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
723 assert(0 && "LowerFrameReturnAddress unimplemented");
728 PPC32TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
729 MachineBasicBlock *BB) {
730 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
731 MI->getOpcode() == PPC::SELECT_CC_F4 ||
732 MI->getOpcode() == PPC::SELECT_CC_F8) &&
733 "Unexpected instr type to insert");
735 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
736 // control-flow pattern. The incoming instruction knows the destination vreg
737 // to set, the condition code register to branch on, the true/false values to
738 // select between, and a branch opcode to use.
739 const BasicBlock *LLVM_BB = BB->getBasicBlock();
740 ilist<MachineBasicBlock>::iterator It = BB;
748 // fallthrough --> copy0MBB
749 MachineBasicBlock *thisMBB = BB;
750 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
751 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
752 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
753 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
754 MachineFunction *F = BB->getParent();
755 F->getBasicBlockList().insert(It, copy0MBB);
756 F->getBasicBlockList().insert(It, sinkMBB);
757 // Update machine-CFG edges
758 BB->addSuccessor(copy0MBB);
759 BB->addSuccessor(sinkMBB);
763 // # fallthrough to sinkMBB
766 // Update machine-CFG edges
767 BB->addSuccessor(sinkMBB);
770 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
773 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
774 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
775 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
777 delete MI; // The pseudo instruction is gone now.