blob: 281399e84eeed50c8c4a7fb3c0bca0302e2702ab [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55: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"
Akira Hatanaka57fa3822012-01-25 03:01:35 +000016#include "MipsAnalyzeImmediate.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"
Akira Hatanaka648f00c2012-02-24 22:34:47 +000021#include "MCTargetDesc/MipsBaseInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/GlobalValue.h"
23#include "llvm/Instructions.h"
24#include "llvm/Intrinsics.h"
25#include "llvm/Support/CFG.h"
26#include "llvm/Type.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
33#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000037using namespace llvm;
38
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000039//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000040// Instruction Selector Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000041//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000042
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000043//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000044// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
45// instructions for SelectionDAG operations.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000046//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000047namespace {
48
Nick Lewycky6726b6d2009-10-25 06:33:48 +000049class MipsDAGToDAGISel : public SelectionDAGISel {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000050
51 /// TM - Keep a reference to MipsTargetMachine.
52 MipsTargetMachine &TM;
53
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000054 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
55 /// make the right decision when generating code for different targets.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000056 const MipsSubtarget &Subtarget;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000057
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000058public:
Dan Gohman1002c022008-07-07 18:00:37 +000059 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +000060 SelectionDAGISel(tm),
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000061 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000062
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000063 // Pass Name
64 virtual const char *getPassName() const {
65 return "MIPS DAG->DAG Pattern Instruction Selection";
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000066 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000067
Akira Hatanaka648f00c2012-02-24 22:34:47 +000068 virtual bool runOnMachineFunction(MachineFunction &MF);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000069
70private:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000071 // Include the pieces autogenerated from the target description.
72 #include "MipsGenDAGISel.inc"
73
Dan Gohman99114052009-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();
Akira Hatanaka2fd04752011-12-20 23:10:57 +000087
88 std::pair<SDNode*, SDNode*> SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl,
89 EVT Ty, bool HasLo, bool HasHi);
90
Dan Gohmaneeb3a002010-01-05 01:24:18 +000091 SDNode *Select(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000092
93 // Complex Pattern.
Chris Lattner52a261b2010-09-21 20:31:19 +000094 bool SelectAddr(SDValue N, SDValue &Base, SDValue &Offset);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000095
Akira Hatanakabd150902011-12-07 20:15:01 +000096 // getImm - Return a target constant with the specified value.
Akira Hatanaka4d0eb632011-12-07 20:10:24 +000097 inline SDValue getImm(const SDNode *Node, unsigned Imm) {
98 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000099 }
Akira Hatanaka21afc632011-06-21 00:40:49 +0000100
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000101 void InitGlobalBaseReg(MachineFunction &MF);
102
Akira Hatanaka21afc632011-06-21 00:40:49 +0000103 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
104 char ConstraintCode,
105 std::vector<SDValue> &OutOps);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000106};
107
108}
109
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000110// Insert instructions to initialize the global base register in the
111// first MBB of the function. When the ABI is O32 and the relocation model is
112// PIC, the necessary instructions are emitted later to prevent optimization
113// passes from moving them.
114void MipsDAGToDAGISel::InitGlobalBaseReg(MachineFunction &MF) {
115 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
116
117 if (!MipsFI->globalBaseRegSet())
118 return;
119
120 MachineBasicBlock &MBB = MF.front();
121 MachineBasicBlock::iterator I = MBB.begin();
122 MachineRegisterInfo &RegInfo = MF.getRegInfo();
123 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
124 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
125 unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
126 bool FixGlobalBaseReg = MipsFI->globalBaseRegFixed();
127
128 if (FixGlobalBaseReg) // $gp is the global base register.
129 V0 = V1 = GlobalBaseReg;
130 else {
131 const TargetRegisterClass *RC;
132 RC = Subtarget.isABI_N64() ?
133 Mips::CPU64RegsRegisterClass : Mips::CPURegsRegisterClass;
134
135 V0 = RegInfo.createVirtualRegister(RC);
136 V1 = RegInfo.createVirtualRegister(RC);
137 }
138
139 if (Subtarget.isABI_N64()) {
140 MF.getRegInfo().addLiveIn(Mips::T9_64);
141
142 // lui $v0, %hi(%neg(%gp_rel(fname)))
143 // daddu $v1, $v0, $t9
144 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
145 const GlobalValue *FName = MF.getFunction();
146 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
147 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
148 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0).addReg(Mips::T9_64);
149 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
150 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
151 } else if (MF.getTarget().getRelocationModel() == Reloc::Static) {
152 // Set global register to __gnu_local_gp.
153 //
154 // lui $v0, %hi(__gnu_local_gp)
155 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
156 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
157 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
158 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
159 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
160 } else {
161 MF.getRegInfo().addLiveIn(Mips::T9);
162
163 if (Subtarget.isABI_N32()) {
164 // lui $v0, %hi(%neg(%gp_rel(fname)))
165 // addu $v1, $v0, $t9
166 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
167 const GlobalValue *FName = MF.getFunction();
168 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
169 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
170 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
171 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
172 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
173 } else if (!MipsFI->globalBaseRegFixed()) {
174 assert(Subtarget.isABI_O32());
175
176 BuildMI(MBB, I, DL, TII.get(Mips::SETGP2), GlobalBaseReg)
177 .addReg(Mips::T9);
178 }
179 }
180}
181
182bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
183 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
184
185 InitGlobalBaseReg(MF);
186
187 return Ret;
188}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000189
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000190/// getGlobalBaseReg - Output the instructions required to put the
191/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000192SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000193 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000194 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000195}
196
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000197/// ComplexPattern used on MipsInstrInfo
198/// Used on Mips Load/Store instructions
199bool MipsDAGToDAGISel::
Akira Hatanakad3ac47f2011-07-07 18:57:00 +0000200SelectAddr(SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000201 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000202
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000203 // if Address is FI, get the TargetFrameIndex.
204 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000205 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
206 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000207 return true;
208 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000209
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000210 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000211 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000212 Base = Addr.getOperand(0);
213 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000214 return true;
215 }
216
217 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000218 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000219 Addr.getOpcode() == ISD::TargetGlobalAddress))
220 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000221 }
222
Akira Hatanaka5e069032011-06-02 01:03:14 +0000223 // Addresses of the form FI+const or FI|const
224 if (CurDAG->isBaseWithConstantOffset(Addr)) {
225 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
226 if (isInt<16>(CN->getSExtValue())) {
227
228 // If the first operand is a FI, get the TargetFI Node
229 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
230 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000231 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000232 else
233 Base = Addr.getOperand(0);
234
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000235 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000236 return true;
237 }
238 }
239
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000240 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000241 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000242 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000243 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000244 // lui $2, %hi($CPI1_0)
245 // addiu $2, $2, %lo($CPI1_0)
246 // lwc1 $f0, 0($2)
247 // Generate:
248 // lui $2, %hi($CPI1_0)
249 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka89dc8d72011-12-19 19:28:37 +0000250 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000251 SDValue LoVal = Addr.getOperand(1);
Akira Hatanaka8b2b7132011-06-24 17:55:19 +0000252 if (isa<ConstantPoolSDNode>(LoVal.getOperand(0)) ||
253 isa<GlobalAddressSDNode>(LoVal.getOperand(0))) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000254 Base = Addr.getOperand(0);
255 Offset = LoVal.getOperand(0);
256 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000257 }
258 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000259 }
260
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000261 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000262 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000263 return true;
264}
265
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000266/// Select multiply instructions.
267std::pair<SDNode*, SDNode*>
268MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
269 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000270 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000271 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
272 N->getOperand(1));
273 SDValue InFlag = SDValue(Mul, 0);
274
275 if (HasLo) {
276 Lo = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64, dl,
277 Ty, MVT::Glue, InFlag);
278 InFlag = SDValue(Lo, 1);
279 }
280 if (HasHi)
281 Hi = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64, dl,
282 Ty, InFlag);
283
284 return std::make_pair(Lo, Hi);
285}
286
287
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000288/// Select instructions not customized! Used for
289/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000290SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000291 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000292 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000293
294 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000295 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000296
297 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000298 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000299 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000300 return NULL;
301 }
302
303 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000304 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000305 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000306 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000307 EVT NodeTy = Node->getValueType(0);
308 unsigned MultOpc;
309
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000310 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000311 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000312
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000313 case ISD::SUBE:
314 case ISD::ADDE: {
315 SDValue InFlag = Node->getOperand(2), CmpLHS;
316 unsigned Opc = InFlag.getOpcode(); (void)Opc;
317 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
318 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
319 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000320
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000321 unsigned MOp;
322 if (Opcode == ISD::ADDE) {
323 CmpLHS = InFlag.getValue(0);
324 MOp = Mips::ADDu;
325 } else {
326 CmpLHS = InFlag.getOperand(0);
327 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000328 }
329
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000330 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000331
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000332 SDValue LHS = Node->getOperand(0);
333 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000334
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000335 EVT VT = LHS.getValueType();
336 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
337 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
338 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000339
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000340 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
341 LHS, SDValue(AddCarry,0));
342 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000343
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000344 /// Mul with two results
345 case ISD::SMUL_LOHI:
346 case ISD::UMUL_LOHI: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000347 if (NodeTy == MVT::i32)
348 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
349 else
350 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000351
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000352 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
353 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000354
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000355 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000356 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000357
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000358 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000359 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000360
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000361 return NULL;
362 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000363
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000364 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000365 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000366 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000367 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000368 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000369 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
370 dl, NodeTy, true, false).first;
371 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000372 case ISD::MULHS:
373 case ISD::MULHU: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000374 if (NodeTy == MVT::i32)
375 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000376 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000377 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
378
379 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000380 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000381
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000382 // Get target GOT address.
383 case ISD::GLOBAL_OFFSET_TABLE:
384 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000385
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000386 case ISD::ConstantFP: {
387 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
388 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
389 if (Subtarget.hasMips64()) {
390 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
391 Mips::ZERO_64, MVT::i64);
392 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000393 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000394
395 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
396 Mips::ZERO, MVT::i32);
397 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
398 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000399 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000400 break;
401 }
402
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000403 case ISD::Constant: {
404 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
405 unsigned Size = CN->getValueSizeInBits(0);
406
407 if (Size == 32)
408 break;
409
410 MipsAnalyzeImmediate AnalyzeImm;
411 int64_t Imm = CN->getSExtValue();
412
413 const MipsAnalyzeImmediate::InstSeq &Seq =
414 AnalyzeImm.Analyze(Imm, Size, false);
415
416 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
417 DebugLoc DL = CN->getDebugLoc();
418 SDNode *RegOpnd;
419 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
420 MVT::i64);
421
422 // The first instruction can be a LUi which is different from other
423 // instructions (ADDiu, ORI and SLL) in that it does not have a register
424 // operand.
425 if (Inst->Opc == Mips::LUi64)
426 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
427 else
428 RegOpnd =
429 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
430 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
431 ImmOpnd);
432
433 // The remaining instructions in the sequence are handled here.
434 for (++Inst; Inst != Seq.end(); ++Inst) {
435 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
436 MVT::i64);
437 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
438 SDValue(RegOpnd, 0), ImmOpnd);
439 }
440
441 return RegOpnd;
442 }
443
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000444 case MipsISD::ThreadPointer: {
445 EVT PtrVT = TLI.getPointerTy();
446 unsigned RdhwrOpc, SrcReg, DestReg;
447
448 if (PtrVT == MVT::i32) {
449 RdhwrOpc = Mips::RDHWR;
450 SrcReg = Mips::HWR29;
451 DestReg = Mips::V1;
452 } else {
453 RdhwrOpc = Mips::RDHWR64;
454 SrcReg = Mips::HWR29_64;
455 DestReg = Mips::V1_64;
456 }
457
458 SDNode *Rdhwr =
459 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
460 Node->getValueType(0),
461 CurDAG->getRegister(SrcReg, PtrVT));
462 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
463 SDValue(Rdhwr, 0));
464 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
465 ReplaceUses(SDValue(Node, 0), ResNode);
466 return ResNode.getNode();
467 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000468 }
469
470 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000471 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000472
Chris Lattner7c306da2010-03-02 06:34:30 +0000473 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000474 if (ResNode == NULL || ResNode == Node)
475 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000476 else
477 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000478 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000479 return ResNode;
480}
481
Akira Hatanaka21afc632011-06-21 00:40:49 +0000482bool MipsDAGToDAGISel::
483SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
484 std::vector<SDValue> &OutOps) {
485 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
486 OutOps.push_back(Op);
487 return false;
488}
489
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000490/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000491/// MIPS-specific DAG, ready for instruction scheduling.
492FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
493 return new MipsDAGToDAGISel(TM);
494}