blob: 8784737d33ea576931dc7bab3f8c9f8c8c41bbef [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 Hatanaka648f00c2012-02-24 22:34:47 +0000120 if (!MipsFI->globalBaseRegSet())
121 return;
122
123 MachineBasicBlock &MBB = MF.front();
124 MachineBasicBlock::iterator I = MBB.begin();
125 MachineRegisterInfo &RegInfo = MF.getRegInfo();
126 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
127 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
128 unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg();
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000129
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000130 const TargetRegisterClass *RC = Subtarget.isABI_N64() ?
131 (const TargetRegisterClass*)&Mips::CPU64RegsRegClass :
132 (const TargetRegisterClass*)&Mips::CPURegsRegClass;
Jia Liubb481f82012-02-28 07:46:26 +0000133
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000134 V0 = RegInfo.createVirtualRegister(RC);
135 V1 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000136
137 if (Subtarget.isABI_N64()) {
138 MF.getRegInfo().addLiveIn(Mips::T9_64);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000139 MBB.addLiveIn(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000140
141 // lui $v0, %hi(%neg(%gp_rel(fname)))
142 // daddu $v1, $v0, $t9
143 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
144 const GlobalValue *FName = MF.getFunction();
145 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
146 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
147 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0).addReg(Mips::T9_64);
148 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
149 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000150 return;
151 }
152
153 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000154 // Set global register to __gnu_local_gp.
155 //
156 // lui $v0, %hi(__gnu_local_gp)
157 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
158 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
159 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
160 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
161 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000162 return;
Jia Liubb481f82012-02-28 07:46:26 +0000163 }
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000164
165 MF.getRegInfo().addLiveIn(Mips::T9);
166 MBB.addLiveIn(Mips::T9);
167
168 if (Subtarget.isABI_N32()) {
169 // lui $v0, %hi(%neg(%gp_rel(fname)))
170 // addu $v1, $v0, $t9
171 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
172 const GlobalValue *FName = MF.getFunction();
173 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
174 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
175 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
176 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
177 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
178 return;
179 }
180
181 assert(Subtarget.isABI_O32());
182
183 // For O32 ABI, the following instruction sequence is emitted to initialize
184 // the global base register:
185 //
186 // 0. lui $2, %hi(_gp_disp)
187 // 1. addiu $2, $2, %lo(_gp_disp)
188 // 2. addu $globalbasereg, $2, $t9
189 //
190 // We emit only the last instruction here.
191 //
192 // GNU linker requires that the first two instructions appear at the beginning
193 // of a funtion and no instructions be inserted before or between them.
194 // The two instructions are emitted during lowering to MC layer in order to
195 // avoid any reordering.
196 //
197 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
198 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
199 // reads it.
200 MF.getRegInfo().addLiveIn(Mips::V0);
201 MBB.addLiveIn(Mips::V0);
202 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
203 .addReg(Mips::V0).addReg(Mips::T9);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000204}
205
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000206bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
207 const MachineInstr& MI) {
208 unsigned DstReg = 0, ZeroReg = 0;
209
210 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
211 if ((MI.getOpcode() == Mips::ADDiu) &&
212 (MI.getOperand(1).getReg() == Mips::ZERO) &&
213 (MI.getOperand(2).getImm() == 0)) {
214 DstReg = MI.getOperand(0).getReg();
215 ZeroReg = Mips::ZERO;
216 } else if ((MI.getOpcode() == Mips::DADDiu) &&
217 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
218 (MI.getOperand(2).getImm() == 0)) {
219 DstReg = MI.getOperand(0).getReg();
220 ZeroReg = Mips::ZERO_64;
221 }
222
223 if (!DstReg)
224 return false;
225
226 // Replace uses with ZeroReg.
227 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
228 E = MRI->use_end(); U != E; ++U) {
229 MachineOperand &MO = U.getOperand();
230 MachineInstr *MI = MO.getParent();
231
232 // Do not replace if it is a phi's operand or is tied to def operand.
Akira Hatanaka3011a332012-05-11 23:22:18 +0000233 if (MI->isPHI() || MI->isRegTiedToDefOperand(U.getOperandNo()) ||
234 MI->isPseudo())
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000235 continue;
236
237 MO.setReg(ZeroReg);
238 }
239
240 return true;
241}
242
243void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
244 InitGlobalBaseReg(MF);
245
246 MachineRegisterInfo *MRI = &MF.getRegInfo();
247
248 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
249 ++MFI)
250 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
251 ReplaceUsesWithZeroReg(MRI, *I);
252}
253
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000254bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
255 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000256
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000257 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000258
259 return Ret;
260}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000261
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000262/// getGlobalBaseReg - Output the instructions required to put the
263/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000264SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000265 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000266 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000267}
268
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000269/// ComplexPattern used on MipsInstrInfo
270/// Used on Mips Load/Store instructions
271bool MipsDAGToDAGISel::
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000272SelectAddr(SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000273 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000274
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000275 // If Parent is an unaligned f32 load or store, select a (base + index)
276 // floating point load/store instruction (luxc1 or suxc1).
277 const LSBaseSDNode* LS = 0;
278
279 if (Parent && (LS = dyn_cast<LSBaseSDNode>(Parent))) {
280 EVT VT = LS->getMemoryVT();
281
282 if (VT.getSizeInBits() / 8 > LS->getAlignment()) {
283 assert(TLI.allowsUnalignedMemoryAccesses(VT) &&
284 "Unaligned loads/stores not supported for this type.");
285 if (VT == MVT::f32)
286 return false;
287 }
288 }
289
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000290 // if Address is FI, get the TargetFrameIndex.
291 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000292 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
293 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000294 return true;
295 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000296
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000297 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000298 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000299 Base = Addr.getOperand(0);
300 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000301 return true;
302 }
303
304 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000305 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000306 Addr.getOpcode() == ISD::TargetGlobalAddress))
307 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000308 }
309
Akira Hatanaka5e069032011-06-02 01:03:14 +0000310 // Addresses of the form FI+const or FI|const
311 if (CurDAG->isBaseWithConstantOffset(Addr)) {
312 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
313 if (isInt<16>(CN->getSExtValue())) {
314
315 // If the first operand is a FI, get the TargetFI Node
316 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
317 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000318 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000319 else
320 Base = Addr.getOperand(0);
321
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000322 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000323 return true;
324 }
325 }
326
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000327 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000328 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000329 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000330 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000331 // lui $2, %hi($CPI1_0)
332 // addiu $2, $2, %lo($CPI1_0)
333 // lwc1 $f0, 0($2)
334 // Generate:
335 // lui $2, %hi($CPI1_0)
336 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka89dc8d72011-12-19 19:28:37 +0000337 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo) {
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000338 SDValue LoVal = Addr.getOperand(1);
Jia Liubb481f82012-02-28 07:46:26 +0000339 if (isa<ConstantPoolSDNode>(LoVal.getOperand(0)) ||
Akira Hatanaka8b2b7132011-06-24 17:55:19 +0000340 isa<GlobalAddressSDNode>(LoVal.getOperand(0))) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000341 Base = Addr.getOperand(0);
342 Offset = LoVal.getOperand(0);
343 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000344 }
345 }
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000346
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000347 // If an indexed floating point load/store can be emitted, return false.
348 if (LS && (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
349 Subtarget.hasMips32r2Or64())
350 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000351 }
352
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000353 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000354 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000355 return true;
356}
357
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000358/// Select multiply instructions.
359std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000360MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000361 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000362 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000363 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
364 N->getOperand(1));
365 SDValue InFlag = SDValue(Mul, 0);
366
367 if (HasLo) {
368 Lo = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64, dl,
369 Ty, MVT::Glue, InFlag);
370 InFlag = SDValue(Lo, 1);
371 }
372 if (HasHi)
373 Hi = CurDAG->getMachineNode(Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64, dl,
374 Ty, InFlag);
Jia Liubb481f82012-02-28 07:46:26 +0000375
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000376 return std::make_pair(Lo, Hi);
377}
378
379
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000380/// Select instructions not customized! Used for
381/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000382SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000383 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000384 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000385
386 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000387 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000388
389 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000390 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000391 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000392 return NULL;
393 }
394
395 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000396 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000397 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000398 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000399 EVT NodeTy = Node->getValueType(0);
400 unsigned MultOpc;
401
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000402 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000403 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000404
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000405 case ISD::SUBE:
406 case ISD::ADDE: {
407 SDValue InFlag = Node->getOperand(2), CmpLHS;
408 unsigned Opc = InFlag.getOpcode(); (void)Opc;
409 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
410 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
411 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000412
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000413 unsigned MOp;
414 if (Opcode == ISD::ADDE) {
415 CmpLHS = InFlag.getValue(0);
416 MOp = Mips::ADDu;
417 } else {
418 CmpLHS = InFlag.getOperand(0);
419 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000420 }
421
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000422 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000423
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000424 SDValue LHS = Node->getOperand(0);
425 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000426
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000427 EVT VT = LHS.getValueType();
428 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
429 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
430 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000431
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000432 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
433 LHS, SDValue(AddCarry,0));
434 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000435
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000436 /// Mul with two results
437 case ISD::SMUL_LOHI:
438 case ISD::UMUL_LOHI: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000439 if (NodeTy == MVT::i32)
440 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
441 else
442 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000443
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000444 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
445 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000446
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000447 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000448 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000449
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000450 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000451 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000452
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000453 return NULL;
454 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000455
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000456 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000457 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000458 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000459 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000460 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000461 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
462 dl, NodeTy, true, false).first;
463 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000464 case ISD::MULHS:
465 case ISD::MULHU: {
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000466 if (NodeTy == MVT::i32)
467 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000468 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000469 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
470
471 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000472 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000473
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000474 // Get target GOT address.
475 case ISD::GLOBAL_OFFSET_TABLE:
476 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000477
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000478 case ISD::ConstantFP: {
479 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
480 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
481 if (Subtarget.hasMips64()) {
482 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
483 Mips::ZERO_64, MVT::i64);
484 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000485 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000486
487 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
488 Mips::ZERO, MVT::i32);
489 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
490 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000491 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000492 break;
493 }
494
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000495 case ISD::Constant: {
496 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
497 unsigned Size = CN->getValueSizeInBits(0);
498
499 if (Size == 32)
500 break;
501
502 MipsAnalyzeImmediate AnalyzeImm;
503 int64_t Imm = CN->getSExtValue();
504
505 const MipsAnalyzeImmediate::InstSeq &Seq =
506 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000507
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000508 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
509 DebugLoc DL = CN->getDebugLoc();
510 SDNode *RegOpnd;
511 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
512 MVT::i64);
513
514 // The first instruction can be a LUi which is different from other
515 // instructions (ADDiu, ORI and SLL) in that it does not have a register
516 // operand.
517 if (Inst->Opc == Mips::LUi64)
518 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
519 else
520 RegOpnd =
521 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
522 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
523 ImmOpnd);
524
525 // The remaining instructions in the sequence are handled here.
526 for (++Inst; Inst != Seq.end(); ++Inst) {
527 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
528 MVT::i64);
529 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
530 SDValue(RegOpnd, 0), ImmOpnd);
531 }
532
533 return RegOpnd;
534 }
535
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000536 case MipsISD::ThreadPointer: {
537 EVT PtrVT = TLI.getPointerTy();
538 unsigned RdhwrOpc, SrcReg, DestReg;
539
540 if (PtrVT == MVT::i32) {
541 RdhwrOpc = Mips::RDHWR;
542 SrcReg = Mips::HWR29;
543 DestReg = Mips::V1;
544 } else {
545 RdhwrOpc = Mips::RDHWR64;
546 SrcReg = Mips::HWR29_64;
547 DestReg = Mips::V1_64;
548 }
Jia Liubb481f82012-02-28 07:46:26 +0000549
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000550 SDNode *Rdhwr =
551 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
552 Node->getValueType(0),
553 CurDAG->getRegister(SrcReg, PtrVT));
554 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
555 SDValue(Rdhwr, 0));
556 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
557 ReplaceUses(SDValue(Node, 0), ResNode);
558 return ResNode.getNode();
559 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000560 }
561
562 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000563 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000564
Chris Lattner7c306da2010-03-02 06:34:30 +0000565 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000566 if (ResNode == NULL || ResNode == Node)
567 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000568 else
569 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000570 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000571 return ResNode;
572}
573
Akira Hatanaka21afc632011-06-21 00:40:49 +0000574bool MipsDAGToDAGISel::
575SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
576 std::vector<SDValue> &OutOps) {
577 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
578 OutOps.push_back(Op);
579 return false;
580}
581
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000582/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000583/// MIPS-specific DAG, ready for instruction scheduling.
584FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
585 return new MipsDAGToDAGISel(TM);
586}