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