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