Modify the ppc backend to use two register classes for FP: F8RC and F4RC.
[oota-llvm.git] / lib / Target / PowerPC / PPCISelLowering.cpp
1 //===-- 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"
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"
23 using namespace llvm;
24
25 PPC32TargetLowering::PPC32TargetLowering(TargetMachine &TM)
26   : TargetLowering(TM) {
27     
28   // Fold away setcc operations if possible.
29   setSetCCIsExpensive();
30   
31   // Use _setjmp/_longjmp instead of setjmp/longjmp.
32   setUseUnderscoreSetJmpLongJmp(true);
33     
34   // Set up the register classes.
35   addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass);
36   addRegisterClass(MVT::f32, PPC32::F4RCRegisterClass);
37   addRegisterClass(MVT::f64, PPC32::F8RCRegisterClass);
38   
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);
43   
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);
47   
48   // PowerPC has no SREM/UREM instructions
49   setOperationAction(ISD::SREM, MVT::i32, Expand);
50   setOperationAction(ISD::UREM, MVT::i32, Expand);
51   
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);
59   
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);
64   }
65   
66   // PowerPC does not have CTPOP or CTTZ
67   setOperationAction(ISD::CTPOP, MVT::i32  , Expand);
68   setOperationAction(ISD::CTTZ , MVT::i32  , Expand);
69   
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);
74   
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);
78   
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);
83
84   // PowerPC does not have BRCOND* which requires SetCC
85   setOperationAction(ISD::BRCOND,       MVT::Other, Expand);
86   setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
87   
88   // PowerPC does not have FP_TO_UINT
89   setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
90   
91   // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
92   setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
93
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);
97
98   // PowerPC does not have truncstore for i1.
99   setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
100   
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);
106   }
107   
108   setSetCCResultContents(ZeroOrOneSetCCResult);
109   
110   computeRegisterProperties();
111 }
112
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);
122   }
123   return false;
124 }
125
126 /// LowerOperation - Provide custom lowering hooks for some operations.
127 ///
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!");
135     case MVT::i32:
136       Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Op.getOperand(0));
137       break;
138     case MVT::i64:
139       Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Op.getOperand(0));
140       break;
141     }
142    
143     int FrameIdx =
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));
150     } else {
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));
153     }
154     return Op;
155   }
156   case ISD::SINT_TO_FP: {
157     assert(MVT::i64 == Op.getOperand(0).getValueType() && 
158            "Unhandled SINT_TO_FP type in custom expander!");
159     int FrameIdx =
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);
168     return FP;
169   }
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()))
174       break;
175     
176     ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
177     
178     // Cannot handle SETEQ/SETNE.
179     if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
180     
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);
185
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))
189       switch (CC) {
190       default: assert(0 && "Invalid FSEL condition"); abort();
191       case ISD::SETULT:
192       case ISD::SETLT:
193         std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
194       case ISD::SETUGE:
195       case ISD::SETGE:
196         return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
197       case ISD::SETUGT:
198       case ISD::SETGT:
199         std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
200       case ISD::SETULE:
201       case ISD::SETLE:
202         return DAG.getNode(PPCISD::FSEL, ResVT,
203                            DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
204       }
205     
206     switch (CC) {
207     default: assert(0 && "Invalid FSEL condition"); abort();
208     case ISD::SETULT:
209     case ISD::SETLT:
210       return DAG.getNode(PPCISD::FSEL, ResVT,
211                          DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), FV, TV);
212     case ISD::SETUGE:
213     case ISD::SETGE:
214       return DAG.getNode(PPCISD::FSEL, ResVT,
215                          DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), TV, FV);
216     case ISD::SETUGT:
217     case ISD::SETGT:
218       return DAG.getNode(PPCISD::FSEL, ResVT,
219                          DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), FV, TV);
220     case ISD::SETULE:
221     case ISD::SETLE:
222       return DAG.getNode(PPCISD::FSEL, ResVT,
223                          DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), TV, FV);
224     }
225     break;
226   }
227   case ISD::SHL: {
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;
232     
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);
240     
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);
252   }
253   case ISD::SRL: {
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;
258     
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);
266     
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);
278   }    
279   case ISD::SRA: {
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;
284       
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);
291     
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);
304   }
305   }
306   return SDOperand();
307 }
308
309 std::vector<SDOperand>
310 PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
311   //
312   // add beautiful description of PPC stack frame format, or at least some docs
313   //
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;
319   
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,
327   };
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
331   };
332   
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
335   // in registers.
336   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
337     SDOperand newroot, argt;
338     unsigned ObjSize;
339     bool needsLoad = false;
340     bool ArgLive = !I->use_empty();
341     MVT::ValueType ObjectVT = getValueType(I->getType());
342     
343     switch (ObjectVT) {
344     default: assert(0 && "Unhandled argument type!");
345     case MVT::i1:
346     case MVT::i8:
347     case MVT::i16:
348     case MVT::i32:
349       ObjSize = 4;
350       if (!ArgLive) break;
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 
357                                                        : ISD::AssertZext;
358           argt = DAG.getNode(AssertOp, MVT::i32, argt, 
359                              DAG.getValueType(ObjectVT));
360           argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
361         }
362       } else {
363         needsLoad = true;
364       }
365       break;
366     case MVT::i64: ObjSize = 8;
367       if (!ArgLive) break;
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);
381         } else {
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));
386         }
387         // Build the outgoing arg thingy
388         argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
389         newroot = argLo;
390       } else {
391         needsLoad = true;
392       }
393       break;
394     case MVT::f32:
395     case MVT::f64:
396       ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
397       if (!ArgLive) break;
398       if (FPR_remaining > 0) {
399         unsigned VReg;
400         if (ObjectVT == MVT::f32)
401           VReg = RegMap->createVirtualRegister(&PPC32::F4RCRegClass);
402         else
403           VReg = RegMap->createVirtualRegister(&PPC32::F8RCRegClass);
404         MF.addLiveIn(FPR[FPR_idx], VReg);
405         argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
406         --FPR_remaining;
407         ++FPR_idx;
408       } else {
409         needsLoad = true;
410       }
411       break;
412     }
413     
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
416     if (needsLoad) {
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));
426     }
427     
428     // Every 4 bytes of argument space consumes one of the GPRs available for
429     // argument passing.
430     if (GPR_remaining > 0) {
431       unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
432       GPR_remaining -= delta;
433       GPR_idx += delta;
434     }
435     ArgOffset += ObjSize;
436     if (newroot.Val)
437       DAG.setRoot(newroot.getValue(1));
438     
439     ArgValues.push_back(argt);
440   }
441   
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.
444   if (F.isVarArg()) {
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);
461     }
462     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
463   }
464   
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;
469     case MVT::i1:
470     case MVT::i8:
471     case MVT::i16:
472     case MVT::i32:
473       MF.addLiveOut(PPC::R3);
474       break;
475     case MVT::i64:
476       MF.addLiveOut(PPC::R3);
477       MF.addLiveOut(PPC::R4);
478       break;
479     case MVT::f32:
480     case MVT::f64:
481       MF.addLiveOut(PPC::F1);
482       break;
483   }
484   
485   return ArgValues;
486 }
487
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,
493                                  SelectionDAG &DAG) {
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;
497   
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;
501   
502   if (Args.empty()) {
503     Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
504                         DAG.getConstant(NumBytes, getPointerTy()));
505   } else {
506     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
507       switch (getValueType(Args[i].second)) {
508       default: assert(0 && "Unknown value type!");
509       case MVT::i1:
510       case MVT::i8:
511       case MVT::i16:
512       case MVT::i32:
513       case MVT::f32:
514         NumBytes += 4;
515         break;
516       case MVT::i64:
517       case MVT::f64:
518         NumBytes += 8;
519         break;
520       }
521     }
522         
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;
527     
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()));
532     
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
535     // passing.
536     SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
537                                             PPC::R1, MVT::i32);
538     
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;
546     
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);
554       
555       switch (ArgVT) {
556       default: assert(0 && "Unexpected ValueType for argument!");
557       case MVT::i1:
558       case MVT::i8:
559       case MVT::i16:
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);
564         else
565           Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
566         // FALL THROUGH
567       case MVT::i32:
568         if (GPR_remaining > 0) {
569           args_to_use.push_back(Args[i].first);
570           --GPR_remaining;
571         } else {
572           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
573                                        Args[i].first, PtrOff,
574                                        DAG.getSrcValue(NULL)));
575         }
576         ArgOffset += 4;
577         break;
578       case MVT::i64:
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);
588           --GPR_remaining;
589           if (GPR_remaining > 0) {
590             args_to_use.push_back(Lo);
591             --GPR_remaining;
592           } else {
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)));
597           }
598         } else {
599           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
600                                        Args[i].first, PtrOff,
601                                        DAG.getSrcValue(NULL)));
602         }
603         ArgOffset += 8;
604         break;
605       case MVT::f32:
606       case MVT::f64:
607         if (FPR_remaining > 0) {
608           args_to_use.push_back(Args[i].first);
609           --FPR_remaining;
610           if (isVarArg) {
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);
621               --GPR_remaining;
622             }
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);
630               --GPR_remaining;
631             }
632           } else {
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
635             // GPRs.
636             if (GPR_remaining > 0) {
637               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
638               --GPR_remaining;
639             }
640             if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
641               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
642               --GPR_remaining;
643             }
644           }
645         } else {
646           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
647                                        Args[i].first, PtrOff,
648                                        DAG.getSrcValue(NULL)));
649         }
650         ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
651         break;
652       }
653     }
654     if (!MemOps.empty())
655       Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
656   }
657   
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.
663     
664   if (RetTyVT != MVT::isVoid)
665     RetVals.push_back(ActualRetTyVT);
666   RetVals.push_back(MVT::Other);
667   
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;
674   
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);
681   }
682   
683   return std::make_pair(RetVal, Chain);
684 }
685
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));
693 }
694
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);
700   
701   SDOperand VAList =
702     DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
703   SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
704   unsigned Amt;
705   if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
706     Amt = 4;
707   else {
708     assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
709            "Other types should have been promoted for varargs!");
710     Amt = 8;
711   }
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);
717 }
718
719
720 std::pair<SDOperand, SDOperand> PPC32TargetLowering::
721 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
722                         SelectionDAG &DAG) {
723   assert(0 && "LowerFrameReturnAddress unimplemented");
724   abort();
725 }
726
727 MachineBasicBlock *
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");
734   
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;
741   ++It;
742   
743   //  thisMBB:
744   //  ...
745   //   TrueVal = ...
746   //   cmpTY ccX, r1, r2
747   //   bCC copy1MBB
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);
760   
761   //  copy0MBB:
762   //   %FalseValue = ...
763   //   # fallthrough to sinkMBB
764   BB = copy0MBB;
765   
766   // Update machine-CFG edges
767   BB->addSuccessor(sinkMBB);
768   
769   //  sinkMBB:
770   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
771   //  ...
772   BB = sinkMBB;
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);
776
777   delete MI;   // The pseudo instruction is gone now.
778   return BB;
779 }
780