When lowering direct calls, lower them to use a targetglobaladress directly
[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   setPow2DivIsCheap();
31   
32   // Use _setjmp/_longjmp instead of setjmp/longjmp.
33   setUseUnderscoreSetJmpLongJmp(true);
34     
35   // Set up the register classes.
36   addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
37   addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
38   addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
39   
40   // PowerPC has no intrinsics for these particular operations
41   setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
42   setOperationAction(ISD::MEMSET, MVT::Other, Expand);
43   setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
44   
45   // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
46   setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
47   setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
48   
49   // PowerPC has no SREM/UREM instructions
50   setOperationAction(ISD::SREM, MVT::i32, Expand);
51   setOperationAction(ISD::UREM, MVT::i32, Expand);
52   
53   // We don't support sin/cos/sqrt/fmod
54   setOperationAction(ISD::FSIN , MVT::f64, Expand);
55   setOperationAction(ISD::FCOS , MVT::f64, Expand);
56   setOperationAction(ISD::FREM , MVT::f64, Expand);
57   setOperationAction(ISD::FSIN , MVT::f32, Expand);
58   setOperationAction(ISD::FCOS , MVT::f32, Expand);
59   setOperationAction(ISD::FREM , MVT::f32, Expand);
60   
61   // If we're enabling GP optimizations, use hardware square root
62   if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
63     setOperationAction(ISD::FSQRT, MVT::f64, Expand);
64     setOperationAction(ISD::FSQRT, MVT::f32, Expand);
65   }
66   
67   // PowerPC does not have CTPOP or CTTZ
68   setOperationAction(ISD::CTPOP, MVT::i32  , Expand);
69   setOperationAction(ISD::CTTZ , MVT::i32  , Expand);
70   
71   // PowerPC does not have Select
72   setOperationAction(ISD::SELECT, MVT::i32, Expand);
73   setOperationAction(ISD::SELECT, MVT::f32, Expand);
74   setOperationAction(ISD::SELECT, MVT::f64, Expand);
75   
76   // PowerPC wants to turn select_cc of FP into fsel when possible.
77   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
78   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
79   
80   // PowerPC does not have BRCOND* which requires SetCC
81   setOperationAction(ISD::BRCOND,       MVT::Other, Expand);
82   setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
83   
84   // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
85   setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
86
87   // PowerPC does not have [U|S]INT_TO_FP
88   setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
89   setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
90
91   // PowerPC does not have truncstore for i1.
92   setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote);
93   
94   if (TM.getSubtarget<PPCSubtarget>().is64Bit()) {
95     // They also have instructions for converting between i64 and fp.
96     setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
97     setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
98     // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT
99     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote);
100   } else {
101     // PowerPC does not have FP_TO_UINT on 32 bit implementations.
102     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
103   }
104
105   if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) {
106     // 64 bit PowerPC implementations can support i64 types directly
107     addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
108     // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
109     setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
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         if (LHS.getValueType() == MVT::f32)   // Comparison is always 64-bits
210           LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
211         return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV);
212       case ISD::SETUGT:
213       case ISD::SETGT:
214         std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
215       case ISD::SETULE:
216       case ISD::SETLE:
217         if (LHS.getValueType() == MVT::f32)   // Comparison is always 64-bits
218           LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS);
219         return DAG.getNode(PPCISD::FSEL, ResVT,
220                            DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV);
221       }
222     
223     SDOperand Cmp;
224     switch (CC) {
225     default: assert(0 && "Invalid FSEL condition"); abort();
226     case ISD::SETULT:
227     case ISD::SETLT:
228       Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
229       if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
230         Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
231       return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
232     case ISD::SETUGE:
233     case ISD::SETGE:
234       Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS);
235       if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
236         Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
237       return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
238     case ISD::SETUGT:
239     case ISD::SETGT:
240       Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
241       if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
242         Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
243       return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV);
244     case ISD::SETULE:
245     case ISD::SETLE:
246       Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS);
247       if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
248         Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp);
249       return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV);
250     }
251     break;
252   }
253   case ISD::SHL: {
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::SHL, MVT::i32, Hi, Amt);
270     SDOperand Tmp3 = DAG.getNode(ISD::SRL, MVT::i32, Lo, 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::SHL, MVT::i32, Lo, Tmp5);
275     SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
276     SDOperand OutLo = DAG.getNode(ISD::SHL, MVT::i32, Lo, Amt);
277     return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
278   }
279   case ISD::SRL: {
280     assert(Op.getValueType() == MVT::i64 &&
281            Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!");
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.  Note that these ops
286     // depend on the PPC behavior for oversized shift amounts.
287     SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
288                                DAG.getConstant(0, MVT::i32));
289     SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
290                                DAG.getConstant(1, MVT::i32));
291     SDOperand Amt = Op.getOperand(1);
292     
293     SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
294                                  DAG.getConstant(32, MVT::i32), Amt);
295     SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
296     SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
297     SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
298     SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
299                                  DAG.getConstant(-32U, MVT::i32));
300     SDOperand Tmp6 = DAG.getNode(ISD::SRL, MVT::i32, Hi, Tmp5);
301     SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6);
302     SDOperand OutHi = DAG.getNode(ISD::SRL, MVT::i32, Hi, Amt);
303     return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
304   }    
305   case ISD::SRA: {
306     assert(Op.getValueType() == MVT::i64 &&
307            Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!");
308     // The generic code does a fine job expanding shift by a constant.
309     if (isa<ConstantSDNode>(Op.getOperand(1))) break;
310       
311     // Otherwise, expand into a bunch of logical ops, followed by a select_cc.
312     SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
313                                DAG.getConstant(0, MVT::i32));
314     SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0),
315                                DAG.getConstant(1, MVT::i32));
316     SDOperand Amt = Op.getOperand(1);
317     
318     SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32,
319                                  DAG.getConstant(32, MVT::i32), Amt);
320     SDOperand Tmp2 = DAG.getNode(ISD::SRL, MVT::i32, Lo, Amt);
321     SDOperand Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, Hi, Tmp1);
322     SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3);
323     SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt,
324                                  DAG.getConstant(-32U, MVT::i32));
325     SDOperand Tmp6 = DAG.getNode(ISD::SRA, MVT::i32, Hi, Tmp5);
326     SDOperand OutHi = DAG.getNode(ISD::SRA, MVT::i32, Hi, Amt);
327     SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32),
328                                       Tmp4, Tmp6, ISD::SETLE);
329     return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi);
330   }
331   }
332   return SDOperand();
333 }
334
335 std::vector<SDOperand>
336 PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
337   //
338   // add beautiful description of PPC stack frame format, or at least some docs
339   //
340   MachineFunction &MF = DAG.getMachineFunction();
341   MachineFrameInfo *MFI = MF.getFrameInfo();
342   MachineBasicBlock& BB = MF.front();
343   SSARegMap *RegMap = MF.getSSARegMap();
344   std::vector<SDOperand> ArgValues;
345   
346   unsigned ArgOffset = 24;
347   unsigned GPR_remaining = 8;
348   unsigned FPR_remaining = 13;
349   unsigned GPR_idx = 0, FPR_idx = 0;
350   static const unsigned GPR[] = {
351     PPC::R3, PPC::R4, PPC::R5, PPC::R6,
352     PPC::R7, PPC::R8, PPC::R9, PPC::R10,
353   };
354   static const unsigned FPR[] = {
355     PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
356     PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
357   };
358   
359   // Add DAG nodes to load the arguments...  On entry to a function on PPC,
360   // the arguments start at offset 24, although they are likely to be passed
361   // in registers.
362   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
363     SDOperand newroot, argt;
364     unsigned ObjSize;
365     bool needsLoad = false;
366     bool ArgLive = !I->use_empty();
367     MVT::ValueType ObjectVT = getValueType(I->getType());
368     
369     switch (ObjectVT) {
370     default: assert(0 && "Unhandled argument type!");
371     case MVT::i1:
372     case MVT::i8:
373     case MVT::i16:
374     case MVT::i32:
375       ObjSize = 4;
376       if (!ArgLive) break;
377       if (GPR_remaining > 0) {
378         unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
379         MF.addLiveIn(GPR[GPR_idx], VReg);
380         argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
381         if (ObjectVT != MVT::i32) {
382           unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext 
383                                                        : ISD::AssertZext;
384           argt = DAG.getNode(AssertOp, MVT::i32, argt, 
385                              DAG.getValueType(ObjectVT));
386           argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt);
387         }
388       } else {
389         needsLoad = true;
390       }
391       break;
392     case MVT::i64: ObjSize = 8;
393       if (!ArgLive) break;
394       if (GPR_remaining > 0) {
395         SDOperand argHi, argLo;
396         unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
397         MF.addLiveIn(GPR[GPR_idx], VReg);
398         argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
399         // If we have two or more remaining argument registers, then both halves
400         // of the i64 can be sourced from there.  Otherwise, the lower half will
401         // have to come off the stack.  This can happen when an i64 is preceded
402         // by 28 bytes of arguments.
403         if (GPR_remaining > 1) {
404           unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
405           MF.addLiveIn(GPR[GPR_idx+1], VReg);
406           argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32);
407         } else {
408           int FI = MFI->CreateFixedObject(4, ArgOffset+4);
409           SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
410           argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
411                               DAG.getSrcValue(NULL));
412         }
413         // Build the outgoing arg thingy
414         argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi);
415         newroot = argLo;
416       } else {
417         needsLoad = true;
418       }
419       break;
420     case MVT::f32:
421     case MVT::f64:
422       ObjSize = (ObjectVT == MVT::f64) ? 8 : 4;
423       if (!ArgLive) break;
424       if (FPR_remaining > 0) {
425         unsigned VReg;
426         if (ObjectVT == MVT::f32)
427           VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass);
428         else
429           VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass);
430         MF.addLiveIn(FPR[FPR_idx], VReg);
431         argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT);
432         --FPR_remaining;
433         ++FPR_idx;
434       } else {
435         needsLoad = true;
436       }
437       break;
438     }
439     
440     // We need to load the argument to a virtual register if we determined above
441     // that we ran out of physical registers of the appropriate type
442     if (needsLoad) {
443       unsigned SubregOffset = 0;
444       if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3;
445       if (ObjectVT == MVT::i16) SubregOffset = 2;
446       int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
447       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
448       FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN,
449                         DAG.getConstant(SubregOffset, MVT::i32));
450       argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
451                                    DAG.getSrcValue(NULL));
452     }
453     
454     // Every 4 bytes of argument space consumes one of the GPRs available for
455     // argument passing.
456     if (GPR_remaining > 0) {
457       unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1;
458       GPR_remaining -= delta;
459       GPR_idx += delta;
460     }
461     ArgOffset += ObjSize;
462     if (newroot.Val)
463       DAG.setRoot(newroot.getValue(1));
464     
465     ArgValues.push_back(argt);
466   }
467   
468   // If the function takes variable number of arguments, make a frame index for
469   // the start of the first vararg value... for expansion of llvm.va_start.
470   if (F.isVarArg()) {
471     VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
472     SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
473     // If this function is vararg, store any remaining integer argument regs
474     // to their spots on the stack so that they may be loaded by deferencing the
475     // result of va_next.
476     std::vector<SDOperand> MemOps;
477     for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) {
478       unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
479       MF.addLiveIn(GPR[GPR_idx], VReg);
480       SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
481       SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
482                                     Val, FIN, DAG.getSrcValue(NULL));
483       MemOps.push_back(Store);
484       // Increment the address by four for the next argument to store
485       SDOperand PtrOff = DAG.getConstant(4, getPointerTy());
486       FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff);
487     }
488     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps));
489   }
490   
491   // Finally, inform the code generator which regs we return values in.
492   switch (getValueType(F.getReturnType())) {
493     default: assert(0 && "Unknown type!");
494     case MVT::isVoid: break;
495     case MVT::i1:
496     case MVT::i8:
497     case MVT::i16:
498     case MVT::i32:
499       MF.addLiveOut(PPC::R3);
500       break;
501     case MVT::i64:
502       MF.addLiveOut(PPC::R3);
503       MF.addLiveOut(PPC::R4);
504       break;
505     case MVT::f32:
506     case MVT::f64:
507       MF.addLiveOut(PPC::F1);
508       break;
509   }
510   
511   return ArgValues;
512 }
513
514 std::pair<SDOperand, SDOperand>
515 PPCTargetLowering::LowerCallTo(SDOperand Chain,
516                                const Type *RetTy, bool isVarArg,
517                                unsigned CallingConv, bool isTailCall,
518                                SDOperand Callee, ArgListTy &Args,
519                                SelectionDAG &DAG) {
520   // args_to_use will accumulate outgoing args for the ISD::CALL case in
521   // SelectExpr to use to put the arguments in the appropriate registers.
522   std::vector<SDOperand> args_to_use;
523   
524   // Count how many bytes are to be pushed on the stack, including the linkage
525   // area, and parameter passing area.
526   unsigned NumBytes = 24;
527   
528   if (Args.empty()) {
529     Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
530                         DAG.getConstant(NumBytes, getPointerTy()));
531   } else {
532     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
533       switch (getValueType(Args[i].second)) {
534       default: assert(0 && "Unknown value type!");
535       case MVT::i1:
536       case MVT::i8:
537       case MVT::i16:
538       case MVT::i32:
539       case MVT::f32:
540         NumBytes += 4;
541         break;
542       case MVT::i64:
543       case MVT::f64:
544         NumBytes += 8;
545         break;
546       }
547     }
548         
549     // Just to be safe, we'll always reserve the full 24 bytes of linkage area
550     // plus 32 bytes of argument space in case any called code gets funky on us.
551     // (Required by ABI to support var arg)
552     if (NumBytes < 56) NumBytes = 56;
553     
554     // Adjust the stack pointer for the new arguments...
555     // These operations are automatically eliminated by the prolog/epilog pass
556     Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
557                         DAG.getConstant(NumBytes, getPointerTy()));
558     
559     // Set up a copy of the stack pointer for use loading and storing any
560     // arguments that may not fit in the registers available for argument
561     // passing.
562     SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
563                                             PPC::R1, MVT::i32);
564     
565     // Figure out which arguments are going to go in registers, and which in
566     // memory.  Also, if this is a vararg function, floating point operations
567     // must be stored to our stack, and loaded into integer regs as well, if
568     // any integer regs are available for argument passing.
569     unsigned ArgOffset = 24;
570     unsigned GPR_remaining = 8;
571     unsigned FPR_remaining = 13;
572     
573     std::vector<SDOperand> MemOps;
574     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
575       // PtrOff will be used to store the current argument to the stack if a
576       // register cannot be found for it.
577       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
578       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
579       MVT::ValueType ArgVT = getValueType(Args[i].second);
580       
581       switch (ArgVT) {
582       default: assert(0 && "Unexpected ValueType for argument!");
583       case MVT::i1:
584       case MVT::i8:
585       case MVT::i16:
586         // Promote the integer to 32 bits.  If the input type is signed use a
587         // sign extend, otherwise use a zero extend.
588         if (Args[i].second->isSigned())
589           Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
590         else
591           Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
592         // FALL THROUGH
593       case MVT::i32:
594         if (GPR_remaining > 0) {
595           args_to_use.push_back(Args[i].first);
596           --GPR_remaining;
597         } else {
598           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
599                                        Args[i].first, PtrOff,
600                                        DAG.getSrcValue(NULL)));
601         }
602         ArgOffset += 4;
603         break;
604       case MVT::i64:
605         // If we have one free GPR left, we can place the upper half of the i64
606         // in it, and store the other half to the stack.  If we have two or more
607         // free GPRs, then we can pass both halves of the i64 in registers.
608         if (GPR_remaining > 0) {
609           SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
610                                      Args[i].first, DAG.getConstant(1, MVT::i32));
611           SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
612                                      Args[i].first, DAG.getConstant(0, MVT::i32));
613           args_to_use.push_back(Hi);
614           --GPR_remaining;
615           if (GPR_remaining > 0) {
616             args_to_use.push_back(Lo);
617             --GPR_remaining;
618           } else {
619             SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
620             PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
621             MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
622                                          Lo, PtrOff, DAG.getSrcValue(NULL)));
623           }
624         } else {
625           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
626                                        Args[i].first, PtrOff,
627                                        DAG.getSrcValue(NULL)));
628         }
629         ArgOffset += 8;
630         break;
631       case MVT::f32:
632       case MVT::f64:
633         if (FPR_remaining > 0) {
634           args_to_use.push_back(Args[i].first);
635           --FPR_remaining;
636           if (isVarArg) {
637             SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
638                                           Args[i].first, PtrOff,
639                                           DAG.getSrcValue(NULL));
640             MemOps.push_back(Store);
641             // Float varargs are always shadowed in available integer registers
642             if (GPR_remaining > 0) {
643               SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
644                                            DAG.getSrcValue(NULL));
645               MemOps.push_back(Load);
646               args_to_use.push_back(Load);
647               --GPR_remaining;
648             }
649             if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
650               SDOperand ConstFour = DAG.getConstant(4, getPointerTy());
651               PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour);
652               SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff,
653                                            DAG.getSrcValue(NULL));
654               MemOps.push_back(Load);
655               args_to_use.push_back(Load);
656               --GPR_remaining;
657             }
658           } else {
659             // If we have any FPRs remaining, we may also have GPRs remaining.
660             // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
661             // GPRs.
662             if (GPR_remaining > 0) {
663               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
664               --GPR_remaining;
665             }
666             if (GPR_remaining > 0 && MVT::f64 == ArgVT) {
667               args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
668               --GPR_remaining;
669             }
670           }
671         } else {
672           MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
673                                        Args[i].first, PtrOff,
674                                        DAG.getSrcValue(NULL)));
675         }
676         ArgOffset += (ArgVT == MVT::f32) ? 4 : 8;
677         break;
678       }
679     }
680     if (!MemOps.empty())
681       Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps);
682   }
683   
684   std::vector<MVT::ValueType> RetVals;
685   MVT::ValueType RetTyVT = getValueType(RetTy);
686   MVT::ValueType ActualRetTyVT = RetTyVT;
687   if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
688     ActualRetTyVT = MVT::i32;   // Promote result to i32.
689     
690   if (RetTyVT != MVT::isVoid)
691     RetVals.push_back(ActualRetTyVT);
692   RetVals.push_back(MVT::Other);
693   
694   // If the callee is a GlobalAddress node (quite common, every direct call is)
695   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
696   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
697     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
698   
699   SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
700                                             Chain, Callee, args_to_use), 0);
701   Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
702   Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
703                       DAG.getConstant(NumBytes, getPointerTy()));
704   SDOperand RetVal = TheCall;
705   
706   // If the result is a small value, add a note so that we keep track of the
707   // information about whether it is sign or zero extended.
708   if (RetTyVT != ActualRetTyVT) {
709     RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext,
710                          MVT::i32, RetVal, DAG.getValueType(RetTyVT));
711     RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
712   }
713   
714   return std::make_pair(RetVal, Chain);
715 }
716
717 SDOperand PPCTargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
718                                            SelectionDAG &DAG) {
719   if (Op.getValueType() == MVT::i64) {
720     SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op, 
721                                DAG.getConstant(1, MVT::i32));
722     SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
723                                DAG.getConstant(0, MVT::i32));
724     return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
725   } else {
726     return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
727   }
728 }
729
730 SDOperand PPCTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
731                                           Value *VAListV, SelectionDAG &DAG) {
732   // vastart just stores the address of the VarArgsFrameIndex slot into the
733   // memory location argument.
734   SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
735   return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
736                      DAG.getSrcValue(VAListV));
737 }
738
739 std::pair<SDOperand,SDOperand>
740 PPCTargetLowering::LowerVAArg(SDOperand Chain,
741                               SDOperand VAListP, Value *VAListV,
742                               const Type *ArgTy, SelectionDAG &DAG) {
743   MVT::ValueType ArgVT = getValueType(ArgTy);
744   
745   SDOperand VAList =
746     DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV));
747   SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL));
748   unsigned Amt;
749   if (ArgVT == MVT::i32 || ArgVT == MVT::f32)
750     Amt = 4;
751   else {
752     assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
753            "Other types should have been promoted for varargs!");
754     Amt = 8;
755   }
756   VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList,
757                        DAG.getConstant(Amt, VAList.getValueType()));
758   Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
759                       VAList, VAListP, DAG.getSrcValue(VAListV));
760   return std::make_pair(Result, Chain);
761 }
762
763
764 std::pair<SDOperand, SDOperand> PPCTargetLowering::
765 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
766                         SelectionDAG &DAG) {
767   assert(0 && "LowerFrameReturnAddress unimplemented");
768   abort();
769 }
770
771 MachineBasicBlock *
772 PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
773                                            MachineBasicBlock *BB) {
774   assert((MI->getOpcode() == PPC::SELECT_CC_Int ||
775           MI->getOpcode() == PPC::SELECT_CC_F4 ||
776           MI->getOpcode() == PPC::SELECT_CC_F8) &&
777          "Unexpected instr type to insert");
778   
779   // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
780   // control-flow pattern.  The incoming instruction knows the destination vreg
781   // to set, the condition code register to branch on, the true/false values to
782   // select between, and a branch opcode to use.
783   const BasicBlock *LLVM_BB = BB->getBasicBlock();
784   ilist<MachineBasicBlock>::iterator It = BB;
785   ++It;
786   
787   //  thisMBB:
788   //  ...
789   //   TrueVal = ...
790   //   cmpTY ccX, r1, r2
791   //   bCC copy1MBB
792   //   fallthrough --> copy0MBB
793   MachineBasicBlock *thisMBB = BB;
794   MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
795   MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
796   BuildMI(BB, MI->getOperand(4).getImmedValue(), 2)
797     .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
798   MachineFunction *F = BB->getParent();
799   F->getBasicBlockList().insert(It, copy0MBB);
800   F->getBasicBlockList().insert(It, sinkMBB);
801   // Update machine-CFG edges
802   BB->addSuccessor(copy0MBB);
803   BB->addSuccessor(sinkMBB);
804   
805   //  copy0MBB:
806   //   %FalseValue = ...
807   //   # fallthrough to sinkMBB
808   BB = copy0MBB;
809   
810   // Update machine-CFG edges
811   BB->addSuccessor(sinkMBB);
812   
813   //  sinkMBB:
814   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
815   //  ...
816   BB = sinkMBB;
817   BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg())
818     .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
819     .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
820
821   delete MI;   // The pseudo instruction is gone now.
822   return BB;
823 }
824