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