blob: fceb63a1bc9f14a5fe93b30d9c7586ad9427a8d0 [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 Hatanakade4a1272012-07-25 03:16:47 +0000120 if (!MipsFI->globalBaseRegSet())
Akira Hatanaka4782a6e2012-06-27 00:20:39 +0000121 return;
122
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000123 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();
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000128 unsigned V0, V1, V2, GlobalBaseReg = MipsFI->getGlobalBaseReg();
Akira Hatanakade4a1272012-07-25 03:16:47 +0000129 const TargetRegisterClass *RC;
Akira Hatanaka54c5bc82012-06-21 20:39:10 +0000130
Akira Hatanakade4a1272012-07-25 03:16:47 +0000131 if (Subtarget.isABI_N64())
132 RC = (const TargetRegisterClass*)&Mips::CPU64RegsRegClass;
133 else if (Subtarget.inMips16Mode())
134 RC = (const TargetRegisterClass*)&Mips::CPU16RegsRegClass;
135 else
136 RC = (const TargetRegisterClass*)&Mips::CPURegsRegClass;
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000137
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000138 V0 = RegInfo.createVirtualRegister(RC);
139 V1 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000140 V2 = RegInfo.createVirtualRegister(RC);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000141
142 if (Subtarget.isABI_N64()) {
143 MF.getRegInfo().addLiveIn(Mips::T9_64);
Akira Hatanaka56e1ed52012-03-27 02:46:25 +0000144 MBB.addLiveIn(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000145
146 // lui $v0, %hi(%neg(%gp_rel(fname)))
147 // daddu $v1, $v0, $t9
148 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
149 const GlobalValue *FName = MF.getFunction();
150 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0)
151 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
Akira Hatanaka864f6602012-06-14 21:10:56 +0000152 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0)
153 .addReg(Mips::T9_64);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000154 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1)
155 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000156 return;
157 }
158
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000159 if (Subtarget.inMips16Mode()) {
160 BuildMI(MBB, I, DL, TII.get(Mips::LiRxImmX16), V0)
Akira Hatanakade4a1272012-07-25 03:16:47 +0000161 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
162 BuildMI(MBB, I, DL, TII.get(Mips::AddiuRxPcImmX16), V1)
163 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
164 BuildMI(MBB, I, DL, TII.get(Mips::SllX16), V2).addReg(V0).addImm(16);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000165 BuildMI(MBB, I, DL, TII.get(Mips::AdduRxRyRz16), GlobalBaseReg)
166 .addReg(V1).addReg(V2);
Akira Hatanaka3ee306c2012-07-23 23:45:54 +0000167 return;
168 }
169
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000170 if (MF.getTarget().getRelocationModel() == Reloc::Static) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000171 // Set global register to __gnu_local_gp.
172 //
173 // lui $v0, %hi(__gnu_local_gp)
174 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp)
175 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
176 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI);
177 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0)
178 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO);
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000179 return;
Jia Liubb481f82012-02-28 07:46:26 +0000180 }
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000181
182 MF.getRegInfo().addLiveIn(Mips::T9);
183 MBB.addLiveIn(Mips::T9);
184
185 if (Subtarget.isABI_N32()) {
186 // lui $v0, %hi(%neg(%gp_rel(fname)))
187 // addu $v1, $v0, $t9
188 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname)))
189 const GlobalValue *FName = MF.getFunction();
190 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0)
191 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI);
192 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9);
193 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1)
194 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO);
195 return;
196 }
197
198 assert(Subtarget.isABI_O32());
199
200 // For O32 ABI, the following instruction sequence is emitted to initialize
201 // the global base register:
202 //
203 // 0. lui $2, %hi(_gp_disp)
204 // 1. addiu $2, $2, %lo(_gp_disp)
205 // 2. addu $globalbasereg, $2, $t9
206 //
207 // We emit only the last instruction here.
208 //
209 // GNU linker requires that the first two instructions appear at the beginning
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000210 // of a function and no instructions be inserted before or between them.
Akira Hatanaka27ba61d2012-05-12 00:17:17 +0000211 // The two instructions are emitted during lowering to MC layer in order to
212 // avoid any reordering.
213 //
214 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure
215 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu)
216 // reads it.
217 MF.getRegInfo().addLiveIn(Mips::V0);
218 MBB.addLiveIn(Mips::V0);
219 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg)
220 .addReg(Mips::V0).addReg(Mips::T9);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000221}
222
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000223bool MipsDAGToDAGISel::ReplaceUsesWithZeroReg(MachineRegisterInfo *MRI,
224 const MachineInstr& MI) {
225 unsigned DstReg = 0, ZeroReg = 0;
226
227 // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0".
228 if ((MI.getOpcode() == Mips::ADDiu) &&
229 (MI.getOperand(1).getReg() == Mips::ZERO) &&
230 (MI.getOperand(2).getImm() == 0)) {
231 DstReg = MI.getOperand(0).getReg();
232 ZeroReg = Mips::ZERO;
233 } else if ((MI.getOpcode() == Mips::DADDiu) &&
234 (MI.getOperand(1).getReg() == Mips::ZERO_64) &&
235 (MI.getOperand(2).getImm() == 0)) {
236 DstReg = MI.getOperand(0).getReg();
237 ZeroReg = Mips::ZERO_64;
238 }
239
240 if (!DstReg)
241 return false;
242
243 // Replace uses with ZeroReg.
244 for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg),
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000245 E = MRI->use_end(); U != E;) {
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000246 MachineOperand &MO = U.getOperand();
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000247 unsigned OpNo = U.getOperandNo();
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000248 MachineInstr *MI = MO.getParent();
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000249 ++U;
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000250
251 // Do not replace if it is a phi's operand or is tied to def operand.
Jakob Stoklund Olesen69a0aa82012-08-09 22:08:24 +0000252 if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo())
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000253 continue;
254
255 MO.setReg(ZeroReg);
256 }
257
258 return true;
259}
260
261void MipsDAGToDAGISel::ProcessFunctionAfterISel(MachineFunction &MF) {
262 InitGlobalBaseReg(MF);
263
264 MachineRegisterInfo *MRI = &MF.getRegInfo();
265
266 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); MFI != MFE;
267 ++MFI)
268 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
269 ReplaceUsesWithZeroReg(MRI, *I);
270}
271
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000272bool MipsDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) {
273 bool Ret = SelectionDAGISel::runOnMachineFunction(MF);
Jia Liubb481f82012-02-28 07:46:26 +0000274
Akira Hatanaka7065b7b2012-03-08 01:51:59 +0000275 ProcessFunctionAfterISel(MF);
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000276
277 return Ret;
278}
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000279
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000280/// getGlobalBaseReg - Output the instructions required to put the
281/// GOT address into a register.
Dan Gohman99114052009-06-03 20:30:14 +0000282SDNode *MipsDAGToDAGISel::getGlobalBaseReg() {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000283 unsigned GlobalBaseReg = MF->getInfo<MipsFunctionInfo>()->getGlobalBaseReg();
Dan Gohman99114052009-06-03 20:30:14 +0000284 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Bruno Cardoso Lopes753a9872007-11-12 19:49:57 +0000285}
286
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000287/// ComplexPattern used on MipsInstrInfo
288/// Used on Mips Load/Store instructions
289bool MipsDAGToDAGISel::
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000290SelectAddr(SDNode *Parent, SDValue Addr, SDValue &Base, SDValue &Offset) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000291 EVT ValTy = Addr.getValueType();
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000292
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000293 // if Address is FI, get the TargetFrameIndex.
294 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000295 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
296 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000297 return true;
298 }
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000299
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000300 // on PIC code Load GA
Akira Hatanaka6df7e232011-12-09 01:53:17 +0000301 if (Addr.getOpcode() == MipsISD::Wrapper) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +0000302 Base = Addr.getOperand(0);
303 Offset = Addr.getOperand(1);
Akira Hatanakaca074792011-12-08 20:34:32 +0000304 return true;
305 }
306
307 if (TM.getRelocationModel() != Reloc::PIC_) {
Bill Wendling056292f2008-09-16 21:48:12 +0000308 if ((Addr.getOpcode() == ISD::TargetExternalSymbol ||
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000309 Addr.getOpcode() == ISD::TargetGlobalAddress))
310 return false;
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000311 }
312
Akira Hatanaka5e069032011-06-02 01:03:14 +0000313 // Addresses of the form FI+const or FI|const
314 if (CurDAG->isBaseWithConstantOffset(Addr)) {
315 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1));
316 if (isInt<16>(CN->getSExtValue())) {
317
318 // If the first operand is a FI, get the TargetFI Node
319 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>
320 (Addr.getOperand(0)))
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000321 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000322 else
323 Base = Addr.getOperand(0);
324
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000325 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), ValTy);
Akira Hatanaka5e069032011-06-02 01:03:14 +0000326 return true;
327 }
328 }
329
Bruno Cardoso Lopes7ff6fa22007-08-18 02:16:30 +0000330 // Operand is a result from an ADD.
Bruno Cardoso Lopesc7db5612007-11-05 03:02:32 +0000331 if (Addr.getOpcode() == ISD::ADD) {
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000332 // When loading from constant pools, load the lower address part in
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000333 // the instruction itself. Example, instead of:
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000334 // lui $2, %hi($CPI1_0)
335 // addiu $2, $2, %lo($CPI1_0)
336 // lwc1 $f0, 0($2)
337 // Generate:
338 // lui $2, %hi($CPI1_0)
339 // lwc1 $f0, %lo($CPI1_0)($2)
Akira Hatanaka45d8dbc2012-08-24 20:21:49 +0000340 if (Addr.getOperand(1).getOpcode() == MipsISD::Lo ||
341 Addr.getOperand(1).getOpcode() == MipsISD::GPRel) {
342 SDValue Opnd0 = Addr.getOperand(1).getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000343 if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) ||
344 isa<JumpTableSDNode>(Opnd0)) {
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000345 Base = Addr.getOperand(0);
Akira Hatanaka87827072012-06-13 20:33:18 +0000346 Offset = Opnd0;
Bruno Cardoso Lopesd71cebf2009-11-25 12:17:58 +0000347 return true;
Bruno Cardoso Lopes6e0b6582009-11-16 04:33:42 +0000348 }
349 }
Akira Hatanaka44b6c712012-02-28 02:55:02 +0000350
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000351 // If an indexed floating point load/store can be emitted, return false.
Akira Hatanaka36bcc112012-07-31 18:16:49 +0000352 const LSBaseSDNode *LS = dyn_cast<LSBaseSDNode>(Parent);
353
Akira Hatanaka864f6602012-06-14 21:10:56 +0000354 if (LS &&
355 (LS->getMemoryVT() == MVT::f32 || LS->getMemoryVT() == MVT::f64) &&
Akira Hatanakadfa27ae2012-03-01 22:12:30 +0000356 Subtarget.hasMips32r2Or64())
357 return false;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000358 }
359
Bruno Cardoso Lopesa4e82002007-07-11 23:24:41 +0000360 Base = Addr;
Akira Hatanaka381e97d2011-10-11 00:44:20 +0000361 Offset = CurDAG->getTargetConstant(0, ValTy);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000362 return true;
363}
364
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000365/// Select multiply instructions.
366std::pair<SDNode*, SDNode*>
Jia Liubb481f82012-02-28 07:46:26 +0000367MipsDAGToDAGISel::SelectMULT(SDNode *N, unsigned Opc, DebugLoc dl, EVT Ty,
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000368 bool HasLo, bool HasHi) {
Chad Rosiera32a08c2012-01-06 20:02:49 +0000369 SDNode *Lo = 0, *Hi = 0;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000370 SDNode *Mul = CurDAG->getMachineNode(Opc, dl, MVT::Glue, N->getOperand(0),
371 N->getOperand(1));
372 SDValue InFlag = SDValue(Mul, 0);
373
374 if (HasLo) {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000375 unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mflo16 :
376 (Ty == MVT::i32 ? Mips::MFLO : Mips::MFLO64);
377 Lo = CurDAG->getMachineNode(Opcode, dl, Ty, MVT::Glue, InFlag);
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000378 InFlag = SDValue(Lo, 1);
379 }
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000380 if (HasHi) {
381 unsigned Opcode = Subtarget.inMips16Mode() ? Mips::Mfhi16 :
382 (Ty == MVT::i32 ? Mips::MFHI : Mips::MFHI64);
383 Hi = CurDAG->getMachineNode(Opcode, dl, Ty, InFlag);
384 }
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000385 return std::make_pair(Lo, Hi);
386}
387
388
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000389/// Select instructions not customized! Used for
390/// expanded, promoted and normal instructions
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000391SDNode* MipsDAGToDAGISel::Select(SDNode *Node) {
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000392 unsigned Opcode = Node->getOpcode();
Dale Johannesena05dca42009-02-04 23:02:30 +0000393 DebugLoc dl = Node->getDebugLoc();
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000394
395 // Dump information about the Node being selected
Chris Lattner7c306da2010-03-02 06:34:30 +0000396 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000397
398 // If we have a custom node, we already have selected!
Dan Gohmane8be6c62008-07-17 19:10:17 +0000399 if (Node->isMachineOpcode()) {
Chris Lattner7c306da2010-03-02 06:34:30 +0000400 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000401 return NULL;
402 }
403
404 ///
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000405 // Instruction Selection not handled by the auto-generated
Bruno Cardoso Lopesb42abeb2007-09-24 20:15:11 +0000406 // tablegen selection should be handled here.
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000407 ///
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000408 EVT NodeTy = Node->getValueType(0);
409 unsigned MultOpc;
410
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000411 switch(Opcode) {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000412 default: break;
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000413
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000414 case ISD::SUBE:
415 case ISD::ADDE: {
416 SDValue InFlag = Node->getOperand(2), CmpLHS;
417 unsigned Opc = InFlag.getOpcode(); (void)Opc;
418 assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) ||
419 (Opc == ISD::SUBC || Opc == ISD::SUBE)) &&
420 "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn");
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000421
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000422 unsigned MOp;
423 if (Opcode == ISD::ADDE) {
424 CmpLHS = InFlag.getValue(0);
425 MOp = Mips::ADDu;
426 } else {
427 CmpLHS = InFlag.getOperand(0);
428 MOp = Mips::SUBu;
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000429 }
430
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000431 SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) };
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000432
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000433 SDValue LHS = Node->getOperand(0);
434 SDValue RHS = Node->getOperand(1);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000435
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000436 EVT VT = LHS.getValueType();
437 SDNode *Carry = CurDAG->getMachineNode(Mips::SLTu, dl, VT, Ops, 2);
438 SDNode *AddCarry = CurDAG->getMachineNode(Mips::ADDu, dl, VT,
439 SDValue(Carry,0), RHS);
Bruno Cardoso Lopes07cec752008-06-06 00:58:26 +0000440
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000441 return CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue,
442 LHS, SDValue(AddCarry,0));
443 }
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000444
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000445 /// Mul with two results
446 case ISD::SMUL_LOHI:
447 case ISD::UMUL_LOHI: {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000448 if (NodeTy == MVT::i32) {
449 if (Subtarget.inMips16Mode())
450 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MultuRxRy16 :
451 Mips::MultRxRy16);
452 else
453 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::MULTu : Mips::MULT);
454 }
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000455 else
456 MultOpc = (Opcode == ISD::UMUL_LOHI ? Mips::DMULTu : Mips::DMULT);
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000457
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000458 std::pair<SDNode*, SDNode*> LoHi = SelectMULT(Node, MultOpc, dl, NodeTy,
459 true, true);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000460
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000461 if (!SDValue(Node, 0).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000462 ReplaceUses(SDValue(Node, 0), SDValue(LoHi.first, 0));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000463
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000464 if (!SDValue(Node, 1).use_empty())
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000465 ReplaceUses(SDValue(Node, 1), SDValue(LoHi.second, 0));
Bruno Cardoso Lopes0af5e092008-06-06 06:37:31 +0000466
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000467 return NULL;
468 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000469
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000470 /// Special Muls
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000471 case ISD::MUL: {
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000472 // Mips32 has a 32-bit three operand mul instruction.
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000473 if (Subtarget.hasMips32() && NodeTy == MVT::i32)
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000474 break;
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000475 return SelectMULT(Node, NodeTy == MVT::i32 ? Mips::MULT : Mips::DMULT,
476 dl, NodeTy, true, false).first;
477 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000478 case ISD::MULHS:
479 case ISD::MULHU: {
Reed Kotlerdfb8dbb2012-10-05 18:27:54 +0000480 if (NodeTy == MVT::i32) {
481 if (Subtarget.inMips16Mode())
482 MultOpc = (Opcode == ISD::MULHU ?
483 Mips::MultuRxRy16 : Mips::MultRxRy16);
484 else
485 MultOpc = (Opcode == ISD::MULHU ? Mips::MULTu : Mips::MULT);
486 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000487 else
Akira Hatanaka2fd04752011-12-20 23:10:57 +0000488 MultOpc = (Opcode == ISD::MULHU ? Mips::DMULTu : Mips::DMULT);
489
490 return SelectMULT(Node, MultOpc, dl, NodeTy, false, true).second;
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000491 }
Bruno Cardoso Lopesa8173b92009-11-13 18:49:59 +0000492
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000493 // Get target GOT address.
494 case ISD::GLOBAL_OFFSET_TABLE:
495 return getGlobalBaseReg();
Akira Hatanakaca074792011-12-08 20:34:32 +0000496
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000497 case ISD::ConstantFP: {
498 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node);
499 if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) {
500 if (Subtarget.hasMips64()) {
501 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
502 Mips::ZERO_64, MVT::i64);
503 return CurDAG->getMachineNode(Mips::DMTC1, dl, MVT::f64, Zero);
Akira Hatanakaca074792011-12-08 20:34:32 +0000504 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000505
506 SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
507 Mips::ZERO, MVT::i32);
508 return CurDAG->getMachineNode(Mips::BuildPairF64, dl, MVT::f64, Zero,
509 Zero);
Bruno Cardoso Lopesd9796862011-05-31 02:53:58 +0000510 }
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000511 break;
512 }
513
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000514 case ISD::Constant: {
515 const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node);
516 unsigned Size = CN->getValueSizeInBits(0);
517
518 if (Size == 32)
519 break;
520
521 MipsAnalyzeImmediate AnalyzeImm;
522 int64_t Imm = CN->getSExtValue();
523
524 const MipsAnalyzeImmediate::InstSeq &Seq =
525 AnalyzeImm.Analyze(Imm, Size, false);
Jia Liubb481f82012-02-28 07:46:26 +0000526
Akira Hatanaka57fa3822012-01-25 03:01:35 +0000527 MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
528 DebugLoc DL = CN->getDebugLoc();
529 SDNode *RegOpnd;
530 SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
531 MVT::i64);
532
533 // The first instruction can be a LUi which is different from other
534 // instructions (ADDiu, ORI and SLL) in that it does not have a register
535 // operand.
536 if (Inst->Opc == Mips::LUi64)
537 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd);
538 else
539 RegOpnd =
540 CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
541 CurDAG->getRegister(Mips::ZERO_64, MVT::i64),
542 ImmOpnd);
543
544 // The remaining instructions in the sequence are handled here.
545 for (++Inst; Inst != Seq.end(); ++Inst) {
546 ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd),
547 MVT::i64);
548 RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64,
549 SDValue(RegOpnd, 0), ImmOpnd);
550 }
551
552 return RegOpnd;
553 }
554
Akira Hatanaka5a7dd432012-09-15 01:52:08 +0000555#ifndef NDEBUG
556 case ISD::LOAD:
557 case ISD::STORE:
558 assert(cast<MemSDNode>(Node)->getMemoryVT().getSizeInBits() / 8 <=
559 cast<MemSDNode>(Node)->getAlignment() &&
560 "Unexpected unaligned loads/stores.");
561 break;
562#endif
563
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000564 case MipsISD::ThreadPointer: {
565 EVT PtrVT = TLI.getPointerTy();
566 unsigned RdhwrOpc, SrcReg, DestReg;
567
568 if (PtrVT == MVT::i32) {
569 RdhwrOpc = Mips::RDHWR;
570 SrcReg = Mips::HWR29;
571 DestReg = Mips::V1;
572 } else {
573 RdhwrOpc = Mips::RDHWR64;
574 SrcReg = Mips::HWR29_64;
575 DestReg = Mips::V1_64;
576 }
Jia Liubb481f82012-02-28 07:46:26 +0000577
Akira Hatanaka49d534b2011-12-20 22:58:01 +0000578 SDNode *Rdhwr =
579 CurDAG->getMachineNode(RdhwrOpc, Node->getDebugLoc(),
580 Node->getValueType(0),
581 CurDAG->getRegister(SrcReg, PtrVT));
582 SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, DestReg,
583 SDValue(Rdhwr, 0));
584 SDValue ResNode = CurDAG->getCopyFromReg(Chain, dl, DestReg, PtrVT);
585 ReplaceUses(SDValue(Node, 0), ResNode);
586 return ResNode.getNode();
587 }
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000588 }
589
590 // Select the default instruction
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000591 SDNode *ResNode = SelectCode(Node);
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000592
Chris Lattner7c306da2010-03-02 06:34:30 +0000593 DEBUG(errs() << "=> ");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000594 if (ResNode == NULL || ResNode == Node)
595 DEBUG(Node->dump(CurDAG));
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000596 else
597 DEBUG(ResNode->dump(CurDAG));
Chris Lattner893e1c92009-08-23 06:49:22 +0000598 DEBUG(errs() << "\n");
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000599 return ResNode;
600}
601
Akira Hatanaka21afc632011-06-21 00:40:49 +0000602bool MipsDAGToDAGISel::
603SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
604 std::vector<SDValue> &OutOps) {
605 assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
606 OutOps.push_back(Op);
607 return false;
608}
609
Bruno Cardoso Lopes81092dc2011-03-04 17:51:39 +0000610/// createMipsISelDag - This pass converts a legalized DAG into a
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +0000611/// MIPS-specific DAG, ready for instruction scheduling.
612FunctionPass *llvm::createMipsISelDag(MipsTargetMachine &TM) {
613 return new MipsDAGToDAGISel(TM);
614}