blob: 536879e1e59a48d6a8fcd8a78a2f79ece1020bb7 [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"
Akira Hatanaka44b6c712012-02-28 02:55:02 +000033#include "llvm/CodeGen/SelectionDAGNodes.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038using namespace llvm;
39
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000040//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041// Instruction Selector Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000042//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000044//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000045// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46// instructions for SelectionDAG operations.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000047//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000048namespace {
49
Nick Lewycky6726b6d2009-10-25 06:33:48 +000050class MipsDAGToDAGISel : public SelectionDAGISel {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000051
52 /// TM - Keep a reference to MipsTargetMachine.
53 MipsTargetMachine &TM;
54
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000055 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
56 /// make the right decision when generating code for different targets.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000057 const MipsSubtarget &Subtarget;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000058
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000059public:
Dan Gohman1002c022008-07-07 18:00:37 +000060 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +000061 SelectionDAGISel(tm),
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000062 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000063
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064 // Pass Name
65 virtual const char *getPassName() const {
66 return "MIPS DAG->DAG Pattern Instruction Selection";
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000067 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068
Akira Hatanaka648f00c2012-02-24 22:34:47 +000069 virtual bool runOnMachineFunction(MachineFunction &MF);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000070
71private:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000072 // Include the pieces autogenerated from the target description.
73 #include "MipsGenDAGISel.inc"
74
Dan Gohman99114052009-06-03 20:30:14 +000075 /// getTargetMachine - Return a reference to the TargetMachine, casted
76 /// to the target-specific type.
77 const MipsTargetMachine &getTargetMachine() {
78 return static_cast<const MipsTargetMachine &>(TM);
79 }
80
81 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
82 /// to the target-specific type.
83 const MipsInstrInfo *getInstrInfo() {
84 return getTargetMachine().getInstrInfo();
85 }
86
87 SDNode *getGlobalBaseReg();
Akira Hatanaka2fd04752011-12-20 23:10:57 +000088
89 std::pair<SDNode*, SDNode*> SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl,
90 EVT Ty, bool HasLo, bool HasHi);
91
Dan Gohmaneeb3a002010-01-05 01:24:18 +000092 SDNode *Select(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093
94 // Complex Pattern.
Akira Hatanaka44b6c712012-02-28 02:55:02 +000095 bool SelectAddr(SDNode *Parent, SDValue N, SDValue &Base, SDValue &Offset);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000096
Akira Hatanakabd150902011-12-07 20:15:01 +000097 // getImm - Return a target constant with the specified value.
Akira Hatanaka4d0eb632011-12-07 20:10:24 +000098 inline SDValue getImm(const SDNode *Node, unsigned Imm) {
99 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000100 }
Akira Hatanaka21afc632011-06-21 00:40:49 +0000101
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000102 void ProcessFunctionAfterISel(MachineFunction &MF);
103 bool ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI, const MachineInstr&);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000104 void InitGlobalBaseReg(MachineFunction &MF);
105
Akira Hatanaka21afc632011-06-21 00:40:49 +0000106 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
107 char ConstraintCode,
108 std::vector<SDValue> &OutOps);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000109};
110
111}
112
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000113// Insert instructions to initialize the global base register in the
114// first MBB of the function. When the ABI is O32 and the relocation model is
115// PIC, the necessary instructions are emitted later to prevent optimization
116// passes from moving them.
117void MipsDAGToDAGISel::InitGlobalBaseReg(MachineFunction &MF) {
118 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Jia Liubb481f82012-02-28 07:46:26 +0000119
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000120 if (!MipsFI->globalBaseRegSet())
121 return;
122
123 MachineBasicBlock &MBB = MF.front();
124 MachineBasicBlock::iterator I = MBB.begin();
125 MachineRegisterInfo &RegInfo = MF.getRegInfo();
126 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
127 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
128 unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
129 bool FixGlobalBaseReg = MipsFI->globalBaseRegFixed();
130
Akira Hatanakab75673b2012-02-28 03:17:38 +0000131 if (Subtarget.isABI_O32() && FixGlobalBaseReg)
132 // $gp is the global base register.
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000133 V0 = V1 = GlobalBaseReg;
134 else {
135 const TargetRegisterClass *RC;
136 RC = Subtarget.isABI_N64() ?
Jia Liubb481f82012-02-28 07:46:26 +0000137 Mips::CPU64RegsRegisterClass : Mips::CPURegsRegisterClass;
138
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000139 V0 = RegInfo.createVirtualRegister(RC);
140 V1 = RegInfo.createVirtualRegister(RC);
141 }
142
143 if (Subtarget.isABI_N64()) {
144 MF.getRegInfo().addLiveIn(Mips::T9_64);
145
146 // lui $v0, %hi(%neg(%gp_rel(fname)))
147 // daddu $v1, $v0, $t9
148 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
149 const GlobalValue *FName = MF.getFunction();
150 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
151 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
152 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0).addReg(Mips::T9_64);
153 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
154 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
155 } else if (MF.getTarget().getRelocationModel() == Reloc::Static) {
156 // Set global register to __gnu_local_gp.
157 //
158 // lui $v0, %hi(__gnu_local_gp)
159 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
160 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
161 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
162 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
163 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
164 } else {
165 MF.getRegInfo().addLiveIn(Mips::T9);
166
167 if (Subtarget.isABI_N32()) {
168 // lui $v0, %hi(%neg(%gp_rel(fname)))
169 // addu $v1, $v0, $t9
170 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
171 const GlobalValue *FName = MF.getFunction();
172 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
173 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
174 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
175 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
176 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
177 } else if (!MipsFI->globalBaseRegFixed()) {
178 assert(Subtarget.isABI_O32());
179
180 BuildMI(MBB, I, DL, TII.get(Mips::SETGP2), GlobalBaseReg)
181 .addReg(Mips::T9);
182 }
Jia Liubb481f82012-02-28 07:46:26 +0000183 }
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000184}
185
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000186bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
187 const MachineInstr& MI) {
188 unsigned DstReg = 0, ZeroReg = 0;
189
190 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
191 if ((MI.getOpcode() == Mips::ADDiu) &&
192 (MI.getOperand(1).getReg() == Mips::ZERO) &&
193 (MI.getOperand(2).getImm() == 0)) {
194 DstReg = MI.getOperand(0).getReg();
195 ZeroReg = Mips::ZERO;
196 } else if ((MI.getOpcode() == Mips::DADDiu) &&
197 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
198 (MI.getOperand(2).getImm() == 0)) {
199 DstReg = MI.getOperand(0).getReg();
200 ZeroReg = Mips::ZERO_64;
201 }
202
203 if (!DstReg)
204 return false;
205
206 // Replace uses with ZeroReg.
207 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
208 E = MRI->use_end(); U != E; ++U) {
209 MachineOperand &MO = U.getOperand();
210 MachineInstr *MI = MO.getParent();
211
212 // Do not replace if it is a phi's operand or is tied to def operand.
213 if (MI->isPHI() || MI->isRegTiedToDefOperand(U.getOperandNo()))
214 continue;
215
216 MO.setReg(ZeroReg);
217 }
218
219 return true;
220}
221
222void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
223 InitGlobalBaseReg(MF);
224
225 MachineRegisterInfo *MRI = &MF.getRegInfo();
226
227 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
228 ++MFI)
229 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
230 ReplaceUsesWithZeroReg(MRI, *I);
231}
232
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000233bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
234 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000235
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000236 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000237
238 return Ret;
239}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000240
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000241/// getGlobalBaseReg - Output the instructions required to put the
242/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000243SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000244 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000245 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000246}
247
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000248/// ComplexPattern used on MipsInstrInfo
249/// Used on Mips Load/Store instructions
250bool MipsDAGToDAGISel::
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000251SelectAddr(SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000252 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000253
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000254 // If Parent is an unaligned f32 load or store, select a (base + index)
255 // floating point load/store instruction (luxc1 or suxc1).
256 const LSBaseSDNode* LS = 0;
257
258 if (Parent && (LS = dyn_cast<LSBaseSDNode>(Parent))) {
259 EVT VT = LS->getMemoryVT();
260
261 if (VT.getSizeInBits() / 8 > LS->getAlignment()) {
262 assert(TLI.allowsUnalignedMemoryAccesses(VT) &&
263 "Unaligned loads/stores not supported for this type.");
264 if (VT == MVT::f32)
265 return false;
266 }
267 }
268
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000269 // if Address is FI, get the TargetFrameIndex.
270 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000271 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
272 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000273 return true;
274 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000275
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000276 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000277 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000278 Base = Addr.getOperand(0);
279 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000280 return true;
281 }
282
283 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000284 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000285 Addr.getOpcode() == ISD::TargetGlobalAddress))
286 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000287 }
288
Akira Hatanaka5e069032011-06-02 01:03:14 +0000289 // Addresses of the form FI+const or FI|const
290 if (CurDAG->isBaseWithConstantOffset(Addr)) {
291 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
292 if (isInt<16>(CN->getSExtValue())) {
293
294 // If the first operand is a FI, get the TargetFI Node
295 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
296 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000297 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000298 else
299 Base = Addr.getOperand(0);
300
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000301 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000302 return true;
303 }
304 }
305
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000306 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000307 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000308 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000309 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000310 // lui $2, %hi($CPI1_0)
311 // addiu $2, $2, %lo($CPI1_0)
312 // lwc1 $f0, 0($2)
313 // Generate:
314 // lui $2, %hi($CPI1_0)
315 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka89dc8d72011-12-19 19:28:37 +0000316 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000317 SDValue LoVal = Addr.getOperand(1);
Jia Liubb481f82012-02-28 07:46:26 +0000318 if (isa<ConstantPoolSDNode>(LoVal.getOperand(0)) ||
Akira Hatanaka8b2b7132011-06-24 17:55:19 +0000319 isa<GlobalAddressSDNode>(LoVal.getOperand(0))) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000320 Base = Addr.getOperand(0);
321 Offset = LoVal.getOperand(0);
322 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000323 }
324 }
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000325
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000326 // If an indexed floating point load/store can be emitted, return false.
327 if (LS && (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
328 Subtarget.hasMips32r2Or64())
329 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000330 }
331
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000332 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000333 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000334 return true;
335}
336
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000337/// Select multiply instructions.
338std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000339MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000340 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000341 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000342 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
343 N->getOperand(1));
344 SDValue InFlag = SDValue(Mul, 0);
345
346 if (HasLo) {
347 Lo = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64, dl,
348 Ty, MVT::Glue, InFlag);
349 InFlag = SDValue(Lo, 1);
350 }
351 if (HasHi)
352 Hi = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64, dl,
353 Ty, InFlag);
Jia Liubb481f82012-02-28 07:46:26 +0000354
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000355 return std::make_pair(Lo, Hi);
356}
357
358
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000359/// Select instructions not customized! Used for
360/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000361SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000362 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000363 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000364
365 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000366 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000367
368 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000369 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000370 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000371 return NULL;
372 }
373
374 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000375 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000376 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000377 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000378 EVT NodeTy = Node->getValueType(0);
379 unsigned MultOpc;
380
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000381 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000382 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000383
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000384 case ISD::SUBE:
385 case ISD::ADDE: {
386 SDValue InFlag = Node->getOperand(2), CmpLHS;
387 unsigned Opc = InFlag.getOpcode(); (void)Opc;
388 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
389 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
390 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000391
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000392 unsigned MOp;
393 if (Opcode == ISD::ADDE) {
394 CmpLHS = InFlag.getValue(0);
395 MOp = Mips::ADDu;
396 } else {
397 CmpLHS = InFlag.getOperand(0);
398 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000399 }
400
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000401 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000402
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000403 SDValue LHS = Node->getOperand(0);
404 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000405
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000406 EVT VT = LHS.getValueType();
407 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
408 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
409 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000410
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000411 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
412 LHS, SDValue(AddCarry,0));
413 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000414
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000415 /// Mul with two results
416 case ISD::SMUL_LOHI:
417 case ISD::UMUL_LOHI: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000418 if (NodeTy == MVT::i32)
419 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
420 else
421 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000422
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000423 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
424 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000425
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000426 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000427 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000428
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000429 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000430 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000431
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000432 return NULL;
433 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000434
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000435 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000436 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000437 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000438 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000439 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000440 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
441 dl, NodeTy, true, false).first;
442 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000443 case ISD::MULHS:
444 case ISD::MULHU: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000445 if (NodeTy == MVT::i32)
446 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000447 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000448 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
449
450 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000451 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000452
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000453 // Get target GOT address.
454 case ISD::GLOBAL_OFFSET_TABLE:
455 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000456
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000457 case ISD::ConstantFP: {
458 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
459 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
460 if (Subtarget.hasMips64()) {
461 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
462 Mips::ZERO_64, MVT::i64);
463 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000464 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000465
466 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
467 Mips::ZERO, MVT::i32);
468 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
469 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000470 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000471 break;
472 }
473
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000474 case ISD::Constant: {
475 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
476 unsigned Size = CN->getValueSizeInBits(0);
477
478 if (Size == 32)
479 break;
480
481 MipsAnalyzeImmediate AnalyzeImm;
482 int64_t Imm = CN->getSExtValue();
483
484 const MipsAnalyzeImmediate::InstSeq &Seq =
485 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000486
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000487 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
488 DebugLoc DL = CN->getDebugLoc();
489 SDNode *RegOpnd;
490 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
491 MVT::i64);
492
493 // The first instruction can be a LUi which is different from other
494 // instructions (ADDiu, ORI and SLL) in that it does not have a register
495 // operand.
496 if (Inst->Opc == Mips::LUi64)
497 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
498 else
499 RegOpnd =
500 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
501 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
502 ImmOpnd);
503
504 // The remaining instructions in the sequence are handled here.
505 for (++Inst; Inst != Seq.end(); ++Inst) {
506 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
507 MVT::i64);
508 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
509 SDValue(RegOpnd, 0), ImmOpnd);
510 }
511
512 return RegOpnd;
513 }
514
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000515 case MipsISD::ThreadPointer: {
516 EVT PtrVT = TLI.getPointerTy();
517 unsigned RdhwrOpc, SrcReg, DestReg;
518
519 if (PtrVT == MVT::i32) {
520 RdhwrOpc = Mips::RDHWR;
521 SrcReg = Mips::HWR29;
522 DestReg = Mips::V1;
523 } else {
524 RdhwrOpc = Mips::RDHWR64;
525 SrcReg = Mips::HWR29_64;
526 DestReg = Mips::V1_64;
527 }
Jia Liubb481f82012-02-28 07:46:26 +0000528
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000529 SDNode *Rdhwr =
530 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
531 Node->getValueType(0),
532 CurDAG->getRegister(SrcReg, PtrVT));
533 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
534 SDValue(Rdhwr, 0));
535 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
536 ReplaceUses(SDValue(Node, 0), ResNode);
537 return ResNode.getNode();
538 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000539 }
540
541 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000542 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000543
Chris Lattner7c306da2010-03-02 06:34:30 +0000544 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000545 if (ResNode == NULL || ResNode == Node)
546 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000547 else
548 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000549 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000550 return ResNode;
551}
552
Akira Hatanaka21afc632011-06-21 00:40:49 +0000553bool MipsDAGToDAGISel::
554SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
555 std::vector<SDValue> &OutOps) {
556 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
557 OutOps.push_back(Op);
558 return false;
559}
560
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000561/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000562/// MIPS-specific DAG, ready for instruction scheduling.
563FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
564 return new MipsDAGToDAGISel(TM);
565}