First bits of 64 bit PowerPC stuff, currently disabled. A lot of this is
[oota-llvm.git] / lib / Target / PowerPC / PPCISelLowering.cpp
1 //===-- PPCISelLowering.cpp - PPC 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 PPCISelLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
23 using namespace llvm;
24
25 PPCTargetLowering::PPCTargetLowering(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, PPC::GPRCRegisterClass);
36   addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
37   addRegisterClass(MVT::f64, PPC::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 does not have BRCOND* which requires SetCC
80   setOperationAction(ISD::BRCOND,       MVT::Other, Expand);
81   setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
82   
83   // PowerPC does not have FP_TO_UINT
84   setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
85   
86   // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
87   setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
88
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);
92
93   // PowerPC does not have truncstore for i1.
94   setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
95   
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);
110   } else {
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);
115   }
116   
117   setSetCCResultContents(ZeroOrOneSetCCResult);
118   
119   computeRegisterProperties();
120 }
121
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);
131   }
132   return false;
133 }
134
135 /// LowerOperation - Provide custom lowering hooks for some operations.
136 ///
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);
145     
146     switch (Op.getValueType()) {
147     default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!");
148     case MVT::i32:
149       Op = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src);
150       break;
151     case MVT::i64:
152       Op = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src);
153       break;
154     }
155    
156     int FrameIdx =
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));
163     } else {
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));
166     }
167     return Op;
168   }
169   case ISD::SINT_TO_FP: {
170     assert(MVT::i64 == Op.getOperand(0).getValueType() && 
171            "Unhandled SINT_TO_FP type in custom expander!");
172     int FrameIdx =
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);
181     return FP;
182   }
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()))
187       break;
188     
189     ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
190     
191     // Cannot handle SETEQ/SETNE.
192     if (CC == ISD::SETEQ || CC == ISD::SETNE) break;
193     
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);
198
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))
202       switch (CC) {
203       default: assert(0 && "Invalid FSEL condition"); abort();
204       case ISD::SETULT:
205       case ISD::SETLT:
206         std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
207       case ISD::SETUGE:
208       case ISD::SETGE:
209         return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
210       case ISD::SETUGT:
211       case ISD::SETGT:
212         std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
213       case ISD::SETULE:
214       case ISD::SETLE:
215         return DAG.getNode(PPCISD::FSEL, ResVT,
216                            DAG.getNode(ISD::FNEG, ResVT, LHS), TV, FV);
217       }
218     
219     switch (CC) {
220     default: assert(0 && "Invalid FSEL condition"); abort();
221     case ISD::SETULT:
222     case ISD::SETLT:
223       return DAG.getNode(PPCISD::FSEL, ResVT,
224                          DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), FV, TV);
225     case ISD::SETUGE:
226     case ISD::SETGE:
227       return DAG.getNode(PPCISD::FSEL, ResVT,
228                          DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS), TV, FV);
229     case ISD::SETUGT:
230     case ISD::SETGT:
231       return DAG.getNode(PPCISD::FSEL, ResVT,
232                          DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), FV, TV);
233     case ISD::SETULE:
234     case ISD::SETLE:
235       return DAG.getNode(PPCISD::FSEL, ResVT,
236                          DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS), TV, FV);
237     }
238     break;
239   }
240   case ISD::SHL: {
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;
245     
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);
253     
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);
265   }
266   case ISD::SRL: {
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;
271     
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);
279     
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);
291   }    
292   case ISD::SRA: {
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;
297       
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);
304     
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);
317   }
318   }
319   return SDOperand();
320 }
321
322 std::vector<SDOperand>
323 PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
324   //
325   // add beautiful description of PPC stack frame format, or at least some docs
326   //
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;
332   
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,
340   };
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
344   };
345   
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
348   // in registers.
349   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
350     SDOperand newroot, argt;
351     unsigned ObjSize;
352     bool needsLoad = false;
353     bool ArgLive = !I->use_empty();
354     MVT::ValueType ObjectVT = getValueType(I->getType());
355     
356     switch (ObjectVT) {
357     default: assert(0 && "Unhandled argument type!");
358     case MVT::i1:
359     case MVT::i8:
360     case MVT::i16:
361     case MVT::i32:
362       ObjSize = 4;
363       if (!ArgLive) break;
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 
370                                                        : ISD::AssertZext;
371           argt = DAG.getNode(AssertOp, MVT::i32, argt, 
372                              DAG.getValueType(ObjectVT));
373           argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
374         }
375       } else {
376         needsLoad = true;
377       }
378       break;
379     case MVT::i64: ObjSize = 8;
380       if (!ArgLive) break;
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);
394         } else {
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));
399         }
400         // Build the outgoing arg thingy
401         argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
402         newroot = argLo;
403       } else {
404         needsLoad = true;
405       }
406       break;
407     case MVT::f32:
408     case MVT::f64:
409       ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
410       if (!ArgLive) break;
411       if (FPR_remaining > 0) {
412         unsigned VReg;
413         if (ObjectVT == MVT::f32)
414           VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
415         else
416           VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
417         MF.addLiveIn(FPR[FPR_idx], VReg);
418         argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
419         --FPR_remaining;
420         ++FPR_idx;
421       } else {
422         needsLoad = true;
423       }
424       break;
425     }
426     
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
429     if (needsLoad) {
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));
439     }
440     
441     // Every 4 bytes of argument space consumes one of the GPRs available for
442     // argument passing.
443     if (GPR_remaining > 0) {
444       unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
445       GPR_remaining -= delta;
446       GPR_idx += delta;
447     }
448     ArgOffset += ObjSize;
449     if (newroot.Val)
450       DAG.setRoot(newroot.getValue(1));
451     
452     ArgValues.push_back(argt);
453   }
454   
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.
457   if (F.isVarArg()) {
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);
474     }
475     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
476   }
477   
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;
482     case MVT::i1:
483     case MVT::i8:
484     case MVT::i16:
485     case MVT::i32:
486       MF.addLiveOut(PPC::R3);
487       break;
488     case MVT::i64:
489       MF.addLiveOut(PPC::R3);
490       MF.addLiveOut(PPC::R4);
491       break;
492     case MVT::f32:
493     case MVT::f64:
494       MF.addLiveOut(PPC::F1);
495       break;
496   }
497   
498   return ArgValues;
499 }
500
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,
506                                SelectionDAG &DAG) {
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;
510   
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;
514   
515   if (Args.empty()) {
516     Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
517                         DAG.getConstant(NumBytes, getPointerTy()));
518   } else {
519     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
520       switch (getValueType(Args[i].second)) {
521       default: assert(0 && "Unknown value type!");
522       case MVT::i1:
523       case MVT::i8:
524       case MVT::i16:
525       case MVT::i32:
526       case MVT::f32:
527         NumBytes += 4;
528         break;
529       case MVT::i64:
530       case MVT::f64:
531         NumBytes += 8;
532         break;
533       }
534     }
535         
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;
540     
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()));
545     
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
548     // passing.
549     SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
550                                             PPC::R1, MVT::i32);
551     
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;
559     
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);
567       
568       switch (ArgVT) {
569       default: assert(0 && "Unexpected ValueType for argument!");
570       case MVT::i1:
571       case MVT::i8:
572       case MVT::i16:
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);
577         else
578           Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
579         // FALL THROUGH
580       case MVT::i32:
581         if (GPR_remaining > 0) {
582           args_to_use.push_back(Args[i].first);
583           --GPR_remaining;
584         } else {
585           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
586                                        Args[i].first, PtrOff,
587                                        DAG.getSrcValue(NULL)));
588         }
589         ArgOffset += 4;
590         break;
591       case MVT::i64:
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);
601           --GPR_remaining;
602           if (GPR_remaining > 0) {
603             args_to_use.push_back(Lo);
604             --GPR_remaining;
605           } else {
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)));
610           }
611         } else {
612           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
613                                        Args[i].first, PtrOff,
614                                        DAG.getSrcValue(NULL)));
615         }
616         ArgOffset += 8;
617         break;
618       case MVT::f32:
619       case MVT::f64:
620         if (FPR_remaining > 0) {
621           args_to_use.push_back(Args[i].first);
622           --FPR_remaining;
623           if (isVarArg) {
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);
634               --GPR_remaining;
635             }
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);
643               --GPR_remaining;
644             }
645           } else {
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
648             // GPRs.
649             if (GPR_remaining > 0) {
650               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
651               --GPR_remaining;
652             }
653             if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
654               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
655               --GPR_remaining;
656             }
657           }
658         } else {
659           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
660                                        Args[i].first, PtrOff,
661                                        DAG.getSrcValue(NULL)));
662         }
663         ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
664         break;
665       }
666     }
667     if (!MemOps.empty())
668       Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
669   }
670   
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.
676     
677   if (RetTyVT != MVT::isVoid)
678     RetVals.push_back(ActualRetTyVT);
679   RetVals.push_back(MVT::Other);
680   
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;
687   
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);
694   }
695   
696   return std::make_pair(RetVal, Chain);
697 }
698
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));
706 }
707
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);
713   
714   SDOperand VAList =
715     DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
716   SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
717   unsigned Amt;
718   if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
719     Amt = 4;
720   else {
721     assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
722            "Other types should have been promoted for varargs!");
723     Amt = 8;
724   }
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);
730 }
731
732
733 std::pair<SDOperand, SDOperand> PPCTargetLowering::
734 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
735                         SelectionDAG &DAG) {
736   assert(0 && "LowerFrameReturnAddress unimplemented");
737   abort();
738 }
739
740 MachineBasicBlock *
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");
747   
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;
754   ++It;
755   
756   //  thisMBB:
757   //  ...
758   //   TrueVal = ...
759   //   cmpTY ccX, r1, r2
760   //   bCC copy1MBB
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);
773   
774   //  copy0MBB:
775   //   %FalseValue = ...
776   //   # fallthrough to sinkMBB
777   BB = copy0MBB;
778   
779   // Update machine-CFG edges
780   BB->addSuccessor(sinkMBB);
781   
782   //  sinkMBB:
783   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
784   //  ...
785   BB = sinkMBB;
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);
789
790   delete MI;   // The pseudo instruction is gone now.
791   return BB;
792 }
793