1 //===-- PPCISelLowering.cpp - PPC 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 PPCISelLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "PPCISelLowering.h"
15 #include "PPCTargetMachine.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 PPCTargetLowering::PPCTargetLowering(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, PPC::GPRCRegisterClass);
36 addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
37 addRegisterClass(MVT::f64, PPC::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 does not have BRCOND* which requires SetCC
80 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
81 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
83 // PowerPC does not have FP_TO_UINT
84 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
86 // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
87 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
89 // PowerPC does not have [U|S]INT_TO_FP
90 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
91 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
93 // PowerPC does not have truncstore for i1.
94 setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
96 if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
97 // 64 bit PowerPC implementations can support i64 types directly
98 // FIXME: enable this once it works.
99 //addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
100 // They also have instructions for converting between i64 and fp.
101 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
102 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
103 // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
104 setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
105 // 32 bit PowerPC wants to expand i64 shifts itself.
106 // FIXME: remove these once we natively handle i64 shifts.
107 setOperationAction(ISD::SHL, MVT::i64, Custom);
108 setOperationAction(ISD::SRL, MVT::i64, Custom);
109 setOperationAction(ISD::SRA, MVT::i64, Custom);
111 // 32 bit PowerPC wants to expand i64 shifts itself.
112 setOperationAction(ISD::SHL, MVT::i64, Custom);
113 setOperationAction(ISD::SRL, MVT::i64, Custom);
114 setOperationAction(ISD::SRA, MVT::i64, Custom);
117 setSetCCResultContents(ZeroOrOneSetCCResult);
119 computeRegisterProperties();
122 /// isFloatingPointZero - Return true if this is 0.0 or -0.0.
123 static bool isFloatingPointZero(SDOperand Op) {
124 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
125 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
126 else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) {
127 // Maybe this has already been legalized into the constant pool?
128 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
129 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get()))
130 return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0);
135 /// LowerOperation - Provide custom lowering hooks for some operations.
137 SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
138 switch (Op.getOpcode()) {
139 default: assert(0 && "Wasn't expecting to be able to lower this!");
140 case ISD::FP_TO_SINT: {
141 assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType()));
142 SDOperand Src = Op.getOperand(0);
143 if (Src.getValueType() == MVT::f32)
144 Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src);
146 switch (Op.getValueType()) {
147 default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
149 Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
152 Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
157 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
158 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
159 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
160 Op, FI, DAG.getSrcValue(0));
161 if (Op.getOpcode() == PPCISD::FCTIDZ) {
162 Op = DAG.getLoad(MVT::i64, ST, FI, DAG.getSrcValue(0));
164 FI = DAG.getNode(ISD::ADD, MVT::i32, FI, DAG.getConstant(4, MVT::i32));
165 Op = DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
169 case ISD::SINT_TO_FP: {
170 assert(MVT::i64 == Op.getOperand(0).getValueType() &&
171 "Unhandled SINT_TO_FP type in custom expander!");
173 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
174 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
175 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
176 Op.getOperand(0), FI, DAG.getSrcValue(0));
177 SDOperand LD = DAG.getLoad(MVT::f64, ST, FI, DAG.getSrcValue(0));
178 SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, LD);
179 if (MVT::f32 == Op.getValueType())
180 FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP);
183 case ISD::SELECT_CC: {
184 // Turn FP only select_cc's into fsel instructions.
185 if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) ||
186 !MVT::isFloatingPoint(Op.getOperand(2).getValueType()))
189 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
191 // Cannot handle SETEQ/SETNE.
192 if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
194 MVT::ValueType ResVT = Op.getValueType();
195 MVT::ValueType CmpVT = Op.getOperand(0).getValueType();
196 SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1);
197 SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3);
199 // If the RHS of the comparison is a 0.0, we don't need to do the
200 // subtraction at all.
201 if (isFloatingPointZero(RHS))
203 default: assert(0 && "Invalid FSEL condition"); abort();
206 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
209 return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
212 std::swap(TV, FV); // fsel is natively setge, swap operands for setlt
215 return DAG.getNode(PPCISD::FSEL, ResVT,
216 DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
220 default: assert(0 && "Invalid FSEL condition"); abort();
223 return DAG.getNode(PPCISD::FSEL, ResVT,
224 DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), FV, TV);
227 return DAG.getNode(PPCISD::FSEL, ResVT,
228 DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), TV, FV);
231 return DAG.getNode(PPCISD::FSEL, ResVT,
232 DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), FV, TV);
235 return DAG.getNode(PPCISD::FSEL, ResVT,
236 DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), TV, FV);
241 assert(Op.getValueType() == MVT::i64 &&
242 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
243 // The generic code does a fine job expanding shift by a constant.
244 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
246 // Otherwise, expand into a bunch of logical ops. Note that these ops
247 // depend on the PPC behavior for oversized shift amounts.
248 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
249 DAG.getConstant(0, MVT::i32));
250 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
251 DAG.getConstant(1, MVT::i32));
252 SDOperand Amt = Op.getOperand(1);
254 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
255 DAG.getConstant(32, MVT::i32), Amt);
256 SDOperand Tmp2 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Amt);
257 SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Tmp1);
258 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
259 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
260 DAG.getConstant(-32U, MVT::i32));
261 SDOperand Tmp6 = DAG.getNode(ISD::SHL, MVT::i32, Lo, Tmp5);
262 SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
263 SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
264 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
267 assert(Op.getValueType() == MVT::i64 &&
268 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
269 // The generic code does a fine job expanding shift by a constant.
270 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
272 // Otherwise, expand into a bunch of logical ops. Note that these ops
273 // depend on the PPC behavior for oversized shift amounts.
274 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
275 DAG.getConstant(0, MVT::i32));
276 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
277 DAG.getConstant(1, MVT::i32));
278 SDOperand Amt = Op.getOperand(1);
280 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
281 DAG.getConstant(32, MVT::i32), Amt);
282 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
283 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
284 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
285 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
286 DAG.getConstant(-32U, MVT::i32));
287 SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
288 SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
289 SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
290 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
293 assert(Op.getValueType() == MVT::i64 &&
294 Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
295 // The generic code does a fine job expanding shift by a constant.
296 if (isa<ConstantSDNode>(Op.getOperand(1))) break;
298 // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
299 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
300 DAG.getConstant(0, MVT::i32));
301 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
302 DAG.getConstant(1, MVT::i32));
303 SDOperand Amt = Op.getOperand(1);
305 SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
306 DAG.getConstant(32, MVT::i32), Amt);
307 SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
308 SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
309 SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
310 SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
311 DAG.getConstant(-32U, MVT::i32));
312 SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
313 SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
314 SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
315 Tmp4, Tmp6, ISD::SETLE);
316 return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
322 std::vector<SDOperand>
323 PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
325 // add beautiful description of PPC stack frame format, or at least some docs
327 MachineFunction &MF = DAG.getMachineFunction();
328 MachineFrameInfo *MFI = MF.getFrameInfo();
329 MachineBasicBlock& BB = MF.front();
330 SSARegMap *RegMap = MF.getSSARegMap();
331 std::vector<SDOperand> ArgValues;
333 unsigned ArgOffset = 24;
334 unsigned GPR_remaining = 8;
335 unsigned FPR_remaining = 13;
336 unsigned GPR_idx = 0, FPR_idx = 0;
337 static const unsigned GPR[] = {
338 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
339 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
341 static const unsigned FPR[] = {
342 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
343 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
346 // Add DAG nodes to load the arguments... On entry to a function on PPC,
347 // the arguments start at offset 24, although they are likely to be passed
349 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
350 SDOperand newroot, argt;
352 bool needsLoad = false;
353 bool ArgLive = !I->use_empty();
354 MVT::ValueType ObjectVT = getValueType(I->getType());
357 default: assert(0 && "Unhandled argument type!");
364 if (GPR_remaining > 0) {
365 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
366 MF.addLiveIn(GPR[GPR_idx], VReg);
367 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
368 if (ObjectVT != MVT::i32) {
369 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
371 argt = DAG.getNode(AssertOp, MVT::i32, argt,
372 DAG.getValueType(ObjectVT));
373 argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
379 case MVT::i64: ObjSize = 8;
381 if (GPR_remaining > 0) {
382 SDOperand argHi, argLo;
383 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
384 MF.addLiveIn(GPR[GPR_idx], VReg);
385 argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
386 // If we have two or more remaining argument registers, then both halves
387 // of the i64 can be sourced from there. Otherwise, the lower half will
388 // have to come off the stack. This can happen when an i64 is preceded
389 // by 28 bytes of arguments.
390 if (GPR_remaining > 1) {
391 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
392 MF.addLiveIn(GPR[GPR_idx+1], VReg);
393 argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
395 int FI = MFI->CreateFixedObject(4, ArgOffset+4);
396 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
397 argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
398 DAG.getSrcValue(NULL));
400 // Build the outgoing arg thingy
401 argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
409 ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
411 if (FPR_remaining > 0) {
413 if (ObjectVT == MVT::f32)
414 VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
416 VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
417 MF.addLiveIn(FPR[FPR_idx], VReg);
418 argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
427 // We need to load the argument to a virtual register if we determined above
428 // that we ran out of physical registers of the appropriate type
430 unsigned SubregOffset = 0;
431 if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
432 if (ObjectVT == MVT::i16) SubregOffset = 2;
433 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
434 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
435 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
436 DAG.getConstant(SubregOffset, MVT::i32));
437 argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
438 DAG.getSrcValue(NULL));
441 // Every 4 bytes of argument space consumes one of the GPRs available for
443 if (GPR_remaining > 0) {
444 unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
445 GPR_remaining -= delta;
448 ArgOffset += ObjSize;
450 DAG.setRoot(newroot.getValue(1));
452 ArgValues.push_back(argt);
455 // If the function takes variable number of arguments, make a frame index for
456 // the start of the first vararg value... for expansion of llvm.va_start.
458 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
459 SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
460 // If this function is vararg, store any remaining integer argument regs
461 // to their spots on the stack so that they may be loaded by deferencing the
462 // result of va_next.
463 std::vector<SDOperand> MemOps;
464 for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
465 unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
466 MF.addLiveIn(GPR[GPR_idx], VReg);
467 SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
468 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
469 Val, FIN, DAG.getSrcValue(NULL));
470 MemOps.push_back(Store);
471 // Increment the address by four for the next argument to store
472 SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
473 FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
475 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
478 // Finally, inform the code generator which regs we return values in.
479 switch (getValueType(F.getReturnType())) {
480 default: assert(0 && "Unknown type!");
481 case MVT::isVoid: break;
486 MF.addLiveOut(PPC::R3);
489 MF.addLiveOut(PPC::R3);
490 MF.addLiveOut(PPC::R4);
494 MF.addLiveOut(PPC::F1);
501 std::pair<SDOperand, SDOperand>
502 PPCTargetLowering::LowerCallTo(SDOperand Chain,
503 const Type *RetTy, bool isVarArg,
504 unsigned CallingConv, bool isTailCall,
505 SDOperand Callee, ArgListTy &Args,
507 // args_to_use will accumulate outgoing args for the ISD::CALL case in
508 // SelectExpr to use to put the arguments in the appropriate registers.
509 std::vector<SDOperand> args_to_use;
511 // Count how many bytes are to be pushed on the stack, including the linkage
512 // area, and parameter passing area.
513 unsigned NumBytes = 24;
516 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
517 DAG.getConstant(NumBytes, getPointerTy()));
519 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
520 switch (getValueType(Args[i].second)) {
521 default: assert(0 && "Unknown value type!");
536 // Just to be safe, we'll always reserve the full 24 bytes of linkage area
537 // plus 32 bytes of argument space in case any called code gets funky on us.
538 // (Required by ABI to support var arg)
539 if (NumBytes < 56) NumBytes = 56;
541 // Adjust the stack pointer for the new arguments...
542 // These operations are automatically eliminated by the prolog/epilog pass
543 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
544 DAG.getConstant(NumBytes, getPointerTy()));
546 // Set up a copy of the stack pointer for use loading and storing any
547 // arguments that may not fit in the registers available for argument
549 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
552 // Figure out which arguments are going to go in registers, and which in
553 // memory. Also, if this is a vararg function, floating point operations
554 // must be stored to our stack, and loaded into integer regs as well, if
555 // any integer regs are available for argument passing.
556 unsigned ArgOffset = 24;
557 unsigned GPR_remaining = 8;
558 unsigned FPR_remaining = 13;
560 std::vector<SDOperand> MemOps;
561 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
562 // PtrOff will be used to store the current argument to the stack if a
563 // register cannot be found for it.
564 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
565 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
566 MVT::ValueType ArgVT = getValueType(Args[i].second);
569 default: assert(0 && "Unexpected ValueType for argument!");
573 // Promote the integer to 32 bits. If the input type is signed use a
574 // sign extend, otherwise use a zero extend.
575 if (Args[i].second->isSigned())
576 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
578 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
581 if (GPR_remaining > 0) {
582 args_to_use.push_back(Args[i].first);
585 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
586 Args[i].first, PtrOff,
587 DAG.getSrcValue(NULL)));
592 // If we have one free GPR left, we can place the upper half of the i64
593 // in it, and store the other half to the stack. If we have two or more
594 // free GPRs, then we can pass both halves of the i64 in registers.
595 if (GPR_remaining > 0) {
596 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
597 Args[i].first, DAG.getConstant(1, MVT::i32));
598 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
599 Args[i].first, DAG.getConstant(0, MVT::i32));
600 args_to_use.push_back(Hi);
602 if (GPR_remaining > 0) {
603 args_to_use.push_back(Lo);
606 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
607 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
608 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
609 Lo, PtrOff, DAG.getSrcValue(NULL)));
612 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
613 Args[i].first, PtrOff,
614 DAG.getSrcValue(NULL)));
620 if (FPR_remaining > 0) {
621 args_to_use.push_back(Args[i].first);
624 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
625 Args[i].first, PtrOff,
626 DAG.getSrcValue(NULL));
627 MemOps.push_back(Store);
628 // Float varargs are always shadowed in available integer registers
629 if (GPR_remaining > 0) {
630 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
631 DAG.getSrcValue(NULL));
632 MemOps.push_back(Load);
633 args_to_use.push_back(Load);
636 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
637 SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
638 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
639 SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
640 DAG.getSrcValue(NULL));
641 MemOps.push_back(Load);
642 args_to_use.push_back(Load);
646 // If we have any FPRs remaining, we may also have GPRs remaining.
647 // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
649 if (GPR_remaining > 0) {
650 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
653 if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
654 args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
659 MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
660 Args[i].first, PtrOff,
661 DAG.getSrcValue(NULL)));
663 ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
668 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
671 std::vector<MVT::ValueType> RetVals;
672 MVT::ValueType RetTyVT = getValueType(RetTy);
673 MVT::ValueType ActualRetTyVT = RetTyVT;
674 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
675 ActualRetTyVT = MVT::i32; // Promote result to i32.
677 if (RetTyVT != MVT::isVoid)
678 RetVals.push_back(ActualRetTyVT);
679 RetVals.push_back(MVT::Other);
681 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
682 Chain, Callee, args_to_use), 0);
683 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
684 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
685 DAG.getConstant(NumBytes, getPointerTy()));
686 SDOperand RetVal = TheCall;
688 // If the result is a small value, add a note so that we keep track of the
689 // information about whether it is sign or zero extended.
690 if (RetTyVT != ActualRetTyVT) {
691 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
692 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
693 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
696 return std::make_pair(RetVal, Chain);
699 SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
700 Value *VAListV, SelectionDAG &DAG) {
701 // vastart just stores the address of the VarArgsFrameIndex slot into the
702 // memory location argument.
703 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
704 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
705 DAG.getSrcValue(VAListV));
708 std::pair<SDOperand,SDOperand>
709 PPCTargetLowering::LowerVAArg(SDOperand Chain,
710 SDOperand VAListP, Value *VAListV,
711 const Type *ArgTy, SelectionDAG &DAG) {
712 MVT::ValueType ArgVT = getValueType(ArgTy);
715 DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
716 SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
718 if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
721 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
722 "Other types should have been promoted for varargs!");
725 VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
726 DAG.getConstant(Amt, VAList.getValueType()));
727 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
728 VAList, VAListP, DAG.getSrcValue(VAListV));
729 return std::make_pair(Result, Chain);
733 std::pair<SDOperand, SDOperand> PPCTargetLowering::
734 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
736 assert(0 && "LowerFrameReturnAddress unimplemented");
741 PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
742 MachineBasicBlock *BB) {
743 assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
744 MI->getOpcode() == PPC::SELECT_CC_F4 ||
745 MI->getOpcode() == PPC::SELECT_CC_F8) &&
746 "Unexpected instr type to insert");
748 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
749 // control-flow pattern. The incoming instruction knows the destination vreg
750 // to set, the condition code register to branch on, the true/false values to
751 // select between, and a branch opcode to use.
752 const BasicBlock *LLVM_BB = BB->getBasicBlock();
753 ilist<MachineBasicBlock>::iterator It = BB;
761 // fallthrough --> copy0MBB
762 MachineBasicBlock *thisMBB = BB;
763 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
764 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
765 BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
766 .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
767 MachineFunction *F = BB->getParent();
768 F->getBasicBlockList().insert(It, copy0MBB);
769 F->getBasicBlockList().insert(It, sinkMBB);
770 // Update machine-CFG edges
771 BB->addSuccessor(copy0MBB);
772 BB->addSuccessor(sinkMBB);
776 // # fallthrough to sinkMBB
779 // Update machine-CFG edges
780 BB->addSuccessor(sinkMBB);
783 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
786 BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
787 .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
788 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
790 delete MI; // The pseudo instruction is gone now.