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