Give V8 select_cc, in the spirit of the PPC backend
[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/Function.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/CodeGen/SelectionDAGISel.h"
22 #include "llvm/CodeGen/SSARegMap.h"
23 #include "llvm/Target/TargetLowering.h"
24 #include "llvm/Support/Debug.h"
25 #include <iostream>
26 using namespace llvm;
27
28 //===----------------------------------------------------------------------===//
29 // TargetLowering Implementation
30 //===----------------------------------------------------------------------===//
31
32 namespace V8ISD {
33   enum {
34     FIRST_NUMBER = ISD::BUILTIN_OP_END+V8::INSTRUCTION_LIST_END,
35     CMPICC,   // Compare two GPR operands, set icc.
36     CMPFCC,   // Compare two FP operands, set fcc.
37     BRICC,    // Branch to dest on icc condition
38     BRFCC,    // Branch to dest on fcc condition
39     
40     Hi, Lo,   // Hi/Lo operations, typically on a global address.
41     
42     FTOI,     // FP to Int within a FP register.
43     ITOF,     // Int to FP within a FP register.
44     
45     SELECT_ICC, // Select between two values using the current ICC flags.
46     SELECT_FCC, // Select between two values using the current FCC flags.
47   };
48 }
49
50 namespace {
51   class SparcV8TargetLowering : public TargetLowering {
52   public:
53     SparcV8TargetLowering(TargetMachine &TM);
54     virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
55     virtual std::vector<SDOperand>
56       LowerArguments(Function &F, SelectionDAG &DAG);
57     virtual std::pair<SDOperand, SDOperand>
58       LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
59                   unsigned CC,
60                   bool isTailCall, SDOperand Callee, ArgListTy &Args,
61                   SelectionDAG &DAG);
62     
63     virtual SDOperand LowerReturnTo(SDOperand Chain, SDOperand Op,
64                                     SelectionDAG &DAG);
65     virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
66                                    Value *VAListV, SelectionDAG &DAG);
67     virtual std::pair<SDOperand,SDOperand>
68       LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
69                  const Type *ArgTy, SelectionDAG &DAG);
70     virtual std::pair<SDOperand, SDOperand>
71       LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
72                               SelectionDAG &DAG);
73     virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
74                                                        MachineBasicBlock *MBB);
75   };
76 }
77
78 SparcV8TargetLowering::SparcV8TargetLowering(TargetMachine &TM)
79   : TargetLowering(TM) {
80   
81   // Set up the register classes.
82   addRegisterClass(MVT::i32, V8::IntRegsRegisterClass);
83   addRegisterClass(MVT::f32, V8::FPRegsRegisterClass);
84   addRegisterClass(MVT::f64, V8::DFPRegsRegisterClass);
85
86   // Custom legalize GlobalAddress nodes into LO/HI parts.
87   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
88   setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
89   
90   // Sparc doesn't have sext_inreg, replace them with shl/sra
91   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
92   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
93   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
94
95   // Sparc has no REM operation.
96   setOperationAction(ISD::UREM, MVT::i32, Expand);
97   setOperationAction(ISD::SREM, MVT::i32, Expand);
98
99   // Custom expand fp<->sint
100   setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
101   setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
102
103   // Expand fp<->uint
104   setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
105   setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
106   
107   setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
108   
109   // Sparc has no select or setcc: expand to SELECT_CC.
110   setOperationAction(ISD::SELECT, MVT::i32, Expand);
111   setOperationAction(ISD::SELECT, MVT::f32, Expand);
112   setOperationAction(ISD::SELECT, MVT::f64, Expand);
113   setOperationAction(ISD::SETCC, MVT::i32, Expand);
114   setOperationAction(ISD::SETCC, MVT::f32, Expand);
115   setOperationAction(ISD::SETCC, MVT::f64, Expand);
116   
117   // Sparc doesn't have BRCOND either, it has BR_CC.
118   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
119   setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
120   setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
121   setOperationAction(ISD::BR_CC, MVT::i32, Custom);
122   setOperationAction(ISD::BR_CC, MVT::f32, Custom);
123   setOperationAction(ISD::BR_CC, MVT::f64, Custom);
124   
125   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
126   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
127   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
128   
129   computeRegisterProperties();
130 }
131
132 std::vector<SDOperand>
133 SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
134   MachineFunction &MF = DAG.getMachineFunction();
135   SSARegMap *RegMap = MF.getSSARegMap();
136   std::vector<SDOperand> ArgValues;
137   
138   static const unsigned GPR[] = {
139     V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
140   };
141   unsigned ArgNo = 0;
142   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
143     MVT::ValueType ObjectVT = getValueType(I->getType());
144     assert(ArgNo < 6 && "Only args in regs for now");
145     
146     switch (ObjectVT) {
147     default: assert(0 && "Unhandled argument type!");
148     // TODO: MVT::i64 & FP
149     case MVT::i1:
150     case MVT::i8:
151     case MVT::i16:
152     case MVT::i32: {
153       unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
154       MF.addLiveIn(GPR[ArgNo++], VReg);
155       SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
156       DAG.setRoot(Arg.getValue(1));
157       if (ObjectVT != MVT::i32) {
158         unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext 
159                                                      : ISD::AssertZext;
160         Arg = DAG.getNode(AssertOp, MVT::i32, Arg, 
161                           DAG.getValueType(ObjectVT));
162         Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
163       }
164       ArgValues.push_back(Arg);
165       break;
166     }
167     case MVT::i64: {
168       unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
169       MF.addLiveIn(GPR[ArgNo++], VRegHi);
170       unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
171       MF.addLiveIn(GPR[ArgNo++], VRegLo);
172       SDOperand ArgLo = DAG.getCopyFromReg(DAG.getRoot(), VRegLo, MVT::i32);
173       SDOperand ArgHi = DAG.getCopyFromReg(ArgLo.getValue(1), VRegHi, MVT::i32);
174       DAG.setRoot(ArgHi.getValue(1));
175       ArgValues.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgLo, ArgHi));
176       break;
177     }
178     }
179   }
180   
181   assert(!F.isVarArg() && "Unimp");
182   
183   // Finally, inform the code generator which regs we return values in.
184   switch (getValueType(F.getReturnType())) {
185   default: assert(0 && "Unknown type!");
186   case MVT::isVoid: break;
187   case MVT::i1:
188   case MVT::i8:
189   case MVT::i16:
190   case MVT::i32:
191     MF.addLiveOut(V8::I0);
192     break;
193   case MVT::i64:
194     MF.addLiveOut(V8::I0);
195     MF.addLiveOut(V8::I1);
196     break;
197   case MVT::f32:
198     MF.addLiveOut(V8::F0);
199     break;
200   case MVT::f64:
201     MF.addLiveOut(V8::D0);
202     break;
203   }
204   
205   return ArgValues;
206 }
207
208 std::pair<SDOperand, SDOperand>
209 SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
210                                    bool isVarArg, unsigned CC,
211                                    bool isTailCall, SDOperand Callee, 
212                                    ArgListTy &Args, SelectionDAG &DAG) {
213   assert(0 && "Unimp");
214   abort();
215 }
216
217 SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
218                                                SelectionDAG &DAG) {
219   if (Op.getValueType() == MVT::i64) {
220     SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op, 
221                                DAG.getConstant(1, MVT::i32));
222     SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
223                                DAG.getConstant(0, MVT::i32));
224     return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
225   } else {
226     return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
227   }
228 }
229
230 SDOperand SparcV8TargetLowering::
231 LowerVAStart(SDOperand Chain, SDOperand VAListP, Value *VAListV, 
232              SelectionDAG &DAG) {
233              
234   assert(0 && "Unimp");
235   abort();
236 }
237
238 std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
239 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
240            const Type *ArgTy, SelectionDAG &DAG) {
241   assert(0 && "Unimp");
242   abort();
243 }
244
245 std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
246 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
247                         SelectionDAG &DAG) {
248   assert(0 && "Unimp");
249   abort();
250 }
251
252 SDOperand SparcV8TargetLowering::
253 LowerOperation(SDOperand Op, SelectionDAG &DAG) {
254   switch (Op.getOpcode()) {
255   default: assert(0 && "Should not custom lower this!");
256   case ISD::GlobalAddress: {
257     GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
258     SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
259     SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
260     SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
261     return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
262   }
263   case ISD::ConstantPool: {
264     Constant *C = cast<ConstantPoolSDNode>(Op)->get();
265     SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
266     SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
267     SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
268     return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
269   }
270   case ISD::FP_TO_SINT: {
271     // Convert the fp value to integer in an FP register.
272     Op = DAG.getNode(V8ISD::FTOI, Op.getOperand(0).getValueType(),
273                      Op.getOperand(0));
274     int Size = Op.getOperand(0).getValueType() == MVT::f32 ? 4 : 8;
275     int FrameIdx =
276       DAG.getMachineFunction().getFrameInfo()->CreateStackObject(Size, Size);
277     SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
278     SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
279                                Op, FI, DAG.getSrcValue(0));
280     return DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
281   }
282   case ISD::SINT_TO_FP: {
283     int Size = Op.getOperand(0).getValueType() == MVT::f32 ? 4 : 8;
284     int FrameIdx =
285       DAG.getMachineFunction().getFrameInfo()->CreateStackObject(Size, Size);
286     SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
287     SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
288                                Op.getOperand(0), FI, DAG.getSrcValue(0));
289     
290     Op = DAG.getLoad(Op.getValueType(), ST, FI, DAG.getSrcValue(0));
291     
292     // Convert the int value to FP in an FP register.
293     return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Op);
294   }
295   case ISD::BR_CC: {
296     SDOperand Chain = Op.getOperand(0);
297     SDOperand CC = Op.getOperand(1);
298     SDOperand LHS = Op.getOperand(2);
299     SDOperand RHS = Op.getOperand(3);
300     SDOperand Dest = Op.getOperand(4);
301     
302     // Get the condition flag.
303     if (LHS.getValueType() == MVT::i32) {
304       SDOperand Cond = DAG.getNode(V8ISD::CMPICC, MVT::Flag, LHS, RHS);
305       return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
306     } else {
307       SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
308       return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
309     }
310   }
311   case ISD::SELECT_CC: {
312     SDOperand LHS = Op.getOperand(0);
313     SDOperand RHS = Op.getOperand(1);
314     unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
315     SDOperand TrueVal = Op.getOperand(2);
316     SDOperand FalseVal = Op.getOperand(3);
317     
318     unsigned Opc;
319     Opc = LHS.getValueType() == MVT::i32 ? V8ISD::CMPICC : V8ISD::CMPFCC;
320     SDOperand CompareFlag = DAG.getNode(Opc, MVT::Flag, LHS, RHS);
321     
322     Opc = LHS.getValueType() == MVT::i32 ? 
323       V8ISD::SELECT_ICC : V8ISD::SELECT_FCC;
324     return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal, 
325                        DAG.getConstant(CC, MVT::i32), CompareFlag);
326   }
327   }  
328 }
329
330 MachineBasicBlock *
331 SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
332                                                MachineBasicBlock *BB) {
333   unsigned BROpcode;
334   // Figure out the conditional branch opcode to use for this select_cc.
335   switch (MI->getOpcode()) {
336   default: assert(0 && "Unknown SELECT_CC!");
337   case V8::SELECT_CC_Int_ICC:
338   case V8::SELECT_CC_FP_ICC:
339   case V8::SELECT_CC_DFP_ICC:
340     // Integer compare.
341     switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
342     default: assert(0 && "Unknown integer condition code!");
343     case ISD::SETEQ:  BROpcode = V8::BE; break;
344     case ISD::SETNE:  BROpcode = V8::BNE; break;
345     case ISD::SETLT:  BROpcode = V8::BL; break;
346     case ISD::SETGT:  BROpcode = V8::BG; break;
347     case ISD::SETLE:  BROpcode = V8::BLE; break;
348     case ISD::SETGE:  BROpcode = V8::BGE; break;
349     case ISD::SETULT: BROpcode = V8::BCS; break;
350     case ISD::SETULE: BROpcode = V8::BLEU; break;
351     case ISD::SETUGT: BROpcode = V8::BGU; break;
352     case ISD::SETUGE: BROpcode = V8::BCC; break;
353     }
354     break;
355   case V8::SELECT_CC_Int_FCC:
356   case V8::SELECT_CC_FP_FCC:
357   case V8::SELECT_CC_DFP_FCC:
358     // FP compare.
359     switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
360     default: assert(0 && "Unknown fp condition code!");
361     case ISD::SETEQ:  BROpcode = V8::FBE; break;
362     case ISD::SETNE:  BROpcode = V8::FBNE; break;
363     case ISD::SETLT:  BROpcode = V8::FBL; break;
364     case ISD::SETGT:  BROpcode = V8::FBG; break;
365     case ISD::SETLE:  BROpcode = V8::FBLE; break;
366     case ISD::SETGE:  BROpcode = V8::FBGE; break;
367     case ISD::SETULT: BROpcode = V8::FBUL; break;
368     case ISD::SETULE: BROpcode = V8::FBULE; break;
369     case ISD::SETUGT: BROpcode = V8::FBUG; break;
370     case ISD::SETUGE: BROpcode = V8::FBUGE; break;
371     case ISD::SETUO:  BROpcode = V8::FBU; break;
372     case ISD::SETO:   BROpcode = V8::FBO; break;
373     case ISD::SETONE: BROpcode = V8::FBLG; break;
374     case ISD::SETUEQ: BROpcode = V8::FBUE; break;
375     }
376     break;
377   }
378   
379   // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
380   // control-flow pattern.  The incoming instruction knows the destination vreg
381   // to set, the condition code register to branch on, the true/false values to
382   // select between, and a branch opcode to use.
383   const BasicBlock *LLVM_BB = BB->getBasicBlock();
384   ilist<MachineBasicBlock>::iterator It = BB;
385   ++It;
386   
387   //  thisMBB:
388   //  ...
389   //   TrueVal = ...
390   //   [f]bCC copy1MBB
391   //   fallthrough --> copy0MBB
392   MachineBasicBlock *thisMBB = BB;
393   MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
394   MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
395   BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
396   MachineFunction *F = BB->getParent();
397   F->getBasicBlockList().insert(It, copy0MBB);
398   F->getBasicBlockList().insert(It, sinkMBB);
399   // Update machine-CFG edges
400   BB->addSuccessor(copy0MBB);
401   BB->addSuccessor(sinkMBB);
402   
403   //  copy0MBB:
404   //   %FalseValue = ...
405   //   # fallthrough to sinkMBB
406   BB = copy0MBB;
407   
408   // Update machine-CFG edges
409   BB->addSuccessor(sinkMBB);
410   
411   //  sinkMBB:
412   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
413   //  ...
414   BB = sinkMBB;
415   BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
416     .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
417     .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
418   
419   delete MI;   // The pseudo instruction is gone now.
420   return BB;
421 }
422   
423 //===----------------------------------------------------------------------===//
424 // Instruction Selector Implementation
425 //===----------------------------------------------------------------------===//
426
427 //===--------------------------------------------------------------------===//
428 /// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
429 /// instructions for SelectionDAG operations.
430 ///
431 namespace {
432 class SparcV8DAGToDAGISel : public SelectionDAGISel {
433   SparcV8TargetLowering V8Lowering;
434 public:
435   SparcV8DAGToDAGISel(TargetMachine &TM)
436     : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
437
438   SDOperand Select(SDOperand Op);
439
440   // Complex Pattern Selectors.
441   bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
442   bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
443   
444   /// InstructionSelectBasicBlock - This callback is invoked by
445   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
446   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
447   
448   virtual const char *getPassName() const {
449     return "PowerPC DAG->DAG Pattern Instruction Selection";
450   } 
451   
452   // Include the pieces autogenerated from the target description.
453 #include "SparcV8GenDAGISel.inc"
454 };
455 }  // end anonymous namespace
456
457 /// InstructionSelectBasicBlock - This callback is invoked by
458 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
459 void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
460   DEBUG(BB->dump());
461   
462   // Select target instructions for the DAG.
463   DAG.setRoot(Select(DAG.getRoot()));
464   CodeGenMap.clear();
465   DAG.RemoveDeadNodes();
466   
467   // Emit machine code to BB. 
468   ScheduleAndEmitDAG(DAG);
469 }
470
471 bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
472                                        SDOperand &Offset) {
473   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
474     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
475     Offset = CurDAG->getTargetConstant(0, MVT::i32);
476     return true;
477   }
478   
479   if (Addr.getOpcode() == ISD::ADD) {
480     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
481       if (Predicate_simm13(CN)) {
482         if (FrameIndexSDNode *FIN = 
483                 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
484           // Constant offset from frame ref.
485           Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
486         } else {
487           Base = Select(Addr.getOperand(0));
488         }
489         Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
490         return true;
491       }
492     }
493     if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
494       Base = Select(Addr.getOperand(1));
495       Offset = Addr.getOperand(0).getOperand(0);
496       return true;
497     }
498     if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
499       Base = Select(Addr.getOperand(0));
500       Offset = Addr.getOperand(1).getOperand(0);
501       return true;
502     }
503   }
504   Base = Select(Addr);
505   Offset = CurDAG->getTargetConstant(0, MVT::i32);
506   return true;
507 }
508
509 bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1, 
510                                        SDOperand &R2) {
511   if (Addr.getOpcode() == ISD::FrameIndex) return false; 
512   if (Addr.getOpcode() == ISD::ADD) {
513     if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
514         Predicate_simm13(Addr.getOperand(1).Val))
515       return false;  // Let the reg+imm pattern catch this!
516     if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
517         Addr.getOperand(1).getOpcode() == V8ISD::Lo)
518       return false;  // Let the reg+imm pattern catch this!
519     R1 = Select(Addr.getOperand(0));
520     R2 = Select(Addr.getOperand(1));
521     return true;
522   }
523
524   R1 = Select(Addr);
525   R2 = CurDAG->getRegister(V8::G0, MVT::i32);
526   return true;
527 }
528
529 SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
530   SDNode *N = Op.Val;
531   if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
532       N->getOpcode() < V8ISD::FIRST_NUMBER)
533     return Op;   // Already selected.
534                  // If this has already been converted, use it.
535   std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
536   if (CGMI != CodeGenMap.end()) return CGMI->second;
537   
538   switch (N->getOpcode()) {
539   default: break;
540   case ISD::BasicBlock:         return CodeGenMap[Op] = Op;
541   case ISD::FrameIndex: {
542     int FI = cast<FrameIndexSDNode>(N)->getIndex();
543     if (N->hasOneUse())
544       return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
545                                   CurDAG->getTargetFrameIndex(FI, MVT::i32),
546                                   CurDAG->getTargetConstant(0, MVT::i32));
547     return CodeGenMap[Op] = 
548       CurDAG->getTargetNode(V8::ADDri, MVT::i32,
549                             CurDAG->getTargetFrameIndex(FI, MVT::i32),
550                             CurDAG->getTargetConstant(0, MVT::i32));
551   }
552   case V8ISD::CMPICC: {
553     // FIXME: Handle compare with immediate.
554     SDOperand LHS = Select(N->getOperand(0));
555     SDOperand RHS = Select(N->getOperand(1));
556     SDOperand Result = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
557                                              LHS, RHS);
558     return CodeGenMap[Op] = Result.getValue(1);
559   }
560   case ISD::ADD_PARTS: {
561     SDOperand LHSL = Select(N->getOperand(0));
562     SDOperand LHSH = Select(N->getOperand(1));
563     SDOperand RHSL = Select(N->getOperand(2));
564     SDOperand RHSH = Select(N->getOperand(3));
565     // FIXME, handle immediate RHS.
566     SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
567                                           LHSL, RHSL);
568     SDOperand Hi  = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH, 
569                                           Low.getValue(1));
570     CodeGenMap[SDOperand(N, 0)] = Low;
571     CodeGenMap[SDOperand(N, 1)] = Hi;
572     return Op.ResNo ? Hi : Low;
573   }
574   case ISD::SUB_PARTS: {
575     SDOperand LHSL = Select(N->getOperand(0));
576     SDOperand LHSH = Select(N->getOperand(1));
577     SDOperand RHSL = Select(N->getOperand(2));
578     SDOperand RHSH = Select(N->getOperand(3));
579     // FIXME, handle immediate RHS.
580     SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
581                                           LHSL, RHSL);
582     SDOperand Hi  = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH, 
583                                           Low.getValue(1));
584     CodeGenMap[SDOperand(N, 0)] = Low;
585     CodeGenMap[SDOperand(N, 1)] = Hi;
586     return Op.ResNo ? Hi : Low;
587   }
588   case ISD::SDIV:
589   case ISD::UDIV: {
590     // FIXME: should use a custom expander to expose the SRA to the dag.
591     SDOperand DivLHS = Select(N->getOperand(0));
592     SDOperand DivRHS = Select(N->getOperand(1));
593     
594     // Set the Y register to the high-part.
595     SDOperand TopPart;
596     if (N->getOpcode() == ISD::SDIV) {
597       TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
598                                       CurDAG->getTargetConstant(31, MVT::i32));
599     } else {
600       TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
601     }
602     TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
603                                     CurDAG->getRegister(V8::G0, MVT::i32));
604
605     // FIXME: Handle div by immediate.
606     unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
607     return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
608   }    
609   case ISD::MULHU:
610   case ISD::MULHS: {
611     // FIXME: Handle mul by immediate.
612     SDOperand MulLHS = Select(N->getOperand(0));
613     SDOperand MulRHS = Select(N->getOperand(1));
614     unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
615     SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
616                                           MulLHS, MulRHS);
617     // The high part is in the Y register.
618     return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
619   }
620     
621   case ISD::RET: {
622     if (N->getNumOperands() == 2) {
623       SDOperand Chain = Select(N->getOperand(0));     // Token chain.
624       SDOperand Val = Select(N->getOperand(1));
625       if (N->getOperand(1).getValueType() == MVT::i32) {
626         Chain = CurDAG->getCopyToReg(Chain, V8::I0, Val);
627       } else if (N->getOperand(1).getValueType() == MVT::f32) {
628         Chain = CurDAG->getCopyToReg(Chain, V8::F0, Val);
629       } else {
630         assert(N->getOperand(1).getValueType() == MVT::f64);
631         Chain = CurDAG->getCopyToReg(Chain, V8::D0, Val);
632       }
633       return CurDAG->SelectNodeTo(N, V8::RETL, MVT::Other, Chain);
634     } else if (N->getNumOperands() > 1) {
635       SDOperand Chain = Select(N->getOperand(0));     // Token chain.
636       assert(N->getOperand(1).getValueType() == MVT::i32 &&
637              N->getOperand(2).getValueType() == MVT::i32 &&
638              N->getNumOperands() == 3 && "Unknown two-register ret value!");
639       Chain = CurDAG->getCopyToReg(Chain, V8::I1, Select(N->getOperand(1)));
640       Chain = CurDAG->getCopyToReg(Chain, V8::I0, Select(N->getOperand(2)));
641       return CurDAG->SelectNodeTo(N, V8::RETL, MVT::Other, Chain);
642     }
643     break;  // Generated code handles the void case.
644   }
645   }
646   
647   return SelectCode(Op);
648 }
649
650
651 /// createPPCISelDag - This pass converts a legalized DAG into a 
652 /// PowerPC-specific DAG, ready for instruction scheduling.
653 ///
654 FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
655   return new SparcV8DAGToDAGISel(TM);
656 }