Implement 64-bit add/sub, make sure to receive and return 64-bit args with
[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/MachineFunction.h"
18 #include "llvm/CodeGen/SelectionDAG.h"
19 #include "llvm/CodeGen/SelectionDAGISel.h"
20 #include "llvm/CodeGen/SSARegMap.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Support/Debug.h"
23 #include <iostream>
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 // TargetLowering Implementation
28 //===----------------------------------------------------------------------===//
29
30 namespace {
31   class SparcV8TargetLowering : public TargetLowering {
32   public:
33     SparcV8TargetLowering(TargetMachine &TM);
34
35     virtual std::vector<SDOperand>
36       LowerArguments(Function &F, SelectionDAG &DAG);
37     virtual std::pair<SDOperand, SDOperand>
38       LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
39                   unsigned CC,
40                   bool isTailCall, SDOperand Callee, ArgListTy &Args,
41                   SelectionDAG &DAG);
42     
43     virtual SDOperand LowerReturnTo(SDOperand Chain, SDOperand Op,
44                                     SelectionDAG &DAG);
45     virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
46                                    Value *VAListV, SelectionDAG &DAG);
47     virtual std::pair<SDOperand,SDOperand>
48       LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
49                  const Type *ArgTy, SelectionDAG &DAG);
50     virtual std::pair<SDOperand, SDOperand>
51       LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
52                               SelectionDAG &DAG);
53   };
54 }
55
56 SparcV8TargetLowering::SparcV8TargetLowering(TargetMachine &TM)
57   : TargetLowering(TM) {
58   
59   // Set up the register classes.
60   addRegisterClass(MVT::i32, V8::IntRegsRegisterClass);
61   addRegisterClass(MVT::f32, V8::FPRegsRegisterClass);
62   addRegisterClass(MVT::f64, V8::DFPRegsRegisterClass);
63
64   // Sparc doesn't have sext_inreg, replace them with shl/sra
65   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16  , Expand);
66   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8   , Expand);
67   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1   , Expand);
68
69   // Sparc has no REM operation.
70   setOperationAction(ISD::UREM, MVT::i32, Expand);
71   setOperationAction(ISD::SREM, MVT::i32, Expand);
72   
73   computeRegisterProperties();
74 }
75
76 std::vector<SDOperand>
77 SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
78   MachineFunction &MF = DAG.getMachineFunction();
79   SSARegMap *RegMap = MF.getSSARegMap();
80   std::vector<SDOperand> ArgValues;
81   
82   static const unsigned GPR[] = {
83     V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
84   };
85   unsigned ArgNo = 0;
86   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
87     MVT::ValueType ObjectVT = getValueType(I->getType());
88     assert(ArgNo < 6 && "Only args in regs for now");
89     
90     switch (ObjectVT) {
91     default: assert(0 && "Unhandled argument type!");
92     // TODO: MVT::i64 & FP
93     case MVT::i1:
94     case MVT::i8:
95     case MVT::i16:
96     case MVT::i32: {
97       unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
98       MF.addLiveIn(GPR[ArgNo++], VReg);
99       SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
100       DAG.setRoot(Arg.getValue(1));
101       if (ObjectVT != MVT::i32) {
102         unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext 
103                                                      : ISD::AssertZext;
104         Arg = DAG.getNode(AssertOp, MVT::i32, Arg, 
105                           DAG.getValueType(ObjectVT));
106         Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
107       }
108       ArgValues.push_back(Arg);
109       break;
110     }
111     case MVT::i64: {
112       unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
113       MF.addLiveIn(GPR[ArgNo++], VRegHi);
114       unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
115       MF.addLiveIn(GPR[ArgNo++], VRegLo);
116       SDOperand ArgLo = DAG.getCopyFromReg(DAG.getRoot(), VRegLo, MVT::i32);
117       SDOperand ArgHi = DAG.getCopyFromReg(ArgLo.getValue(1), VRegHi, MVT::i32);
118       DAG.setRoot(ArgHi.getValue(1));
119       ArgValues.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgLo, ArgHi));
120       break;
121     }
122     }
123   }
124   
125   assert(!F.isVarArg() && "Unimp");
126   
127   // Finally, inform the code generator which regs we return values in.
128   switch (getValueType(F.getReturnType())) {
129   default: assert(0 && "Unknown type!");
130   case MVT::isVoid: break;
131   case MVT::i1:
132   case MVT::i8:
133   case MVT::i16:
134   case MVT::i32:
135     MF.addLiveOut(V8::I0);
136     break;
137   case MVT::i64:
138     MF.addLiveOut(V8::I0);
139     MF.addLiveOut(V8::I1);
140     break;
141   case MVT::f32:
142     MF.addLiveOut(V8::F0);
143     break;
144   case MVT::f64:
145     MF.addLiveOut(V8::D0);
146     break;
147   }
148   
149   return ArgValues;
150 }
151
152 std::pair<SDOperand, SDOperand>
153 SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
154                                    bool isVarArg, unsigned CC,
155                                    bool isTailCall, SDOperand Callee, 
156                                    ArgListTy &Args, SelectionDAG &DAG) {
157   assert(0 && "Unimp");
158   abort();
159 }
160
161 SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
162                                                SelectionDAG &DAG) {
163   if (Op.getValueType() == MVT::i64) {
164     SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op, 
165                                DAG.getConstant(1, MVT::i32));
166     SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
167                                DAG.getConstant(0, MVT::i32));
168     return DAG.getNode(ISD::RET, MVT::Other, Chain, Lo, Hi);
169   } else {
170     return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
171   }
172 }
173
174 SDOperand SparcV8TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
175                                               Value *VAListV, SelectionDAG &DAG) {
176   assert(0 && "Unimp");
177   abort();
178 }
179
180 std::pair<SDOperand,SDOperand>
181 SparcV8TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
182                                   const Type *ArgTy, SelectionDAG &DAG) {
183   assert(0 && "Unimp");
184   abort();
185 }
186
187 std::pair<SDOperand, SDOperand>
188 SparcV8TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
189                                                SelectionDAG &DAG) {
190   assert(0 && "Unimp");
191   abort();
192 }
193
194 //===----------------------------------------------------------------------===//
195 // Instruction Selector Implementation
196 //===----------------------------------------------------------------------===//
197
198 //===--------------------------------------------------------------------===//
199 /// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
200 /// instructions for SelectionDAG operations.
201 ///
202 namespace {
203 class SparcV8DAGToDAGISel : public SelectionDAGISel {
204   SparcV8TargetLowering V8Lowering;
205 public:
206   SparcV8DAGToDAGISel(TargetMachine &TM)
207     : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
208
209   SDOperand Select(SDOperand Op);
210
211   // Complex Pattern Selectors.
212   bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
213   bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
214   
215   /// InstructionSelectBasicBlock - This callback is invoked by
216   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
217   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
218   
219   virtual const char *getPassName() const {
220     return "PowerPC DAG->DAG Pattern Instruction Selection";
221   } 
222   
223   // Include the pieces autogenerated from the target description.
224 #include "SparcV8GenDAGISel.inc"
225 };
226 }  // end anonymous namespace
227
228 /// InstructionSelectBasicBlock - This callback is invoked by
229 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
230 void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
231   DEBUG(BB->dump());
232   
233   // Select target instructions for the DAG.
234   DAG.setRoot(Select(DAG.getRoot()));
235   CodeGenMap.clear();
236   DAG.RemoveDeadNodes();
237   
238   // Emit machine code to BB. 
239   ScheduleAndEmitDAG(DAG);
240 }
241
242 bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1, 
243                                        SDOperand &R2) {
244   if (Addr.getOpcode() == ISD::ADD) {
245     if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
246         Predicate_simm13(Addr.getOperand(1).Val))
247       return false;  // Let the reg+imm pattern catch this!
248     R1 = Addr.getOperand(0);
249     R2 = Addr.getOperand(1);
250     return true;
251   }
252
253   R1 = Select(Addr);
254   R2 = CurDAG->getRegister(V8::G0, MVT::i32);
255   return true;
256 }
257
258 bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
259                                        SDOperand &Offset) {
260   if (Addr.getOpcode() == ISD::ADD) {
261     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
262       if (Predicate_simm13(CN)) {
263         Base = Addr.getOperand(0);
264         Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
265         return true;
266       }
267   }
268   Base = Select(Addr);
269   Offset = CurDAG->getTargetConstant(0, MVT::i32);
270   return true;
271 }
272
273
274 SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
275   SDNode *N = Op.Val;
276   if (N->getOpcode() >= ISD::BUILTIN_OP_END/* &&
277       N->getOpcode() < V8ISD::FIRST_NUMBER*/)
278     return Op;   // Already selected.
279                  // If this has already been converted, use it.
280   std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
281   if (CGMI != CodeGenMap.end()) return CGMI->second;
282   
283   switch (N->getOpcode()) {
284   default: break;
285   case ISD::ADD_PARTS: {
286     SDOperand LHSL = Select(N->getOperand(0));
287     SDOperand LHSH = Select(N->getOperand(1));
288     SDOperand RHSL = Select(N->getOperand(2));
289     SDOperand RHSH = Select(N->getOperand(3));
290     // FIXME, handle immediate RHS.
291     SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
292                                           LHSL, RHSL);
293     SDOperand Hi  = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH, 
294                                           Low.getValue(1));
295     CodeGenMap[SDOperand(N, 0)] = Low;
296     CodeGenMap[SDOperand(N, 1)] = Hi;
297     return Op.ResNo ? Hi : Low;
298   }
299   case ISD::SUB_PARTS: {
300     SDOperand LHSL = Select(N->getOperand(0));
301     SDOperand LHSH = Select(N->getOperand(1));
302     SDOperand RHSL = Select(N->getOperand(2));
303     SDOperand RHSH = Select(N->getOperand(3));
304     // FIXME, handle immediate RHS.
305     SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
306                                           LHSL, RHSL);
307     SDOperand Hi  = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH, 
308                                           Low.getValue(1));
309     CodeGenMap[SDOperand(N, 0)] = Low;
310     CodeGenMap[SDOperand(N, 1)] = Hi;
311     return Op.ResNo ? Hi : Low;
312   }
313   case ISD::SDIV:
314   case ISD::UDIV: {
315     // FIXME: should use a custom expander to expose the SRA to the dag.
316     SDOperand DivLHS = Select(N->getOperand(0));
317     SDOperand DivRHS = Select(N->getOperand(1));
318     
319     // Set the Y register to the high-part.
320     SDOperand TopPart;
321     if (N->getOpcode() == ISD::SDIV) {
322       TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
323                                       CurDAG->getTargetConstant(31, MVT::i32));
324     } else {
325       TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
326     }
327     TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
328                                     CurDAG->getRegister(V8::G0, MVT::i32));
329
330     // FIXME: Handle div by immediate.
331     unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
332     return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
333   }    
334   case ISD::MULHU:
335   case ISD::MULHS: {
336     // FIXME: Handle mul by immediate.
337     SDOperand MulLHS = Select(N->getOperand(0));
338     SDOperand MulRHS = Select(N->getOperand(1));
339     unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
340     SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
341                                           MulLHS, MulRHS);
342     // The high part is in the Y register.
343     return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
344   }
345     
346   case ISD::RET: {
347     if (N->getNumOperands() == 2) {
348       SDOperand Chain = Select(N->getOperand(0));     // Token chain.
349       SDOperand Val = Select(N->getOperand(1));
350       if (N->getOperand(1).getValueType() == MVT::i32) {
351         Chain = CurDAG->getCopyToReg(Chain, V8::I0, Val);
352       } else if (N->getOperand(1).getValueType() == MVT::f32) {
353         Chain = CurDAG->getCopyToReg(Chain, V8::F0, Val);
354       } else {
355         assert(N->getOperand(1).getValueType() == MVT::f64);
356         Chain = CurDAG->getCopyToReg(Chain, V8::D0, Val);
357       }
358       return CurDAG->SelectNodeTo(N, V8::RETL, MVT::Other, Chain);
359     } else if (N->getNumOperands() > 1) {
360       SDOperand Chain = Select(N->getOperand(0));     // Token chain.
361       assert(N->getOperand(1).getValueType() == MVT::i32 &&
362              N->getOperand(2).getValueType() == MVT::i32 &&
363              N->getNumOperands() == 3 && "Unknown two-register ret value!");
364       Chain = CurDAG->getCopyToReg(Chain, V8::I1, Select(N->getOperand(1)));
365       Chain = CurDAG->getCopyToReg(Chain, V8::I0, Select(N->getOperand(2)));
366       return CurDAG->SelectNodeTo(N, V8::RETL, MVT::Other, Chain);
367     }
368     break;  // Generated code handles the void case.
369   }
370   }
371   
372   return SelectCode(Op);
373 }
374
375
376 /// createPPCISelDag - This pass converts a legalized DAG into a 
377 /// PowerPC-specific DAG, ready for instruction scheduling.
378 ///
379 FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
380   return new SparcV8DAGToDAGISel(TM);
381 }