blob: b2aba2730540e99defeada31f422e9f8be042347 [file] [log] [blame]
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00001//===-- MipsISelDAGToDAG.cpp - A dag to dag inst selector for Mips --------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
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//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines an instruction selector for the MIPS target.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
14#define DEBUG_TYPE "mips-isel"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "Mips.h"
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000016#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000017#include "MipsRegisterInfo.h"
18#include "MipsSubtarget.h"
19#include "MipsTargetMachine.h"
20#include "llvm/GlobalValue.h"
21#include "llvm/Instructions.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/Support/CFG.h"
24#include "llvm/Type.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000030#include "llvm/CodeGen/SelectionDAGISel.h"
31#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035using namespace llvm;
36
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000037//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038// Instruction Selector Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000039//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000041//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
43// instructions for SelectionDAG operations.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000044//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000045namespace {
46
Nick Lewycky6726b6d2009-10-25 06:33:48 +000047class MipsDAGToDAGISel : public SelectionDAGISel {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000048
49 /// TM - Keep a reference to MipsTargetMachine.
50 MipsTargetMachine &TM;
51
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000052 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
53 /// make the right decision when generating code for different targets.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000054 const MipsSubtarget &Subtarget;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000055
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000056public:
Dan Gohman1002c022008-07-07 18:00:37 +000057 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +000058 SelectionDAGISel(tm),
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000059 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000060
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000061 // Pass Name
62 virtual const char *getPassName() const {
63 return "MIPS DAG->DAG Pattern Instruction Selection";
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000064 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000065
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000066
67private:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068 // Include the pieces autogenerated from the target description.
69 #include "MipsGenDAGISel.inc"
70
Dan Gohman99114052009-06-03 20:30:14 +000071 /// getTargetMachine - Return a reference to the TargetMachine, casted
72 /// to the target-specific type.
73 const MipsTargetMachine &getTargetMachine() {
74 return static_cast<const MipsTargetMachine &>(TM);
75 }
76
77 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
78 /// to the target-specific type.
79 const MipsInstrInfo *getInstrInfo() {
80 return getTargetMachine().getInstrInfo();
81 }
82
83 SDNode *getGlobalBaseReg();
Dan Gohmaneeb3a002010-01-05 01:24:18 +000084 SDNode *Select(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000085
86 // Complex Pattern.
Chris Lattner52a261b2010-09-21 20:31:19 +000087 bool SelectAddr(SDValue N, SDValue &Base, SDValue &Offset);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000088
Dan Gohmaneeb3a002010-01-05 01:24:18 +000089 SDNode *SelectLoadFp64(SDNode *N);
90 SDNode *SelectStoreFp64(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000091
92 // getI32Imm - Return a target constant with the specified
93 // value, of type i32.
Dan Gohman475871a2008-07-27 21:46:04 +000094 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +000095 return CurDAG->getTargetConstant(Imm, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000096 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000097};
98
99}
100
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000101
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000102/// getGlobalBaseReg - Output the instructions required to put the
103/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000104SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Dan Gohman99114052009-06-03 20:30:14 +0000105 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
106 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000107}
108
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000109/// ComplexPattern used on MipsInstrInfo
110/// Used on Mips Load/Store instructions
111bool MipsDAGToDAGISel::
Chris Lattner52a261b2010-09-21 20:31:19 +0000112SelectAddr(SDValue Addr, SDValue &Offset, SDValue &Base) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000113 // if Address is FI, get the TargetFrameIndex.
114 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
116 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000117 return true;
118 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000119
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000120 // on PIC code Load GA
121 if (TM.getRelocationModel() == Reloc::PIC_) {
Akira Hatanaka342837d2011-05-28 01:07:07 +0000122 if (Addr.getOpcode() == MipsISD::WrapperPIC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
Akira Hatanaka342837d2011-05-28 01:07:07 +0000124 Offset = Addr.getOperand(0);
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000125 return true;
126 }
127 } else {
Bill Wendling056292f2008-09-16 21:48:12 +0000128 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000129 Addr.getOpcode() == ISD::TargetGlobalAddress))
130 return false;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000131 else if (Addr.getOpcode() == ISD::TargetGlobalTLSAddress) {
132 Base = CurDAG->getRegister(Mips::GP, MVT::i32);
133 Offset = Addr;
134 return true;
135 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000136 }
137
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))) {
Jakob Stoklund Olesen7552a3d2010-08-18 23:56:46 +0000141 if (isInt<16>(CN->getSExtValue())) {
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000142
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 Lopes81092dc2011-03-04 17:51:39 +0000164 if ((Addr.getOperand(0).getOpcode() == MipsISD::Hi ||
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000165 Addr.getOperand(0).getOpcode() == ISD::LOAD) &&
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000166 Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000167 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 Lopes81092dc2011-03-04 17:51:39 +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
Jakob Stoklund Olesen7fa846f2010-09-03 00:35:13 +0000188 LoadSDNode *LN = cast<LoadSDNode>(N);
189 if (LN->getExtensionType() != ISD::NON_EXTLOAD ||
190 LN->getAddressingMode() != ISD::UNINDEXED)
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000191 return NULL;
192
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000193 SDValue Chain = N->getOperand(0);
194 SDValue N1 = N->getOperand(1);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000195 SDValue Offset0, Offset1, Base;
196
Chris Lattner52a261b2010-09-21 20:31:19 +0000197 if (!SelectAddr(N1, Offset0, Base) ||
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000198 N1.getValueType() != MVT::i32)
199 return NULL;
200
201 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
202 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000203 DebugLoc dl = N->getDebugLoc();
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000204
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000205 // The second load should start after for 4 bytes.
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000206 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
207 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
208 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Offset0))
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000209 Offset1 = CurDAG->getTargetConstantPool(CP->getConstVal(),
210 MVT::i32,
211 CP->getAlignment(),
212 CP->getOffset()+4,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000213 CP->getTargetFlags());
214 else
215 return NULL;
216
Bruno Cardoso Lopes37fd5372009-11-25 01:05:25 +0000217 // Choose the offsets depending on the endianess
218 if (TM.getTargetData()->isBigEndian())
219 std::swap(Offset0, Offset1);
220
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000221 // Instead of:
222 // ldc $f0, X($3)
223 // Generate:
224 // lwc $f0, X($3)
225 // lwc $f1, X+4($3)
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000226 SDNode *LD0 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000227 MVT::Other, Offset0, Base, Chain);
Chris Lattner518bb532010-02-09 19:54:29 +0000228 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000229 dl, NVT), 0);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000230 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::sub_fpeven, dl,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000231 MVT::f64, Undef, SDValue(LD0, 0));
232
233 SDNode *LD1 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
234 MVT::Other, Offset1, Base, SDValue(LD0, 1));
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000235 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::sub_fpodd, dl,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000236 MVT::f64, I0, SDValue(LD1, 0));
237
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000238 ReplaceUses(SDValue(N, 0), I1);
239 ReplaceUses(SDValue(N, 1), Chain);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000240 cast<MachineSDNode>(LD0)->setMemRefs(MemRefs0, MemRefs0 + 1);
241 cast<MachineSDNode>(LD1)->setMemRefs(MemRefs0, MemRefs0 + 1);
242 return I1.getNode();
243}
244
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000245SDNode *MipsDAGToDAGISel::SelectStoreFp64(SDNode *N) {
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000246
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000247 if (!Subtarget.isMips1() ||
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000248 N->getOperand(1).getValueType() != MVT::f64)
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000249 return NULL;
250
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000251 SDValue Chain = N->getOperand(0);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000252
Jakob Stoklund Olesen7552a3d2010-08-18 23:56:46 +0000253 StoreSDNode *SN = cast<StoreSDNode>(N);
254 if (SN->isTruncatingStore() || SN->getAddressingMode() != ISD::UNINDEXED)
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000255 return NULL;
256
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000257 SDValue N1 = N->getOperand(1);
258 SDValue N2 = N->getOperand(2);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000259 SDValue Offset0, Offset1, Base;
260
Chris Lattner52a261b2010-09-21 20:31:19 +0000261 if (!SelectAddr(N2, Offset0, Base) ||
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000262 N1.getValueType() != MVT::f64 ||
263 N2.getValueType() != MVT::i32)
264 return NULL;
265
266 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
267 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000268 DebugLoc dl = N->getDebugLoc();
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000269
270 // Get the even and odd part from the f64 register
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000271 SDValue FPOdd = CurDAG->getTargetExtractSubreg(Mips::sub_fpodd,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000272 dl, MVT::f32, N1);
Jakob Stoklund Olesenfff916a2010-05-24 17:42:58 +0000273 SDValue FPEven = CurDAG->getTargetExtractSubreg(Mips::sub_fpeven,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000274 dl, MVT::f32, N1);
275
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000276 // The second store should start after for 4 bytes.
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000277 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
278 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
279 else
280 return NULL;
281
Bruno Cardoso Lopes37fd5372009-11-25 01:05:25 +0000282 // Choose the offsets depending on the endianess
283 if (TM.getTargetData()->isBigEndian())
284 std::swap(Offset0, Offset1);
285
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000286 // Instead of:
287 // sdc $f0, X($3)
288 // Generate:
289 // swc $f0, X($3)
290 // swc $f1, X+4($3)
291 SDValue Ops0[] = { FPEven, Offset0, Base, Chain };
292 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
293 MVT::Other, Ops0, 4), 0);
294 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
295
296 SDValue Ops1[] = { FPOdd, Offset1, Base, Chain };
297 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
298 MVT::Other, Ops1, 4), 0);
299 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
300
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000301 ReplaceUses(SDValue(N, 0), Chain);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000302 return Chain.getNode();
303}
304
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000305/// Select instructions not customized! Used for
306/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000307SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000308 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000309 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000310
311 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000312 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000313
314 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000315 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000316 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000317 return NULL;
318 }
319
320 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000321 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000322 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000323 ///
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000324 switch(Opcode) {
325
326 default: break;
327
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000328 case ISD::SUBE:
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000329 case ISD::ADDE: {
Dan Gohman475871a2008-07-27 21:46:04 +0000330 SDValue InFlag = Node->getOperand(2), CmpLHS;
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000331 unsigned Opc = InFlag.getOpcode(); (void)Opc;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000332 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
333 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000334 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
335
Chris Lattner30baa952008-12-14 21:38:24 +0000336 unsigned MOp;
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000337 if (Opcode == ISD::ADDE) {
338 CmpLHS = InFlag.getValue(0);
339 MOp = Mips::ADDu;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000340 } else {
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000341 CmpLHS = InFlag.getOperand(0);
342 MOp = Mips::SUBu;
343 }
344
Dan Gohman475871a2008-07-27 21:46:04 +0000345 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000346
Dan Gohman475871a2008-07-27 21:46:04 +0000347 SDValue LHS = Node->getOperand(0);
348 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000349
Owen Andersone50ed302009-08-10 22:56:29 +0000350 EVT VT = LHS.getValueType();
Dan Gohman602b0c82009-09-25 18:54:59 +0000351 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000352 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
Dan Gohman602b0c82009-09-25 18:54:59 +0000353 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000354
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000355 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
Dan Gohman475871a2008-07-27 21:46:04 +0000356 LHS, SDValue(AddCarry,0));
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000357 }
358
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000359 /// Mul/Div with two results
360 case ISD::SDIVREM:
361 case ISD::UDIVREM:
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000362 break;
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000363 case ISD::SMUL_LOHI:
364 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +0000365 SDValue Op1 = Node->getOperand(0);
366 SDValue Op2 = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000367
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000368 unsigned Op;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000369 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000370
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000371 SDNode *Mul = CurDAG->getMachineNode(Op, dl, MVT::Glue, Op1, Op2);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000372
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000373 SDValue InFlag = SDValue(Mul, 0);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000374 SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000375 MVT::Glue, InFlag);
Dan Gohman475871a2008-07-27 21:46:04 +0000376 InFlag = SDValue(Lo,1);
Dan Gohman602b0c82009-09-25 18:54:59 +0000377 SDNode *Hi = CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000378
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000379 if (!SDValue(Node, 0).use_empty())
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000380 ReplaceUses(SDValue(Node, 0), SDValue(Lo,0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000381
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000382 if (!SDValue(Node, 1).use_empty())
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000383 ReplaceUses(SDValue(Node, 1), SDValue(Hi,0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000384
385 return NULL;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000386 }
387
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000388 /// Special Muls
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000389 case ISD::MUL:
Bruno Cardoso Lopes7d5652d2010-11-12 00:38:32 +0000390 if (Subtarget.isMips32())
391 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000392 case ISD::MULHS:
393 case ISD::MULHU: {
Dan Gohman475871a2008-07-27 21:46:04 +0000394 SDValue MulOp1 = Node->getOperand(0);
395 SDValue MulOp2 = Node->getOperand(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000396
397 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000398 SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000399 MVT::Glue, MulOp1, MulOp2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000400
Dan Gohman475871a2008-07-27 21:46:04 +0000401 SDValue InFlag = SDValue(MulNode, 0);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000402
Bruno Cardoso Lopes9a720b02010-02-01 12:16:39 +0000403 if (Opcode == ISD::MUL)
Dan Gohman602b0c82009-09-25 18:54:59 +0000404 return CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000405 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000406 return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000407 }
408
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000409 /// Div/Rem operations
410 case ISD::SREM:
411 case ISD::UREM:
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000412 case ISD::SDIV:
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000413 case ISD::UDIV:
414 break;
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000415
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000416 // Get target GOT address.
Dan Gohman99114052009-06-03 20:30:14 +0000417 case ISD::GLOBAL_OFFSET_TABLE:
418 return getGlobalBaseReg();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000419
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000420 case ISD::ConstantFP: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000421 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000422 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
423 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000424 Mips::ZERO, MVT::i32);
425 SDValue Undef = SDValue(
Chris Lattner518bb532010-02-09 19:54:29 +0000426 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::f64), 0);
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000427 SDNode *MTC = CurDAG->getMachineNode(Mips::MTC1, dl, MVT::f32, Zero);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000428 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::sub_fpeven, dl,
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000429 MVT::f64, Undef, SDValue(MTC, 0));
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000430 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::sub_fpodd, dl,
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000431 MVT::f64, I0, SDValue(MTC, 0));
432 ReplaceUses(SDValue(Node, 0), I1);
433 return I1.getNode();
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000434 }
435 break;
436 }
437
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000438 case ISD::LOAD:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000439 if (SDNode *ResNode = SelectLoadFp64(Node))
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000440 return ResNode;
441 // Other cases are autogenerated.
442 break;
443
444 case ISD::STORE:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000445 if (SDNode *ResNode = SelectStoreFp64(Node))
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000446 return ResNode;
447 // Other cases are autogenerated.
448 break;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000449
450 case MipsISD::ThreadPointer: {
451 unsigned SrcReg = Mips::HWR29;
452 unsigned DestReg = Mips::V1;
453 SDNode *Rdhwr = CurDAG->getMachineNode(Mips::RDHWR, Node->getDebugLoc(),
454 Node->getValueType(0), CurDAG->getRegister(SrcReg, MVT::i32));
455 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
456 SDValue(Rdhwr, 0));
457 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, MVT::i32);
458 ReplaceUses(SDValue(Node, 0), ResNode);
459 return ResNode.getNode();
460 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000461 }
462
463 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000464 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000465
Chris Lattner7c306da2010-03-02 06:34:30 +0000466 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000467 if (ResNode == NULL || ResNode == Node)
468 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000469 else
470 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000471 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000472 return ResNode;
473}
474
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000475/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000476/// MIPS-specific DAG, ready for instruction scheduling.
477FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
478 return new MipsDAGToDAGISel(TM);
479}