blob: 78c74ef8795540403248a957feaf2400714a2cfb [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "MCTargetDesc/MipsBaseInfo.h"
Akira Hatanaka57fa3822012-01-25 03:01:35 +000017#include "MipsAnalyzeImmediate.h"
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000018#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000019#include "MipsRegisterInfo.h"
20#include "MipsSubtarget.h"
21#include "MipsTargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000023#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/MachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000027#include "llvm/CodeGen/SelectionDAGISel.h"
Akira Hatanaka44b6c712012-02-28 02:55:02 +000028#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000029#include "llvm/IR/GlobalValue.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Intrinsics.h"
32#include "llvm/IR/Type.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000033#include "llvm/Support/CFG.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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000037#include "llvm/Target/TargetMachine.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
Reed Kotlerf99998a2012-10-28 06:02:37 +000089 SDValue getMips16SPAliasReg();
90
91 void getMips16SPRefReg(SDNode *parent, SDValue &AliasReg);
92
Akira Hatanaka2fd04752011-12-20 23:10:57 +000093 std::pair<SDNode*, SDNode*> SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl,
94 EVT Ty, bool HasLo, bool HasHi);
95
Dan Gohmaneeb3a002010-01-05 01:24:18 +000096 SDNode *Select(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000097
98 // Complex Pattern.
Akira Hatanakadc2f7922013-02-15 21:20:45 +000099 /// (reg + imm).
Akira Hatanakaabbf9df2013-02-16 00:14:37 +0000100 bool selectAddrRegImm(SDValue Addr, SDValue &Base, SDValue &Offset) const;
Akira Hatanakadc2f7922013-02-15 21:20:45 +0000101
102 /// Fall back on this function if all else fails.
Akira Hatanakaabbf9df2013-02-16 00:14:37 +0000103 bool selectAddrDefault(SDValue Addr, SDValue &Base, SDValue &Offset) const;
Akira Hatanakadc2f7922013-02-15 21:20:45 +0000104
105 /// Match integer address pattern.
Akira Hatanakaabbf9df2013-02-16 00:14:37 +0000106 bool selectIntAddr(SDValue Addr, SDValue &Base, SDValue &Offset) const;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000107
Reed Kotlerf99998a2012-10-28 06:02:37 +0000108 bool SelectAddr16(SDNode *Parent, SDValue N, SDValue &Base, SDValue &Offset,
109 SDValue &Alias);
110
Akira Hatanakabd150902011-12-07 20:15:01 +0000111 // getImm - Return a target constant with the specified value.
Akira Hatanaka4d0eb632011-12-07 20:10:24 +0000112 inline SDValue getImm(const SDNode *Node, unsigned Imm) {
113 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000114 }
Akira Hatanaka21afc632011-06-21 00:40:49 +0000115
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000116 void ProcessFunctionAfterISel(MachineFunction &MF);
117 bool ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI, const MachineInstr&);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000118 void InitGlobalBaseReg(MachineFunction &MF);
Reed Kotlerf99998a2012-10-28 06:02:37 +0000119 void InitMips16SPAliasReg(MachineFunction &MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000120
Akira Hatanaka21afc632011-06-21 00:40:49 +0000121 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
122 char ConstraintCode,
123 std::vector<SDValue> &OutOps);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000124};
125
126}
127
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000128// Insert instructions to initialize the global base register in the
129// first MBB of the function. When the ABI is O32 and the relocation model is
130// PIC, the necessary instructions are emitted later to prevent optimization
131// passes from moving them.
132void MipsDAGToDAGISel::InitGlobalBaseReg(MachineFunction &MF) {
133 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Jia Liubb481f82012-02-28 07:46:26 +0000134
Akira Hatanakade4a1272012-07-25 03:16:47 +0000135 if (!MipsFI->globalBaseRegSet())
Akira Hatanaka4782a6e2012-06-27 00:20:39 +0000136 return;
137
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000138 MachineBasicBlock &MBB = MF.front();
139 MachineBasicBlock::iterator I = MBB.begin();
140 MachineRegisterInfo &RegInfo = MF.getRegInfo();
141 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
142 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000143 unsigned V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg();
Akira Hatanakade4a1272012-07-25 03:16:47 +0000144 const TargetRegisterClass *RC;
Akira Hatanaka54c5bc82012-06-21 20:39:10 +0000145
Akira Hatanakade4a1272012-07-25 03:16:47 +0000146 if (Subtarget.isABI_N64())
147 RC = (const TargetRegisterClass*)&Mips::CPU64RegsRegClass;
148 else if (Subtarget.inMips16Mode())
149 RC = (const TargetRegisterClass*)&Mips::CPU16RegsRegClass;
150 else
151 RC = (const TargetRegisterClass*)&Mips::CPURegsRegClass;
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000152
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000153 V0 = RegInfo.createVirtualRegister(RC);
154 V1 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000155 V2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000156
157 if (Subtarget.isABI_N64()) {
158 MF.getRegInfo().addLiveIn(Mips::T9_64);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000159 MBB.addLiveIn(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000160
161 // lui $v0, %hi(%neg(%gp_rel(fname)))
162 // daddu $v1, $v0, $t9
163 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
164 const GlobalValue *FName = MF.getFunction();
165 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
166 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000167 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
168 .addReg(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000169 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
170 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000171 return;
172 }
173
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000174 if (Subtarget.inMips16Mode()) {
175 BuildMI(MBB, I, DL, TII.get(Mips::LiRxImmX16), V0)
Akira Hatanakade4a1272012-07-25 03:16:47 +0000176 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
177 BuildMI(MBB, I, DL, TII.get(Mips::AddiuRxPcImmX16), V1)
178 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
179 BuildMI(MBB, I, DL, TII.get(Mips::SllX16), V2).addReg(V0).addImm(16);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000180 BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg)
181 .addReg(V1).addReg(V2);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000182 return;
183 }
184
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000185 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000186 // Set global register to __gnu_local_gp.
187 //
188 // lui $v0, %hi(__gnu_local_gp)
189 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
190 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
191 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
192 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
193 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000194 return;
Jia Liubb481f82012-02-28 07:46:26 +0000195 }
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000196
197 MF.getRegInfo().addLiveIn(Mips::T9);
198 MBB.addLiveIn(Mips::T9);
199
200 if (Subtarget.isABI_N32()) {
201 // lui $v0, %hi(%neg(%gp_rel(fname)))
202 // addu $v1, $v0, $t9
203 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
204 const GlobalValue *FName = MF.getFunction();
205 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
206 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
207 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
208 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
209 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
210 return;
211 }
212
213 assert(Subtarget.isABI_O32());
214
215 // For O32 ABI, the following instruction sequence is emitted to initialize
216 // the global base register:
217 //
218 // 0. lui $2, %hi(_gp_disp)
219 // 1. addiu $2, $2, %lo(_gp_disp)
220 // 2. addu $globalbasereg, $2, $t9
221 //
222 // We emit only the last instruction here.
223 //
224 // GNU linker requires that the first two instructions appear at the beginning
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000225 // of a function and no instructions be inserted before or between them.
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000226 // The two instructions are emitted during lowering to MC layer in order to
227 // avoid any reordering.
228 //
229 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
230 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
231 // reads it.
232 MF.getRegInfo().addLiveIn(Mips::V0);
233 MBB.addLiveIn(Mips::V0);
234 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
235 .addReg(Mips::V0).addReg(Mips::T9);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000236}
237
Reed Kotlerf99998a2012-10-28 06:02:37 +0000238// Insert instructions to initialize the Mips16 SP Alias register in the
239// first MBB of the function.
240//
241void MipsDAGToDAGISel::InitMips16SPAliasReg(MachineFunction &MF) {
242 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
243
244 if (!MipsFI->mips16SPAliasRegSet())
245 return;
246
247 MachineBasicBlock &MBB = MF.front();
248 MachineBasicBlock::iterator I = MBB.begin();
249 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
250 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
251 unsigned Mips16SPAliasReg = MipsFI->getMips16SPAliasReg();
252
253 BuildMI(MBB, I, DL, TII.get(Mips::MoveR3216), Mips16SPAliasReg)
254 .addReg(Mips::SP);
255}
256
257
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000258bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
259 const MachineInstr& MI) {
260 unsigned DstReg = 0, ZeroReg = 0;
261
262 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
263 if ((MI.getOpcode() == Mips::ADDiu) &&
264 (MI.getOperand(1).getReg() == Mips::ZERO) &&
265 (MI.getOperand(2).getImm() == 0)) {
266 DstReg = MI.getOperand(0).getReg();
267 ZeroReg = Mips::ZERO;
268 } else if ((MI.getOpcode() == Mips::DADDiu) &&
269 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
270 (MI.getOperand(2).getImm() == 0)) {
271 DstReg = MI.getOperand(0).getReg();
272 ZeroReg = Mips::ZERO_64;
273 }
274
275 if (!DstReg)
276 return false;
277
278 // Replace uses with ZeroReg.
279 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000280 E = MRI->use_end(); U != E;) {
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000281 MachineOperand &MO = U.getOperand();
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000282 unsigned OpNo = U.getOperandNo();
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000283 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000284 ++U;
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000285
286 // Do not replace if it is a phi's operand or is tied to def operand.
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000287 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000288 continue;
289
290 MO.setReg(ZeroReg);
291 }
292
293 return true;
294}
295
296void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
297 InitGlobalBaseReg(MF);
Reed Kotlerf99998a2012-10-28 06:02:37 +0000298 InitMips16SPAliasReg(MF);
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000299
300 MachineRegisterInfo *MRI = &MF.getRegInfo();
301
302 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
303 ++MFI)
304 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
305 ReplaceUsesWithZeroReg(MRI, *I);
306}
307
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000308bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
309 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000310
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000311 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000312
313 return Ret;
314}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000315
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000316/// getGlobalBaseReg - Output the instructions required to put the
317/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000318SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000319 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000320 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000321}
322
Reed Kotlerf99998a2012-10-28 06:02:37 +0000323/// getMips16SPAliasReg - Output the instructions required to put the
324/// SP into a Mips16 accessible aliased register.
325SDValue MipsDAGToDAGISel::getMips16SPAliasReg() {
326 unsigned Mips16SPAliasReg =
327 MF->getInfo<MipsFunctionInfo>()->getMips16SPAliasReg();
328 return CurDAG->getRegister(Mips16SPAliasReg, TLI.getPointerTy());
329}
330
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000331/// ComplexPattern used on MipsInstrInfo
332/// Used on Mips Load/Store instructions
Akira Hatanakaabbf9df2013-02-16 00:14:37 +0000333bool MipsDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base,
334 SDValue &Offset) const {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000335 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000336
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000337 // if Address is FI, get the TargetFrameIndex.
338 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000339 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
340 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000341 return true;
342 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000343
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000344 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000345 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000346 Base = Addr.getOperand(0);
347 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000348 return true;
349 }
350
351 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000352 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000353 Addr.getOpcode() == ISD::TargetGlobalAddress))
354 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000355 }
356
Akira Hatanaka5e069032011-06-02 01:03:14 +0000357 // Addresses of the form FI+const or FI|const
358 if (CurDAG->isBaseWithConstantOffset(Addr)) {
359 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
360 if (isInt<16>(CN->getSExtValue())) {
361
362 // If the first operand is a FI, get the TargetFI Node
363 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
364 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000365 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000366 else
367 Base = Addr.getOperand(0);
368
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000369 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000370 return true;
371 }
372 }
373
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000374 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000375 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000376 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000377 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000378 // lui $2, %hi($CPI1_0)
379 // addiu $2, $2, %lo($CPI1_0)
380 // lwc1 $f0, 0($2)
381 // Generate:
382 // lui $2, %hi($CPI1_0)
383 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka45d8dbc2012-08-24 20:21:49 +0000384 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
385 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
386 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000387 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
388 isa<JumpTableSDNode>(Opnd0)) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000389 Base = Addr.getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000390 Offset = Opnd0;
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000391 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000392 }
393 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000394 }
395
Akira Hatanakadc2f7922013-02-15 21:20:45 +0000396 return false;
397}
398
Akira Hatanakaabbf9df2013-02-16 00:14:37 +0000399bool MipsDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base,
400 SDValue &Offset) const {
Akira Hatanakadc2f7922013-02-15 21:20:45 +0000401 Base = Addr;
402 Offset = CurDAG->getTargetConstant(0, Addr.getValueType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000403 return true;
404}
405
Akira Hatanakaabbf9df2013-02-16 00:14:37 +0000406bool MipsDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base,
407 SDValue &Offset) const {
408 return selectAddrRegImm(Addr, Base, Offset) ||
409 selectAddrDefault(Addr, Base, Offset);
Akira Hatanakadc2f7922013-02-15 21:20:45 +0000410}
411
Reed Kotlerf99998a2012-10-28 06:02:37 +0000412void MipsDAGToDAGISel::getMips16SPRefReg(SDNode *Parent, SDValue &AliasReg) {
413 SDValue AliasFPReg = CurDAG->getRegister(Mips::S0, TLI.getPointerTy());
414 if (Parent) {
415 switch (Parent->getOpcode()) {
416 case ISD::LOAD: {
417 LoadSDNode *SD = dyn_cast<LoadSDNode>(Parent);
418 switch (SD->getMemoryVT().getSizeInBits()) {
419 case 8:
420 case 16:
421 AliasReg = TM.getFrameLowering()->hasFP(*MF)?
422 AliasFPReg: getMips16SPAliasReg();
423 return;
424 }
425 break;
426 }
427 case ISD::STORE: {
428 StoreSDNode *SD = dyn_cast<StoreSDNode>(Parent);
429 switch (SD->getMemoryVT().getSizeInBits()) {
430 case 8:
431 case 16:
432 AliasReg = TM.getFrameLowering()->hasFP(*MF)?
433 AliasFPReg: getMips16SPAliasReg();
434 return;
435 }
436 break;
437 }
438 }
439 }
440 AliasReg = CurDAG->getRegister(Mips::SP, TLI.getPointerTy());
441 return;
442
443}
444bool MipsDAGToDAGISel::SelectAddr16(
445 SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset,
446 SDValue &Alias) {
447 EVT ValTy = Addr.getValueType();
448
449 Alias = CurDAG->getTargetConstant(0, ValTy);
450
451 // if Address is FI, get the TargetFrameIndex.
452 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
453 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
454 Offset = CurDAG->getTargetConstant(0, ValTy);
455 getMips16SPRefReg(Parent, Alias);
456 return true;
457 }
458 // on PIC code Load GA
459 if (Addr.getOpcode() == MipsISD::Wrapper) {
460 Base = Addr.getOperand(0);
461 Offset = Addr.getOperand(1);
462 return true;
463 }
464 if (TM.getRelocationModel() != Reloc::PIC_) {
465 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
466 Addr.getOpcode() == ISD::TargetGlobalAddress))
467 return false;
468 }
469 // Addresses of the form FI+const or FI|const
470 if (CurDAG->isBaseWithConstantOffset(Addr)) {
471 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
472 if (isInt<16>(CN->getSExtValue())) {
473
474 // If the first operand is a FI, get the TargetFI Node
475 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
476 (Addr.getOperand(0))) {
477 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
478 getMips16SPRefReg(Parent, Alias);
479 }
480 else
481 Base = Addr.getOperand(0);
482
483 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
484 return true;
485 }
486 }
487 // Operand is a result from an ADD.
488 if (Addr.getOpcode() == ISD::ADD) {
489 // When loading from constant pools, load the lower address part in
490 // the instruction itself. Example, instead of:
491 // lui $2, %hi($CPI1_0)
492 // addiu $2, $2, %lo($CPI1_0)
493 // lwc1 $f0, 0($2)
494 // Generate:
495 // lui $2, %hi($CPI1_0)
496 // lwc1 $f0, %lo($CPI1_0)($2)
497 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
498 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
499 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
500 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
501 isa<JumpTableSDNode>(Opnd0)) {
502 Base = Addr.getOperand(0);
503 Offset = Opnd0;
504 return true;
505 }
506 }
507
508 // If an indexed floating point load/store can be emitted, return false.
509 const LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(Parent);
510
511 if (LS &&
512 (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
Akira Hatanaka0301bc52012-11-15 21:17:13 +0000513 Subtarget.hasFPIdx())
Reed Kotlerf99998a2012-10-28 06:02:37 +0000514 return false;
515 }
516 Base = Addr;
517 Offset = CurDAG->getTargetConstant(0, ValTy);
518 return true;
519}
520
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000521/// Select multiply instructions.
522std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000523MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000524 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000525 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000526 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
527 N->getOperand(1));
528 SDValue InFlag = SDValue(Mul, 0);
529
530 if (HasLo) {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000531 unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mflo16 :
532 (Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64);
533 Lo = CurDAG->getMachineNode(Opcode, dl, Ty, MVT::Glue, InFlag);
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000534 InFlag = SDValue(Lo, 1);
535 }
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000536 if (HasHi) {
537 unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mfhi16 :
538 (Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64);
539 Hi = CurDAG->getMachineNode(Opcode, dl, Ty, InFlag);
540 }
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000541 return std::make_pair(Lo, Hi);
542}
543
544
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000545/// Select instructions not customized! Used for
546/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000547SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000548 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000549 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000550
551 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000552 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000553
554 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000555 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000556 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000557 return NULL;
558 }
559
560 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000561 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000562 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000563 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000564 EVT NodeTy = Node->getValueType(0);
565 unsigned MultOpc;
566
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000567 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000568 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000569
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000570 case ISD::SUBE:
571 case ISD::ADDE: {
Reed Kotlera81be802012-10-26 04:46:26 +0000572 bool inMips16Mode = Subtarget.inMips16Mode();
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000573 SDValue InFlag = Node->getOperand(2), CmpLHS;
574 unsigned Opc = InFlag.getOpcode(); (void)Opc;
575 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
576 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
577 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000578
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000579 unsigned MOp;
580 if (Opcode == ISD::ADDE) {
581 CmpLHS = InFlag.getValue(0);
Reed Kotlera81be802012-10-26 04:46:26 +0000582 if (inMips16Mode)
583 MOp = Mips::AdduRxRyRz16;
584 else
585 MOp = Mips::ADDu;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000586 } else {
587 CmpLHS = InFlag.getOperand(0);
Reed Kotlera81be802012-10-26 04:46:26 +0000588 if (inMips16Mode)
589 MOp = Mips::SubuRxRyRz16;
590 else
591 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000592 }
593
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000594 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000595
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000596 SDValue LHS = Node->getOperand(0);
597 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000598
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000599 EVT VT = LHS.getValueType();
Reed Kotlera81be802012-10-26 04:46:26 +0000600
601 unsigned Sltu_op = inMips16Mode? Mips::SltuRxRyRz16: Mips::SLTu;
602 SDNode *Carry = CurDAG->getMachineNode(Sltu_op, dl, VT, Ops, 2);
603 unsigned Addu_op = inMips16Mode? Mips::AdduRxRyRz16 : Mips::ADDu;
604 SDNode *AddCarry = CurDAG->getMachineNode(Addu_op, dl, VT,
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000605 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000606
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000607 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
608 LHS, SDValue(AddCarry,0));
609 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000610
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000611 /// Mul with two results
612 case ISD::SMUL_LOHI:
613 case ISD::UMUL_LOHI: {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000614 if (NodeTy == MVT::i32) {
615 if (Subtarget.inMips16Mode())
616 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MultuRxRy16 :
617 Mips::MultRxRy16);
618 else
619 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
620 }
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000621 else
622 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000623
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000624 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
625 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000626
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000627 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000628 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000629
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000630 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000631 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000632
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000633 return NULL;
634 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000635
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000636 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000637 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000638 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000639 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000640 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000641 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
642 dl, NodeTy, true, false).first;
643 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000644 case ISD::MULHS:
645 case ISD::MULHU: {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000646 if (NodeTy == MVT::i32) {
647 if (Subtarget.inMips16Mode())
648 MultOpc = (Opcode == ISD::MULHU ?
649 Mips::MultuRxRy16 : Mips::MultRxRy16);
650 else
651 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
652 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000653 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000654 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
655
656 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000657 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000658
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000659 // Get target GOT address.
660 case ISD::GLOBAL_OFFSET_TABLE:
661 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000662
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000663 case ISD::ConstantFP: {
664 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
665 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
666 if (Subtarget.hasMips64()) {
667 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
668 Mips::ZERO_64, MVT::i64);
669 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000670 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000671
672 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
673 Mips::ZERO, MVT::i32);
674 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
675 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000676 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000677 break;
678 }
679
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000680 case ISD::Constant: {
681 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
682 unsigned Size = CN->getValueSizeInBits(0);
683
684 if (Size == 32)
685 break;
686
687 MipsAnalyzeImmediate AnalyzeImm;
688 int64_t Imm = CN->getSExtValue();
689
690 const MipsAnalyzeImmediate::InstSeq &Seq =
691 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000692
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000693 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
694 DebugLoc DL = CN->getDebugLoc();
695 SDNode *RegOpnd;
696 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
697 MVT::i64);
698
699 // The first instruction can be a LUi which is different from other
700 // instructions (ADDiu, ORI and SLL) in that it does not have a register
701 // operand.
702 if (Inst->Opc == Mips::LUi64)
703 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
704 else
705 RegOpnd =
706 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
707 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
708 ImmOpnd);
709
710 // The remaining instructions in the sequence are handled here.
711 for (++Inst; Inst != Seq.end(); ++Inst) {
712 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
713 MVT::i64);
714 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
715 SDValue(RegOpnd, 0), ImmOpnd);
716 }
717
718 return RegOpnd;
719 }
720
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000721#ifndef NDEBUG
722 case ISD::LOAD:
723 case ISD::STORE:
724 assert(cast<MemSDNode>(Node)->getMemoryVT().getSizeInBits() / 8 <=
725 cast<MemSDNode>(Node)->getAlignment() &&
726 "Unexpected unaligned loads/stores.");
727 break;
728#endif
729
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000730 case MipsISD::ThreadPointer: {
731 EVT PtrVT = TLI.getPointerTy();
732 unsigned RdhwrOpc, SrcReg, DestReg;
733
734 if (PtrVT == MVT::i32) {
735 RdhwrOpc = Mips::RDHWR;
736 SrcReg = Mips::HWR29;
737 DestReg = Mips::V1;
738 } else {
739 RdhwrOpc = Mips::RDHWR64;
740 SrcReg = Mips::HWR29_64;
741 DestReg = Mips::V1_64;
742 }
Jia Liubb481f82012-02-28 07:46:26 +0000743
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000744 SDNode *Rdhwr =
745 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
746 Node->getValueType(0),
747 CurDAG->getRegister(SrcReg, PtrVT));
748 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
749 SDValue(Rdhwr, 0));
750 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
751 ReplaceUses(SDValue(Node, 0), ResNode);
752 return ResNode.getNode();
753 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000754 }
755
756 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000757 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000758
Chris Lattner7c306da2010-03-02 06:34:30 +0000759 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000760 if (ResNode == NULL || ResNode == Node)
761 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000762 else
763 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000764 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000765 return ResNode;
766}
767
Akira Hatanaka21afc632011-06-21 00:40:49 +0000768bool MipsDAGToDAGISel::
769SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
770 std::vector<SDValue> &OutOps) {
771 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
772 OutOps.push_back(Op);
773 return false;
774}
775
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000776/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000777/// MIPS-specific DAG, ready for instruction scheduling.
778FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
779 return new MipsDAGToDAGISel(TM);
780}