Clean up MipsInstrInfo::copyPhysReg and handle copies from and to 64-bit integer
[oota-llvm.git] / lib / Target / Mips / MipsISelDAGToDAG.cpp
1 //===-- MipsISelDAGToDAG.cpp - A dag to dag inst selector for Mips --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an instruction selector for the MIPS target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "mips-isel"
15 #include "Mips.h"
16 #include "MipsMachineFunction.h"
17 #include "MipsRegisterInfo.h"
18 #include "MipsSubtarget.h"
19 #include "MipsTargetMachine.h"
20 #include "llvm/GlobalValue.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/Type.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/SelectionDAGISel.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 using namespace llvm;
36
37 //===----------------------------------------------------------------------===//
38 // Instruction Selector Implementation
39 //===----------------------------------------------------------------------===//
40
41 //===----------------------------------------------------------------------===//
42 // MipsDAGToDAGISel - MIPS specific code to select MIPS machine
43 // instructions for SelectionDAG operations.
44 //===----------------------------------------------------------------------===//
45 namespace {
46
47 class MipsDAGToDAGISel : public SelectionDAGISel {
48
49   /// TM - Keep a reference to MipsTargetMachine.
50   MipsTargetMachine &TM;
51
52   /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
53   /// make the right decision when generating code for different targets.
54   const MipsSubtarget &Subtarget;
55
56 public:
57   explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
58   SelectionDAGISel(tm),
59   TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
60
61   // Pass Name
62   virtual const char *getPassName() const {
63     return "MIPS DAG->DAG Pattern Instruction Selection";
64   }
65
66
67 private:
68   // Include the pieces autogenerated from the target description.
69   #include "MipsGenDAGISel.inc"
70
71   /// getTargetMachine - Return a reference to the TargetMachine, casted
72   /// to the target-specific type.
73   const MipsTargetMachine &getTargetMachine() {
74     return static_cast<const MipsTargetMachine &>(TM);
75   }
76
77   /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
78   /// to the target-specific type.
79   const MipsInstrInfo *getInstrInfo() {
80     return getTargetMachine().getInstrInfo();
81   }
82
83   SDNode *getGlobalBaseReg();
84   SDNode *Select(SDNode *N);
85
86   // Complex Pattern.
87   bool SelectAddr(SDValue N, SDValue &Base, SDValue &Offset);
88
89   // getI32Imm - Return a target constant with the specified
90   // value, of type i32.
91   inline SDValue getI32Imm(unsigned Imm) {
92     return CurDAG->getTargetConstant(Imm, MVT::i32);
93   }
94
95   virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
96                                             char ConstraintCode,
97                                             std::vector<SDValue> &OutOps);
98 };
99
100 }
101
102
103 /// getGlobalBaseReg - Output the instructions required to put the
104 /// GOT address into a register.
105 SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
106   unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
107   return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
108 }
109
110 /// ComplexPattern used on MipsInstrInfo
111 /// Used on Mips Load/Store instructions
112 bool MipsDAGToDAGISel::
113 SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset) {
114   // if Address is FI, get the TargetFrameIndex.
115   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
116     Base   = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
117     Offset = CurDAG->getTargetConstant(0, MVT::i32);
118     return true;
119   }
120
121   // on PIC code Load GA
122   if (TM.getRelocationModel() == Reloc::PIC_) {
123     if (Addr.getOpcode() == MipsISD::WrapperPIC) {
124       Base   = CurDAG->getRegister(Mips::GP, MVT::i32);
125       Offset = Addr.getOperand(0);
126       return true;
127     }
128   } else {
129     if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
130         Addr.getOpcode() == ISD::TargetGlobalAddress))
131       return false;
132     else if (Addr.getOpcode() == ISD::TargetGlobalTLSAddress) {
133       Base   = CurDAG->getRegister(Mips::GP, MVT::i32);
134       Offset = Addr;
135       return true;
136     }
137   }
138
139   // Addresses of the form FI+const or FI|const
140   if (CurDAG->isBaseWithConstantOffset(Addr)) {
141     ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
142     if (isInt<16>(CN->getSExtValue())) {
143
144       // If the first operand is a FI, get the TargetFI Node
145       if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
146                                   (Addr.getOperand(0)))
147         Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
148       else
149         Base = Addr.getOperand(0);
150
151       Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
152       return true;
153     }
154   }
155
156   // Operand is a result from an ADD.
157   if (Addr.getOpcode() == ISD::ADD) {
158     // When loading from constant pools, load the lower address part in
159     // the instruction itself. Example, instead of:
160     //  lui $2, %hi($CPI1_0)
161     //  addiu $2, $2, %lo($CPI1_0)
162     //  lwc1 $f0, 0($2)
163     // Generate:
164     //  lui $2, %hi($CPI1_0)
165     //  lwc1 $f0, %lo($CPI1_0)($2)
166     if ((Addr.getOperand(0).getOpcode() == MipsISD::Hi ||
167          Addr.getOperand(0).getOpcode() == ISD::LOAD) &&
168         Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
169       SDValue LoVal = Addr.getOperand(1);
170       if (isa<ConstantPoolSDNode>(LoVal.getOperand(0)) || 
171           isa<GlobalAddressSDNode>(LoVal.getOperand(0))) {
172         Base = Addr.getOperand(0);
173         Offset = LoVal.getOperand(0);
174         return true;
175       }
176     }
177   }
178
179   Base   = Addr;
180   Offset = CurDAG->getTargetConstant(0, MVT::i32);
181   return true;
182 }
183
184 /// Select instructions not customized! Used for
185 /// expanded, promoted and normal instructions
186 SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
187   unsigned Opcode = Node->getOpcode();
188   DebugLoc dl = Node->getDebugLoc();
189
190   // Dump information about the Node being selected
191   DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
192
193   // If we have a custom node, we already have selected!
194   if (Node->isMachineOpcode()) {
195     DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
196     return NULL;
197   }
198
199   ///
200   // Instruction Selection not handled by the auto-generated
201   // tablegen selection should be handled here.
202   ///
203   switch(Opcode) {
204     default: break;
205
206     case ISD::SUBE:
207     case ISD::ADDE: {
208       SDValue InFlag = Node->getOperand(2), CmpLHS;
209       unsigned Opc = InFlag.getOpcode(); (void)Opc;
210       assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
211               (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
212              "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
213
214       unsigned MOp;
215       if (Opcode == ISD::ADDE) {
216         CmpLHS = InFlag.getValue(0);
217         MOp = Mips::ADDu;
218       } else {
219         CmpLHS = InFlag.getOperand(0);
220         MOp = Mips::SUBu;
221       }
222
223       SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
224
225       SDValue LHS = Node->getOperand(0);
226       SDValue RHS = Node->getOperand(1);
227
228       EVT VT = LHS.getValueType();
229       SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
230       SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
231                                                 SDValue(Carry,0), RHS);
232
233       return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
234                                   LHS, SDValue(AddCarry,0));
235     }
236
237     /// Mul with two results
238     case ISD::SMUL_LOHI:
239     case ISD::UMUL_LOHI: {
240       assert(Node->getValueType(0) != MVT::i64 &&
241              "64-bit multiplication with two results not handled.");
242       SDValue Op1 = Node->getOperand(0);
243       SDValue Op2 = Node->getOperand(1);
244
245       unsigned Op;
246       Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
247
248       SDNode *Mul = CurDAG->getMachineNode(Op, dl, MVT::Glue, Op1, Op2);
249
250       SDValue InFlag = SDValue(Mul, 0);
251       SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32,
252                                           MVT::Glue, InFlag);
253       InFlag = SDValue(Lo,1);
254       SDNode *Hi = CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
255
256       if (!SDValue(Node, 0).use_empty())
257         ReplaceUses(SDValue(Node, 0), SDValue(Lo,0));
258
259       if (!SDValue(Node, 1).use_empty())
260         ReplaceUses(SDValue(Node, 1), SDValue(Hi,0));
261
262       return NULL;
263     }
264
265     /// Special Muls
266     case ISD::MUL:
267       // Mips32 has a 32-bit three operand mul instruction.
268       if (Subtarget.hasMips32() && Node->getValueType(0) == MVT::i32)
269         break;
270     case ISD::MULHS:
271     case ISD::MULHU: {
272       assert((Opcode == ISD::MUL || Node->getValueType(0) != MVT::i64) &&
273              "64-bit MULH* not handled.");
274       EVT Ty = Node->getValueType(0);
275       SDValue MulOp1 = Node->getOperand(0);
276       SDValue MulOp2 = Node->getOperand(1);
277
278       unsigned MulOp  = (Opcode == ISD::MULHU ?
279                          Mips::MULTu :
280                          (Ty == MVT::i32 ? Mips::MULT : Mips::DMULT));
281       SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl,
282                                                MVT::Glue, MulOp1, MulOp2);
283
284       SDValue InFlag = SDValue(MulNode, 0);
285
286       if (Opcode == ISD::MUL) {
287         unsigned Opc = (Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64);
288         return CurDAG->getMachineNode(Opc, dl, Ty, InFlag);
289       }
290       else
291         return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
292     }
293
294     // Get target GOT address.
295     case ISD::GLOBAL_OFFSET_TABLE:
296       return getGlobalBaseReg();
297
298     case ISD::ConstantFP: {
299       ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
300       if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
301         SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
302                                         Mips::ZERO, MVT::i32);
303         return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
304                                       Zero);
305       }
306       break;
307     }
308
309     case MipsISD::ThreadPointer: {
310       unsigned SrcReg = Mips::HWR29;
311       unsigned DestReg = Mips::V1;
312       SDNode *Rdhwr = CurDAG->getMachineNode(Mips::RDHWR, Node->getDebugLoc(),
313           Node->getValueType(0), CurDAG->getRegister(SrcReg, MVT::i32));
314       SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
315           SDValue(Rdhwr, 0));
316       SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, MVT::i32);
317       ReplaceUses(SDValue(Node, 0), ResNode);
318       return ResNode.getNode();
319     }
320   }
321
322   // Select the default instruction
323   SDNode *ResNode = SelectCode(Node);
324
325   DEBUG(errs() << "=> ");
326   if (ResNode == NULL || ResNode == Node)
327     DEBUG(Node->dump(CurDAG));
328   else
329     DEBUG(ResNode->dump(CurDAG));
330   DEBUG(errs() << "\n");
331   return ResNode;
332 }
333
334 bool MipsDAGToDAGISel::
335 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
336                              std::vector<SDValue> &OutOps) {
337   assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
338   OutOps.push_back(Op);
339   return false;
340 }
341
342 /// createMipsISelDag - This pass converts a legalized DAG into a
343 /// MIPS-specific DAG, ready for instruction scheduling.
344 FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
345   return new MipsDAGToDAGISel(TM);
346 }