Avoid creating two TargetLowering objects for each target.
[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 "MipsISelLowering.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsRegisterInfo.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h"
21 #include "llvm/GlobalValue.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/Intrinsics.h"
24 #include "llvm/Support/CFG.h"
25 #include "llvm/Type.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/SelectionDAGISel.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 #include <queue>
36 #include <set>
37
38 using namespace llvm;
39
40 //===----------------------------------------------------------------------===//
41 // Instruction Selector Implementation
42 //===----------------------------------------------------------------------===//
43
44 //===----------------------------------------------------------------------===//
45 // MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46 // instructions for SelectionDAG operations.
47 //===----------------------------------------------------------------------===//
48 namespace {
49
50 class VISIBILITY_HIDDEN MipsDAGToDAGISel : public SelectionDAGISel {
51
52   /// TM - Keep a reference to MipsTargetMachine.
53   MipsTargetMachine &TM;
54
55   /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
56   /// make the right decision when generating code for different targets.
57   const MipsSubtarget &Subtarget;
58  
59 public:
60   explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
61   SelectionDAGISel(*tm.getTargetLowering()),
62   TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
63   
64   virtual void InstructionSelect();
65
66   // Pass Name
67   virtual const char *getPassName() const {
68     return "MIPS DAG->DAG Pattern Instruction Selection";
69   } 
70   
71
72 private:  
73   // Include the pieces autogenerated from the target description.
74   #include "MipsGenDAGISel.inc"
75
76   SDValue getGlobalBaseReg();
77   SDNode *Select(SDValue N);
78
79   // Complex Pattern.
80   bool SelectAddr(SDValue Op, SDValue N, 
81                   SDValue &Base, SDValue &Offset);
82
83
84   // getI32Imm - Return a target constant with the specified
85   // value, of type i32.
86   inline SDValue getI32Imm(unsigned Imm) {
87     return CurDAG->getTargetConstant(Imm, MVT::i32);
88   }
89
90
91   #ifndef NDEBUG
92   unsigned Indent;
93   #endif
94 };
95
96 }
97
98 /// InstructionSelect - This callback is invoked by
99 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
100 void MipsDAGToDAGISel::
101 InstructionSelect() 
102 {
103   DEBUG(BB->dump());
104   // Codegen the basic block.
105   #ifndef NDEBUG
106   DOUT << "===== Instruction selection begins:\n";
107   Indent = 0;
108   #endif
109
110   // Select target instructions for the DAG.
111   SelectRoot();
112
113   #ifndef NDEBUG
114   DOUT << "===== Instruction selection ends:\n";
115   #endif
116
117   CurDAG->RemoveDeadNodes();
118 }
119
120 /// getGlobalBaseReg - Output the instructions required to put the
121 /// GOT address into a register.
122 SDValue MipsDAGToDAGISel::getGlobalBaseReg() {
123   MachineFunction* MF = BB->getParent();
124   unsigned GP = 0;
125   for(MachineRegisterInfo::livein_iterator ii = MF->getRegInfo().livein_begin(),
126         ee = MF->getRegInfo().livein_end(); ii != ee; ++ii)
127     if (ii->first == Mips::GP) {
128       GP = ii->second;
129       break;
130     }
131   assert(GP && "GOT PTR not in liveins");
132   return CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 
133                                 GP, MVT::i32);
134 }
135
136 /// ComplexPattern used on MipsInstrInfo
137 /// Used on Mips Load/Store instructions
138 bool MipsDAGToDAGISel::
139 SelectAddr(SDValue Op, SDValue Addr, SDValue &Offset, SDValue &Base)
140 {
141   // if Address is FI, get the TargetFrameIndex.
142   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
143     Base   = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
144     Offset = CurDAG->getTargetConstant(0, MVT::i32);
145     return true;
146   }
147     
148   // on PIC code Load GA
149   if (TM.getRelocationModel() == Reloc::PIC_) {
150     if ((Addr.getOpcode() == ISD::TargetGlobalAddress) || 
151         (Addr.getOpcode() == ISD::TargetJumpTable)){
152       Base   = CurDAG->getRegister(Mips::GP, MVT::i32);
153       Offset = Addr;
154       return true;
155     }
156   } else {
157     if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
158         Addr.getOpcode() == ISD::TargetGlobalAddress))
159       return false;
160   }    
161   
162   // Operand is a result from an ADD.
163   if (Addr.getOpcode() == ISD::ADD) {
164     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
165       if (Predicate_immSExt16(CN)) {
166
167         // If the first operand is a FI, get the TargetFI Node
168         if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
169                                     (Addr.getOperand(0))) {
170           Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
171         } else {
172           Base = Addr.getOperand(0);
173         }
174
175         Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
176         return true;
177       }
178     }
179   }
180
181   Base   = Addr;
182   Offset = CurDAG->getTargetConstant(0, MVT::i32);
183   return true;
184 }
185
186 /// Select instructions not customized! Used for
187 /// expanded, promoted and normal instructions
188 SDNode* MipsDAGToDAGISel::
189 Select(SDValue N) 
190 {
191   SDNode *Node = N.getNode();
192   unsigned Opcode = Node->getOpcode();
193
194   // Dump information about the Node being selected
195   #ifndef NDEBUG
196   DOUT << std::string(Indent, ' ') << "Selecting: ";
197   DEBUG(Node->dump(CurDAG));
198   DOUT << "\n";
199   Indent += 2;
200   #endif
201
202   // If we have a custom node, we already have selected!
203   if (Node->isMachineOpcode()) {
204     #ifndef NDEBUG
205     DOUT << std::string(Indent-2, ' ') << "== ";
206     DEBUG(Node->dump(CurDAG));
207     DOUT << "\n";
208     Indent -= 2;
209     #endif
210     return NULL;
211   }
212
213   ///
214   // Instruction Selection not handled by the auto-generated 
215   // tablegen selection should be handled here.
216   /// 
217   switch(Opcode) {
218
219     default: break;
220
221     case ISD::SUBE: 
222     case ISD::ADDE: {
223       SDValue InFlag = Node->getOperand(2), CmpLHS;
224       unsigned Opc = InFlag.getOpcode(), MOp;
225
226       assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) || 
227               (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&  
228              "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
229
230       if (Opcode == ISD::ADDE) {
231         CmpLHS = InFlag.getValue(0);
232         MOp = Mips::ADDu;
233       } else { 
234         CmpLHS = InFlag.getOperand(0);
235         MOp = Mips::SUBu;
236       }
237
238       SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
239
240       SDValue LHS = Node->getOperand(0);
241       SDValue RHS = Node->getOperand(1);
242       AddToISelQueue(LHS);
243       AddToISelQueue(RHS);
244
245       MVT VT = LHS.getValueType();
246       SDNode *Carry = CurDAG->getTargetNode(Mips::SLTu, VT, Ops, 2);
247       SDNode *AddCarry = CurDAG->getTargetNode(Mips::ADDu, VT, 
248                                                SDValue(Carry,0), RHS);
249
250       return CurDAG->SelectNodeTo(N.getNode(), MOp, VT, MVT::Flag, 
251                                   LHS, SDValue(AddCarry,0));
252     }
253
254     /// Mul/Div with two results
255     case ISD::SDIVREM:
256     case ISD::UDIVREM:
257     case ISD::SMUL_LOHI:
258     case ISD::UMUL_LOHI: {
259       SDValue Op1 = Node->getOperand(0);
260       SDValue Op2 = Node->getOperand(1);
261       AddToISelQueue(Op1);
262       AddToISelQueue(Op2);
263
264       unsigned Op;
265       if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI)
266         Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
267       else
268         Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV);
269
270       SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2);
271
272       SDValue InFlag = SDValue(Node, 0);
273       SDNode *Lo = CurDAG->getTargetNode(Mips::MFLO, MVT::i32, 
274                                          MVT::Flag, InFlag);
275       InFlag = SDValue(Lo,1);
276       SDNode *Hi = CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag);
277
278       if (!N.getValue(0).use_empty()) 
279         ReplaceUses(N.getValue(0), SDValue(Lo,0));
280
281       if (!N.getValue(1).use_empty()) 
282         ReplaceUses(N.getValue(1), SDValue(Hi,0));
283
284       return NULL;
285     }
286
287     /// Special Muls
288     case ISD::MUL: 
289     case ISD::MULHS:
290     case ISD::MULHU: {
291       SDValue MulOp1 = Node->getOperand(0);
292       SDValue MulOp2 = Node->getOperand(1);
293       AddToISelQueue(MulOp1);
294       AddToISelQueue(MulOp2);
295
296       unsigned MulOp  = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
297       SDNode *MulNode = CurDAG->getTargetNode(MulOp, MVT::Flag, MulOp1, MulOp2);
298
299       SDValue InFlag = SDValue(MulNode, 0);
300
301       if (MulOp == ISD::MUL)
302         return CurDAG->getTargetNode(Mips::MFLO, MVT::i32, InFlag);
303       else
304         return CurDAG->getTargetNode(Mips::MFHI, MVT::i32, InFlag);
305     }
306
307     /// Div/Rem operations
308     case ISD::SREM:
309     case ISD::UREM:
310     case ISD::SDIV: 
311     case ISD::UDIV: {
312       SDValue Op1 = Node->getOperand(0);
313       SDValue Op2 = Node->getOperand(1);
314       AddToISelQueue(Op1);
315       AddToISelQueue(Op2);
316
317       unsigned Op, MOp;
318       if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) {
319         Op  = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
320         MOp = Mips::MFLO;
321       } else {
322         Op  = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
323         MOp = Mips::MFHI;
324       }
325       SDNode *Node = CurDAG->getTargetNode(Op, MVT::Flag, Op1, Op2);
326
327       SDValue InFlag = SDValue(Node, 0);
328       return CurDAG->getTargetNode(MOp, MVT::i32, InFlag);
329     }
330
331     // Get target GOT address.
332     case ISD::GLOBAL_OFFSET_TABLE: {
333       SDValue Result = getGlobalBaseReg();
334       ReplaceUses(N, Result);
335       return NULL;
336     }
337
338     /// Handle direct and indirect calls when using PIC. On PIC, when 
339     /// GOT is smaller than about 64k (small code) the GA target is 
340     /// loaded with only one instruction. Otherwise GA's target must 
341     /// be loaded with 3 instructions. 
342     case MipsISD::JmpLink: {
343       if (TM.getRelocationModel() == Reloc::PIC_) {
344         //bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large);
345         SDValue Chain  = Node->getOperand(0);
346         SDValue Callee = Node->getOperand(1);
347         AddToISelQueue(Chain);
348         SDValue T9Reg = CurDAG->getRegister(Mips::T9, MVT::i32);
349         SDValue InFlag(0, 0);
350
351         if ( (isa<GlobalAddressSDNode>(Callee)) ||
352              (isa<ExternalSymbolSDNode>(Callee)) )
353         {
354           /// Direct call for global addresses and external symbols
355           SDValue GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
356
357           // Use load to get GOT target
358           SDValue Ops[] = { Callee, GPReg, Chain };
359           SDValue Load = SDValue(CurDAG->getTargetNode(Mips::LW, MVT::i32, 
360                                      MVT::Other, Ops, 3), 0);
361           Chain = Load.getValue(1);
362           AddToISelQueue(Chain);
363
364           // Call target must be on T9
365           Chain = CurDAG->getCopyToReg(Chain, T9Reg, Load, InFlag);
366         } else 
367           /// Indirect call
368           Chain = CurDAG->getCopyToReg(Chain, T9Reg, Callee, InFlag);
369
370         AddToISelQueue(Chain);
371
372         // Emit Jump and Link Register
373         SDNode *ResNode = CurDAG->getTargetNode(Mips::JALR, MVT::Other,
374                                   MVT::Flag, T9Reg, Chain);
375         Chain  = SDValue(ResNode, 0);
376         InFlag = SDValue(ResNode, 1);
377         ReplaceUses(SDValue(Node, 0), Chain);
378         ReplaceUses(SDValue(Node, 1), InFlag);
379         return ResNode;
380       } 
381     }
382   }
383
384   // Select the default instruction
385   SDNode *ResNode = SelectCode(N);
386
387   #ifndef NDEBUG
388   DOUT << std::string(Indent-2, ' ') << "=> ";
389   if (ResNode == NULL || ResNode == N.getNode())
390     DEBUG(N.getNode()->dump(CurDAG));
391   else
392     DEBUG(ResNode->dump(CurDAG));
393   DOUT << "\n";
394   Indent -= 2;
395   #endif
396
397   return ResNode;
398 }
399
400 /// createMipsISelDag - This pass converts a legalized DAG into a 
401 /// MIPS-specific DAG, ready for instruction scheduling.
402 FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
403   return new MipsDAGToDAGISel(TM);
404 }