blob: f1d4a67c81a259f23baa5b93c3c07cf5bd219ecc [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 Gohman5f082a72010-01-05 01:24:18 +000087 SDNode *Select(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
89 // Complex Pattern.
Dan Gohman5f082a72010-01-05 01:24:18 +000090 bool SelectAddr(SDNode *Op, SDValue N,
Dan Gohman8181bd12008-07-27 21:46:04 +000091 SDValue &Base, SDValue &Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
Dan Gohman5f082a72010-01-05 01:24:18 +000093 SDNode *SelectLoadFp64(SDNode *N);
94 SDNode *SelectStoreFp64(SDNode *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 Gohman5f082a72010-01-05 01:24:18 +0000135SelectAddr(SDNode *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) ||
Bruno Cardoso Lopeseba44ce2009-11-25 12:17:58 +0000147 (Addr.getOpcode() == ISD::TargetConstantPool) ||
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000148 (Addr.getOpcode() == ISD::TargetJumpTable)){
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000149 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000150 Offset = Addr;
151 return true;
152 }
153 } else {
Bill Wendlingfef06052008-09-16 21:48:12 +0000154 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000155 Addr.getOpcode() == ISD::TargetGlobalAddress))
156 return false;
157 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Bruno Cardoso Lopes77ba7152007-08-18 02:16:30 +0000159 // Operand is a result from an ADD.
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000160 if (Addr.getOpcode() == ISD::ADD) {
161 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
162 if (Predicate_immSExt16(CN)) {
163
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 // If the first operand is a FI, get the TargetFI Node
165 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
166 (Addr.getOperand(0))) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000167 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 } else {
169 Base = Addr.getOperand(0);
170 }
171
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000172 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 return true;
174 }
175 }
Bruno Cardoso Lopes5ad52d02009-11-16 04:33:42 +0000176
177 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopeseba44ce2009-11-25 12:17:58 +0000178 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes5ad52d02009-11-16 04:33:42 +0000179 // lui $2, %hi($CPI1_0)
180 // addiu $2, $2, %lo($CPI1_0)
181 // lwc1 $f0, 0($2)
182 // Generate:
183 // lui $2, %hi($CPI1_0)
184 // lwc1 $f0, %lo($CPI1_0)($2)
Bruno Cardoso Lopeseba44ce2009-11-25 12:17:58 +0000185 if ((Addr.getOperand(0).getOpcode() == MipsISD::Hi ||
186 Addr.getOperand(0).getOpcode() == ISD::LOAD) &&
Bruno Cardoso Lopes5ad52d02009-11-16 04:33:42 +0000187 Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
188 SDValue LoVal = Addr.getOperand(1);
Bruno Cardoso Lopeseba44ce2009-11-25 12:17:58 +0000189 if (dyn_cast<ConstantPoolSDNode>(LoVal.getOperand(0))) {
190 Base = Addr.getOperand(0);
191 Offset = LoVal.getOperand(0);
192 return true;
Bruno Cardoso Lopes5ad52d02009-11-16 04:33:42 +0000193 }
194 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196
197 Base = Addr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000198 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 return true;
200}
201
Dan Gohman5f082a72010-01-05 01:24:18 +0000202SDNode *MipsDAGToDAGISel::SelectLoadFp64(SDNode *N) {
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000203 MVT::SimpleValueType NVT =
Dan Gohman5f082a72010-01-05 01:24:18 +0000204 N->getValueType(0).getSimpleVT().SimpleTy;
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000205
206 if (!Subtarget.isMips1() || NVT != MVT::f64)
207 return NULL;
208
Dan Gohman5f082a72010-01-05 01:24:18 +0000209 if (!Predicate_unindexedload(N) ||
210 !Predicate_load(N))
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000211 return NULL;
212
Dan Gohman5f082a72010-01-05 01:24:18 +0000213 SDValue Chain = N->getOperand(0);
214 SDValue N1 = N->getOperand(1);
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000215 SDValue Offset0, Offset1, Base;
216
217 if (!SelectAddr(N, N1, Offset0, Base) ||
218 N1.getValueType() != MVT::i32)
219 return NULL;
220
221 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
222 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
Dan Gohman5f082a72010-01-05 01:24:18 +0000223 DebugLoc dl = N->getDebugLoc();
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000224
225 // The second load should start after for 4 bytes.
226 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
227 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
228 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Offset0))
229 Offset1 = CurDAG->getTargetConstantPool(CP->getConstVal(),
230 MVT::i32,
231 CP->getAlignment(),
232 CP->getOffset()+4,
233 CP->getTargetFlags());
234 else
235 return NULL;
236
Bruno Cardoso Lopes421b7002009-11-25 01:05:25 +0000237 // Choose the offsets depending on the endianess
238 if (TM.getTargetData()->isBigEndian())
239 std::swap(Offset0, Offset1);
240
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000241 // Instead of:
242 // ldc $f0, X($3)
243 // Generate:
244 // lwc $f0, X($3)
245 // lwc $f1, X+4($3)
246 SDNode *LD0 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
247 MVT::Other, Offset0, Base, Chain);
Chris Lattner4052b292010-02-09 19:54:29 +0000248 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000249 dl, NVT), 0);
250 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPEVEN, dl,
251 MVT::f64, Undef, SDValue(LD0, 0));
252
253 SDNode *LD1 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
254 MVT::Other, Offset1, Base, SDValue(LD0, 1));
255 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPODD, dl,
256 MVT::f64, I0, SDValue(LD1, 0));
257
Dan Gohman5f082a72010-01-05 01:24:18 +0000258 ReplaceUses(SDValue(N, 0), I1);
259 ReplaceUses(SDValue(N, 1), Chain);
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000260 cast<MachineSDNode>(LD0)->setMemRefs(MemRefs0, MemRefs0 + 1);
261 cast<MachineSDNode>(LD1)->setMemRefs(MemRefs0, MemRefs0 + 1);
262 return I1.getNode();
263}
264
Dan Gohman5f082a72010-01-05 01:24:18 +0000265SDNode *MipsDAGToDAGISel::SelectStoreFp64(SDNode *N) {
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000266
267 if (!Subtarget.isMips1() ||
Dan Gohman5f082a72010-01-05 01:24:18 +0000268 N->getOperand(1).getValueType() != MVT::f64)
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000269 return NULL;
270
Dan Gohman5f082a72010-01-05 01:24:18 +0000271 SDValue Chain = N->getOperand(0);
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000272
Dan Gohman5f082a72010-01-05 01:24:18 +0000273 if (!Predicate_unindexedstore(N) ||
274 !Predicate_store(N))
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000275 return NULL;
276
Dan Gohman5f082a72010-01-05 01:24:18 +0000277 SDValue N1 = N->getOperand(1);
278 SDValue N2 = N->getOperand(2);
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000279 SDValue Offset0, Offset1, Base;
280
281 if (!SelectAddr(N, N2, Offset0, Base) ||
282 N1.getValueType() != MVT::f64 ||
283 N2.getValueType() != MVT::i32)
284 return NULL;
285
286 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
287 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
Dan Gohman5f082a72010-01-05 01:24:18 +0000288 DebugLoc dl = N->getDebugLoc();
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000289
290 // Get the even and odd part from the f64 register
291 SDValue FPOdd = CurDAG->getTargetExtractSubreg(Mips::SUBREG_FPODD,
292 dl, MVT::f32, N1);
293 SDValue FPEven = CurDAG->getTargetExtractSubreg(Mips::SUBREG_FPEVEN,
294 dl, MVT::f32, N1);
295
296 // The second store should start after for 4 bytes.
297 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
298 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
299 else
300 return NULL;
301
Bruno Cardoso Lopes421b7002009-11-25 01:05:25 +0000302 // Choose the offsets depending on the endianess
303 if (TM.getTargetData()->isBigEndian())
304 std::swap(Offset0, Offset1);
305
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000306 // Instead of:
307 // sdc $f0, X($3)
308 // Generate:
309 // swc $f0, X($3)
310 // swc $f1, X+4($3)
311 SDValue Ops0[] = { FPEven, Offset0, Base, Chain };
312 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
313 MVT::Other, Ops0, 4), 0);
314 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
315
316 SDValue Ops1[] = { FPOdd, Offset1, Base, Chain };
317 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
318 MVT::Other, Ops1, 4), 0);
319 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
320
Dan Gohman5f082a72010-01-05 01:24:18 +0000321 ReplaceUses(SDValue(N, 0), Chain);
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000322 return Chain.getNode();
323}
324
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325/// Select instructions not customized! Used for
326/// expanded, promoted and normal instructions
Dan Gohman5f082a72010-01-05 01:24:18 +0000327SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 unsigned Opcode = Node->getOpcode();
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000329 DebugLoc dl = Node->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330
331 // Dump information about the Node being selected
Chris Lattner2c6014b2009-08-23 06:49:22 +0000332 DEBUG(errs().indent(Indent) << "Selecting: ";
333 Node->dump(CurDAG);
334 errs() << "\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000335 DEBUG(Indent += 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336
337 // If we have a custom node, we already have selected!
Dan Gohmanbd68c792008-07-17 19:10:17 +0000338 if (Node->isMachineOpcode()) {
Chris Lattner2c6014b2009-08-23 06:49:22 +0000339 DEBUG(errs().indent(Indent-2) << "== ";
340 Node->dump(CurDAG);
341 errs() << "\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000342 DEBUG(Indent -= 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 return NULL;
344 }
345
346 ///
Bruno Cardoso Lopes96433662007-09-24 20:15:11 +0000347 // Instruction Selection not handled by the auto-generated
348 // tablegen selection should be handled here.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 ///
350 switch(Opcode) {
351
352 default: break;
353
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000354 case ISD::SUBE:
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000355 case ISD::ADDE: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000356 SDValue InFlag = Node->getOperand(2), CmpLHS;
Chris Lattner871ec4e2008-12-14 21:38:24 +0000357 unsigned Opc = InFlag.getOpcode(); Opc=Opc;
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000358 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
359 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
360 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
361
Chris Lattner871ec4e2008-12-14 21:38:24 +0000362 unsigned MOp;
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000363 if (Opcode == ISD::ADDE) {
364 CmpLHS = InFlag.getValue(0);
365 MOp = Mips::ADDu;
366 } else {
367 CmpLHS = InFlag.getOperand(0);
368 MOp = Mips::SUBu;
369 }
370
Dan Gohman8181bd12008-07-27 21:46:04 +0000371 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000372
Dan Gohman8181bd12008-07-27 21:46:04 +0000373 SDValue LHS = Node->getOperand(0);
374 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000375
Owen Andersonac9de032009-08-10 22:56:29 +0000376 EVT VT = LHS.getValueType();
Dan Gohman61fda0d2009-09-25 18:54:59 +0000377 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
378 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
379 SDValue(Carry,0), RHS);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000380
Dan Gohman5f082a72010-01-05 01:24:18 +0000381 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Flag,
Dan Gohman8181bd12008-07-27 21:46:04 +0000382 LHS, SDValue(AddCarry,0));
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000383 }
384
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000385 /// Mul/Div with two results
386 case ISD::SDIVREM:
387 case ISD::UDIVREM:
388 case ISD::SMUL_LOHI:
389 case ISD::UMUL_LOHI: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000390 SDValue Op1 = Node->getOperand(0);
391 SDValue Op2 = Node->getOperand(1);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000392
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000393 unsigned Op;
394 if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI)
395 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
396 else
397 Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000398
Bruno Cardoso Lopesda4298e2010-01-19 19:57:07 +0000399 SDNode *MulDiv = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2);
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000400
Bruno Cardoso Lopesda4298e2010-01-19 19:57:07 +0000401 SDValue InFlag = SDValue(MulDiv, 0);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000402 SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32,
403 MVT::Flag, InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +0000404 InFlag = SDValue(Lo,1);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000405 SDNode *Hi = CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000406
Dan Gohman5f082a72010-01-05 01:24:18 +0000407 if (!SDValue(Node, 0).use_empty())
408 ReplaceUses(SDValue(Node, 0), SDValue(Lo,0));
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000409
Dan Gohman5f082a72010-01-05 01:24:18 +0000410 if (!SDValue(Node, 1).use_empty())
411 ReplaceUses(SDValue(Node, 1), SDValue(Hi,0));
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000412
413 return NULL;
Bruno Cardoso Lopesed7723d2008-06-06 00:58:26 +0000414 }
415
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000416 /// Special Muls
417 case ISD::MUL:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 case ISD::MULHS:
419 case ISD::MULHU: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000420 SDValue MulOp1 = Node->getOperand(0);
421 SDValue MulOp2 = Node->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422
423 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000424 SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl,
425 MVT::Flag, MulOp1, MulOp2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426
Dan Gohman8181bd12008-07-27 21:46:04 +0000427 SDValue InFlag = SDValue(MulNode, 0);
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000428
Bruno Cardoso Lopesf2d67622010-02-01 12:16:39 +0000429 if (Opcode == ISD::MUL)
Dan Gohman61fda0d2009-09-25 18:54:59 +0000430 return CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, InFlag);
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000431 else
Dan Gohman61fda0d2009-09-25 18:54:59 +0000432 return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 }
434
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000435 /// Div/Rem operations
436 case ISD::SREM:
437 case ISD::UREM:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 case ISD::SDIV:
439 case ISD::UDIV: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000440 SDValue Op1 = Node->getOperand(0);
441 SDValue Op2 = Node->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442
Bruno Cardoso Lopesf2377552008-06-06 06:37:31 +0000443 unsigned Op, MOp;
444 if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) {
445 Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
446 MOp = Mips::MFLO;
447 } else {
448 Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
449 MOp = Mips::MFHI;
450 }
Dan Gohman61fda0d2009-09-25 18:54:59 +0000451 SDNode *Node = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452
Dan Gohman8181bd12008-07-27 21:46:04 +0000453 SDValue InFlag = SDValue(Node, 0);
Dan Gohman61fda0d2009-09-25 18:54:59 +0000454 return CurDAG->getMachineNode(MOp, dl, MVT::i32, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 }
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000456
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000457 // Get target GOT address.
Dan Gohman40653f32009-06-03 20:30:14 +0000458 case ISD::GLOBAL_OFFSET_TABLE:
459 return getGlobalBaseReg();
Bruno Cardoso Lopesea377302007-11-12 19:49:57 +0000460
Bruno Cardoso Lopese1498352009-11-13 18:49:59 +0000461 case ISD::ConstantFP: {
Dan Gohman5f082a72010-01-05 01:24:18 +0000462 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
463 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
Bruno Cardoso Lopesbca73852010-01-19 12:53:04 +0000464 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
465 Mips::ZERO, MVT::i32);
466 SDValue Undef = SDValue(
Chris Lattner4052b292010-02-09 19:54:29 +0000467 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::f64), 0);
Bruno Cardoso Lopesbca73852010-01-19 12:53:04 +0000468 SDNode *MTC = CurDAG->getMachineNode(Mips::MTC1, dl, MVT::f32, Zero);
469 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPEVEN, dl,
470 MVT::f64, Undef, SDValue(MTC, 0));
471 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPODD, dl,
472 MVT::f64, I0, SDValue(MTC, 0));
473 ReplaceUses(SDValue(Node, 0), I1);
474 return I1.getNode();
Bruno Cardoso Lopese1498352009-11-13 18:49:59 +0000475 }
476 break;
477 }
478
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000479 case ISD::LOAD:
Dan Gohman5f082a72010-01-05 01:24:18 +0000480 if (SDNode *ResNode = SelectLoadFp64(Node))
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000481 return ResNode;
482 // Other cases are autogenerated.
483 break;
484
485 case ISD::STORE:
Dan Gohman5f082a72010-01-05 01:24:18 +0000486 if (SDNode *ResNode = SelectStoreFp64(Node))
Bruno Cardoso Lopes59b23542009-11-19 06:06:13 +0000487 return ResNode;
488 // Other cases are autogenerated.
489 break;
490
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000491 /// Handle direct and indirect calls when using PIC. On PIC, when
492 /// GOT is smaller than about 64k (small code) the GA target is
493 /// loaded with only one instruction. Otherwise GA's target must
494 /// be loaded with 3 instructions.
495 case MipsISD::JmpLink: {
496 if (TM.getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopes03ae5de2010-01-19 17:00:43 +0000497 unsigned LastOpNum = Node->getNumOperands()-1;
498
Dan Gohman8181bd12008-07-27 21:46:04 +0000499 SDValue Chain = Node->getOperand(0);
500 SDValue Callee = Node->getOperand(1);
Bruno Cardoso Lopes03ae5de2010-01-19 17:00:43 +0000501 SDValue InFlag;
502
503 // Skip the incomming flag if present
504 if (Node->getOperand(LastOpNum).getValueType() == MVT::Flag)
505 LastOpNum--;
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000506
507 if ( (isa<GlobalAddressSDNode>(Callee)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000508 (isa<ExternalSymbolSDNode>(Callee)) )
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000509 {
510 /// Direct call for global addresses and external symbols
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000511 SDValue GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000512
513 // Use load to get GOT target
Dan Gohman8181bd12008-07-27 21:46:04 +0000514 SDValue Ops[] = { Callee, GPReg, Chain };
Dan Gohman61fda0d2009-09-25 18:54:59 +0000515 SDValue Load = SDValue(CurDAG->getMachineNode(Mips::LW, dl, MVT::i32,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000516 MVT::Other, Ops, 3), 0);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000517 Chain = Load.getValue(1);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000518
519 // Call target must be on T9
Bruno Cardoso Lopes03ae5de2010-01-19 17:00:43 +0000520 Chain = CurDAG->getCopyToReg(Chain, dl, Mips::T9, Load, InFlag);
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000521 } else
522 /// Indirect call
Bruno Cardoso Lopes03ae5de2010-01-19 17:00:43 +0000523 Chain = CurDAG->getCopyToReg(Chain, dl, Mips::T9, Callee, InFlag);
524
525 // Map the JmpLink operands to JALR
526 SDVTList NodeTys = CurDAG->getVTList(MVT::Other, MVT::Flag);
527 SmallVector<SDValue, 8> Ops;
528 Ops.push_back(CurDAG->getRegister(Mips::T9, MVT::i32));
529
530 for (unsigned i = 2, e = LastOpNum+1; i != e; ++i)
531 Ops.push_back(Node->getOperand(i));
532 Ops.push_back(Chain);
533 Ops.push_back(Chain.getValue(1));
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000534
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000535 // Emit Jump and Link Register
Bruno Cardoso Lopes03ae5de2010-01-19 17:00:43 +0000536 SDNode *ResNode = CurDAG->getMachineNode(Mips::JALR, dl, NodeTys,
537 &Ops[0], Ops.size());
538
539 // Replace Chain and InFlag
540 ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
541 ReplaceUses(SDValue(Node, 1), SDValue(ResNode, 1));
Bruno Cardoso Lopes218d5822007-11-05 03:02:32 +0000542 return ResNode;
543 }
544 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 }
546
547 // Select the default instruction
Dan Gohman5f082a72010-01-05 01:24:18 +0000548 SDNode *ResNode = SelectCode(Node);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549
Chris Lattner2c6014b2009-08-23 06:49:22 +0000550 DEBUG(errs().indent(Indent-2) << "=> ");
Dan Gohman5f082a72010-01-05 01:24:18 +0000551 if (ResNode == NULL || ResNode == Node)
552 DEBUG(Node->dump(CurDAG));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 else
554 DEBUG(ResNode->dump(CurDAG));
Chris Lattner2c6014b2009-08-23 06:49:22 +0000555 DEBUG(errs() << "\n");
Daniel Dunbar33b26632009-08-23 08:50:52 +0000556 DEBUG(Indent -= 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557
558 return ResNode;
559}
560
561/// createMipsISelDag - This pass converts a legalized DAG into a
562/// MIPS-specific DAG, ready for instruction scheduling.
563FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
564 return new MipsDAGToDAGISel(TM);
565}