blob: 2990ba9064780cab53c72ba7a741eece4d5fbf8b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- MipsISelDAGToDAG.cpp - A dag to dag inst selector for Mips --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the MIPS target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "mips-isel"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "Mips.h"
16#include "MipsISelLowering.h"
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +000017#include "MipsMachineFunction.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#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"
Chris Lattner1b989192007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39// Instruction Selector Implementation
40//===----------------------------------------------------------------------===//
41
42//===----------------------------------------------------------------------===//
43// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
44// instructions for SelectionDAG operations.
45//===----------------------------------------------------------------------===//
46namespace {
47
Nick Lewycky492d06e2009-10-25 06:33:48 +000048class MipsDAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049
50 /// TM - Keep a reference to MipsTargetMachine.
51 MipsTargetMachine &TM;
52
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
54 /// make the right decision when generating code for different targets.
Bruno Cardoso Lopes4fb1f542008-07-05 19:05:21 +000055 const MipsSubtarget &Subtarget;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
57public:
Dan Gohmane887fdf2008-07-07 18:00:37 +000058 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
Dan Gohman96eb47a2009-01-15 19:20:50 +000059 SelectionDAGISel(tm),
Dan Gohmanf2b29572008-10-03 16:55:19 +000060 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
Dan Gohman14a66442008-08-23 02:25:05 +000062 virtual void InstructionSelect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063
64 // Pass Name
65 virtual const char *getPassName() const {
66 return "MIPS DAG->DAG Pattern Instruction Selection";
67 }
68
69
70private:
71 // Include the pieces autogenerated from the target description.
72 #include "MipsGenDAGISel.inc"
73
Dan Gohman40653f32009-06-03 20:30:14 +000074 /// getTargetMachine - Return a reference to the TargetMachine, casted
75 /// to the target-specific type.
76 const MipsTargetMachine &getTargetMachine() {
77 return static_cast<const MipsTargetMachine &>(TM);
78 }
79
80 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
81 /// to the target-specific type.
82 const MipsInstrInfo *getInstrInfo() {
83 return getTargetMachine().getInstrInfo();
84 }
85
86 SDNode *getGlobalBaseReg();
Dan Gohman8181bd12008-07-27 21:46:04 +000087 SDNode *Select(SDValue N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
89 // Complex Pattern.
Dan Gohman8181bd12008-07-27 21:46:04 +000090 bool SelectAddr(SDValue Op, SDValue N,
91 SDValue &Base, SDValue &Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +000093 SDNode *SelectLoadFp64(SDValue N);
94 SDNode *SelectStoreFp64(SDValue N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095
96 // getI32Imm - Return a target constant with the specified
97 // value, of type i32.
Dan Gohman8181bd12008-07-27 21:46:04 +000098 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000099 return CurDAG->getTargetConstant(Imm, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 }
101
102
103 #ifndef NDEBUG
104 unsigned Indent;
105 #endif
106};
107
108}
109
Evan Cheng34fd4f32008-06-30 20:45:06 +0000110/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Chris Lattner2c6014b2009-08-23 06:49:22 +0000112void MipsDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 // Codegen the basic block.
Chris Lattner2c6014b2009-08-23 06:49:22 +0000114 DEBUG(errs() << "===== Instruction selection begins:\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000115 DEBUG(Indent = 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116
117 // Select target instructions for the DAG.
David Greene932618b2008-10-27 21:56:29 +0000118 SelectRoot(*CurDAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119
Chris Lattner2c6014b2009-08-23 06:49:22 +0000120 DEBUG(errs() << "===== Instruction selection ends:\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
Dan Gohman14a66442008-08-23 02:25:05 +0000122 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123}
124
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000125/// getGlobalBaseReg - Output the instructions required to put the
126/// GOT address into a register.
Dan Gohman40653f32009-06-03 20:30:14 +0000127SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Dan Gohman40653f32009-06-03 20:30:14 +0000128 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
129 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000130}
131
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132/// ComplexPattern used on MipsInstrInfo
133/// Used on Mips Load/Store instructions
134bool MipsDAGToDAGISel::
Dan Gohman8181bd12008-07-27 21:46:04 +0000135SelectAddr(SDValue Op, SDValue Addr, SDValue &Offset, SDValue &Base)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136{
137 // if Address is FI, get the TargetFrameIndex.
138 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000139 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
140 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 return true;
142 }
143
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000144 // on PIC code Load GA
145 if (TM.getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000146 if ((Addr.getOpcode() == ISD::TargetGlobalAddress) ||
147 (Addr.getOpcode() == ISD::TargetJumpTable)){
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000148 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000149 Offset = Addr;
150 return true;
151 }
152 } else {
Bill Wendlingfef06052008-09-16 21:48:12 +0000153 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000154 Addr.getOpcode() == ISD::TargetGlobalAddress))
155 return false;
156 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Bruno Cardoso Lopes77ba7152007-08-18 02:16:30 +0000158 // Operand is a result from an ADD.
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000159 if (Addr.getOpcode() == ISD::ADD) {
160 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
161 if (Predicate_immSExt16(CN)) {
162
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 // If the first operand is a FI, get the TargetFI Node
164 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
165 (Addr.getOperand(0))) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000166 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 } else {
168 Base = Addr.getOperand(0);
169 }
170
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000171 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 return true;
173 }
174 }
Bruno Cardoso Lopes5ad52d02009-11-16 04:33:42 +0000175
176 // When loading from constant pools, load the lower address part in
177 // the instruction itself. Instead of:
178 // lui $2, %hi($CPI1_0)
179 // addiu $2, $2, %lo($CPI1_0)
180 // lwc1 $f0, 0($2)
181 // Generate:
182 // lui $2, %hi($CPI1_0)
183 // lwc1 $f0, %lo($CPI1_0)($2)
184 if (Addr.getOperand(0).getOpcode() == MipsISD::Hi &&
185 Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
186 SDValue LoVal = Addr.getOperand(1);
187 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(
188 LoVal.getOperand(0))) {
189 if (!CP->getOffset()) {
190 Base = Addr.getOperand(0);
191 Offset = LoVal.getOperand(0);
192 return true;
193 }
194 }
195 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 }
197
198 Base = Addr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000199 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 return true;
201}
202
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000203SDNode *MipsDAGToDAGISel::SelectLoadFp64(SDValue N) {
204 MVT::SimpleValueType NVT =
205 N.getNode()->getValueType(0).getSimpleVT().SimpleTy;
206
207 if (!Subtarget.isMips1() || NVT != MVT::f64)
208 return NULL;
209
210 if (!Predicate_unindexedload(N.getNode()) ||
211 !Predicate_load(N.getNode()))
212 return NULL;
213
214 SDValue Chain = N.getOperand(0);
215 SDValue N1 = N.getOperand(1);
216 SDValue Offset0, Offset1, Base;
217
218 if (!SelectAddr(N, N1, Offset0, Base) ||
219 N1.getValueType() != MVT::i32)
220 return NULL;
221
222 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
223 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
224 DebugLoc dl = N.getDebugLoc();
225
226 // The second load should start after for 4 bytes.
227 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
228 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
229 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Offset0))
230 Offset1 = CurDAG->getTargetConstantPool(CP->getConstVal(),
231 MVT::i32,
232 CP->getAlignment(),
233 CP->getOffset()+4,
234 CP->getTargetFlags());
235 else
236 return NULL;
237
238 // Instead of:
239 // ldc $f0, X($3)
240 // Generate:
241 // lwc $f0, X($3)
242 // lwc $f1, X+4($3)
243 SDNode *LD0 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
244 MVT::Other, Offset0, Base, Chain);
245 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
246 dl, NVT), 0);
247 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPEVEN, dl,
248 MVT::f64, Undef, SDValue(LD0, 0));
249
250 SDNode *LD1 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
251 MVT::Other, Offset1, Base, SDValue(LD0, 1));
252 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPODD, dl,
253 MVT::f64, I0, SDValue(LD1, 0));
254
255 ReplaceUses(N, I1);
256 ReplaceUses(N.getValue(1), Chain);
257 cast<MachineSDNode>(LD0)->setMemRefs(MemRefs0, MemRefs0 + 1);
258 cast<MachineSDNode>(LD1)->setMemRefs(MemRefs0, MemRefs0 + 1);
259 return I1.getNode();
260}
261
262SDNode *MipsDAGToDAGISel::SelectStoreFp64(SDValue N) {
263
264 if (!Subtarget.isMips1() ||
265 N.getOperand(1).getValueType() != MVT::f64)
266 return NULL;
267
268 SDValue Chain = N.getOperand(0);
269
270 if (!Predicate_unindexedstore(N.getNode()) ||
271 !Predicate_store(N.getNode()))
272 return NULL;
273
274 SDValue N1 = N.getOperand(1);
275 SDValue N2 = N.getOperand(2);
276 SDValue Offset0, Offset1, Base;
277
278 if (!SelectAddr(N, N2, Offset0, Base) ||
279 N1.getValueType() != MVT::f64 ||
280 N2.getValueType() != MVT::i32)
281 return NULL;
282
283 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
284 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
285 DebugLoc dl = N.getDebugLoc();
286
287 // Get the even and odd part from the f64 register
288 SDValue FPOdd = CurDAG->getTargetExtractSubreg(Mips::SUBREG_FPODD,
289 dl, MVT::f32, N1);
290 SDValue FPEven = CurDAG->getTargetExtractSubreg(Mips::SUBREG_FPEVEN,
291 dl, MVT::f32, N1);
292
293 // The second store should start after for 4 bytes.
294 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
295 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
296 else
297 return NULL;
298
299 // Instead of:
300 // sdc $f0, X($3)
301 // Generate:
302 // swc $f0, X($3)
303 // swc $f1, X+4($3)
304 SDValue Ops0[] = { FPEven, Offset0, Base, Chain };
305 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
306 MVT::Other, Ops0, 4), 0);
307 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
308
309 SDValue Ops1[] = { FPOdd, Offset1, Base, Chain };
310 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
311 MVT::Other, Ops1, 4), 0);
312 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
313
314 ReplaceUses(N.getValue(0), Chain);
315 return Chain.getNode();
316}
317
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318/// Select instructions not customized! Used for
319/// expanded, promoted and normal instructions
Chris Lattner2c6014b2009-08-23 06:49:22 +0000320SDNode* MipsDAGToDAGISel::Select(SDValue N) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000321 SDNode *Node = N.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 unsigned Opcode = Node->getOpcode();
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000323 DebugLoc dl = Node->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 // Dump information about the Node being selected
Chris Lattner2c6014b2009-08-23 06:49:22 +0000326 DEBUG(errs().indent(Indent) << "Selecting: ";
327 Node->dump(CurDAG);
328 errs() << "\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000329 DEBUG(Indent += 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330
331 // If we have a custom node, we already have selected!
Dan Gohmanbd68c792008-07-17 19:10:17 +0000332 if (Node->isMachineOpcode()) {
Chris Lattner2c6014b2009-08-23 06:49:22 +0000333 DEBUG(errs().indent(Indent-2) << "== ";
334 Node->dump(CurDAG);
335 errs() << "\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000336 DEBUG(Indent -= 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 return NULL;
338 }
339
340 ///
Bruno Cardoso Lopes96433662007-09-24 20:15:11 +0000341 // Instruction Selection not handled by the auto-generated
342 // tablegen selection should be handled here.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 ///
344 switch(Opcode) {
345
346 default: break;
347
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000348 case ISD::SUBE:
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000349 case ISD::ADDE: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000350 SDValue InFlag = Node->getOperand(2), CmpLHS;
Chris Lattner871ec4e2008-12-14 21:38:24 +0000351 unsigned Opc = InFlag.getOpcode(); Opc=Opc;
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000352 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
353 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
354 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
355
Chris Lattner871ec4e2008-12-14 21:38:24 +0000356 unsigned MOp;
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000357 if (Opcode == ISD::ADDE) {
358 CmpLHS = InFlag.getValue(0);
359 MOp = Mips::ADDu;
360 } else {
361 CmpLHS = InFlag.getOperand(0);
362 MOp = Mips::SUBu;
363 }
364
Dan Gohman8181bd12008-07-27 21:46:04 +0000365 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000366
Dan Gohman8181bd12008-07-27 21:46:04 +0000367 SDValue LHS = Node->getOperand(0);
368 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000369
Owen Andersonac9de032009-08-10 22:56:29 +0000370 EVT VT = LHS.getValueType();
Dan Gohman61fda0d2009-09-25 18:54:59 +0000371 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
372 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
373 SDValue(Carry,0), RHS);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000374
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000375 return CurDAG->SelectNodeTo(N.getNode(), MOp, VT, MVT::Flag,
Dan Gohman8181bd12008-07-27 21:46:04 +0000376 LHS, SDValue(AddCarry,0));
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000377 }
378
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000379 /// Mul/Div with two results
380 case ISD::SDIVREM:
381 case ISD::UDIVREM:
382 case ISD::SMUL_LOHI:
383 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000384 SDValue Op1 = Node->getOperand(0);
385 SDValue Op2 = Node->getOperand(1);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000386
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000387 unsigned Op;
388 if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI)
389 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
390 else
391 Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000392
Dan Gohman61fda0d2009-09-25 18:54:59 +0000393 SDNode *Node = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000394
Dan Gohman8181bd12008-07-27 21:46:04 +0000395 SDValue InFlag = SDValue(Node, 0);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000396 SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32,
397 MVT::Flag, InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +0000398 InFlag = SDValue(Lo,1);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000399 SDNode *Hi = CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000400
401 if (!N.getValue(0).use_empty())
Dan Gohman8181bd12008-07-27 21:46:04 +0000402 ReplaceUses(N.getValue(0), SDValue(Lo,0));
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000403
404 if (!N.getValue(1).use_empty())
Dan Gohman8181bd12008-07-27 21:46:04 +0000405 ReplaceUses(N.getValue(1), SDValue(Hi,0));
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000406
407 return NULL;
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000408 }
409
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000410 /// Special Muls
411 case ISD::MUL:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 case ISD::MULHS:
413 case ISD::MULHU: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000414 SDValue MulOp1 = Node->getOperand(0);
415 SDValue MulOp2 = Node->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416
417 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000418 SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl,
419 MVT::Flag, MulOp1, MulOp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420
Dan Gohman8181bd12008-07-27 21:46:04 +0000421 SDValue InFlag = SDValue(MulNode, 0);
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000422
423 if (MulOp == ISD::MUL)
Dan Gohman61fda0d2009-09-25 18:54:59 +0000424 return CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, InFlag);
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000425 else
Dan Gohman61fda0d2009-09-25 18:54:59 +0000426 return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 }
428
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000429 /// Div/Rem operations
430 case ISD::SREM:
431 case ISD::UREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 case ISD::SDIV:
433 case ISD::UDIV: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000434 SDValue Op1 = Node->getOperand(0);
435 SDValue Op2 = Node->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000437 unsigned Op, MOp;
438 if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) {
439 Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
440 MOp = Mips::MFLO;
441 } else {
442 Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
443 MOp = Mips::MFHI;
444 }
Dan Gohman61fda0d2009-09-25 18:54:59 +0000445 SDNode *Node = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446
Dan Gohman8181bd12008-07-27 21:46:04 +0000447 SDValue InFlag = SDValue(Node, 0);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000448 return CurDAG->getMachineNode(MOp, dl, MVT::i32, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 }
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000450
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000451 // Get target GOT address.
Dan Gohman40653f32009-06-03 20:30:14 +0000452 case ISD::GLOBAL_OFFSET_TABLE:
453 return getGlobalBaseReg();
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000454
Bruno Cardoso Lopese1498352009-11-13 18:49:59 +0000455 case ISD::ConstantFP: {
456 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
457 if (N.getValueType() == MVT::f64 && CN->isExactlyValue(+0.0)) {
458 SDValue Zero = CurDAG->getRegister(Mips::ZERO, MVT::i32);
459 ReplaceUses(N, Zero);
460 return Zero.getNode();
461 }
462 break;
463 }
464
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000465 case ISD::LOAD:
466 if (SDNode *ResNode = SelectLoadFp64(N))
467 return ResNode;
468 // Other cases are autogenerated.
469 break;
470
471 case ISD::STORE:
472 if (SDNode *ResNode = SelectStoreFp64(N))
473 return ResNode;
474 // Other cases are autogenerated.
475 break;
476
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000477 /// Handle direct and indirect calls when using PIC. On PIC, when
478 /// GOT is smaller than about 64k (small code) the GA target is
479 /// loaded with only one instruction. Otherwise GA's target must
480 /// be loaded with 3 instructions.
481 case MipsISD::JmpLink: {
482 if (TM.getRelocationModel() == Reloc::PIC_) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000483 SDValue Chain = Node->getOperand(0);
484 SDValue Callee = Node->getOperand(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000485 SDValue T9Reg = CurDAG->getRegister(Mips::T9, MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +0000486 SDValue InFlag(0, 0);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000487
488 if ( (isa<GlobalAddressSDNode>(Callee)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000489 (isa<ExternalSymbolSDNode>(Callee)) )
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000490 {
491 /// Direct call for global addresses and external symbols
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000492 SDValue GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000493
494 // Use load to get GOT target
Dan Gohman8181bd12008-07-27 21:46:04 +0000495 SDValue Ops[] = { Callee, GPReg, Chain };
Dan Gohman61fda0d2009-09-25 18:54:59 +0000496 SDValue Load = SDValue(CurDAG->getMachineNode(Mips::LW, dl, MVT::i32,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000497 MVT::Other, Ops, 3), 0);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000498 Chain = Load.getValue(1);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000499
500 // Call target must be on T9
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000501 Chain = CurDAG->getCopyToReg(Chain, dl, T9Reg, Load, InFlag);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000502 } else
503 /// Indirect call
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000504 Chain = CurDAG->getCopyToReg(Chain, dl, T9Reg, Callee, InFlag);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000505
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000506 // Emit Jump and Link Register
Dan Gohman61fda0d2009-09-25 18:54:59 +0000507 SDNode *ResNode = CurDAG->getMachineNode(Mips::JALR, dl, MVT::Other,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000508 MVT::Flag, T9Reg, Chain);
Dan Gohman8181bd12008-07-27 21:46:04 +0000509 Chain = SDValue(ResNode, 0);
510 InFlag = SDValue(ResNode, 1);
511 ReplaceUses(SDValue(Node, 0), Chain);
512 ReplaceUses(SDValue(Node, 1), InFlag);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000513 return ResNode;
514 }
515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 }
517
518 // Select the default instruction
519 SDNode *ResNode = SelectCode(N);
520
Chris Lattner2c6014b2009-08-23 06:49:22 +0000521 DEBUG(errs().indent(Indent-2) << "=> ");
Gabor Greif1c80d112008-08-28 21:40:38 +0000522 if (ResNode == NULL || ResNode == N.getNode())
523 DEBUG(N.getNode()->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 else
525 DEBUG(ResNode->dump(CurDAG));
Chris Lattner2c6014b2009-08-23 06:49:22 +0000526 DEBUG(errs() << "\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000527 DEBUG(Indent -= 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528
529 return ResNode;
530}
531
532/// createMipsISelDag - This pass converts a legalized DAG into a
533/// MIPS-specific DAG, ready for instruction scheduling.
534FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
535 return new MipsDAGToDAGISel(TM);
536}