blob: 385ade5b2afb34dbb8acd12b699112c6d85fe5bc [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).
100 bool selectAddrRegImm(SDNode *Parent, SDValue Addr, SDValue &Base,
101 SDValue &Offset) const;
102
103 /// Fall back on this function if all else fails.
104 bool selectAddrDefault(SDNode *Parent, SDValue Addr, SDValue &Base,
105 SDValue &Offset) const;
106
107 /// Match integer address pattern.
108 bool selectIntAddr(SDNode *Parent, SDValue Addr, SDValue &Base,
109 SDValue &Offset) const;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000110
Reed Kotlerf99998a2012-10-28 06:02:37 +0000111 bool SelectAddr16(SDNode *Parent, SDValue N, SDValue &Base, SDValue &Offset,
112 SDValue &Alias);
113
Akira Hatanakabd150902011-12-07 20:15:01 +0000114 // getImm - Return a target constant with the specified value.
Akira Hatanaka4d0eb632011-12-07 20:10:24 +0000115 inline SDValue getImm(const SDNode *Node, unsigned Imm) {
116 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000117 }
Akira Hatanaka21afc632011-06-21 00:40:49 +0000118
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000119 void ProcessFunctionAfterISel(MachineFunction &MF);
120 bool ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI, const MachineInstr&);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000121 void InitGlobalBaseReg(MachineFunction &MF);
Reed Kotlerf99998a2012-10-28 06:02:37 +0000122 void InitMips16SPAliasReg(MachineFunction &MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000123
Akira Hatanaka21afc632011-06-21 00:40:49 +0000124 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
125 char ConstraintCode,
126 std::vector<SDValue> &OutOps);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000127};
128
129}
130
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000131// Insert instructions to initialize the global base register in the
132// first MBB of the function. When the ABI is O32 and the relocation model is
133// PIC, the necessary instructions are emitted later to prevent optimization
134// passes from moving them.
135void MipsDAGToDAGISel::InitGlobalBaseReg(MachineFunction &MF) {
136 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Jia Liubb481f82012-02-28 07:46:26 +0000137
Akira Hatanakade4a1272012-07-25 03:16:47 +0000138 if (!MipsFI->globalBaseRegSet())
Akira Hatanaka4782a6e2012-06-27 00:20:39 +0000139 return;
140
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000141 MachineBasicBlock &MBB = MF.front();
142 MachineBasicBlock::iterator I = MBB.begin();
143 MachineRegisterInfo &RegInfo = MF.getRegInfo();
144 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
145 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000146 unsigned V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg();
Akira Hatanakade4a1272012-07-25 03:16:47 +0000147 const TargetRegisterClass *RC;
Akira Hatanaka54c5bc82012-06-21 20:39:10 +0000148
Akira Hatanakade4a1272012-07-25 03:16:47 +0000149 if (Subtarget.isABI_N64())
150 RC = (const TargetRegisterClass*)&Mips::CPU64RegsRegClass;
151 else if (Subtarget.inMips16Mode())
152 RC = (const TargetRegisterClass*)&Mips::CPU16RegsRegClass;
153 else
154 RC = (const TargetRegisterClass*)&Mips::CPURegsRegClass;
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000155
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000156 V0 = RegInfo.createVirtualRegister(RC);
157 V1 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000158 V2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000159
160 if (Subtarget.isABI_N64()) {
161 MF.getRegInfo().addLiveIn(Mips::T9_64);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000162 MBB.addLiveIn(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000163
164 // lui $v0, %hi(%neg(%gp_rel(fname)))
165 // daddu $v1, $v0, $t9
166 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
167 const GlobalValue *FName = MF.getFunction();
168 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
169 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000170 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
171 .addReg(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000172 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
173 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000174 return;
175 }
176
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000177 if (Subtarget.inMips16Mode()) {
178 BuildMI(MBB, I, DL, TII.get(Mips::LiRxImmX16), V0)
Akira Hatanakade4a1272012-07-25 03:16:47 +0000179 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
180 BuildMI(MBB, I, DL, TII.get(Mips::AddiuRxPcImmX16), V1)
181 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
182 BuildMI(MBB, I, DL, TII.get(Mips::SllX16), V2).addReg(V0).addImm(16);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000183 BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg)
184 .addReg(V1).addReg(V2);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000185 return;
186 }
187
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000188 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000189 // Set global register to __gnu_local_gp.
190 //
191 // lui $v0, %hi(__gnu_local_gp)
192 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
193 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
194 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
195 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
196 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000197 return;
Jia Liubb481f82012-02-28 07:46:26 +0000198 }
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000199
200 MF.getRegInfo().addLiveIn(Mips::T9);
201 MBB.addLiveIn(Mips::T9);
202
203 if (Subtarget.isABI_N32()) {
204 // lui $v0, %hi(%neg(%gp_rel(fname)))
205 // addu $v1, $v0, $t9
206 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
207 const GlobalValue *FName = MF.getFunction();
208 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
209 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
210 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
211 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
212 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
213 return;
214 }
215
216 assert(Subtarget.isABI_O32());
217
218 // For O32 ABI, the following instruction sequence is emitted to initialize
219 // the global base register:
220 //
221 // 0. lui $2, %hi(_gp_disp)
222 // 1. addiu $2, $2, %lo(_gp_disp)
223 // 2. addu $globalbasereg, $2, $t9
224 //
225 // We emit only the last instruction here.
226 //
227 // GNU linker requires that the first two instructions appear at the beginning
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000228 // of a function and no instructions be inserted before or between them.
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000229 // The two instructions are emitted during lowering to MC layer in order to
230 // avoid any reordering.
231 //
232 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
233 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
234 // reads it.
235 MF.getRegInfo().addLiveIn(Mips::V0);
236 MBB.addLiveIn(Mips::V0);
237 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
238 .addReg(Mips::V0).addReg(Mips::T9);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000239}
240
Reed Kotlerf99998a2012-10-28 06:02:37 +0000241// Insert instructions to initialize the Mips16 SP Alias register in the
242// first MBB of the function.
243//
244void MipsDAGToDAGISel::InitMips16SPAliasReg(MachineFunction &MF) {
245 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
246
247 if (!MipsFI->mips16SPAliasRegSet())
248 return;
249
250 MachineBasicBlock &MBB = MF.front();
251 MachineBasicBlock::iterator I = MBB.begin();
252 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
253 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
254 unsigned Mips16SPAliasReg = MipsFI->getMips16SPAliasReg();
255
256 BuildMI(MBB, I, DL, TII.get(Mips::MoveR3216), Mips16SPAliasReg)
257 .addReg(Mips::SP);
258}
259
260
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000261bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
262 const MachineInstr& MI) {
263 unsigned DstReg = 0, ZeroReg = 0;
264
265 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
266 if ((MI.getOpcode() == Mips::ADDiu) &&
267 (MI.getOperand(1).getReg() == Mips::ZERO) &&
268 (MI.getOperand(2).getImm() == 0)) {
269 DstReg = MI.getOperand(0).getReg();
270 ZeroReg = Mips::ZERO;
271 } else if ((MI.getOpcode() == Mips::DADDiu) &&
272 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
273 (MI.getOperand(2).getImm() == 0)) {
274 DstReg = MI.getOperand(0).getReg();
275 ZeroReg = Mips::ZERO_64;
276 }
277
278 if (!DstReg)
279 return false;
280
281 // Replace uses with ZeroReg.
282 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000283 E = MRI->use_end(); U != E;) {
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000284 MachineOperand &MO = U.getOperand();
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000285 unsigned OpNo = U.getOperandNo();
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000286 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000287 ++U;
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000288
289 // Do not replace if it is a phi's operand or is tied to def operand.
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000290 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000291 continue;
292
293 MO.setReg(ZeroReg);
294 }
295
296 return true;
297}
298
299void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
300 InitGlobalBaseReg(MF);
Reed Kotlerf99998a2012-10-28 06:02:37 +0000301 InitMips16SPAliasReg(MF);
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000302
303 MachineRegisterInfo *MRI = &MF.getRegInfo();
304
305 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
306 ++MFI)
307 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
308 ReplaceUsesWithZeroReg(MRI, *I);
309}
310
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000311bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
312 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000313
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000314 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000315
316 return Ret;
317}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000318
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000319/// getGlobalBaseReg - Output the instructions required to put the
320/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000321SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000322 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000323 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000324}
325
Reed Kotlerf99998a2012-10-28 06:02:37 +0000326/// getMips16SPAliasReg - Output the instructions required to put the
327/// SP into a Mips16 accessible aliased register.
328SDValue MipsDAGToDAGISel::getMips16SPAliasReg() {
329 unsigned Mips16SPAliasReg =
330 MF->getInfo<MipsFunctionInfo>()->getMips16SPAliasReg();
331 return CurDAG->getRegister(Mips16SPAliasReg, TLI.getPointerTy());
332}
333
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000334/// ComplexPattern used on MipsInstrInfo
335/// Used on Mips Load/Store instructions
Akira Hatanakadc2f7922013-02-15 21:20:45 +0000336bool MipsDAGToDAGISel::selectAddrRegImm(SDNode *Parent, SDValue Addr,
337 SDValue &Base, SDValue &Offset) const {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000338 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000339
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000340 // if Address is FI, get the TargetFrameIndex.
341 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000342 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
343 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000344 return true;
345 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000346
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000347 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000348 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000349 Base = Addr.getOperand(0);
350 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000351 return true;
352 }
353
354 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000355 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000356 Addr.getOpcode() == ISD::TargetGlobalAddress))
357 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000358 }
359
Akira Hatanaka5e069032011-06-02 01:03:14 +0000360 // Addresses of the form FI+const or FI|const
361 if (CurDAG->isBaseWithConstantOffset(Addr)) {
362 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
363 if (isInt<16>(CN->getSExtValue())) {
364
365 // If the first operand is a FI, get the TargetFI Node
366 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
367 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000368 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000369 else
370 Base = Addr.getOperand(0);
371
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000372 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000373 return true;
374 }
375 }
376
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000377 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000378 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000379 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000380 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000381 // lui $2, %hi($CPI1_0)
382 // addiu $2, $2, %lo($CPI1_0)
383 // lwc1 $f0, 0($2)
384 // Generate:
385 // lui $2, %hi($CPI1_0)
386 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka45d8dbc2012-08-24 20:21:49 +0000387 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
388 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
389 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000390 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
391 isa<JumpTableSDNode>(Opnd0)) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000392 Base = Addr.getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000393 Offset = Opnd0;
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000394 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000395 }
396 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000397 }
398
Akira Hatanakadc2f7922013-02-15 21:20:45 +0000399 return false;
400}
401
402bool MipsDAGToDAGISel::selectAddrDefault(SDNode *Parent, SDValue Addr,
403 SDValue &Base, SDValue &Offset) const {
404 Base = Addr;
405 Offset = CurDAG->getTargetConstant(0, Addr.getValueType());
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000406 return true;
407}
408
Akira Hatanakadc2f7922013-02-15 21:20:45 +0000409bool MipsDAGToDAGISel::selectIntAddr(SDNode *Parent, SDValue Addr,
410 SDValue &Base, SDValue &Offset) const {
411 return selectAddrRegImm(Parent, Addr, Base, Offset) ||
412 selectAddrDefault(Parent, Addr, Base, Offset);
413}
414
Reed Kotlerf99998a2012-10-28 06:02:37 +0000415void MipsDAGToDAGISel::getMips16SPRefReg(SDNode *Parent, SDValue &AliasReg) {
416 SDValue AliasFPReg = CurDAG->getRegister(Mips::S0, TLI.getPointerTy());
417 if (Parent) {
418 switch (Parent->getOpcode()) {
419 case ISD::LOAD: {
420 LoadSDNode *SD = dyn_cast<LoadSDNode>(Parent);
421 switch (SD->getMemoryVT().getSizeInBits()) {
422 case 8:
423 case 16:
424 AliasReg = TM.getFrameLowering()->hasFP(*MF)?
425 AliasFPReg: getMips16SPAliasReg();
426 return;
427 }
428 break;
429 }
430 case ISD::STORE: {
431 StoreSDNode *SD = dyn_cast<StoreSDNode>(Parent);
432 switch (SD->getMemoryVT().getSizeInBits()) {
433 case 8:
434 case 16:
435 AliasReg = TM.getFrameLowering()->hasFP(*MF)?
436 AliasFPReg: getMips16SPAliasReg();
437 return;
438 }
439 break;
440 }
441 }
442 }
443 AliasReg = CurDAG->getRegister(Mips::SP, TLI.getPointerTy());
444 return;
445
446}
447bool MipsDAGToDAGISel::SelectAddr16(
448 SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset,
449 SDValue &Alias) {
450 EVT ValTy = Addr.getValueType();
451
452 Alias = CurDAG->getTargetConstant(0, ValTy);
453
454 // if Address is FI, get the TargetFrameIndex.
455 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
456 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
457 Offset = CurDAG->getTargetConstant(0, ValTy);
458 getMips16SPRefReg(Parent, Alias);
459 return true;
460 }
461 // on PIC code Load GA
462 if (Addr.getOpcode() == MipsISD::Wrapper) {
463 Base = Addr.getOperand(0);
464 Offset = Addr.getOperand(1);
465 return true;
466 }
467 if (TM.getRelocationModel() != Reloc::PIC_) {
468 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
469 Addr.getOpcode() == ISD::TargetGlobalAddress))
470 return false;
471 }
472 // Addresses of the form FI+const or FI|const
473 if (CurDAG->isBaseWithConstantOffset(Addr)) {
474 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
475 if (isInt<16>(CN->getSExtValue())) {
476
477 // If the first operand is a FI, get the TargetFI Node
478 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
479 (Addr.getOperand(0))) {
480 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
481 getMips16SPRefReg(Parent, Alias);
482 }
483 else
484 Base = Addr.getOperand(0);
485
486 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
487 return true;
488 }
489 }
490 // Operand is a result from an ADD.
491 if (Addr.getOpcode() == ISD::ADD) {
492 // When loading from constant pools, load the lower address part in
493 // the instruction itself. Example, instead of:
494 // lui $2, %hi($CPI1_0)
495 // addiu $2, $2, %lo($CPI1_0)
496 // lwc1 $f0, 0($2)
497 // Generate:
498 // lui $2, %hi($CPI1_0)
499 // lwc1 $f0, %lo($CPI1_0)($2)
500 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
501 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
502 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
503 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
504 isa<JumpTableSDNode>(Opnd0)) {
505 Base = Addr.getOperand(0);
506 Offset = Opnd0;
507 return true;
508 }
509 }
510
511 // If an indexed floating point load/store can be emitted, return false.
512 const LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(Parent);
513
514 if (LS &&
515 (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
Akira Hatanaka0301bc52012-11-15 21:17:13 +0000516 Subtarget.hasFPIdx())
Reed Kotlerf99998a2012-10-28 06:02:37 +0000517 return false;
518 }
519 Base = Addr;
520 Offset = CurDAG->getTargetConstant(0, ValTy);
521 return true;
522}
523
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000524/// Select multiply instructions.
525std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000526MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000527 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000528 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000529 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
530 N->getOperand(1));
531 SDValue InFlag = SDValue(Mul, 0);
532
533 if (HasLo) {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000534 unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mflo16 :
535 (Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64);
536 Lo = CurDAG->getMachineNode(Opcode, dl, Ty, MVT::Glue, InFlag);
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000537 InFlag = SDValue(Lo, 1);
538 }
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000539 if (HasHi) {
540 unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mfhi16 :
541 (Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64);
542 Hi = CurDAG->getMachineNode(Opcode, dl, Ty, InFlag);
543 }
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000544 return std::make_pair(Lo, Hi);
545}
546
547
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000548/// Select instructions not customized! Used for
549/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000550SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000551 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000552 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000553
554 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000555 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000556
557 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000558 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000559 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000560 return NULL;
561 }
562
563 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000564 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000565 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000566 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000567 EVT NodeTy = Node->getValueType(0);
568 unsigned MultOpc;
569
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000570 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000571 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000572
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000573 case ISD::SUBE:
574 case ISD::ADDE: {
Reed Kotlera81be802012-10-26 04:46:26 +0000575 bool inMips16Mode = Subtarget.inMips16Mode();
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000576 SDValue InFlag = Node->getOperand(2), CmpLHS;
577 unsigned Opc = InFlag.getOpcode(); (void)Opc;
578 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
579 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
580 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000581
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000582 unsigned MOp;
583 if (Opcode == ISD::ADDE) {
584 CmpLHS = InFlag.getValue(0);
Reed Kotlera81be802012-10-26 04:46:26 +0000585 if (inMips16Mode)
586 MOp = Mips::AdduRxRyRz16;
587 else
588 MOp = Mips::ADDu;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000589 } else {
590 CmpLHS = InFlag.getOperand(0);
Reed Kotlera81be802012-10-26 04:46:26 +0000591 if (inMips16Mode)
592 MOp = Mips::SubuRxRyRz16;
593 else
594 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000595 }
596
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000597 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000598
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000599 SDValue LHS = Node->getOperand(0);
600 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000601
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000602 EVT VT = LHS.getValueType();
Reed Kotlera81be802012-10-26 04:46:26 +0000603
604 unsigned Sltu_op = inMips16Mode? Mips::SltuRxRyRz16: Mips::SLTu;
605 SDNode *Carry = CurDAG->getMachineNode(Sltu_op, dl, VT, Ops, 2);
606 unsigned Addu_op = inMips16Mode? Mips::AdduRxRyRz16 : Mips::ADDu;
607 SDNode *AddCarry = CurDAG->getMachineNode(Addu_op, dl, VT,
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000608 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000609
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000610 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
611 LHS, SDValue(AddCarry,0));
612 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000613
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000614 /// Mul with two results
615 case ISD::SMUL_LOHI:
616 case ISD::UMUL_LOHI: {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000617 if (NodeTy == MVT::i32) {
618 if (Subtarget.inMips16Mode())
619 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MultuRxRy16 :
620 Mips::MultRxRy16);
621 else
622 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
623 }
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000624 else
625 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000626
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000627 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
628 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000629
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000630 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000631 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000632
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000633 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000634 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000635
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000636 return NULL;
637 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000638
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000639 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000640 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000641 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000642 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000643 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000644 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
645 dl, NodeTy, true, false).first;
646 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000647 case ISD::MULHS:
648 case ISD::MULHU: {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000649 if (NodeTy == MVT::i32) {
650 if (Subtarget.inMips16Mode())
651 MultOpc = (Opcode == ISD::MULHU ?
652 Mips::MultuRxRy16 : Mips::MultRxRy16);
653 else
654 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
655 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000656 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000657 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
658
659 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000660 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000661
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000662 // Get target GOT address.
663 case ISD::GLOBAL_OFFSET_TABLE:
664 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000665
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000666 case ISD::ConstantFP: {
667 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
668 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
669 if (Subtarget.hasMips64()) {
670 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
671 Mips::ZERO_64, MVT::i64);
672 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000673 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000674
675 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
676 Mips::ZERO, MVT::i32);
677 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
678 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000679 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000680 break;
681 }
682
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000683 case ISD::Constant: {
684 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
685 unsigned Size = CN->getValueSizeInBits(0);
686
687 if (Size == 32)
688 break;
689
690 MipsAnalyzeImmediate AnalyzeImm;
691 int64_t Imm = CN->getSExtValue();
692
693 const MipsAnalyzeImmediate::InstSeq &Seq =
694 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000695
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000696 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
697 DebugLoc DL = CN->getDebugLoc();
698 SDNode *RegOpnd;
699 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
700 MVT::i64);
701
702 // The first instruction can be a LUi which is different from other
703 // instructions (ADDiu, ORI and SLL) in that it does not have a register
704 // operand.
705 if (Inst->Opc == Mips::LUi64)
706 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
707 else
708 RegOpnd =
709 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
710 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
711 ImmOpnd);
712
713 // The remaining instructions in the sequence are handled here.
714 for (++Inst; Inst != Seq.end(); ++Inst) {
715 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
716 MVT::i64);
717 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
718 SDValue(RegOpnd, 0), ImmOpnd);
719 }
720
721 return RegOpnd;
722 }
723
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000724#ifndef NDEBUG
725 case ISD::LOAD:
726 case ISD::STORE:
727 assert(cast<MemSDNode>(Node)->getMemoryVT().getSizeInBits() / 8 <=
728 cast<MemSDNode>(Node)->getAlignment() &&
729 "Unexpected unaligned loads/stores.");
730 break;
731#endif
732
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000733 case MipsISD::ThreadPointer: {
734 EVT PtrVT = TLI.getPointerTy();
735 unsigned RdhwrOpc, SrcReg, DestReg;
736
737 if (PtrVT == MVT::i32) {
738 RdhwrOpc = Mips::RDHWR;
739 SrcReg = Mips::HWR29;
740 DestReg = Mips::V1;
741 } else {
742 RdhwrOpc = Mips::RDHWR64;
743 SrcReg = Mips::HWR29_64;
744 DestReg = Mips::V1_64;
745 }
Jia Liubb481f82012-02-28 07:46:26 +0000746
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000747 SDNode *Rdhwr =
748 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
749 Node->getValueType(0),
750 CurDAG->getRegister(SrcReg, PtrVT));
751 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
752 SDValue(Rdhwr, 0));
753 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
754 ReplaceUses(SDValue(Node, 0), ResNode);
755 return ResNode.getNode();
756 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000757 }
758
759 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000760 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000761
Chris Lattner7c306da2010-03-02 06:34:30 +0000762 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000763 if (ResNode == NULL || ResNode == Node)
764 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000765 else
766 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000767 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000768 return ResNode;
769}
770
Akira Hatanaka21afc632011-06-21 00:40:49 +0000771bool MipsDAGToDAGISel::
772SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
773 std::vector<SDValue> &OutOps) {
774 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
775 OutOps.push_back(Op);
776 return false;
777}
778
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000779/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000780/// MIPS-specific DAG, ready for instruction scheduling.
781FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
782 return new MipsDAGToDAGISel(TM);
783}