blob: ea33b74a750137e57265eaea0935f67524e5bedd [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsISelDAGToDAG.cpp - A Dag to Dag Inst Selector for Mips --------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00007//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00009//
10// This file defines an instruction selector for the MIPS target.
11//
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000013
14#define DEBUG_TYPE "mips-isel"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000015#include "Mips.h"
Akira Hatanaka57fa3822012-01-25 03:01:35 +000016#include "MipsAnalyzeImmediate.h"
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +000017#include "MipsMachineFunction.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000018#include "MipsRegisterInfo.h"
19#include "MipsSubtarget.h"
20#include "MipsTargetMachine.h"
Akira Hatanaka648f00c2012-02-24 22:34:47 +000021#include "MCTargetDesc/MipsBaseInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000022#include "llvm/GlobalValue.h"
23#include "llvm/Instructions.h"
24#include "llvm/Intrinsics.h"
25#include "llvm/Support/CFG.h"
26#include "llvm/Type.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
Akira Hatanaka44b6c712012-02-28 02:55:02 +000033#include "llvm/CodeGen/SelectionDAGNodes.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000034#include "llvm/Target/TargetMachine.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000035#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000038using namespace llvm;
39
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000040//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000041// Instruction Selector Implementation
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000042//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000043
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000044//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000045// MipsDAGToDAGISel - MIPS specific code to select MIPS machine
46// instructions for SelectionDAG operations.
Akira Hatanaka4552c9a2011-04-15 21:51:11 +000047//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000048namespace {
49
Nick Lewycky6726b6d2009-10-25 06:33:48 +000050class MipsDAGToDAGISel : public SelectionDAGISel {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000051
52 /// TM - Keep a reference to MipsTargetMachine.
53 MipsTargetMachine &TM;
54
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000055 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
56 /// make the right decision when generating code for different targets.
Bruno Cardoso Lopes225ca9c2008-07-05 19:05:21 +000057 const MipsSubtarget &Subtarget;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000058
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000059public:
Dan Gohman1002c022008-07-07 18:00:37 +000060 explicit MipsDAGToDAGISel(MipsTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +000061 SelectionDAGISel(tm),
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000062 TM(tm), Subtarget(tm.getSubtarget<MipsSubtarget>()) {}
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000063
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000064 // Pass Name
65 virtual const char *getPassName() const {
66 return "MIPS DAG->DAG Pattern Instruction Selection";
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000067 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000068
Akira Hatanaka648f00c2012-02-24 22:34:47 +000069 virtual bool runOnMachineFunction(MachineFunction &MF);
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +000070
71private:
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000072 // Include the pieces autogenerated from the target description.
73 #include "MipsGenDAGISel.inc"
74
Dan Gohman99114052009-06-03 20:30:14 +000075 /// getTargetMachine - Return a reference to the TargetMachine, casted
76 /// to the target-specific type.
77 const MipsTargetMachine &getTargetMachine() {
78 return static_cast<const MipsTargetMachine &>(TM);
79 }
80
81 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
82 /// to the target-specific type.
83 const MipsInstrInfo *getInstrInfo() {
84 return getTargetMachine().getInstrInfo();
85 }
86
87 SDNode *getGlobalBaseReg();
Akira Hatanaka2fd04752011-12-20 23:10:57 +000088
89 std::pair<SDNode*, SDNode*> SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl,
90 EVT Ty, bool HasLo, bool HasHi);
91
Dan Gohmaneeb3a002010-01-05 01:24:18 +000092 SDNode *Select(SDNode *N);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000093
94 // Complex Pattern.
Akira Hatanaka44b6c712012-02-28 02:55:02 +000095 bool SelectAddr(SDNode *Parent, SDValue N, SDValue &Base, SDValue &Offset);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +000096
Akira Hatanakabd150902011-12-07 20:15:01 +000097 // getImm - Return a target constant with the specified value.
Akira Hatanaka4d0eb632011-12-07 20:10:24 +000098 inline SDValue getImm(const SDNode *Node, unsigned Imm) {
99 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000100 }
Akira Hatanaka21afc632011-06-21 00:40:49 +0000101
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000102 void ProcessFunctionAfterISel(MachineFunction &MF);
103 bool ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI, const MachineInstr&);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000104 void InitGlobalBaseReg(MachineFunction &MF);
105
Akira Hatanaka21afc632011-06-21 00:40:49 +0000106 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
107 char ConstraintCode,
108 std::vector<SDValue> &OutOps);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000109};
110
111}
112
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000113// Insert instructions to initialize the global base register in the
114// first MBB of the function. When the ABI is O32 and the relocation model is
115// PIC, the necessary instructions are emitted later to prevent optimization
116// passes from moving them.
117void MipsDAGToDAGISel::InitGlobalBaseReg(MachineFunction &MF) {
118 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
Jia Liubb481f82012-02-28 07:46:26 +0000119
Akira Hatanaka4782a6e2012-06-27 00:20:39 +0000120 if (((MF.getTarget().getRelocationModel() == Reloc::Static) ||
121 Subtarget.inMips16Mode()) && !MipsFI->globalBaseRegSet())
122 return;
123
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000124 MachineBasicBlock &MBB = MF.front();
125 MachineBasicBlock::iterator I = MBB.begin();
126 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Akira Hatanaka4654e582012-06-14 01:16:15 +0000127 const MipsRegisterInfo *TargetRegInfo = TM.getRegisterInfo();
128 const MipsInstrInfo *MII = TM.getInstrInfo();
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000129 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
130 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000131 unsigned V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg();
132 int FI = 0;
Akira Hatanaka54c5bc82012-06-21 20:39:10 +0000133
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000134 FI= MipsFI->initGlobalRegFI();
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000135
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000136 const TargetRegisterClass *RC = Subtarget.isABI_N64() ?
137 (const TargetRegisterClass*)&Mips::CPU64RegsRegClass :
138 (const TargetRegisterClass*)&Mips::CPURegsRegClass;
Jia Liubb481f82012-02-28 07:46:26 +0000139
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000140 if (Subtarget.inMips16Mode())
141 RC=(const TargetRegisterClass*)&Mips::CPU16RegsRegClass;
142
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000143 V0 = RegInfo.createVirtualRegister(RC);
144 V1 = RegInfo.createVirtualRegister(RC);
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000145 V2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000146
147 if (Subtarget.isABI_N64()) {
148 MF.getRegInfo().addLiveIn(Mips::T9_64);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000149 MBB.addLiveIn(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000150
151 // lui $v0, %hi(%neg(%gp_rel(fname)))
152 // daddu $v1, $v0, $t9
153 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
154 const GlobalValue *FName = MF.getFunction();
155 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
156 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000157 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
158 .addReg(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000159 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
160 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka4654e582012-06-14 01:16:15 +0000161 MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC,
162 TargetRegInfo);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000163 return;
164 }
165
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000166 if (Subtarget.inMips16Mode()) {
167 BuildMI(MBB, I, DL, TII.get(Mips::LiRxImmX16), V0)
168 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
169 BuildMI(MBB, I, DL, TII.get(Mips::AddiuRxPcImmX16),
170 V1)
171 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
172 BuildMI(MBB, I, DL, TII.get(Mips::SllX16),
173 V2 ).addReg(V0).addImm(16);
174 BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg)
175 .addReg(V1).addReg(V2);
176
177
178 return;
179 }
180
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000181 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000182 // Set global register to __gnu_local_gp.
183 //
184 // lui $v0, %hi(__gnu_local_gp)
185 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
186 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
187 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
188 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
189 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000190 return;
Jia Liubb481f82012-02-28 07:46:26 +0000191 }
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000192
193 MF.getRegInfo().addLiveIn(Mips::T9);
194 MBB.addLiveIn(Mips::T9);
195
196 if (Subtarget.isABI_N32()) {
197 // lui $v0, %hi(%neg(%gp_rel(fname)))
198 // addu $v1, $v0, $t9
199 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
200 const GlobalValue *FName = MF.getFunction();
201 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
202 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
203 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
204 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
205 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka4654e582012-06-14 01:16:15 +0000206 MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC,
207 TargetRegInfo);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000208 return;
209 }
210
211 assert(Subtarget.isABI_O32());
212
Akira Hatanakab7dd9fc2012-07-21 02:15:19 +0000213
214 //if (Subtarget.inMips16Mode())
215 // return; // no need to load GP. It can be calculated anywhere
216
Akira Hatanaka54c5bc82012-06-21 20:39:10 +0000217
218
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000219 // For O32 ABI, the following instruction sequence is emitted to initialize
220 // the global base register:
221 //
222 // 0. lui $2, %hi(_gp_disp)
223 // 1. addiu $2, $2, %lo(_gp_disp)
224 // 2. addu $globalbasereg, $2, $t9
225 //
226 // We emit only the last instruction here.
227 //
228 // GNU linker requires that the first two instructions appear at the beginning
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000229 // of a function and no instructions be inserted before or between them.
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000230 // The two instructions are emitted during lowering to MC layer in order to
231 // avoid any reordering.
232 //
233 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
234 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
235 // reads it.
236 MF.getRegInfo().addLiveIn(Mips::V0);
237 MBB.addLiveIn(Mips::V0);
238 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
239 .addReg(Mips::V0).addReg(Mips::T9);
Akira Hatanaka4654e582012-06-14 01:16:15 +0000240 MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC, TargetRegInfo);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000241}
242
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000243bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
244 const MachineInstr& MI) {
245 unsigned DstReg = 0, ZeroReg = 0;
246
247 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
248 if ((MI.getOpcode() == Mips::ADDiu) &&
249 (MI.getOperand(1).getReg() == Mips::ZERO) &&
250 (MI.getOperand(2).getImm() == 0)) {
251 DstReg = MI.getOperand(0).getReg();
252 ZeroReg = Mips::ZERO;
253 } else if ((MI.getOpcode() == Mips::DADDiu) &&
254 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
255 (MI.getOperand(2).getImm() == 0)) {
256 DstReg = MI.getOperand(0).getReg();
257 ZeroReg = Mips::ZERO_64;
258 }
259
260 if (!DstReg)
261 return false;
262
263 // Replace uses with ZeroReg.
264 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
265 E = MRI->use_end(); U != E; ++U) {
266 MachineOperand &MO = U.getOperand();
267 MachineInstr *MI = MO.getParent();
268
269 // Do not replace if it is a phi's operand or is tied to def operand.
Akira Hatanaka3011a332012-05-11 23:22:18 +0000270 if (MI->isPHI() || MI->isRegTiedToDefOperand(U.getOperandNo()) ||
271 MI->isPseudo())
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000272 continue;
273
274 MO.setReg(ZeroReg);
275 }
276
277 return true;
278}
279
280void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
281 InitGlobalBaseReg(MF);
282
283 MachineRegisterInfo *MRI = &MF.getRegInfo();
284
285 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
286 ++MFI)
287 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
288 ReplaceUsesWithZeroReg(MRI, *I);
289}
290
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000291bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
292 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000293
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000294 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000295
296 return Ret;
297}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000298
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000299/// getGlobalBaseReg - Output the instructions required to put the
300/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000301SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000302 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000303 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000304}
305
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000306/// ComplexPattern used on MipsInstrInfo
307/// Used on Mips Load/Store instructions
308bool MipsDAGToDAGISel::
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000309SelectAddr(SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000310 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000311
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000312 // If Parent is an unaligned f32 load or store, select a (base + index)
313 // floating point load/store instruction (luxc1 or suxc1).
Akira Hatanaka864f6602012-06-14 21:10:56 +0000314 const LSBaseSDNode *LS = 0;
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000315
316 if (Parent && (LS = dyn_cast<LSBaseSDNode>(Parent))) {
317 EVT VT = LS->getMemoryVT();
318
319 if (VT.getSizeInBits() / 8 > LS->getAlignment()) {
320 assert(TLI.allowsUnalignedMemoryAccesses(VT) &&
321 "Unaligned loads/stores not supported for this type.");
322 if (VT == MVT::f32)
323 return false;
324 }
325 }
326
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000327 // if Address is FI, get the TargetFrameIndex.
328 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000329 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
330 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000331 return true;
332 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000333
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000334 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000335 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000336 Base = Addr.getOperand(0);
337 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000338 return true;
339 }
340
341 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000342 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000343 Addr.getOpcode() == ISD::TargetGlobalAddress))
344 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000345 }
346
Akira Hatanaka5e069032011-06-02 01:03:14 +0000347 // Addresses of the form FI+const or FI|const
348 if (CurDAG->isBaseWithConstantOffset(Addr)) {
349 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
350 if (isInt<16>(CN->getSExtValue())) {
351
352 // If the first operand is a FI, get the TargetFI Node
353 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
354 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000355 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000356 else
357 Base = Addr.getOperand(0);
358
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000359 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000360 return true;
361 }
362 }
363
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000364 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000365 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000366 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000367 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000368 // lui $2, %hi($CPI1_0)
369 // addiu $2, $2, %lo($CPI1_0)
370 // lwc1 $f0, 0($2)
371 // Generate:
372 // lui $2, %hi($CPI1_0)
373 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka89dc8d72011-12-19 19:28:37 +0000374 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000375 SDValue LoVal = Addr.getOperand(1), Opnd0 = LoVal.getOperand(0);
376 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
377 isa<JumpTableSDNode>(Opnd0)) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000378 Base = Addr.getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000379 Offset = Opnd0;
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000380 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000381 }
382 }
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000383
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000384 // If an indexed floating point load/store can be emitted, return false.
Akira Hatanaka864f6602012-06-14 21:10:56 +0000385 if (LS &&
386 (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000387 Subtarget.hasMips32r2Or64())
388 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000389 }
390
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000391 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000392 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000393 return true;
394}
395
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000396/// Select multiply instructions.
397std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000398MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000399 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000400 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000401 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
402 N->getOperand(1));
403 SDValue InFlag = SDValue(Mul, 0);
404
405 if (HasLo) {
406 Lo = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64, dl,
407 Ty, MVT::Glue, InFlag);
408 InFlag = SDValue(Lo, 1);
409 }
410 if (HasHi)
411 Hi = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64, dl,
412 Ty, InFlag);
Jia Liubb481f82012-02-28 07:46:26 +0000413
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000414 return std::make_pair(Lo, Hi);
415}
416
417
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000418/// Select instructions not customized! Used for
419/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000420SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000421 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000422 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000423
424 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000425 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000426
427 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000428 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000429 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000430 return NULL;
431 }
432
433 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000434 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000435 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000436 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000437 EVT NodeTy = Node->getValueType(0);
438 unsigned MultOpc;
439
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000440 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000441 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000442
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000443 case ISD::SUBE:
444 case ISD::ADDE: {
445 SDValue InFlag = Node->getOperand(2), CmpLHS;
446 unsigned Opc = InFlag.getOpcode(); (void)Opc;
447 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
448 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
449 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000450
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000451 unsigned MOp;
452 if (Opcode == ISD::ADDE) {
453 CmpLHS = InFlag.getValue(0);
454 MOp = Mips::ADDu;
455 } else {
456 CmpLHS = InFlag.getOperand(0);
457 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000458 }
459
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000460 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000461
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000462 SDValue LHS = Node->getOperand(0);
463 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000464
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000465 EVT VT = LHS.getValueType();
466 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
467 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
468 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000469
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000470 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
471 LHS, SDValue(AddCarry,0));
472 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000473
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000474 /// Mul with two results
475 case ISD::SMUL_LOHI:
476 case ISD::UMUL_LOHI: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000477 if (NodeTy == MVT::i32)
478 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
479 else
480 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000481
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000482 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
483 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000484
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000485 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000486 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000487
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000488 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000489 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000490
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000491 return NULL;
492 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000493
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000494 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000495 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000496 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000497 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000498 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000499 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
500 dl, NodeTy, true, false).first;
501 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000502 case ISD::MULHS:
503 case ISD::MULHU: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000504 if (NodeTy == MVT::i32)
505 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000506 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000507 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
508
509 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000510 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000511
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000512 // Get target GOT address.
513 case ISD::GLOBAL_OFFSET_TABLE:
514 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000515
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000516 case ISD::ConstantFP: {
517 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
518 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
519 if (Subtarget.hasMips64()) {
520 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
521 Mips::ZERO_64, MVT::i64);
522 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000523 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000524
525 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
526 Mips::ZERO, MVT::i32);
527 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
528 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000529 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000530 break;
531 }
532
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000533 case ISD::Constant: {
534 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
535 unsigned Size = CN->getValueSizeInBits(0);
536
537 if (Size == 32)
538 break;
539
540 MipsAnalyzeImmediate AnalyzeImm;
541 int64_t Imm = CN->getSExtValue();
542
543 const MipsAnalyzeImmediate::InstSeq &Seq =
544 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000545
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000546 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
547 DebugLoc DL = CN->getDebugLoc();
548 SDNode *RegOpnd;
549 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
550 MVT::i64);
551
552 // The first instruction can be a LUi which is different from other
553 // instructions (ADDiu, ORI and SLL) in that it does not have a register
554 // operand.
555 if (Inst->Opc == Mips::LUi64)
556 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
557 else
558 RegOpnd =
559 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
560 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
561 ImmOpnd);
562
563 // The remaining instructions in the sequence are handled here.
564 for (++Inst; Inst != Seq.end(); ++Inst) {
565 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
566 MVT::i64);
567 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
568 SDValue(RegOpnd, 0), ImmOpnd);
569 }
570
571 return RegOpnd;
572 }
573
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000574 case MipsISD::ThreadPointer: {
575 EVT PtrVT = TLI.getPointerTy();
576 unsigned RdhwrOpc, SrcReg, DestReg;
577
578 if (PtrVT == MVT::i32) {
579 RdhwrOpc = Mips::RDHWR;
580 SrcReg = Mips::HWR29;
581 DestReg = Mips::V1;
582 } else {
583 RdhwrOpc = Mips::RDHWR64;
584 SrcReg = Mips::HWR29_64;
585 DestReg = Mips::V1_64;
586 }
Jia Liubb481f82012-02-28 07:46:26 +0000587
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000588 SDNode *Rdhwr =
589 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
590 Node->getValueType(0),
591 CurDAG->getRegister(SrcReg, PtrVT));
592 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
593 SDValue(Rdhwr, 0));
594 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
595 ReplaceUses(SDValue(Node, 0), ResNode);
596 return ResNode.getNode();
597 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000598 }
599
600 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000601 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000602
Chris Lattner7c306da2010-03-02 06:34:30 +0000603 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000604 if (ResNode == NULL || ResNode == Node)
605 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000606 else
607 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000608 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000609 return ResNode;
610}
611
Akira Hatanaka21afc632011-06-21 00:40:49 +0000612bool MipsDAGToDAGISel::
613SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
614 std::vector<SDValue> &OutOps) {
615 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
616 OutOps.push_back(Op);
617 return false;
618}
619
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000620/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000621/// MIPS-specific DAG, ready for instruction scheduling.
622FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
623 return new MipsDAGToDAGISel(TM);
624}