blob: 6e5bad7f1531eb14a9ad00ef5b2bba68d864c31d [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() ?
Craig Topper420761a2012-04-20 07:30:17 +0000137 (const TargetRegisterClass*)&Mips::CPU64RegsRegClass :
138 (const TargetRegisterClass*)&Mips::CPURegsRegClass;
Jia Liubb481f82012-02-28 07:46:26 +0000139
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000140 V0 = RegInfo.createVirtualRegister(RC);
141 V1 = RegInfo.createVirtualRegister(RC);
142 }
143
144 if (Subtarget.isABI_N64()) {
145 MF.getRegInfo().addLiveIn(Mips::T9_64);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000146 MBB.addLiveIn(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000147
148 // lui $v0, %hi(%neg(%gp_rel(fname)))
149 // daddu $v1, $v0, $t9
150 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
151 const GlobalValue *FName = MF.getFunction();
152 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
153 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
154 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0).addReg(Mips::T9_64);
155 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
156 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
157 } else if (MF.getTarget().getRelocationModel() == Reloc::Static) {
158 // Set global register to __gnu_local_gp.
159 //
160 // lui $v0, %hi(__gnu_local_gp)
161 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
162 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
163 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
164 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
165 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
166 } else {
167 MF.getRegInfo().addLiveIn(Mips::T9);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000168 MBB.addLiveIn(Mips::T9);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000169
170 if (Subtarget.isABI_N32()) {
171 // lui $v0, %hi(%neg(%gp_rel(fname)))
172 // addu $v1, $v0, $t9
173 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
174 const GlobalValue *FName = MF.getFunction();
175 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
176 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
177 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
178 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
179 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
180 } else if (!MipsFI->globalBaseRegFixed()) {
181 assert(Subtarget.isABI_O32());
182
183 BuildMI(MBB, I, DL, TII.get(Mips::SETGP2), GlobalBaseReg)
184 .addReg(Mips::T9);
185 }
Jia Liubb481f82012-02-28 07:46:26 +0000186 }
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000187}
188
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000189bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
190 const MachineInstr& MI) {
191 unsigned DstReg = 0, ZeroReg = 0;
192
193 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
194 if ((MI.getOpcode() == Mips::ADDiu) &&
195 (MI.getOperand(1).getReg() == Mips::ZERO) &&
196 (MI.getOperand(2).getImm() == 0)) {
197 DstReg = MI.getOperand(0).getReg();
198 ZeroReg = Mips::ZERO;
199 } else if ((MI.getOpcode() == Mips::DADDiu) &&
200 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
201 (MI.getOperand(2).getImm() == 0)) {
202 DstReg = MI.getOperand(0).getReg();
203 ZeroReg = Mips::ZERO_64;
204 }
205
206 if (!DstReg)
207 return false;
208
209 // Replace uses with ZeroReg.
210 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
211 E = MRI->use_end(); U != E; ++U) {
212 MachineOperand &MO = U.getOperand();
213 MachineInstr *MI = MO.getParent();
214
215 // Do not replace if it is a phi's operand or is tied to def operand.
216 if (MI->isPHI() || MI->isRegTiedToDefOperand(U.getOperandNo()))
217 continue;
218
219 MO.setReg(ZeroReg);
220 }
221
222 return true;
223}
224
225void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
226 InitGlobalBaseReg(MF);
227
228 MachineRegisterInfo *MRI = &MF.getRegInfo();
229
230 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
231 ++MFI)
232 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
233 ReplaceUsesWithZeroReg(MRI, *I);
234}
235
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000236bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
237 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000238
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000239 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000240
241 return Ret;
242}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000243
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000244/// getGlobalBaseReg - Output the instructions required to put the
245/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000246SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000247 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000248 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000249}
250
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000251/// ComplexPattern used on MipsInstrInfo
252/// Used on Mips Load/Store instructions
253bool MipsDAGToDAGISel::
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000254SelectAddr(SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000255 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000256
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000257 // If Parent is an unaligned f32 load or store, select a (base + index)
258 // floating point load/store instruction (luxc1 or suxc1).
259 const LSBaseSDNode* LS = 0;
260
261 if (Parent && (LS = dyn_cast<LSBaseSDNode>(Parent))) {
262 EVT VT = LS->getMemoryVT();
263
264 if (VT.getSizeInBits() / 8 > LS->getAlignment()) {
265 assert(TLI.allowsUnalignedMemoryAccesses(VT) &&
266 "Unaligned loads/stores not supported for this type.");
267 if (VT == MVT::f32)
268 return false;
269 }
270 }
271
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000272 // if Address is FI, get the TargetFrameIndex.
273 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000274 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
275 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000276 return true;
277 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000278
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000279 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000280 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000281 Base = Addr.getOperand(0);
282 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000283 return true;
284 }
285
286 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000287 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000288 Addr.getOpcode() == ISD::TargetGlobalAddress))
289 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000290 }
291
Akira Hatanaka5e069032011-06-02 01:03:14 +0000292 // Addresses of the form FI+const or FI|const
293 if (CurDAG->isBaseWithConstantOffset(Addr)) {
294 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
295 if (isInt<16>(CN->getSExtValue())) {
296
297 // If the first operand is a FI, get the TargetFI Node
298 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
299 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000300 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000301 else
302 Base = Addr.getOperand(0);
303
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000304 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000305 return true;
306 }
307 }
308
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000309 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000310 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000311 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000312 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000313 // lui $2, %hi($CPI1_0)
314 // addiu $2, $2, %lo($CPI1_0)
315 // lwc1 $f0, 0($2)
316 // Generate:
317 // lui $2, %hi($CPI1_0)
318 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka89dc8d72011-12-19 19:28:37 +0000319 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000320 SDValue LoVal = Addr.getOperand(1);
Jia Liubb481f82012-02-28 07:46:26 +0000321 if (isa<ConstantPoolSDNode>(LoVal.getOperand(0)) ||
Akira Hatanaka8b2b7132011-06-24 17:55:19 +0000322 isa<GlobalAddressSDNode>(LoVal.getOperand(0))) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000323 Base = Addr.getOperand(0);
324 Offset = LoVal.getOperand(0);
325 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000326 }
327 }
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000328
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000329 // If an indexed floating point load/store can be emitted, return false.
330 if (LS && (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
331 Subtarget.hasMips32r2Or64())
332 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000333 }
334
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000335 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000336 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000337 return true;
338}
339
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000340/// Select multiply instructions.
341std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000342MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000343 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000344 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000345 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
346 N->getOperand(1));
347 SDValue InFlag = SDValue(Mul, 0);
348
349 if (HasLo) {
350 Lo = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64, dl,
351 Ty, MVT::Glue, InFlag);
352 InFlag = SDValue(Lo, 1);
353 }
354 if (HasHi)
355 Hi = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64, dl,
356 Ty, InFlag);
Jia Liubb481f82012-02-28 07:46:26 +0000357
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000358 return std::make_pair(Lo, Hi);
359}
360
361
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000362/// Select instructions not customized! Used for
363/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000364SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000365 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000366 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000367
368 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000369 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000370
371 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000372 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000373 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000374 return NULL;
375 }
376
377 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000378 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000379 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000380 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000381 EVT NodeTy = Node->getValueType(0);
382 unsigned MultOpc;
383
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000384 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000385 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000386
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000387 case ISD::SUBE:
388 case ISD::ADDE: {
389 SDValue InFlag = Node->getOperand(2), CmpLHS;
390 unsigned Opc = InFlag.getOpcode(); (void)Opc;
391 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
392 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
393 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000394
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000395 unsigned MOp;
396 if (Opcode == ISD::ADDE) {
397 CmpLHS = InFlag.getValue(0);
398 MOp = Mips::ADDu;
399 } else {
400 CmpLHS = InFlag.getOperand(0);
401 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000402 }
403
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000404 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000405
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000406 SDValue LHS = Node->getOperand(0);
407 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000408
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000409 EVT VT = LHS.getValueType();
410 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
411 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
412 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000413
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000414 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
415 LHS, SDValue(AddCarry,0));
416 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000417
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000418 /// Mul with two results
419 case ISD::SMUL_LOHI:
420 case ISD::UMUL_LOHI: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000421 if (NodeTy == MVT::i32)
422 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
423 else
424 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000425
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000426 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
427 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000428
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000429 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000430 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000431
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000432 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000433 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000434
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000435 return NULL;
436 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000437
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000438 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000439 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000440 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000441 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000442 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000443 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
444 dl, NodeTy, true, false).first;
445 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000446 case ISD::MULHS:
447 case ISD::MULHU: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000448 if (NodeTy == MVT::i32)
449 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000450 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000451 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
452
453 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000454 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000455
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000456 // Get target GOT address.
457 case ISD::GLOBAL_OFFSET_TABLE:
458 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000459
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000460 case ISD::ConstantFP: {
461 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
462 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
463 if (Subtarget.hasMips64()) {
464 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
465 Mips::ZERO_64, MVT::i64);
466 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000467 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000468
469 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
470 Mips::ZERO, MVT::i32);
471 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
472 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000473 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000474 break;
475 }
476
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000477 case ISD::Constant: {
478 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
479 unsigned Size = CN->getValueSizeInBits(0);
480
481 if (Size == 32)
482 break;
483
484 MipsAnalyzeImmediate AnalyzeImm;
485 int64_t Imm = CN->getSExtValue();
486
487 const MipsAnalyzeImmediate::InstSeq &Seq =
488 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000489
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000490 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
491 DebugLoc DL = CN->getDebugLoc();
492 SDNode *RegOpnd;
493 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
494 MVT::i64);
495
496 // The first instruction can be a LUi which is different from other
497 // instructions (ADDiu, ORI and SLL) in that it does not have a register
498 // operand.
499 if (Inst->Opc == Mips::LUi64)
500 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
501 else
502 RegOpnd =
503 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
504 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
505 ImmOpnd);
506
507 // The remaining instructions in the sequence are handled here.
508 for (++Inst; Inst != Seq.end(); ++Inst) {
509 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
510 MVT::i64);
511 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
512 SDValue(RegOpnd, 0), ImmOpnd);
513 }
514
515 return RegOpnd;
516 }
517
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000518 case MipsISD::ThreadPointer: {
519 EVT PtrVT = TLI.getPointerTy();
520 unsigned RdhwrOpc, SrcReg, DestReg;
521
522 if (PtrVT == MVT::i32) {
523 RdhwrOpc = Mips::RDHWR;
524 SrcReg = Mips::HWR29;
525 DestReg = Mips::V1;
526 } else {
527 RdhwrOpc = Mips::RDHWR64;
528 SrcReg = Mips::HWR29_64;
529 DestReg = Mips::V1_64;
530 }
Jia Liubb481f82012-02-28 07:46:26 +0000531
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000532 SDNode *Rdhwr =
533 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
534 Node->getValueType(0),
535 CurDAG->getRegister(SrcReg, PtrVT));
536 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
537 SDValue(Rdhwr, 0));
538 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
539 ReplaceUses(SDValue(Node, 0), ResNode);
540 return ResNode.getNode();
541 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000542 }
543
544 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000545 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000546
Chris Lattner7c306da2010-03-02 06:34:30 +0000547 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000548 if (ResNode == NULL || ResNode == Node)
549 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000550 else
551 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000552 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000553 return ResNode;
554}
555
Akira Hatanaka21afc632011-06-21 00:40:49 +0000556bool MipsDAGToDAGISel::
557SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
558 std::vector<SDValue> &OutOps) {
559 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
560 OutOps.push_back(Op);
561 return false;
562}
563
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000564/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000565/// MIPS-specific DAG, ready for instruction scheduling.
566FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
567 return new MipsDAGToDAGISel(TM);
568}