blob: 76fc2a66085cf6ad76c96c2637aef439ae898fb1 [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 Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/GlobalValue.h"
30#include "llvm/Instructions.h"
31#include "llvm/Intrinsics.h"
32#include "llvm/Support/CFG.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000033#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/Target/TargetMachine.h"
37#include "llvm/Type.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 Hatanaka44b6c712012-02-28 02:55:02 +000099 bool SelectAddr(SDNode *Parent, SDValue N, SDValue &Base, SDValue &Offset);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000100
Reed Kotlerf99998a2012-10-28 06:02:37 +0000101 bool SelectAddr16(SDNode *Parent, SDValue N, SDValue &Base, SDValue &Offset,
102 SDValue &Alias);
103
Akira Hatanakabd150902011-12-07 20:15:01 +0000104 // getImm - Return a target constant with the specified value.
Akira Hatanaka4d0eb632011-12-07 20:10:24 +0000105 inline SDValue getImm(const SDNode *Node, unsigned Imm) {
106 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000107 }
Akira Hatanaka21afc632011-06-21 00:40:49 +0000108
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000109 void ProcessFunctionAfterISel(MachineFunction &MF);
110 bool ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI, const MachineInstr&);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000111 void InitGlobalBaseReg(MachineFunction &MF);
Reed Kotlerf99998a2012-10-28 06:02:37 +0000112 void InitMips16SPAliasReg(MachineFunction &MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000113
Akira Hatanaka21afc632011-06-21 00:40:49 +0000114 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
115 char ConstraintCode,
116 std::vector<SDValue> &OutOps);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000117};
118
119}
120
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000121// Insert instructions to initialize the global base register in the
122// first MBB of the function. When the ABI is O32 and the relocation model is
123// PIC, the necessary instructions are emitted later to prevent optimization
124// passes from moving them.
125void MipsDAGToDAGISel::InitGlobalBaseReg(MachineFunction &MF) {
126 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Jia Liubb481f82012-02-28 07:46:26 +0000127
Akira Hatanakade4a1272012-07-25 03:16:47 +0000128 if (!MipsFI->globalBaseRegSet())
Akira Hatanaka4782a6e2012-06-27 00:20:39 +0000129 return;
130
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000131 MachineBasicBlock &MBB = MF.front();
132 MachineBasicBlock::iterator I = MBB.begin();
133 MachineRegisterInfo &RegInfo = MF.getRegInfo();
134 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
135 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000136 unsigned V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg();
Akira Hatanakade4a1272012-07-25 03:16:47 +0000137 const TargetRegisterClass *RC;
Akira Hatanaka54c5bc82012-06-21 20:39:10 +0000138
Akira Hatanakade4a1272012-07-25 03:16:47 +0000139 if (Subtarget.isABI_N64())
140 RC = (const TargetRegisterClass*)&Mips::CPU64RegsRegClass;
141 else if (Subtarget.inMips16Mode())
142 RC = (const TargetRegisterClass*)&Mips::CPU16RegsRegClass;
143 else
144 RC = (const TargetRegisterClass*)&Mips::CPURegsRegClass;
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000145
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000146 V0 = RegInfo.createVirtualRegister(RC);
147 V1 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000148 V2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000149
150 if (Subtarget.isABI_N64()) {
151 MF.getRegInfo().addLiveIn(Mips::T9_64);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000152 MBB.addLiveIn(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000153
154 // lui $v0, %hi(%neg(%gp_rel(fname)))
155 // daddu $v1, $v0, $t9
156 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
157 const GlobalValue *FName = MF.getFunction();
158 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
159 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000160 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
161 .addReg(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000162 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
163 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000164 return;
165 }
166
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000167 if (Subtarget.inMips16Mode()) {
168 BuildMI(MBB, I, DL, TII.get(Mips::LiRxImmX16), V0)
Akira Hatanakade4a1272012-07-25 03:16:47 +0000169 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
170 BuildMI(MBB, I, DL, TII.get(Mips::AddiuRxPcImmX16), V1)
171 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
172 BuildMI(MBB, I, DL, TII.get(Mips::SllX16), V2).addReg(V0).addImm(16);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000173 BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg)
174 .addReg(V1).addReg(V2);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000175 return;
176 }
177
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000178 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000179 // Set global register to __gnu_local_gp.
180 //
181 // lui $v0, %hi(__gnu_local_gp)
182 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
183 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
184 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
185 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
186 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000187 return;
Jia Liubb481f82012-02-28 07:46:26 +0000188 }
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000189
190 MF.getRegInfo().addLiveIn(Mips::T9);
191 MBB.addLiveIn(Mips::T9);
192
193 if (Subtarget.isABI_N32()) {
194 // lui $v0, %hi(%neg(%gp_rel(fname)))
195 // addu $v1, $v0, $t9
196 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
197 const GlobalValue *FName = MF.getFunction();
198 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
199 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
200 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
201 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
202 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
203 return;
204 }
205
206 assert(Subtarget.isABI_O32());
207
208 // For O32 ABI, the following instruction sequence is emitted to initialize
209 // the global base register:
210 //
211 // 0. lui $2, %hi(_gp_disp)
212 // 1. addiu $2, $2, %lo(_gp_disp)
213 // 2. addu $globalbasereg, $2, $t9
214 //
215 // We emit only the last instruction here.
216 //
217 // GNU linker requires that the first two instructions appear at the beginning
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000218 // of a function and no instructions be inserted before or between them.
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000219 // The two instructions are emitted during lowering to MC layer in order to
220 // avoid any reordering.
221 //
222 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
223 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
224 // reads it.
225 MF.getRegInfo().addLiveIn(Mips::V0);
226 MBB.addLiveIn(Mips::V0);
227 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
228 .addReg(Mips::V0).addReg(Mips::T9);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000229}
230
Reed Kotlerf99998a2012-10-28 06:02:37 +0000231// Insert instructions to initialize the Mips16 SP Alias register in the
232// first MBB of the function.
233//
234void MipsDAGToDAGISel::InitMips16SPAliasReg(MachineFunction &MF) {
235 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
236
237 if (!MipsFI->mips16SPAliasRegSet())
238 return;
239
240 MachineBasicBlock &MBB = MF.front();
241 MachineBasicBlock::iterator I = MBB.begin();
242 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
243 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
244 unsigned Mips16SPAliasReg = MipsFI->getMips16SPAliasReg();
245
246 BuildMI(MBB, I, DL, TII.get(Mips::MoveR3216), Mips16SPAliasReg)
247 .addReg(Mips::SP);
248}
249
250
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000251bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
252 const MachineInstr& MI) {
253 unsigned DstReg = 0, ZeroReg = 0;
254
255 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
256 if ((MI.getOpcode() == Mips::ADDiu) &&
257 (MI.getOperand(1).getReg() == Mips::ZERO) &&
258 (MI.getOperand(2).getImm() == 0)) {
259 DstReg = MI.getOperand(0).getReg();
260 ZeroReg = Mips::ZERO;
261 } else if ((MI.getOpcode() == Mips::DADDiu) &&
262 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
263 (MI.getOperand(2).getImm() == 0)) {
264 DstReg = MI.getOperand(0).getReg();
265 ZeroReg = Mips::ZERO_64;
266 }
267
268 if (!DstReg)
269 return false;
270
271 // Replace uses with ZeroReg.
272 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000273 E = MRI->use_end(); U != E;) {
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000274 MachineOperand &MO = U.getOperand();
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000275 unsigned OpNo = U.getOperandNo();
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000276 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000277 ++U;
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000278
279 // Do not replace if it is a phi's operand or is tied to def operand.
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000280 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000281 continue;
282
283 MO.setReg(ZeroReg);
284 }
285
286 return true;
287}
288
289void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
290 InitGlobalBaseReg(MF);
Reed Kotlerf99998a2012-10-28 06:02:37 +0000291 InitMips16SPAliasReg(MF);
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000292
293 MachineRegisterInfo *MRI = &MF.getRegInfo();
294
295 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
296 ++MFI)
297 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
298 ReplaceUsesWithZeroReg(MRI, *I);
299}
300
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000301bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
302 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000303
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000304 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000305
306 return Ret;
307}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000308
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000309/// getGlobalBaseReg - Output the instructions required to put the
310/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000311SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000312 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000313 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000314}
315
Reed Kotlerf99998a2012-10-28 06:02:37 +0000316/// getMips16SPAliasReg - Output the instructions required to put the
317/// SP into a Mips16 accessible aliased register.
318SDValue MipsDAGToDAGISel::getMips16SPAliasReg() {
319 unsigned Mips16SPAliasReg =
320 MF->getInfo<MipsFunctionInfo>()->getMips16SPAliasReg();
321 return CurDAG->getRegister(Mips16SPAliasReg, TLI.getPointerTy());
322}
323
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000324/// ComplexPattern used on MipsInstrInfo
325/// Used on Mips Load/Store instructions
326bool MipsDAGToDAGISel::
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000327SelectAddr(SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000328 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000329
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000330 // if Address is FI, get the TargetFrameIndex.
331 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000332 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
333 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000334 return true;
335 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000336
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000337 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000338 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000339 Base = Addr.getOperand(0);
340 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000341 return true;
342 }
343
344 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000345 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000346 Addr.getOpcode() == ISD::TargetGlobalAddress))
347 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000348 }
349
Akira Hatanaka5e069032011-06-02 01:03:14 +0000350 // Addresses of the form FI+const or FI|const
351 if (CurDAG->isBaseWithConstantOffset(Addr)) {
352 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
353 if (isInt<16>(CN->getSExtValue())) {
354
355 // If the first operand is a FI, get the TargetFI Node
356 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
357 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000358 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000359 else
360 Base = Addr.getOperand(0);
361
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000362 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000363 return true;
364 }
365 }
366
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000367 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000368 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000369 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000370 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000371 // lui $2, %hi($CPI1_0)
372 // addiu $2, $2, %lo($CPI1_0)
373 // lwc1 $f0, 0($2)
374 // Generate:
375 // lui $2, %hi($CPI1_0)
376 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka45d8dbc2012-08-24 20:21:49 +0000377 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
378 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
379 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000380 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
381 isa<JumpTableSDNode>(Opnd0)) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000382 Base = Addr.getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000383 Offset = Opnd0;
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000384 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000385 }
386 }
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000387
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000388 // If an indexed floating point load/store can be emitted, return false.
Akira Hatanaka36bcc112012-07-31 18:16:49 +0000389 const LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(Parent);
390
Akira Hatanaka864f6602012-06-14 21:10:56 +0000391 if (LS &&
392 (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
Akira Hatanaka0301bc52012-11-15 21:17:13 +0000393 Subtarget.hasFPIdx())
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000394 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000395 }
396
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000397 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000398 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000399 return true;
400}
401
Reed Kotlerf99998a2012-10-28 06:02:37 +0000402void MipsDAGToDAGISel::getMips16SPRefReg(SDNode *Parent, SDValue &AliasReg) {
403 SDValue AliasFPReg = CurDAG->getRegister(Mips::S0, TLI.getPointerTy());
404 if (Parent) {
405 switch (Parent->getOpcode()) {
406 case ISD::LOAD: {
407 LoadSDNode *SD = dyn_cast<LoadSDNode>(Parent);
408 switch (SD->getMemoryVT().getSizeInBits()) {
409 case 8:
410 case 16:
411 AliasReg = TM.getFrameLowering()->hasFP(*MF)?
412 AliasFPReg: getMips16SPAliasReg();
413 return;
414 }
415 break;
416 }
417 case ISD::STORE: {
418 StoreSDNode *SD = dyn_cast<StoreSDNode>(Parent);
419 switch (SD->getMemoryVT().getSizeInBits()) {
420 case 8:
421 case 16:
422 AliasReg = TM.getFrameLowering()->hasFP(*MF)?
423 AliasFPReg: getMips16SPAliasReg();
424 return;
425 }
426 break;
427 }
428 }
429 }
430 AliasReg = CurDAG->getRegister(Mips::SP, TLI.getPointerTy());
431 return;
432
433}
434bool MipsDAGToDAGISel::SelectAddr16(
435 SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset,
436 SDValue &Alias) {
437 EVT ValTy = Addr.getValueType();
438
439 Alias = CurDAG->getTargetConstant(0, ValTy);
440
441 // if Address is FI, get the TargetFrameIndex.
442 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
443 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
444 Offset = CurDAG->getTargetConstant(0, ValTy);
445 getMips16SPRefReg(Parent, Alias);
446 return true;
447 }
448 // on PIC code Load GA
449 if (Addr.getOpcode() == MipsISD::Wrapper) {
450 Base = Addr.getOperand(0);
451 Offset = Addr.getOperand(1);
452 return true;
453 }
454 if (TM.getRelocationModel() != Reloc::PIC_) {
455 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
456 Addr.getOpcode() == ISD::TargetGlobalAddress))
457 return false;
458 }
459 // Addresses of the form FI+const or FI|const
460 if (CurDAG->isBaseWithConstantOffset(Addr)) {
461 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
462 if (isInt<16>(CN->getSExtValue())) {
463
464 // If the first operand is a FI, get the TargetFI Node
465 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
466 (Addr.getOperand(0))) {
467 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
468 getMips16SPRefReg(Parent, Alias);
469 }
470 else
471 Base = Addr.getOperand(0);
472
473 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
474 return true;
475 }
476 }
477 // Operand is a result from an ADD.
478 if (Addr.getOpcode() == ISD::ADD) {
479 // When loading from constant pools, load the lower address part in
480 // the instruction itself. Example, instead of:
481 // lui $2, %hi($CPI1_0)
482 // addiu $2, $2, %lo($CPI1_0)
483 // lwc1 $f0, 0($2)
484 // Generate:
485 // lui $2, %hi($CPI1_0)
486 // lwc1 $f0, %lo($CPI1_0)($2)
487 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
488 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
489 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
490 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
491 isa<JumpTableSDNode>(Opnd0)) {
492 Base = Addr.getOperand(0);
493 Offset = Opnd0;
494 return true;
495 }
496 }
497
498 // If an indexed floating point load/store can be emitted, return false.
499 const LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(Parent);
500
501 if (LS &&
502 (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
Akira Hatanaka0301bc52012-11-15 21:17:13 +0000503 Subtarget.hasFPIdx())
Reed Kotlerf99998a2012-10-28 06:02:37 +0000504 return false;
505 }
506 Base = Addr;
507 Offset = CurDAG->getTargetConstant(0, ValTy);
508 return true;
509}
510
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000511/// Select multiply instructions.
512std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000513MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000514 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000515 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000516 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
517 N->getOperand(1));
518 SDValue InFlag = SDValue(Mul, 0);
519
520 if (HasLo) {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000521 unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mflo16 :
522 (Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64);
523 Lo = CurDAG->getMachineNode(Opcode, dl, Ty, MVT::Glue, InFlag);
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000524 InFlag = SDValue(Lo, 1);
525 }
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000526 if (HasHi) {
527 unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mfhi16 :
528 (Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64);
529 Hi = CurDAG->getMachineNode(Opcode, dl, Ty, InFlag);
530 }
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000531 return std::make_pair(Lo, Hi);
532}
533
534
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000535/// Select instructions not customized! Used for
536/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000537SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000538 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000539 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000540
541 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000542 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000543
544 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000545 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000546 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000547 return NULL;
548 }
549
550 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000551 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000552 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000553 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000554 EVT NodeTy = Node->getValueType(0);
555 unsigned MultOpc;
556
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000557 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000558 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000559
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000560 case ISD::SUBE:
561 case ISD::ADDE: {
Reed Kotlera81be802012-10-26 04:46:26 +0000562 bool inMips16Mode = Subtarget.inMips16Mode();
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000563 SDValue InFlag = Node->getOperand(2), CmpLHS;
564 unsigned Opc = InFlag.getOpcode(); (void)Opc;
565 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
566 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
567 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000568
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000569 unsigned MOp;
570 if (Opcode == ISD::ADDE) {
571 CmpLHS = InFlag.getValue(0);
Reed Kotlera81be802012-10-26 04:46:26 +0000572 if (inMips16Mode)
573 MOp = Mips::AdduRxRyRz16;
574 else
575 MOp = Mips::ADDu;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000576 } else {
577 CmpLHS = InFlag.getOperand(0);
Reed Kotlera81be802012-10-26 04:46:26 +0000578 if (inMips16Mode)
579 MOp = Mips::SubuRxRyRz16;
580 else
581 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000582 }
583
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000584 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000585
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000586 SDValue LHS = Node->getOperand(0);
587 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000588
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000589 EVT VT = LHS.getValueType();
Reed Kotlera81be802012-10-26 04:46:26 +0000590
591 unsigned Sltu_op = inMips16Mode? Mips::SltuRxRyRz16: Mips::SLTu;
592 SDNode *Carry = CurDAG->getMachineNode(Sltu_op, dl, VT, Ops, 2);
593 unsigned Addu_op = inMips16Mode? Mips::AdduRxRyRz16 : Mips::ADDu;
594 SDNode *AddCarry = CurDAG->getMachineNode(Addu_op, dl, VT,
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000595 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000596
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000597 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
598 LHS, SDValue(AddCarry,0));
599 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000600
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000601 /// Mul with two results
602 case ISD::SMUL_LOHI:
603 case ISD::UMUL_LOHI: {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000604 if (NodeTy == MVT::i32) {
605 if (Subtarget.inMips16Mode())
606 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MultuRxRy16 :
607 Mips::MultRxRy16);
608 else
609 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
610 }
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000611 else
612 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000613
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000614 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
615 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000616
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000617 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000618 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000619
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000620 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000621 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000622
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000623 return NULL;
624 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000625
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000626 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000627 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000628 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000629 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000630 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000631 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
632 dl, NodeTy, true, false).first;
633 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000634 case ISD::MULHS:
635 case ISD::MULHU: {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000636 if (NodeTy == MVT::i32) {
637 if (Subtarget.inMips16Mode())
638 MultOpc = (Opcode == ISD::MULHU ?
639 Mips::MultuRxRy16 : Mips::MultRxRy16);
640 else
641 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
642 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000643 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000644 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
645
646 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000647 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000648
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000649 // Get target GOT address.
650 case ISD::GLOBAL_OFFSET_TABLE:
651 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000652
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000653 case ISD::ConstantFP: {
654 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
655 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
656 if (Subtarget.hasMips64()) {
657 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
658 Mips::ZERO_64, MVT::i64);
659 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000660 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000661
662 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
663 Mips::ZERO, MVT::i32);
664 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
665 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000666 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000667 break;
668 }
669
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000670 case ISD::Constant: {
671 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
672 unsigned Size = CN->getValueSizeInBits(0);
673
674 if (Size == 32)
675 break;
676
677 MipsAnalyzeImmediate AnalyzeImm;
678 int64_t Imm = CN->getSExtValue();
679
680 const MipsAnalyzeImmediate::InstSeq &Seq =
681 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000682
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000683 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
684 DebugLoc DL = CN->getDebugLoc();
685 SDNode *RegOpnd;
686 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
687 MVT::i64);
688
689 // The first instruction can be a LUi which is different from other
690 // instructions (ADDiu, ORI and SLL) in that it does not have a register
691 // operand.
692 if (Inst->Opc == Mips::LUi64)
693 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
694 else
695 RegOpnd =
696 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
697 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
698 ImmOpnd);
699
700 // The remaining instructions in the sequence are handled here.
701 for (++Inst; Inst != Seq.end(); ++Inst) {
702 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
703 MVT::i64);
704 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
705 SDValue(RegOpnd, 0), ImmOpnd);
706 }
707
708 return RegOpnd;
709 }
710
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000711#ifndef NDEBUG
712 case ISD::LOAD:
713 case ISD::STORE:
714 assert(cast<MemSDNode>(Node)->getMemoryVT().getSizeInBits() / 8 <=
715 cast<MemSDNode>(Node)->getAlignment() &&
716 "Unexpected unaligned loads/stores.");
717 break;
718#endif
719
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000720 case MipsISD::ThreadPointer: {
721 EVT PtrVT = TLI.getPointerTy();
722 unsigned RdhwrOpc, SrcReg, DestReg;
723
724 if (PtrVT == MVT::i32) {
725 RdhwrOpc = Mips::RDHWR;
726 SrcReg = Mips::HWR29;
727 DestReg = Mips::V1;
728 } else {
729 RdhwrOpc = Mips::RDHWR64;
730 SrcReg = Mips::HWR29_64;
731 DestReg = Mips::V1_64;
732 }
Jia Liubb481f82012-02-28 07:46:26 +0000733
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000734 SDNode *Rdhwr =
735 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
736 Node->getValueType(0),
737 CurDAG->getRegister(SrcReg, PtrVT));
738 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
739 SDValue(Rdhwr, 0));
740 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
741 ReplaceUses(SDValue(Node, 0), ResNode);
742 return ResNode.getNode();
743 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000744 }
745
746 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000747 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000748
Chris Lattner7c306da2010-03-02 06:34:30 +0000749 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000750 if (ResNode == NULL || ResNode == Node)
751 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000752 else
753 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000754 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000755 return ResNode;
756}
757
Akira Hatanaka21afc632011-06-21 00:40:49 +0000758bool MipsDAGToDAGISel::
759SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
760 std::vector<SDValue> &OutOps) {
761 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
762 OutOps.push_back(Op);
763 return false;
764}
765
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000766/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000767/// MIPS-specific DAG, ready for instruction scheduling.
768FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
769 return new MipsDAGToDAGISel(TM);
770}