blob: c0c1c30753e3f1036199c1d999b52afd9863c9bd [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
Akira Hatanaka5e069032011-06-02 01:03:14 +0000138 // Addresses of the form FI+const or FI|const
139 if (CurDAG->isBaseWithConstantOffset(Addr)) {
140 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
141 if (isInt<16>(CN->getSExtValue())) {
142
143 // If the first operand is a FI, get the TargetFI Node
144 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
145 (Addr.getOperand(0)))
146 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
147 else
148 Base = Addr.getOperand(0);
149
150 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
151 return true;
152 }
153 }
154
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000155 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000156 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000157 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000158 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000159 // lui $2, %hi($CPI1_0)
160 // addiu $2, $2, %lo($CPI1_0)
161 // lwc1 $f0, 0($2)
162 // Generate:
163 // lui $2, %hi($CPI1_0)
164 // lwc1 $f0, %lo($CPI1_0)($2)
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000165 if ((Addr.getOperand(0).getOpcode() == MipsISD::Hi ||
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000166 Addr.getOperand(0).getOpcode() == ISD::LOAD) &&
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000167 Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000168 SDValue LoVal = Addr.getOperand(1);
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000169 if (dyn_cast<ConstantPoolSDNode>(LoVal.getOperand(0))) {
170 Base = Addr.getOperand(0);
171 Offset = LoVal.getOperand(0);
172 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000173 }
174 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000175 }
176
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000177 Base = Addr;
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000179 return true;
180}
181
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000182SDNode *MipsDAGToDAGISel::SelectLoadFp64(SDNode *N) {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000183 MVT::SimpleValueType NVT =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000184 N->getValueType(0).getSimpleVT().SimpleTy;
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000185
186 if (!Subtarget.isMips1() || NVT != MVT::f64)
187 return NULL;
188
Jakob Stoklund Olesen7fa846f2010-09-03 00:35:13 +0000189 LoadSDNode *LN = cast<LoadSDNode>(N);
190 if (LN->getExtensionType() != ISD::NON_EXTLOAD ||
191 LN->getAddressingMode() != ISD::UNINDEXED)
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000192 return NULL;
193
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000194 SDValue Chain = N->getOperand(0);
195 SDValue N1 = N->getOperand(1);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000196 SDValue Offset0, Offset1, Base;
197
Chris Lattner52a261b2010-09-21 20:31:19 +0000198 if (!SelectAddr(N1, Offset0, Base) ||
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000199 N1.getValueType() != MVT::i32)
200 return NULL;
201
202 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
203 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000204 DebugLoc dl = N->getDebugLoc();
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000205
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000206 // The second load should start after for 4 bytes.
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000207 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
208 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
209 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Offset0))
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000210 Offset1 = CurDAG->getTargetConstantPool(CP->getConstVal(),
211 MVT::i32,
212 CP->getAlignment(),
213 CP->getOffset()+4,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000214 CP->getTargetFlags());
215 else
216 return NULL;
217
Bruno Cardoso Lopes37fd5372009-11-25 01:05:25 +0000218 // Choose the offsets depending on the endianess
219 if (TM.getTargetData()->isBigEndian())
220 std::swap(Offset0, Offset1);
221
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000222 // Instead of:
223 // ldc $f0, X($3)
224 // Generate:
225 // lwc $f0, X($3)
226 // lwc $f1, X+4($3)
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000227 SDNode *LD0 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000228 MVT::Other, Offset0, Base, Chain);
Chris Lattner518bb532010-02-09 19:54:29 +0000229 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000230 dl, NVT), 0);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000231 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::sub_fpeven, dl,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000232 MVT::f64, Undef, SDValue(LD0, 0));
233
234 SDNode *LD1 = CurDAG->getMachineNode(Mips::LWC1, dl, MVT::f32,
235 MVT::Other, Offset1, Base, SDValue(LD0, 1));
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000236 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::sub_fpodd, dl,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000237 MVT::f64, I0, SDValue(LD1, 0));
238
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000239 ReplaceUses(SDValue(N, 0), I1);
240 ReplaceUses(SDValue(N, 1), Chain);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000241 cast<MachineSDNode>(LD0)->setMemRefs(MemRefs0, MemRefs0 + 1);
242 cast<MachineSDNode>(LD1)->setMemRefs(MemRefs0, MemRefs0 + 1);
243 return I1.getNode();
244}
245
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000246SDNode *MipsDAGToDAGISel::SelectStoreFp64(SDNode *N) {
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000247
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000248 if (!Subtarget.isMips1() ||
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000249 N->getOperand(1).getValueType() != MVT::f64)
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000250 return NULL;
251
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000252 SDValue Chain = N->getOperand(0);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000253
Jakob Stoklund Olesen7552a3d2010-08-18 23:56:46 +0000254 StoreSDNode *SN = cast<StoreSDNode>(N);
255 if (SN->isTruncatingStore() || SN->getAddressingMode() != ISD::UNINDEXED)
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000256 return NULL;
257
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000258 SDValue N1 = N->getOperand(1);
259 SDValue N2 = N->getOperand(2);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000260 SDValue Offset0, Offset1, Base;
261
Chris Lattner52a261b2010-09-21 20:31:19 +0000262 if (!SelectAddr(N2, Offset0, Base) ||
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000263 N1.getValueType() != MVT::f64 ||
264 N2.getValueType() != MVT::i32)
265 return NULL;
266
267 MachineSDNode::mmo_iterator MemRefs0 = MF->allocateMemRefsArray(1);
268 MemRefs0[0] = cast<MemSDNode>(N)->getMemOperand();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000269 DebugLoc dl = N->getDebugLoc();
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000270
271 // Get the even and odd part from the f64 register
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000272 SDValue FPOdd = CurDAG->getTargetExtractSubreg(Mips::sub_fpodd,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000273 dl, MVT::f32, N1);
Jakob Stoklund Olesenfff916a2010-05-24 17:42:58 +0000274 SDValue FPEven = CurDAG->getTargetExtractSubreg(Mips::sub_fpeven,
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000275 dl, MVT::f32, N1);
276
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000277 // The second store should start after for 4 bytes.
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000278 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Offset0))
279 Offset1 = CurDAG->getTargetConstant(C->getSExtValue()+4, MVT::i32);
280 else
281 return NULL;
282
Bruno Cardoso Lopes37fd5372009-11-25 01:05:25 +0000283 // Choose the offsets depending on the endianess
284 if (TM.getTargetData()->isBigEndian())
285 std::swap(Offset0, Offset1);
286
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000287 // Instead of:
288 // sdc $f0, X($3)
289 // Generate:
290 // swc $f0, X($3)
291 // swc $f1, X+4($3)
292 SDValue Ops0[] = { FPEven, Offset0, Base, Chain };
293 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
294 MVT::Other, Ops0, 4), 0);
295 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
296
297 SDValue Ops1[] = { FPOdd, Offset1, Base, Chain };
298 Chain = SDValue(CurDAG->getMachineNode(Mips::SWC1, dl,
299 MVT::Other, Ops1, 4), 0);
300 cast<MachineSDNode>(Chain.getNode())->setMemRefs(MemRefs0, MemRefs0 + 1);
301
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000302 ReplaceUses(SDValue(N, 0), Chain);
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000303 return Chain.getNode();
304}
305
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000306/// Select instructions not customized! Used for
307/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000308SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000309 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000310 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000311
312 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000313 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000314
315 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000316 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000317 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000318 return NULL;
319 }
320
321 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000322 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000323 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000324 ///
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000325 switch(Opcode) {
326
327 default: break;
328
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000329 case ISD::SUBE:
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000330 case ISD::ADDE: {
Dan Gohman475871a2008-07-27 21:46:04 +0000331 SDValue InFlag = Node->getOperand(2), CmpLHS;
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000332 unsigned Opc = InFlag.getOpcode(); (void)Opc;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000333 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
334 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000335 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
336
Chris Lattner30baa952008-12-14 21:38:24 +0000337 unsigned MOp;
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000338 if (Opcode == ISD::ADDE) {
339 CmpLHS = InFlag.getValue(0);
340 MOp = Mips::ADDu;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000341 } else {
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000342 CmpLHS = InFlag.getOperand(0);
343 MOp = Mips::SUBu;
344 }
345
Dan Gohman475871a2008-07-27 21:46:04 +0000346 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000347
Dan Gohman475871a2008-07-27 21:46:04 +0000348 SDValue LHS = Node->getOperand(0);
349 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000350
Owen Andersone50ed302009-08-10 22:56:29 +0000351 EVT VT = LHS.getValueType();
Dan Gohman602b0c82009-09-25 18:54:59 +0000352 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000353 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
Dan Gohman602b0c82009-09-25 18:54:59 +0000354 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000355
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000356 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
Dan Gohman475871a2008-07-27 21:46:04 +0000357 LHS, SDValue(AddCarry,0));
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000358 }
359
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000360 /// Mul/Div with two results
361 case ISD::SDIVREM:
362 case ISD::UDIVREM:
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000363 break;
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000364 case ISD::SMUL_LOHI:
365 case ISD::UMUL_LOHI: {
Dan Gohman475871a2008-07-27 21:46:04 +0000366 SDValue Op1 = Node->getOperand(0);
367 SDValue Op2 = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000368
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000369 unsigned Op;
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000370 Op = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000371
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000372 SDNode *Mul = CurDAG->getMachineNode(Op, dl, MVT::Glue, Op1, Op2);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000373
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000374 SDValue InFlag = SDValue(Mul, 0);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000375 SDNode *Lo = CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000376 MVT::Glue, 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
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000380 if (!SDValue(Node, 0).use_empty())
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000381 ReplaceUses(SDValue(Node, 0), SDValue(Lo,0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000382
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000383 if (!SDValue(Node, 1).use_empty())
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000384 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
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000390 case ISD::MUL:
Bruno Cardoso Lopes7d5652d2010-11-12 00:38:32 +0000391 if (Subtarget.isMips32())
392 break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000393 case ISD::MULHS:
394 case ISD::MULHU: {
Dan Gohman475871a2008-07-27 21:46:04 +0000395 SDValue MulOp1 = Node->getOperand(0);
396 SDValue MulOp2 = Node->getOperand(1);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000397
398 unsigned MulOp = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000399 SDNode *MulNode = CurDAG->getMachineNode(MulOp, dl,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000400 MVT::Glue, MulOp1, MulOp2);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000401
Dan Gohman475871a2008-07-27 21:46:04 +0000402 SDValue InFlag = SDValue(MulNode, 0);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000403
Bruno Cardoso Lopes9a720b02010-02-01 12:16:39 +0000404 if (Opcode == ISD::MUL)
Dan Gohman602b0c82009-09-25 18:54:59 +0000405 return CurDAG->getMachineNode(Mips::MFLO, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000406 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000407 return CurDAG->getMachineNode(Mips::MFHI, dl, MVT::i32, InFlag);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000408 }
409
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000410 /// Div/Rem operations
411 case ISD::SREM:
412 case ISD::UREM:
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000413 case ISD::SDIV:
Bruno Cardoso Lopes38b5e862011-03-04 21:03:24 +0000414 case ISD::UDIV:
415 break;
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000416
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000417 // Get target GOT address.
Dan Gohman99114052009-06-03 20:30:14 +0000418 case ISD::GLOBAL_OFFSET_TABLE:
419 return getGlobalBaseReg();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000420
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000421 case ISD::ConstantFP: {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000422 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000423 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
424 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000425 Mips::ZERO, MVT::i32);
426 SDValue Undef = SDValue(
Chris Lattner518bb532010-02-09 19:54:29 +0000427 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::f64), 0);
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000428 SDNode *MTC = CurDAG->getMachineNode(Mips::MTC1, dl, MVT::f32, Zero);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000429 SDValue I0 = CurDAG->getTargetInsertSubreg(Mips::sub_fpeven, dl,
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000430 MVT::f64, Undef, SDValue(MTC, 0));
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000431 SDValue I1 = CurDAG->getTargetInsertSubreg(Mips::sub_fpodd, dl,
Bruno Cardoso Lopesea982782010-01-19 12:53:04 +0000432 MVT::f64, I0, SDValue(MTC, 0));
433 ReplaceUses(SDValue(Node, 0), I1);
434 return I1.getNode();
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000435 }
436 break;
437 }
438
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000439 case ISD::LOAD:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000440 if (SDNode *ResNode = SelectLoadFp64(Node))
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000441 return ResNode;
442 // Other cases are autogenerated.
443 break;
444
445 case ISD::STORE:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000446 if (SDNode *ResNode = SelectStoreFp64(Node))
Bruno Cardoso Lopes2045c472009-11-19 06:06:13 +0000447 return ResNode;
448 // Other cases are autogenerated.
449 break;
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000450
451 case MipsISD::ThreadPointer: {
452 unsigned SrcReg = Mips::HWR29;
453 unsigned DestReg = Mips::V1;
454 SDNode *Rdhwr = CurDAG->getMachineNode(Mips::RDHWR, Node->getDebugLoc(),
455 Node->getValueType(0), CurDAG->getRegister(SrcReg, MVT::i32));
456 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
457 SDValue(Rdhwr, 0));
458 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, MVT::i32);
459 ReplaceUses(SDValue(Node, 0), ResNode);
460 return ResNode.getNode();
461 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000462 }
463
464 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000465 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000466
Chris Lattner7c306da2010-03-02 06:34:30 +0000467 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000468 if (ResNode == NULL || ResNode == Node)
469 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000470 else
471 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000472 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000473 return ResNode;
474}
475
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000476/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000477/// MIPS-specific DAG, ready for instruction scheduling.
478FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
479 return new MipsDAGToDAGISel(TM);
480}