blob: c4746dba32673ac6332fd21da963a0db1dcb92f5 [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===-- MipsISelDAGToDAG.cpp - A dag to dag inst selector for Mips --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the MIPS target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "mips-isel"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "Mips.h"
16#include "MipsISelLowering.h"
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +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 Lattner84bc5422007-12-31 04:13:23 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000031#include "llvm/CodeGen/SelectionDAGISel.h"
32#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +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 Lewycky6726b6d2009-10-25 06:33:48 +000048class MipsDAGToDAGISel : public SelectionDAGISel {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000049
50 /// TM - Keep a reference to MipsTargetMachine.
51 MipsTargetMachine &TM;
52
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +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 Lopes225ca9c2008-07-05 19:05:21 +000055 const MipsSubtarget &Subtarget;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000056
57public:
Dan Gohman1002c022008-07-07 18:00:37 +000058 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +000059 SelectionDAGISel(tm),
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000060 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000062 // Pass Name
63 virtual const char *getPassName() const {
64 return "MIPS DAG->DAG Pattern Instruction Selection";
65 }
66
67
68private:
69 // Include the pieces autogenerated from the target description.
70 #include "MipsGenDAGISel.inc"
71
Dan Gohman99114052009-06-03 20:30:14 +000072 /// getTargetMachine - Return a reference to the TargetMachine, casted
73 /// to the target-specific type.
74 const MipsTargetMachine &getTargetMachine() {
75 return static_cast<const MipsTargetMachine &>(TM);
76 }
77
78 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
79 /// to the target-specific type.
80 const MipsInstrInfo *getInstrInfo() {
81 return getTargetMachine().getInstrInfo();
82 }
83
84 SDNode *getGlobalBaseReg();
Dan Gohmaneeb3a002010-01-05 01:24:18 +000085 SDNode *Select(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000086
87 // Complex Pattern.
Dan Gohmaneeb3a002010-01-05 01:24:18 +000088 bool SelectAddr(SDNode *Op, SDValue N,
Dan Gohman475871a2008-07-27 21:46:04 +000089 SDValue &Base, SDValue &Offset);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000090
Dan Gohmaneeb3a002010-01-05 01:24:18 +000091 SDNode *SelectLoadFp64(SDNode *N);
92 SDNode *SelectStoreFp64(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093
94 // getI32Imm - Return a target constant with the specified
95 // value, of type i32.
Dan Gohman475871a2008-07-27 21:46:04 +000096 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +000097 return CurDAG->getTargetConstant(Imm, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000098 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000099};
100
101}
102
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000103
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000104/// getGlobalBaseReg - Output the instructions required to put the
105/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000106SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Dan Gohman99114052009-06-03 20:30:14 +0000107 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
108 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000109}
110
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000111/// ComplexPattern used on MipsInstrInfo
112/// Used on Mips Load/Store instructions
113bool MipsDAGToDAGISel::
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000114SelectAddr(SDNode *Op, SDValue Addr, SDValue &Offset, SDValue &Base)
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000115{
116 // if Address is FI, get the TargetFrameIndex.
117 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
119 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000120 return true;
121 }
122
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000123 // on PIC code Load GA
124 if (TM.getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000125 if ((Addr.getOpcode() == ISD::TargetGlobalAddress) ||
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000126 (Addr.getOpcode() == ISD::TargetConstantPool) ||
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000127 (Addr.getOpcode() == ISD::TargetJumpTable)){
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000129 Offset = Addr;
130 return true;
131 }
132 } else {
Bill Wendling056292f2008-09-16 21:48:12 +0000133 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000134 Addr.getOpcode() == ISD::TargetGlobalAddress))
135 return false;
136 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000137
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000138 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000139 if (Addr.getOpcode() == ISD::ADD) {
140 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
141 if (Predicate_immSExt16(CN)) {
142
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000143 // If the first operand is a FI, get the TargetFI Node
144 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
145 (Addr.getOperand(0))) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000147 } else {
148 Base = Addr.getOperand(0);
149 }
150
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000152 return true;
153 }
154 }
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000155
156 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000157 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000158 // lui $2, %hi($CPI1_0)
159 // addiu $2, $2, %lo($CPI1_0)
160 // lwc1 $f0, 0($2)
161 // Generate:
162 // lui $2, %hi($CPI1_0)
163 // lwc1 $f0, %lo($CPI1_0)($2)
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000164 if ((Addr.getOperand(0).getOpcode() == MipsISD::Hi ||
165 Addr.getOperand(0).getOpcode() == ISD::LOAD) &&
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000166 Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
167 SDValue LoVal = Addr.getOperand(1);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000168 if (dyn_cast<ConstantPoolSDNode>(LoVal.getOperand(0))) {
169 Base = Addr.getOperand(0);
170 Offset = LoVal.getOperand(0);
171 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000172 }
173 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000174 }
175
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000176 Base = Addr;
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000178 return true;
179}
180
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000181SDNode *MipsDAGToDAGISel::SelectLoadFp64(SDNode *N) {
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000182 MVT::SimpleValueType NVT =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000183 N->getValueType(0).getSimpleVT().SimpleTy;
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000184
185 if (!Subtarget.isMips1() || NVT != MVT::f64)
186 return NULL;
187
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000188 if (!Predicate_unindexedload(N) ||
189 !Predicate_load(N))
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000190 return NULL;
191
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000192 SDValue Chain = N->getOperand(0);
193 SDValue N1 = N->getOperand(1);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000194 SDValue Offset0, Offset1, Base;
195
196 if (!SelectAddr(N, N1, Offset0, Base) ||
197 N1.getValueType() != MVT::i32)
198 return NULL;
199
200 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
201 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000202 DebugLoc dl = N->getDebugLoc();
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000203
204 // The second load should start after for 4 bytes.
205 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
206 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
207 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Offset0))
208 Offset1 = CurDAG->getTargetConstantPool(CP->getConstVal(),
209 MVT::i32,
210 CP->getAlignment(),
211 CP->getOffset()+4,
212 CP->getTargetFlags());
213 else
214 return NULL;
215
Bruno Cardoso Lopes37fd5372009-11-25 01:05:25 +0000216 // Choose the offsets depending on the endianess
217 if (TM.getTargetData()->isBigEndian())
218 std::swap(Offset0, Offset1);
219
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000220 // Instead of:
221 // ldc $f0, X($3)
222 // Generate:
223 // lwc $f0, X($3)
224 // lwc $f1, X+4($3)
225 SDNode *LD0 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
226 MVT::Other, Offset0, Base, Chain);
Chris Lattner518bb532010-02-09 19:54:29 +0000227 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000228 dl, NVT), 0);
229 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPEVEN, dl,
230 MVT::f64, Undef, SDValue(LD0, 0));
231
232 SDNode *LD1 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
233 MVT::Other, Offset1, Base, SDValue(LD0, 1));
234 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPODD, dl,
235 MVT::f64, I0, SDValue(LD1, 0));
236
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000237 ReplaceUses(SDValue(N, 0), I1);
238 ReplaceUses(SDValue(N, 1), Chain);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000239 cast<MachineSDNode>(LD0)->setMemRefs(MemRefs0, MemRefs0 + 1);
240 cast<MachineSDNode>(LD1)->setMemRefs(MemRefs0, MemRefs0 + 1);
241 return I1.getNode();
242}
243
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000244SDNode *MipsDAGToDAGISel::SelectStoreFp64(SDNode *N) {
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000245
246 if (!Subtarget.isMips1() ||
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000247 N->getOperand(1).getValueType() != MVT::f64)
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000248 return NULL;
249
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000250 SDValue Chain = N->getOperand(0);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000251
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000252 if (!Predicate_unindexedstore(N) ||
253 !Predicate_store(N))
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000254 return NULL;
255
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000256 SDValue N1 = N->getOperand(1);
257 SDValue N2 = N->getOperand(2);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000258 SDValue Offset0, Offset1, Base;
259
260 if (!SelectAddr(N, N2, Offset0, Base) ||
261 N1.getValueType() != MVT::f64 ||
262 N2.getValueType() != MVT::i32)
263 return NULL;
264
265 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
266 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000267 DebugLoc dl = N->getDebugLoc();
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000268
269 // Get the even and odd part from the f64 register
270 SDValue FPOdd = CurDAG->getTargetExtractSubreg(Mips::SUBREG_FPODD,
271 dl, MVT::f32, N1);
272 SDValue FPEven = CurDAG->getTargetExtractSubreg(Mips::SUBREG_FPEVEN,
273 dl, MVT::f32, N1);
274
275 // The second store should start after for 4 bytes.
276 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
277 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
278 else
279 return NULL;
280
Bruno Cardoso Lopes37fd5372009-11-25 01:05:25 +0000281 // Choose the offsets depending on the endianess
282 if (TM.getTargetData()->isBigEndian())
283 std::swap(Offset0, Offset1);
284
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000285 // Instead of:
286 // sdc $f0, X($3)
287 // Generate:
288 // swc $f0, X($3)
289 // swc $f1, X+4($3)
290 SDValue Ops0[] = { FPEven, Offset0, Base, Chain };
291 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
292 MVT::Other, Ops0, 4), 0);
293 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
294
295 SDValue Ops1[] = { FPOdd, Offset1, Base, Chain };
296 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
297 MVT::Other, Ops1, 4), 0);
298 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
299
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000300 ReplaceUses(SDValue(N, 0), Chain);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000301 return Chain.getNode();
302}
303
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000304/// Select instructions not customized! Used for
305/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000306SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000307 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000308 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000309
310 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000311 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000312
313 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000314 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000315 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000316 return NULL;
317 }
318
319 ///
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000320 // Instruction Selection not handled by the auto-generated
321 // tablegen selection should be handled here.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000322 ///
323 switch(Opcode) {
324
325 default: break;
326
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000327 case ISD::SUBE:
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000328 case ISD::ADDE: {
Dan Gohman475871a2008-07-27 21:46:04 +0000329 SDValue InFlag = Node->getOperand(2), CmpLHS;
Chris Lattner30baa952008-12-14 21:38:24 +0000330 unsigned Opc = InFlag.getOpcode(); Opc=Opc;
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000331 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
332 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
333 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
334
Chris Lattner30baa952008-12-14 21:38:24 +0000335 unsigned MOp;
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000336 if (Opcode == ISD::ADDE) {
337 CmpLHS = InFlag.getValue(0);
338 MOp = Mips::ADDu;
339 } else {
340 CmpLHS = InFlag.getOperand(0);
341 MOp = Mips::SUBu;
342 }
343
Dan Gohman475871a2008-07-27 21:46:04 +0000344 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000345
Dan Gohman475871a2008-07-27 21:46:04 +0000346 SDValue LHS = Node->getOperand(0);
347 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000348
Owen Andersone50ed302009-08-10 22:56:29 +0000349 EVT VT = LHS.getValueType();
Dan Gohman602b0c82009-09-25 18:54:59 +0000350 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
351 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
352 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000353
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000354 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Flag,
Dan Gohman475871a2008-07-27 21:46:04 +0000355 LHS, SDValue(AddCarry,0));
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000356 }
357
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000358 /// Mul/Div with two results
359 case ISD::SDIVREM:
360 case ISD::UDIVREM:
361 case ISD::SMUL_LOHI:
362 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +0000363 SDValue Op1 = Node->getOperand(0);
364 SDValue Op2 = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000365
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000366 unsigned Op;
367 if (Opcode == ISD::UMUL_LOHI || Opcode == ISD::SMUL_LOHI)
368 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
369 else
370 Op = (Opcode == ISD::UDIVREM ? Mips::DIVu : Mips::DIV);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000371
Bruno Cardoso Lopes8c5ee712010-01-19 19:57:07 +0000372 SDNode *MulDiv = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000373
Bruno Cardoso Lopes8c5ee712010-01-19 19:57:07 +0000374 SDValue InFlag = SDValue(MulDiv, 0);
Dan Gohman602b0c82009-09-25 18:54:59 +0000375 SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32,
376 MVT::Flag, InFlag);
Dan Gohman475871a2008-07-27 21:46:04 +0000377 InFlag = SDValue(Lo,1);
Dan Gohman602b0c82009-09-25 18:54:59 +0000378 SDNode *Hi = CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000379
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000380 if (!SDValue(Node, 0).use_empty())
381 ReplaceUses(SDValue(Node, 0), SDValue(Lo,0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000382
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000383 if (!SDValue(Node, 1).use_empty())
384 ReplaceUses(SDValue(Node, 1), SDValue(Hi,0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000385
386 return NULL;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000387 }
388
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000389 /// Special Muls
390 case ISD::MUL:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000391 case ISD::MULHS:
392 case ISD::MULHU: {
Dan Gohman475871a2008-07-27 21:46:04 +0000393 SDValue MulOp1 = Node->getOperand(0);
394 SDValue MulOp2 = Node->getOperand(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000395
396 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Dan Gohman602b0c82009-09-25 18:54:59 +0000397 SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl,
398 MVT::Flag, MulOp1, MulOp2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000399
Dan Gohman475871a2008-07-27 21:46:04 +0000400 SDValue InFlag = SDValue(MulNode, 0);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000401
Bruno Cardoso Lopes9a720b02010-02-01 12:16:39 +0000402 if (Opcode == ISD::MUL)
Dan Gohman602b0c82009-09-25 18:54:59 +0000403 return CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000404 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000405 return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000406 }
407
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000408 /// Div/Rem operations
409 case ISD::SREM:
410 case ISD::UREM:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000411 case ISD::SDIV:
412 case ISD::UDIV: {
Dan Gohman475871a2008-07-27 21:46:04 +0000413 SDValue Op1 = Node->getOperand(0);
414 SDValue Op2 = Node->getOperand(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000415
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000416 unsigned Op, MOp;
417 if (Opcode == ISD::SDIV || Opcode == ISD::UDIV) {
418 Op = (Opcode == ISD::SDIV ? Mips::DIV : Mips::DIVu);
419 MOp = Mips::MFLO;
420 } else {
421 Op = (Opcode == ISD::SREM ? Mips::DIV : Mips::DIVu);
422 MOp = Mips::MFHI;
423 }
Dan Gohman602b0c82009-09-25 18:54:59 +0000424 SDNode *Node = CurDAG->getMachineNode(Op, dl, MVT::Flag, Op1, Op2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000425
Dan Gohman475871a2008-07-27 21:46:04 +0000426 SDValue InFlag = SDValue(Node, 0);
Dan Gohman602b0c82009-09-25 18:54:59 +0000427 return CurDAG->getMachineNode(MOp, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000428 }
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000429
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000430 // Get target GOT address.
Dan Gohman99114052009-06-03 20:30:14 +0000431 case ISD::GLOBAL_OFFSET_TABLE:
432 return getGlobalBaseReg();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000433
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000434 case ISD::ConstantFP: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000435 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
436 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000437 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
438 Mips::ZERO, MVT::i32);
439 SDValue Undef = SDValue(
Chris Lattner518bb532010-02-09 19:54:29 +0000440 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::f64), 0);
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000441 SDNode *MTC = CurDAG->getMachineNode(Mips::MTC1, dl, MVT::f32, Zero);
442 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPEVEN, dl,
443 MVT::f64, Undef, SDValue(MTC, 0));
444 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::SUBREG_FPODD, dl,
445 MVT::f64, I0, SDValue(MTC, 0));
446 ReplaceUses(SDValue(Node, 0), I1);
447 return I1.getNode();
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000448 }
449 break;
450 }
451
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000452 case ISD::LOAD:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000453 if (SDNode *ResNode = SelectLoadFp64(Node))
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000454 return ResNode;
455 // Other cases are autogenerated.
456 break;
457
458 case ISD::STORE:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000459 if (SDNode *ResNode = SelectStoreFp64(Node))
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000460 return ResNode;
461 // Other cases are autogenerated.
462 break;
463
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000464 /// Handle direct and indirect calls when using PIC. On PIC, when
465 /// GOT is smaller than about 64k (small code) the GA target is
466 /// loaded with only one instruction. Otherwise GA's target must
467 /// be loaded with 3 instructions.
468 case MipsISD::JmpLink: {
469 if (TM.getRelocationModel() == Reloc::PIC_) {
Bruno Cardoso Lopes9201b102010-01-19 17:00:43 +0000470 unsigned LastOpNum = Node->getNumOperands()-1;
471
Dan Gohman475871a2008-07-27 21:46:04 +0000472 SDValue Chain = Node->getOperand(0);
473 SDValue Callee = Node->getOperand(1);
Bruno Cardoso Lopes9201b102010-01-19 17:00:43 +0000474 SDValue InFlag;
475
476 // Skip the incomming flag if present
477 if (Node->getOperand(LastOpNum).getValueType() == MVT::Flag)
478 LastOpNum--;
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000479
480 if ( (isa<GlobalAddressSDNode>(Callee)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000481 (isa<ExternalSymbolSDNode>(Callee)) )
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000482 {
483 /// Direct call for global addresses and external symbols
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 SDValue GPReg = CurDAG->getRegister(Mips::GP, MVT::i32);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000485
486 // Use load to get GOT target
Dan Gohman475871a2008-07-27 21:46:04 +0000487 SDValue Ops[] = { Callee, GPReg, Chain };
Dan Gohman602b0c82009-09-25 18:54:59 +0000488 SDValue Load = SDValue(CurDAG->getMachineNode(Mips::LW, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 MVT::Other, Ops, 3), 0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000490 Chain = Load.getValue(1);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000491
492 // Call target must be on T9
Bruno Cardoso Lopes9201b102010-01-19 17:00:43 +0000493 Chain = CurDAG->getCopyToReg(Chain, dl, Mips::T9, Load, InFlag);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000494 } else
495 /// Indirect call
Bruno Cardoso Lopes9201b102010-01-19 17:00:43 +0000496 Chain = CurDAG->getCopyToReg(Chain, dl, Mips::T9, Callee, InFlag);
497
498 // Map the JmpLink operands to JALR
499 SDVTList NodeTys = CurDAG->getVTList(MVT::Other, MVT::Flag);
500 SmallVector<SDValue, 8> Ops;
501 Ops.push_back(CurDAG->getRegister(Mips::T9, MVT::i32));
502
503 for (unsigned i = 2, e = LastOpNum+1; i != e; ++i)
504 Ops.push_back(Node->getOperand(i));
505 Ops.push_back(Chain);
506 Ops.push_back(Chain.getValue(1));
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000507
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000508 // Emit Jump and Link Register
Bruno Cardoso Lopes9201b102010-01-19 17:00:43 +0000509 SDNode *ResNode = CurDAG->getMachineNode(Mips::JALR, dl, NodeTys,
510 &Ops[0], Ops.size());
511
512 // Replace Chain and InFlag
513 ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
514 ReplaceUses(SDValue(Node, 1), SDValue(ResNode, 1));
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000515 return ResNode;
516 }
517 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000518 }
519
520 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000521 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000522
Chris Lattner7c306da2010-03-02 06:34:30 +0000523 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000524 if (ResNode == NULL || ResNode == Node)
525 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000526 else
527 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000528 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000529 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}