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