blob: 9f1c0f6d2f38ad331db487ade9e0e85615d2bf23 [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();
131 unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
Akira Hatanaka54c5bc82012-06-21 20:39:10 +0000132 int FI; // should initialize this to some kind of null
133
134 if (!Subtarget.inMips16Mode())
135 FI= MipsFI->initGlobalRegFI();
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000136
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000137 const TargetRegisterClass *RC = Subtarget.isABI_N64() ?
138 (const TargetRegisterClass*)&Mips::CPU64RegsRegClass :
139 (const TargetRegisterClass*)&Mips::CPURegsRegClass;
Jia Liubb481f82012-02-28 07:46:26 +0000140
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000141 V0 = RegInfo.createVirtualRegister(RC);
142 V1 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000143
144 if (Subtarget.isABI_N64()) {
145 MF.getRegInfo().addLiveIn(Mips::T9_64);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000146 MBB.addLiveIn(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000147
148 // lui $v0, %hi(%neg(%gp_rel(fname)))
149 // daddu $v1, $v0, $t9
150 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
151 const GlobalValue *FName = MF.getFunction();
152 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
153 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000154 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
155 .addReg(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000156 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
157 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka4654e582012-06-14 01:16:15 +0000158 MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC,
159 TargetRegInfo);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000160 return;
161 }
162
163 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000164 // Set global register to __gnu_local_gp.
165 //
166 // lui $v0, %hi(__gnu_local_gp)
167 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
168 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
169 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
170 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
171 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
Akira Hatanaka4654e582012-06-14 01:16:15 +0000172 MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC,
173 TargetRegInfo);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000174 return;
Jia Liubb481f82012-02-28 07:46:26 +0000175 }
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000176
177 MF.getRegInfo().addLiveIn(Mips::T9);
178 MBB.addLiveIn(Mips::T9);
179
180 if (Subtarget.isABI_N32()) {
181 // lui $v0, %hi(%neg(%gp_rel(fname)))
182 // addu $v1, $v0, $t9
183 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
184 const GlobalValue *FName = MF.getFunction();
185 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
186 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
187 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
188 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
189 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka4654e582012-06-14 01:16:15 +0000190 MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC,
191 TargetRegInfo);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000192 return;
193 }
194
195 assert(Subtarget.isABI_O32());
196
Akira Hatanaka54c5bc82012-06-21 20:39:10 +0000197 if (Subtarget.inMips16Mode())
198 return; // no need to load GP. It can be calculated anywhere
199
200
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000201 // For O32 ABI, the following instruction sequence is emitted to initialize
202 // the global base register:
203 //
204 // 0. lui $2, %hi(_gp_disp)
205 // 1. addiu $2, $2, %lo(_gp_disp)
206 // 2. addu $globalbasereg, $2, $t9
207 //
208 // We emit only the last instruction here.
209 //
210 // GNU linker requires that the first two instructions appear at the beginning
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000211 // of a function and no instructions be inserted before or between them.
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000212 // The two instructions are emitted during lowering to MC layer in order to
213 // avoid any reordering.
214 //
215 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
216 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
217 // reads it.
218 MF.getRegInfo().addLiveIn(Mips::V0);
219 MBB.addLiveIn(Mips::V0);
220 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
221 .addReg(Mips::V0).addReg(Mips::T9);
Akira Hatanaka4654e582012-06-14 01:16:15 +0000222 MII->storeRegToStackSlot(MBB, I, GlobalBaseReg, false, FI, RC, TargetRegInfo);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000223}
224
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000225bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
226 const MachineInstr& MI) {
227 unsigned DstReg = 0, ZeroReg = 0;
228
229 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
230 if ((MI.getOpcode() == Mips::ADDiu) &&
231 (MI.getOperand(1).getReg() == Mips::ZERO) &&
232 (MI.getOperand(2).getImm() == 0)) {
233 DstReg = MI.getOperand(0).getReg();
234 ZeroReg = Mips::ZERO;
235 } else if ((MI.getOpcode() == Mips::DADDiu) &&
236 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
237 (MI.getOperand(2).getImm() == 0)) {
238 DstReg = MI.getOperand(0).getReg();
239 ZeroReg = Mips::ZERO_64;
240 }
241
242 if (!DstReg)
243 return false;
244
245 // Replace uses with ZeroReg.
246 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
247 E = MRI->use_end(); U != E; ++U) {
248 MachineOperand &MO = U.getOperand();
249 MachineInstr *MI = MO.getParent();
250
251 // Do not replace if it is a phi's operand or is tied to def operand.
Akira Hatanaka3011a332012-05-11 23:22:18 +0000252 if (MI->isPHI() || MI->isRegTiedToDefOperand(U.getOperandNo()) ||
253 MI->isPseudo())
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000254 continue;
255
256 MO.setReg(ZeroReg);
257 }
258
259 return true;
260}
261
262void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
263 InitGlobalBaseReg(MF);
264
265 MachineRegisterInfo *MRI = &MF.getRegInfo();
266
267 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
268 ++MFI)
269 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
270 ReplaceUsesWithZeroReg(MRI, *I);
271}
272
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000273bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
274 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000275
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000276 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000277
278 return Ret;
279}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000280
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000281/// getGlobalBaseReg - Output the instructions required to put the
282/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000283SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000284 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000285 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000286}
287
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000288/// ComplexPattern used on MipsInstrInfo
289/// Used on Mips Load/Store instructions
290bool MipsDAGToDAGISel::
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000291SelectAddr(SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000292 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000293
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000294 // If Parent is an unaligned f32 load or store, select a (base + index)
295 // floating point load/store instruction (luxc1 or suxc1).
Akira Hatanaka864f6602012-06-14 21:10:56 +0000296 const LSBaseSDNode *LS = 0;
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000297
298 if (Parent && (LS = dyn_cast<LSBaseSDNode>(Parent))) {
299 EVT VT = LS->getMemoryVT();
300
301 if (VT.getSizeInBits() / 8 > LS->getAlignment()) {
302 assert(TLI.allowsUnalignedMemoryAccesses(VT) &&
303 "Unaligned loads/stores not supported for this type.");
304 if (VT == MVT::f32)
305 return false;
306 }
307 }
308
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000309 // if Address is FI, get the TargetFrameIndex.
310 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000311 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
312 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000313 return true;
314 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000315
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000316 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000317 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000318 Base = Addr.getOperand(0);
319 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000320 return true;
321 }
322
323 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000324 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000325 Addr.getOpcode() == ISD::TargetGlobalAddress))
326 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000327 }
328
Akira Hatanaka5e069032011-06-02 01:03:14 +0000329 // Addresses of the form FI+const or FI|const
330 if (CurDAG->isBaseWithConstantOffset(Addr)) {
331 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
332 if (isInt<16>(CN->getSExtValue())) {
333
334 // If the first operand is a FI, get the TargetFI Node
335 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
336 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000337 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000338 else
339 Base = Addr.getOperand(0);
340
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000341 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000342 return true;
343 }
344 }
345
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000346 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000347 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000348 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000349 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000350 // lui $2, %hi($CPI1_0)
351 // addiu $2, $2, %lo($CPI1_0)
352 // lwc1 $f0, 0($2)
353 // Generate:
354 // lui $2, %hi($CPI1_0)
355 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka89dc8d72011-12-19 19:28:37 +0000356 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Akira Hatanaka87827072012-06-13 20:33:18 +0000357 SDValue LoVal = Addr.getOperand(1), Opnd0 = LoVal.getOperand(0);
358 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
359 isa<JumpTableSDNode>(Opnd0)) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000360 Base = Addr.getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000361 Offset = Opnd0;
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000362 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000363 }
364 }
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000365
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000366 // If an indexed floating point load/store can be emitted, return false.
Akira Hatanaka864f6602012-06-14 21:10:56 +0000367 if (LS &&
368 (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000369 Subtarget.hasMips32r2Or64())
370 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000371 }
372
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000373 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000374 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000375 return true;
376}
377
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000378/// Select multiply instructions.
379std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000380MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000381 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000382 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000383 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
384 N->getOperand(1));
385 SDValue InFlag = SDValue(Mul, 0);
386
387 if (HasLo) {
388 Lo = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64, dl,
389 Ty, MVT::Glue, InFlag);
390 InFlag = SDValue(Lo, 1);
391 }
392 if (HasHi)
393 Hi = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64, dl,
394 Ty, InFlag);
Jia Liubb481f82012-02-28 07:46:26 +0000395
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000396 return std::make_pair(Lo, Hi);
397}
398
399
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000400/// Select instructions not customized! Used for
401/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000402SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000403 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000404 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000405
406 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000407 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000408
409 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000410 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000411 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000412 return NULL;
413 }
414
415 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000416 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000417 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000418 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000419 EVT NodeTy = Node->getValueType(0);
420 unsigned MultOpc;
421
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000422 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000423 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000424
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000425 case ISD::SUBE:
426 case ISD::ADDE: {
427 SDValue InFlag = Node->getOperand(2), CmpLHS;
428 unsigned Opc = InFlag.getOpcode(); (void)Opc;
429 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
430 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
431 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000432
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000433 unsigned MOp;
434 if (Opcode == ISD::ADDE) {
435 CmpLHS = InFlag.getValue(0);
436 MOp = Mips::ADDu;
437 } else {
438 CmpLHS = InFlag.getOperand(0);
439 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000440 }
441
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000442 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000443
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000444 SDValue LHS = Node->getOperand(0);
445 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000446
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000447 EVT VT = LHS.getValueType();
448 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
449 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
450 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000451
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000452 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
453 LHS, SDValue(AddCarry,0));
454 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000455
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000456 /// Mul with two results
457 case ISD::SMUL_LOHI:
458 case ISD::UMUL_LOHI: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000459 if (NodeTy == MVT::i32)
460 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
461 else
462 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000463
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000464 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
465 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000466
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000467 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000468 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000469
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000470 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000471 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000472
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000473 return NULL;
474 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000475
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000476 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000477 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000478 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000479 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000480 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000481 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
482 dl, NodeTy, true, false).first;
483 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000484 case ISD::MULHS:
485 case ISD::MULHU: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000486 if (NodeTy == MVT::i32)
487 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000488 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000489 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
490
491 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000492 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000493
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000494 // Get target GOT address.
495 case ISD::GLOBAL_OFFSET_TABLE:
496 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000497
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000498 case ISD::ConstantFP: {
499 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
500 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
501 if (Subtarget.hasMips64()) {
502 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
503 Mips::ZERO_64, MVT::i64);
504 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000505 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000506
507 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
508 Mips::ZERO, MVT::i32);
509 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
510 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000511 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000512 break;
513 }
514
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000515 case ISD::Constant: {
516 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
517 unsigned Size = CN->getValueSizeInBits(0);
518
519 if (Size == 32)
520 break;
521
522 MipsAnalyzeImmediate AnalyzeImm;
523 int64_t Imm = CN->getSExtValue();
524
525 const MipsAnalyzeImmediate::InstSeq &Seq =
526 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000527
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000528 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
529 DebugLoc DL = CN->getDebugLoc();
530 SDNode *RegOpnd;
531 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
532 MVT::i64);
533
534 // The first instruction can be a LUi which is different from other
535 // instructions (ADDiu, ORI and SLL) in that it does not have a register
536 // operand.
537 if (Inst->Opc == Mips::LUi64)
538 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
539 else
540 RegOpnd =
541 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
542 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
543 ImmOpnd);
544
545 // The remaining instructions in the sequence are handled here.
546 for (++Inst; Inst != Seq.end(); ++Inst) {
547 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
548 MVT::i64);
549 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
550 SDValue(RegOpnd, 0), ImmOpnd);
551 }
552
553 return RegOpnd;
554 }
555
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000556 case MipsISD::ThreadPointer: {
557 EVT PtrVT = TLI.getPointerTy();
558 unsigned RdhwrOpc, SrcReg, DestReg;
559
560 if (PtrVT == MVT::i32) {
561 RdhwrOpc = Mips::RDHWR;
562 SrcReg = Mips::HWR29;
563 DestReg = Mips::V1;
564 } else {
565 RdhwrOpc = Mips::RDHWR64;
566 SrcReg = Mips::HWR29_64;
567 DestReg = Mips::V1_64;
568 }
Jia Liubb481f82012-02-28 07:46:26 +0000569
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000570 SDNode *Rdhwr =
571 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
572 Node->getValueType(0),
573 CurDAG->getRegister(SrcReg, PtrVT));
574 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
575 SDValue(Rdhwr, 0));
576 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
577 ReplaceUses(SDValue(Node, 0), ResNode);
578 return ResNode.getNode();
579 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000580 }
581
582 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000583 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000584
Chris Lattner7c306da2010-03-02 06:34:30 +0000585 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000586 if (ResNode == NULL || ResNode == Node)
587 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000588 else
589 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000590 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000591 return ResNode;
592}
593
Akira Hatanaka21afc632011-06-21 00:40:49 +0000594bool MipsDAGToDAGISel::
595SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
596 std::vector<SDValue> &OutOps) {
597 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
598 OutOps.push_back(Op);
599 return false;
600}
601
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000602/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000603/// MIPS-specific DAG, ready for instruction scheduling.
604FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
605 return new MipsDAGToDAGISel(TM);
606}