remove dead code
[oota-llvm.git] / lib / Target / Sparc / SparcISelDAGToDAG.cpp
1 //===-- SparcV8ISelDAGToDAG.cpp - A dag to dag inst selector for SparcV8 --===//
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 defines an instruction selector for the V8 target
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SparcV8.h"
15 #include "SparcV8TargetMachine.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/SelectionDAG.h"
22 #include "llvm/CodeGen/SelectionDAGISel.h"
23 #include "llvm/CodeGen/SSARegMap.h"
24 #include "llvm/Target/TargetLowering.h"
25 #include "llvm/Support/Debug.h"
26 #include <iostream>
27 using namespace llvm;
28
29 //===----------------------------------------------------------------------===//
30 // TargetLowering Implementation
31 //===----------------------------------------------------------------------===//
32
33 namespace V8ISD {
34   enum {
35     FIRST_NUMBER = ISD::BUILTIN_OP_END+V8::INSTRUCTION_LIST_END,
36     CMPICC,   // Compare two GPR operands, set icc.
37     CMPFCC,   // Compare two FP operands, set fcc.
38     BRICC,    // Branch to dest on icc condition
39     BRFCC,    // Branch to dest on fcc condition
40     
41     Hi, Lo,   // Hi/Lo operations, typically on a global address.
42     
43     FTOI,     // FP to Int within a FP register.
44     ITOF,     // Int to FP within a FP register.
45     
46     SELECT_ICC, // Select between two values using the current ICC flags.
47     SELECT_FCC, // Select between two values using the current FCC flags.
48     
49     RET_FLAG,   // Return with a flag operand.
50   };
51 }
52
53 namespace {
54   class SparcV8TargetLowering : public TargetLowering {
55     int VarArgsFrameOffset;   // Frame offset to start of varargs area.
56   public:
57     SparcV8TargetLowering(TargetMachine &TM);
58     virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
59     virtual std::vector<SDOperand>
60       LowerArguments(Function &F, SelectionDAG &DAG);
61     virtual std::pair<SDOperand, SDOperand>
62       LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
63                   unsigned CC,
64                   bool isTailCall, SDOperand Callee, ArgListTy &Args,
65                   SelectionDAG &DAG);
66     
67     virtual SDOperand LowerReturnTo(SDOperand Chain, SDOperand Op,
68                                     SelectionDAG &DAG);
69     virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
70                                    Value *VAListV, SelectionDAG &DAG);
71     virtual std::pair<SDOperand,SDOperand>
72       LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
73                  const Type *ArgTy, SelectionDAG &DAG);
74     virtual std::pair<SDOperand, SDOperand>
75       LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
76                               SelectionDAG &DAG);
77     virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
78                                                        MachineBasicBlock *MBB);
79   };
80 }
81
82 SparcV8TargetLowering::SparcV8TargetLowering(TargetMachine &TM)
83   : TargetLowering(TM) {
84   
85   // Set up the register classes.
86   addRegisterClass(MVT::i32, V8::IntRegsRegisterClass);
87   addRegisterClass(MVT::f32, V8::FPRegsRegisterClass);
88   addRegisterClass(MVT::f64, V8::DFPRegsRegisterClass);
89
90   // Custom legalize GlobalAddress nodes into LO/HI parts.
91   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
92   setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
93   
94   // Sparc doesn't have sext_inreg, replace them with shl/sra
95   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
96   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
97   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
98
99   // Sparc has no REM operation.
100   setOperationAction(ISD::UREM, MVT::i32, Expand);
101   setOperationAction(ISD::SREM, MVT::i32, Expand);
102
103   // Custom expand fp<->sint
104   setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
105   setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
106
107   // Expand fp<->uint
108   setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
109   setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
110   
111   // Turn FP extload into load/fextend
112   setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
113   
114   // Sparc has no select or setcc: expand to SELECT_CC.
115   setOperationAction(ISD::SELECT, MVT::i32, Expand);
116   setOperationAction(ISD::SELECT, MVT::f32, Expand);
117   setOperationAction(ISD::SELECT, MVT::f64, Expand);
118   setOperationAction(ISD::SETCC, MVT::i32, Expand);
119   setOperationAction(ISD::SETCC, MVT::f32, Expand);
120   setOperationAction(ISD::SETCC, MVT::f64, Expand);
121   
122   // Sparc doesn't have BRCOND either, it has BR_CC.
123   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
124   setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
125   setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
126   setOperationAction(ISD::BR_CC, MVT::i32, Custom);
127   setOperationAction(ISD::BR_CC, MVT::f32, Custom);
128   setOperationAction(ISD::BR_CC, MVT::f64, Custom);
129   
130   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
131   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
132   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
133   
134   // V8 has no intrinsics for these particular operations.
135   setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
136   setOperationAction(ISD::MEMSET, MVT::Other, Expand);
137   setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
138   
139   setOperationAction(ISD::FSIN , MVT::f64, Expand);
140   setOperationAction(ISD::FCOS , MVT::f64, Expand);
141   setOperationAction(ISD::FSIN , MVT::f32, Expand);
142   setOperationAction(ISD::FCOS , MVT::f32, Expand);
143   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
144   setOperationAction(ISD::CTTZ , MVT::i32, Expand);
145   setOperationAction(ISD::CTLZ , MVT::i32, Expand);
146
147   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
148   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
149   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
150   computeRegisterProperties();
151 }
152
153 /// LowerArguments - V8 uses a very simple ABI, where all values are passed in
154 /// either one or two GPRs, including FP values.  TODO: we should pass FP values
155 /// in FP registers for fastcc functions.
156 std::vector<SDOperand>
157 SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
158   MachineFunction &MF = DAG.getMachineFunction();
159   SSARegMap *RegMap = MF.getSSARegMap();
160   std::vector<SDOperand> ArgValues;
161   
162   static const unsigned ArgRegs[] = {
163     V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
164   };
165   
166   const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
167   unsigned ArgOffset = 68;
168   
169   SDOperand Root = DAG.getRoot();
170   std::vector<SDOperand> OutChains;
171
172   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
173     MVT::ValueType ObjectVT = getValueType(I->getType());
174     
175     switch (ObjectVT) {
176     default: assert(0 && "Unhandled argument type!");
177     case MVT::i1:
178     case MVT::i8:
179     case MVT::i16:
180     case MVT::i32:
181       if (I->use_empty()) {                // Argument is dead.
182         if (CurArgReg < ArgRegEnd) ++CurArgReg;
183         ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
184       } else if (CurArgReg < ArgRegEnd) {  // Lives in an incoming GPR
185         unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
186         MF.addLiveIn(*CurArgReg++, VReg);
187         SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
188         if (ObjectVT != MVT::i32) {
189           unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext 
190                                                        : ISD::AssertZext;
191           Arg = DAG.getNode(AssertOp, MVT::i32, Arg, 
192                             DAG.getValueType(ObjectVT));
193           Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
194         }
195         ArgValues.push_back(Arg);
196       } else {
197         int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
198         SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
199         SDOperand Load;
200         if (ObjectVT == MVT::i32) {
201           Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
202         } else {
203           unsigned LoadOp =
204             I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
205
206           Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
207                                 DAG.getSrcValue(0), ObjectVT);
208         }
209         ArgValues.push_back(Load);
210       }
211       
212       ArgOffset += 4;
213       break;
214     case MVT::f32:
215       if (I->use_empty()) {                // Argument is dead.
216         if (CurArgReg < ArgRegEnd) ++CurArgReg;
217         ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
218       } else if (CurArgReg < ArgRegEnd) {  // Lives in an incoming GPR
219         // FP value is passed in an integer register.
220         unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
221         MF.addLiveIn(*CurArgReg++, VReg);
222         SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
223
224         // We use the stack space that is already reserved for this reg.
225         int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
226         SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
227
228         SDOperand SV = DAG.getSrcValue(0);
229         SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Root,
230                                       Arg, FIPtr, SV);
231         ArgValues.push_back(DAG.getLoad(MVT::f32, Store, FIPtr, SV));
232       }
233       ArgOffset += 4;
234       break;
235
236     case MVT::i64:
237     case MVT::f64:
238       if (I->use_empty()) {                // Argument is dead.
239         if (CurArgReg < ArgRegEnd) ++CurArgReg;
240         if (CurArgReg < ArgRegEnd) ++CurArgReg;
241         ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
242       } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
243                  ((CurArgReg-ArgRegs) & 1) == 0) {
244         // If this is a double argument and the whole thing lives on the stack,
245         // and the argument is aligned, load the double straight from the stack.
246         // We can't do a load in cases like void foo([6ints], int,double),
247         // because the double wouldn't be aligned!
248         int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
249         SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
250         ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr, 
251                                         DAG.getSrcValue(0)));
252       } else {
253         SDOperand HiVal;
254         if (CurArgReg < ArgRegEnd) {  // Lives in an incoming GPR
255           unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
256           MF.addLiveIn(*CurArgReg++, VRegHi);
257           HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
258         } else {
259           int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
260           SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
261           HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
262         }
263         
264         SDOperand LoVal;
265         if (CurArgReg < ArgRegEnd) {  // Lives in an incoming GPR
266           unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
267           MF.addLiveIn(*CurArgReg++, VRegLo);
268           LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
269         } else {
270           int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
271           SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
272           LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
273         }
274         
275         // Compose the two halves together into an i64 unit.
276         SDOperand WholeValue = 
277           DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
278                       
279         if (ObjectVT == MVT::i64) {
280           // If we are emitting an i64, this is what we want.
281           ArgValues.push_back(WholeValue);
282         } else {
283           assert(ObjectVT == MVT::f64);
284           // Otherwise, emit a store to the stack and reload into FPR.
285           int FrameIdx = MF.getFrameInfo()->CreateStackObject(8, 8);
286           SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
287           SDOperand SV = DAG.getSrcValue(0);
288           SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Root,
289                                         WholeValue, FIPtr, SV);
290           ArgValues.push_back(DAG.getLoad(MVT::f64, Store, FIPtr, SV));
291         }
292       }
293       ArgOffset += 8;
294       break;
295     }
296   }
297   
298   // Store remaining ArgRegs to the stack if this is a varargs function.
299   if (F.getFunctionType()->isVarArg()) {
300     // Remember the vararg offset for the va_start implementation.
301     VarArgsFrameOffset = ArgOffset;
302     
303     for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
304       unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
305       MF.addLiveIn(*CurArgReg, VReg);
306       SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
307
308       int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
309       SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
310
311       OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
312                                       Arg, FIPtr, DAG.getSrcValue(0)));
313       ArgOffset += 4;
314     }
315   }
316   
317   if (!OutChains.empty())
318     DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
319   
320   // Finally, inform the code generator which regs we return values in.
321   switch (getValueType(F.getReturnType())) {
322   default: assert(0 && "Unknown type!");
323   case MVT::isVoid: break;
324   case MVT::i1:
325   case MVT::i8:
326   case MVT::i16:
327   case MVT::i32:
328     MF.addLiveOut(V8::I0);
329     break;
330   case MVT::i64:
331     MF.addLiveOut(V8::I0);
332     MF.addLiveOut(V8::I1);
333     break;
334   case MVT::f32:
335     MF.addLiveOut(V8::F0);
336     break;
337   case MVT::f64:
338     MF.addLiveOut(V8::D0);
339     break;
340   }
341   
342   return ArgValues;
343 }
344
345 std::pair<SDOperand, SDOperand>
346 SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
347                                    bool isVarArg, unsigned CC,
348                                    bool isTailCall, SDOperand Callee, 
349                                    ArgListTy &Args, SelectionDAG &DAG) {
350   MachineFunction &MF = DAG.getMachineFunction();
351   // Count the size of the outgoing arguments.
352   unsigned ArgsSize = 0;
353   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
354     switch (getValueType(Args[i].second)) {
355     default: assert(0 && "Unknown value type!");
356     case MVT::i1:
357     case MVT::i8:
358     case MVT::i16:
359     case MVT::i32:
360     case MVT::f32:
361       ArgsSize += 4;
362       break;
363     case MVT::i64:
364     case MVT::f64:
365       ArgsSize += 8;
366       break;
367     }
368   }
369   if (ArgsSize > 4*6)
370     ArgsSize -= 4*6;    // Space for first 6 arguments is prereserved.
371   else
372     ArgsSize = 0;
373
374   // Keep stack frames 8-byte aligned.
375   ArgsSize = (ArgsSize+7) & ~7;
376
377   Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
378                       DAG.getConstant(ArgsSize, getPointerTy()));
379   
380   SDOperand StackPtr, NullSV;
381   std::vector<SDOperand> Stores;
382   std::vector<SDOperand> RegValuesToPass;
383   unsigned ArgOffset = 68;
384   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
385     SDOperand Val = Args[i].first;
386     MVT::ValueType ObjectVT = Val.getValueType();
387     SDOperand ValToStore;
388     unsigned ObjSize;
389     switch (ObjectVT) {
390     default: assert(0 && "Unhandled argument type!");
391     case MVT::i1:
392     case MVT::i8:
393     case MVT::i16:
394       // Promote the integer to 32-bits.  If the input type is signed, use a
395       // sign extend, otherwise use a zero extend.
396       if (Args[i].second->isSigned())
397         Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
398       else
399         Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
400       // FALL THROUGH
401     case MVT::i32:
402       ObjSize = 4;
403
404       if (RegValuesToPass.size() >= 6) {
405         ValToStore = Val;
406       } else {
407         RegValuesToPass.push_back(Val);
408       }
409       break;
410     case MVT::f32:
411       ObjSize = 4;
412       if (RegValuesToPass.size() >= 6) {
413         ValToStore = Val;
414       } else {
415         // Convert this to a FP value in an int reg.
416         int FrameIdx = MF.getFrameInfo()->CreateStackObject(4, 4);
417         SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
418         SDOperand SV = DAG.getSrcValue(0);
419         SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
420                                       Val, FIPtr, SV);
421         Val = DAG.getLoad(MVT::i32, Store, FIPtr, SV);
422         RegValuesToPass.push_back(Val);
423       }
424       break;
425     case MVT::f64: {
426       ObjSize = 8;
427       // If we can store this directly into the outgoing slot, do so.  We can
428       // do this when all ArgRegs are used and if the outgoing slot is aligned.
429       if (RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
430         ValToStore = Val;
431         break;
432       }
433       
434       // Otherwise, convert this to a FP value in int regs.
435       int FrameIdx = MF.getFrameInfo()->CreateStackObject(8, 8);
436       SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
437       SDOperand SV = DAG.getSrcValue(0);
438       SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
439                                     Val, FIPtr, SV);
440       Val = DAG.getLoad(MVT::i64, Store, FIPtr, SV);
441     }
442       // FALL THROUGH
443     case MVT::i64:
444       ObjSize = 8;
445       if (RegValuesToPass.size() >= 6) {
446         ValToStore = Val;    // Whole thing is passed in memory.
447         break;
448       }
449       
450       // Split the value into top and bottom part.  Top part goes in a reg.
451       SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val, 
452                                  DAG.getConstant(1, MVT::i32));
453       SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
454                                  DAG.getConstant(0, MVT::i32));
455       RegValuesToPass.push_back(Hi);
456       
457       if (RegValuesToPass.size() >= 6) {
458         ValToStore = Lo;
459         ArgOffset += 4;
460         ObjSize = 4;
461       } else {
462         RegValuesToPass.push_back(Lo);
463       }
464       break;
465     }
466     
467     if (ValToStore.Val) {
468       if (!StackPtr.Val) {
469         StackPtr = DAG.getRegister(V8::O6, MVT::i32);
470         NullSV = DAG.getSrcValue(NULL);
471       }
472       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
473       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
474       Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
475                                    ValToStore, PtrOff, NullSV));
476     }
477     ArgOffset += ObjSize;
478   }
479   
480   // Emit all stores, make sure the occur before any copies into physregs.
481   if (!Stores.empty())
482     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
483   
484   static const unsigned ArgRegs[] = {
485     V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
486   };
487   
488   // Build a sequence of copy-to-reg nodes chained together with token chain
489   // and flag operands which copy the outgoing args into O[0-5].
490   SDOperand InFlag;
491   for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
492     Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
493     InFlag = Chain.getValue(1);
494   }
495
496   std::vector<MVT::ValueType> RetVals;
497   RetVals.push_back(MVT::Other);
498   RetVals.push_back(MVT::Flag);
499
500   // If the callee is a GlobalAddress node (quite common, every direct call is)
501   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
502   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
503     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
504
505   std::vector<MVT::ValueType> NodeTys;
506   NodeTys.push_back(MVT::Other);   // Returns a chain
507   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
508   if (InFlag.Val)
509     Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee, InFlag), 0);
510   else
511     Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee), 0);
512   InFlag = Chain.getValue(1);
513   
514   MVT::ValueType RetTyVT = getValueType(RetTy);
515   SDOperand RetVal;
516   if (RetTyVT != MVT::isVoid) {
517     switch (RetTyVT) {
518     default: assert(0 && "Unknown value type to return!");
519     case MVT::i1:
520     case MVT::i8:
521     case MVT::i16:
522       RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
523       Chain = RetVal.getValue(1);
524       
525       // Add a note to keep track of whether it is sign or zero extended.
526       RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
527                            MVT::i32, RetVal, DAG.getValueType(RetTyVT));
528       RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
529       break;
530     case MVT::i32:
531       RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
532       Chain = RetVal.getValue(1);
533       break;
534     case MVT::f32:
535       RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
536       Chain = RetVal.getValue(1);
537       break;
538     case MVT::f64:
539       RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
540       Chain = RetVal.getValue(1);
541       break;
542     case MVT::i64:
543       SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
544       SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32, 
545                                         Lo.getValue(2));
546       RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
547       Chain = Hi.getValue(1);
548       break;
549     }
550   }
551   
552   Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
553                       DAG.getConstant(ArgsSize, getPointerTy()));
554   
555   return std::make_pair(RetVal, Chain);
556 }
557
558 SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
559                                                SelectionDAG &DAG) {
560   SDOperand Copy;
561   switch (Op.getValueType()) {
562   default: assert(0 && "Unknown type to return!");
563   case MVT::i32:
564     Copy = DAG.getCopyToReg(Chain, V8::I0, Op, SDOperand());
565     break;
566   case MVT::f32:
567     Copy = DAG.getCopyToReg(Chain, V8::F0, Op, SDOperand());
568     break;
569   case MVT::f64:
570     Copy = DAG.getCopyToReg(Chain, V8::D0, Op, SDOperand());
571     break;
572   case MVT::i64:
573     SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op, 
574                                DAG.getConstant(1, MVT::i32));
575     SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
576                                DAG.getConstant(0, MVT::i32));
577     Copy = DAG.getCopyToReg(Chain, V8::I0, Hi, SDOperand());
578     Copy = DAG.getCopyToReg(Copy, V8::I1, Lo, Copy.getValue(1));
579     break;
580   }
581   return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
582 }
583
584 SDOperand SparcV8TargetLowering::
585 LowerVAStart(SDOperand Chain, SDOperand VAListP, Value *VAListV, 
586              SelectionDAG &DAG) {
587              
588   assert(0 && "Unimp");
589   abort();
590 }
591
592 std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
593 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
594            const Type *ArgTy, SelectionDAG &DAG) {
595   assert(0 && "Unimp");
596   abort();
597 }
598
599 std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
600 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
601                         SelectionDAG &DAG) {
602   assert(0 && "Unimp");
603   abort();
604 }
605
606 SDOperand SparcV8TargetLowering::
607 LowerOperation(SDOperand Op, SelectionDAG &DAG) {
608   switch (Op.getOpcode()) {
609   default: assert(0 && "Should not custom lower this!");
610   case ISD::GlobalAddress: {
611     GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
612     SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
613     SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
614     SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
615     return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
616   }
617   case ISD::ConstantPool: {
618     Constant *C = cast<ConstantPoolSDNode>(Op)->get();
619     SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
620     SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
621     SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
622     return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
623   }
624   case ISD::FP_TO_SINT: {
625     // Convert the fp value to integer in an FP register.
626     Op = DAG.getNode(V8ISD::FTOI, Op.getOperand(0).getValueType(),
627                      Op.getOperand(0));
628     int Size = Op.getOperand(0).getValueType() == MVT::f32 ? 4 : 8;
629     int FrameIdx =
630       DAG.getMachineFunction().getFrameInfo()->CreateStackObject(Size, Size);
631     SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
632     SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
633                                Op, FI, DAG.getSrcValue(0));
634     return DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
635   }
636   case ISD::SINT_TO_FP: {
637     int Size = Op.getOperand(0).getValueType() == MVT::f32 ? 4 : 8;
638     int FrameIdx =
639       DAG.getMachineFunction().getFrameInfo()->CreateStackObject(Size, Size);
640     SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
641     SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
642                                Op.getOperand(0), FI, DAG.getSrcValue(0));
643     
644     Op = DAG.getLoad(Op.getValueType(), ST, FI, DAG.getSrcValue(0));
645     
646     // Convert the int value to FP in an FP register.
647     return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Op);
648   }
649   case ISD::BR_CC: {
650     SDOperand Chain = Op.getOperand(0);
651     SDOperand CC = Op.getOperand(1);
652     SDOperand LHS = Op.getOperand(2);
653     SDOperand RHS = Op.getOperand(3);
654     SDOperand Dest = Op.getOperand(4);
655     
656     // Get the condition flag.
657     if (LHS.getValueType() == MVT::i32) {
658       SDOperand Cond = DAG.getNode(V8ISD::CMPICC, MVT::Flag, LHS, RHS);
659       return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
660     } else {
661       SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
662       return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
663     }
664   }
665   case ISD::SELECT_CC: {
666     SDOperand LHS = Op.getOperand(0);
667     SDOperand RHS = Op.getOperand(1);
668     unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
669     SDOperand TrueVal = Op.getOperand(2);
670     SDOperand FalseVal = Op.getOperand(3);
671     
672     unsigned Opc;
673     Opc = LHS.getValueType() == MVT::i32 ? V8ISD::CMPICC : V8ISD::CMPFCC;
674     SDOperand CompareFlag = DAG.getNode(Opc, MVT::Flag, LHS, RHS);
675     
676     Opc = LHS.getValueType() == MVT::i32 ? 
677       V8ISD::SELECT_ICC : V8ISD::SELECT_FCC;
678     return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal, 
679                        DAG.getConstant(CC, MVT::i32), CompareFlag);
680   }
681   }  
682 }
683
684 MachineBasicBlock *
685 SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
686                                                MachineBasicBlock *BB) {
687   unsigned BROpcode;
688   // Figure out the conditional branch opcode to use for this select_cc.
689   switch (MI->getOpcode()) {
690   default: assert(0 && "Unknown SELECT_CC!");
691   case V8::SELECT_CC_Int_ICC:
692   case V8::SELECT_CC_FP_ICC:
693   case V8::SELECT_CC_DFP_ICC:
694     // Integer compare.
695     switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
696     default: assert(0 && "Unknown integer condition code!");
697     case ISD::SETEQ:  BROpcode = V8::BE; break;
698     case ISD::SETNE:  BROpcode = V8::BNE; break;
699     case ISD::SETLT:  BROpcode = V8::BL; break;
700     case ISD::SETGT:  BROpcode = V8::BG; break;
701     case ISD::SETLE:  BROpcode = V8::BLE; break;
702     case ISD::SETGE:  BROpcode = V8::BGE; break;
703     case ISD::SETULT: BROpcode = V8::BCS; break;
704     case ISD::SETULE: BROpcode = V8::BLEU; break;
705     case ISD::SETUGT: BROpcode = V8::BGU; break;
706     case ISD::SETUGE: BROpcode = V8::BCC; break;
707     }
708     break;
709   case V8::SELECT_CC_Int_FCC:
710   case V8::SELECT_CC_FP_FCC:
711   case V8::SELECT_CC_DFP_FCC:
712     // FP compare.
713     switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
714     default: assert(0 && "Unknown fp condition code!");
715     case ISD::SETEQ:  BROpcode = V8::FBE; break;
716     case ISD::SETNE:  BROpcode = V8::FBNE; break;
717     case ISD::SETLT:  BROpcode = V8::FBL; break;
718     case ISD::SETGT:  BROpcode = V8::FBG; break;
719     case ISD::SETLE:  BROpcode = V8::FBLE; break;
720     case ISD::SETGE:  BROpcode = V8::FBGE; break;
721     case ISD::SETULT: BROpcode = V8::FBUL; break;
722     case ISD::SETULE: BROpcode = V8::FBULE; break;
723     case ISD::SETUGT: BROpcode = V8::FBUG; break;
724     case ISD::SETUGE: BROpcode = V8::FBUGE; break;
725     case ISD::SETUO:  BROpcode = V8::FBU; break;
726     case ISD::SETO:   BROpcode = V8::FBO; break;
727     case ISD::SETONE: BROpcode = V8::FBLG; break;
728     case ISD::SETUEQ: BROpcode = V8::FBUE; break;
729     }
730     break;
731   }
732   
733   // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
734   // control-flow pattern.  The incoming instruction knows the destination vreg
735   // to set, the condition code register to branch on, the true/false values to
736   // select between, and a branch opcode to use.
737   const BasicBlock *LLVM_BB = BB->getBasicBlock();
738   ilist<MachineBasicBlock>::iterator It = BB;
739   ++It;
740   
741   //  thisMBB:
742   //  ...
743   //   TrueVal = ...
744   //   [f]bCC copy1MBB
745   //   fallthrough --> copy0MBB
746   MachineBasicBlock *thisMBB = BB;
747   MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
748   MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
749   BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
750   MachineFunction *F = BB->getParent();
751   F->getBasicBlockList().insert(It, copy0MBB);
752   F->getBasicBlockList().insert(It, sinkMBB);
753   // Update machine-CFG edges
754   BB->addSuccessor(copy0MBB);
755   BB->addSuccessor(sinkMBB);
756   
757   //  copy0MBB:
758   //   %FalseValue = ...
759   //   # fallthrough to sinkMBB
760   BB = copy0MBB;
761   
762   // Update machine-CFG edges
763   BB->addSuccessor(sinkMBB);
764   
765   //  sinkMBB:
766   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
767   //  ...
768   BB = sinkMBB;
769   BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
770     .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
771     .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
772   
773   delete MI;   // The pseudo instruction is gone now.
774   return BB;
775 }
776   
777 //===----------------------------------------------------------------------===//
778 // Instruction Selector Implementation
779 //===----------------------------------------------------------------------===//
780
781 //===--------------------------------------------------------------------===//
782 /// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
783 /// instructions for SelectionDAG operations.
784 ///
785 namespace {
786 class SparcV8DAGToDAGISel : public SelectionDAGISel {
787   SparcV8TargetLowering V8Lowering;
788 public:
789   SparcV8DAGToDAGISel(TargetMachine &TM)
790     : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
791
792   SDOperand Select(SDOperand Op);
793
794   // Complex Pattern Selectors.
795   bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
796   bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
797   
798   /// InstructionSelectBasicBlock - This callback is invoked by
799   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
800   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
801   
802   virtual const char *getPassName() const {
803     return "PowerPC DAG->DAG Pattern Instruction Selection";
804   } 
805   
806   // Include the pieces autogenerated from the target description.
807 #include "SparcV8GenDAGISel.inc"
808 };
809 }  // end anonymous namespace
810
811 /// InstructionSelectBasicBlock - This callback is invoked by
812 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
813 void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
814   DEBUG(BB->dump());
815   
816   // Select target instructions for the DAG.
817   DAG.setRoot(Select(DAG.getRoot()));
818   CodeGenMap.clear();
819   DAG.RemoveDeadNodes();
820   
821   // Emit machine code to BB. 
822   ScheduleAndEmitDAG(DAG);
823 }
824
825 bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
826                                        SDOperand &Offset) {
827   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
828     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
829     Offset = CurDAG->getTargetConstant(0, MVT::i32);
830     return true;
831   }
832   
833   if (Addr.getOpcode() == ISD::ADD) {
834     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
835       if (Predicate_simm13(CN)) {
836         if (FrameIndexSDNode *FIN = 
837                 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
838           // Constant offset from frame ref.
839           Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
840         } else {
841           Base = Select(Addr.getOperand(0));
842         }
843         Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
844         return true;
845       }
846     }
847     if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
848       Base = Select(Addr.getOperand(1));
849       Offset = Addr.getOperand(0).getOperand(0);
850       return true;
851     }
852     if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
853       Base = Select(Addr.getOperand(0));
854       Offset = Addr.getOperand(1).getOperand(0);
855       return true;
856     }
857   }
858   Base = Select(Addr);
859   Offset = CurDAG->getTargetConstant(0, MVT::i32);
860   return true;
861 }
862
863 bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1, 
864                                        SDOperand &R2) {
865   if (Addr.getOpcode() == ISD::FrameIndex) return false; 
866   if (Addr.getOpcode() == ISD::ADD) {
867     if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
868         Predicate_simm13(Addr.getOperand(1).Val))
869       return false;  // Let the reg+imm pattern catch this!
870     if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
871         Addr.getOperand(1).getOpcode() == V8ISD::Lo)
872       return false;  // Let the reg+imm pattern catch this!
873     R1 = Select(Addr.getOperand(0));
874     R2 = Select(Addr.getOperand(1));
875     return true;
876   }
877
878   R1 = Select(Addr);
879   R2 = CurDAG->getRegister(V8::G0, MVT::i32);
880   return true;
881 }
882
883 SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
884   SDNode *N = Op.Val;
885   if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
886       N->getOpcode() < V8ISD::FIRST_NUMBER)
887     return Op;   // Already selected.
888                  // If this has already been converted, use it.
889   std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
890   if (CGMI != CodeGenMap.end()) return CGMI->second;
891   
892   switch (N->getOpcode()) {
893   default: break;
894   case ISD::Register: return Op;
895   case ISD::FrameIndex: {
896     int FI = cast<FrameIndexSDNode>(N)->getIndex();
897     if (N->hasOneUse())
898       return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
899                                   CurDAG->getTargetFrameIndex(FI, MVT::i32),
900                                   CurDAG->getTargetConstant(0, MVT::i32));
901     return CodeGenMap[Op] = 
902       CurDAG->getTargetNode(V8::ADDri, MVT::i32,
903                             CurDAG->getTargetFrameIndex(FI, MVT::i32),
904                             CurDAG->getTargetConstant(0, MVT::i32));
905   }
906   case V8ISD::CMPICC: {
907     // FIXME: Handle compare with immediate.
908     SDOperand LHS = Select(N->getOperand(0));
909     SDOperand RHS = Select(N->getOperand(1));
910     SDOperand Result = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
911                                              LHS, RHS);
912     return CodeGenMap[Op] = Result.getValue(1);
913   }
914   case ISD::ADD_PARTS: {
915     SDOperand LHSL = Select(N->getOperand(0));
916     SDOperand LHSH = Select(N->getOperand(1));
917     SDOperand RHSL = Select(N->getOperand(2));
918     SDOperand RHSH = Select(N->getOperand(3));
919     // FIXME, handle immediate RHS.
920     SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
921                                           LHSL, RHSL);
922     SDOperand Hi  = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH, 
923                                           Low.getValue(1));
924     CodeGenMap[SDOperand(N, 0)] = Low;
925     CodeGenMap[SDOperand(N, 1)] = Hi;
926     return Op.ResNo ? Hi : Low;
927   }
928   case ISD::SUB_PARTS: {
929     SDOperand LHSL = Select(N->getOperand(0));
930     SDOperand LHSH = Select(N->getOperand(1));
931     SDOperand RHSL = Select(N->getOperand(2));
932     SDOperand RHSH = Select(N->getOperand(3));
933     // FIXME, handle immediate RHS.
934     SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
935                                           LHSL, RHSL);
936     SDOperand Hi  = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH, 
937                                           Low.getValue(1));
938     CodeGenMap[SDOperand(N, 0)] = Low;
939     CodeGenMap[SDOperand(N, 1)] = Hi;
940     return Op.ResNo ? Hi : Low;
941   }
942   case ISD::SDIV:
943   case ISD::UDIV: {
944     // FIXME: should use a custom expander to expose the SRA to the dag.
945     SDOperand DivLHS = Select(N->getOperand(0));
946     SDOperand DivRHS = Select(N->getOperand(1));
947     
948     // Set the Y register to the high-part.
949     SDOperand TopPart;
950     if (N->getOpcode() == ISD::SDIV) {
951       TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
952                                       CurDAG->getTargetConstant(31, MVT::i32));
953     } else {
954       TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
955     }
956     TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
957                                     CurDAG->getRegister(V8::G0, MVT::i32));
958
959     // FIXME: Handle div by immediate.
960     unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
961     return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
962   }    
963   case ISD::MULHU:
964   case ISD::MULHS: {
965     // FIXME: Handle mul by immediate.
966     SDOperand MulLHS = Select(N->getOperand(0));
967     SDOperand MulRHS = Select(N->getOperand(1));
968     unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
969     SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
970                                           MulLHS, MulRHS);
971     // The high part is in the Y register.
972     return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
973   }
974   case ISD::CALL:
975     // FIXME: This is a workaround for a bug in tblgen.
976   { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
977     // Emits: (CALL:void (tglobaladdr:i32):$dst)
978     // Pattern complexity = 2  cost = 1
979     SDOperand N1 = N->getOperand(1);
980     if (N1.getOpcode() != ISD::TargetGlobalAddress &&
981         N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
982     SDOperand InFlag = SDOperand(0, 0);
983     SDOperand Chain = N->getOperand(0);
984     SDOperand Tmp0 = N1;
985     Chain = Select(Chain);
986     SDOperand Result;
987     if (N->getNumOperands() == 3) {
988       InFlag = Select(N->getOperand(2));
989       Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0, 
990                                      Chain, InFlag);
991     } else {
992       Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0, 
993                                      Chain);
994     }
995     Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
996      CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
997     return Result.getValue(Op.ResNo);
998   }
999     P47Fail:;
1000     
1001   }
1002   
1003   return SelectCode(Op);
1004 }
1005
1006
1007 /// createPPCISelDag - This pass converts a legalized DAG into a 
1008 /// PowerPC-specific DAG, ready for instruction scheduling.
1009 ///
1010 FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
1011   return new SparcV8DAGToDAGISel(TM);
1012 }