1 //===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that Mips uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mips-lower"
16 #include "MipsISelLowering.h"
17 #include "InstPrinter/MipsInstPrinter.h"
18 #include "MCTargetDesc/MipsBaseInfo.h"
19 #include "MipsMachineFunction.h"
20 #include "MipsSubtarget.h"
21 #include "MipsTargetMachine.h"
22 #include "MipsTargetObjectFile.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/CodeGen/CallingConvLower.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/SelectionDAGISel.h"
30 #include "llvm/CodeGen/ValueTypes.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/raw_ostream.h"
43 STATISTIC(NumTailCalls, "Number of tail calls");
46 EnableMipsTailCalls("enable-mips-tail-calls", cl::Hidden,
47 cl::desc("MIPS: Enable tail calls."), cl::init(false));
50 LargeGOT("mxgot", cl::Hidden,
51 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
54 Mips16HardFloat("mips16-hard-float", cl::NotHidden,
55 cl::desc("MIPS: mips16 hard float enable."),
58 static cl::opt<bool> DontExpandCondPseudos16(
59 "mips16-dont-expand-cond-pseudo",
61 cl::desc("Dont expand conditional move related "
62 "pseudos for Mips 16"),
66 static const uint16_t O32IntRegs[4] = {
67 Mips::A0, Mips::A1, Mips::A2, Mips::A3
70 static const uint16_t Mips64IntRegs[8] = {
71 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
72 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
75 static const uint16_t Mips64DPRegs[8] = {
76 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
77 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
80 // If I is a shifted mask, set the size (Size) and the first bit of the
81 // mask (Pos), and return true.
82 // For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
83 static bool IsShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
84 if (!isShiftedMask_64(I))
87 Size = CountPopulation_64(I);
88 Pos = CountTrailingZeros_64(I);
92 static SDValue GetGlobalReg(SelectionDAG &DAG, EVT Ty) {
93 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
94 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
97 static SDValue getTargetNode(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
98 EVT Ty = Op.getValueType();
100 if (GlobalAddressSDNode *N = dyn_cast<GlobalAddressSDNode>(Op))
101 return DAG.getTargetGlobalAddress(N->getGlobal(), Op.getDebugLoc(), Ty, 0,
103 if (ExternalSymbolSDNode *N = dyn_cast<ExternalSymbolSDNode>(Op))
104 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
105 if (BlockAddressSDNode *N = dyn_cast<BlockAddressSDNode>(Op))
106 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
107 if (JumpTableSDNode *N = dyn_cast<JumpTableSDNode>(Op))
108 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
109 if (ConstantPoolSDNode *N = dyn_cast<ConstantPoolSDNode>(Op))
110 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
111 N->getOffset(), Flag);
113 llvm_unreachable("Unexpected node type.");
117 static SDValue getAddrNonPIC(SDValue Op, SelectionDAG &DAG) {
118 DebugLoc DL = Op.getDebugLoc();
119 EVT Ty = Op.getValueType();
120 SDValue Hi = getTargetNode(Op, DAG, MipsII::MO_ABS_HI);
121 SDValue Lo = getTargetNode(Op, DAG, MipsII::MO_ABS_LO);
122 return DAG.getNode(ISD::ADD, DL, Ty,
123 DAG.getNode(MipsISD::Hi, DL, Ty, Hi),
124 DAG.getNode(MipsISD::Lo, DL, Ty, Lo));
127 static SDValue getAddrLocal(SDValue Op, SelectionDAG &DAG, bool HasMips64) {
128 DebugLoc DL = Op.getDebugLoc();
129 EVT Ty = Op.getValueType();
130 unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
131 SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, GetGlobalReg(DAG, Ty),
132 getTargetNode(Op, DAG, GOTFlag));
133 SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT,
134 MachinePointerInfo::getGOT(), false, false, false,
136 unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
137 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty, getTargetNode(Op, DAG, LoFlag));
138 return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo);
141 static SDValue getAddrGlobal(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
142 DebugLoc DL = Op.getDebugLoc();
143 EVT Ty = Op.getValueType();
144 SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, GetGlobalReg(DAG, Ty),
145 getTargetNode(Op, DAG, Flag));
146 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt,
147 MachinePointerInfo::getGOT(), false, false, false, 0);
150 static SDValue getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG,
151 unsigned HiFlag, unsigned LoFlag) {
152 DebugLoc DL = Op.getDebugLoc();
153 EVT Ty = Op.getValueType();
154 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty, getTargetNode(Op, DAG, HiFlag));
155 Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, GetGlobalReg(DAG, Ty));
156 SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi,
157 getTargetNode(Op, DAG, LoFlag));
158 return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper,
159 MachinePointerInfo::getGOT(), false, false, false, 0);
162 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
164 case MipsISD::JmpLink: return "MipsISD::JmpLink";
165 case MipsISD::TailCall: return "MipsISD::TailCall";
166 case MipsISD::Hi: return "MipsISD::Hi";
167 case MipsISD::Lo: return "MipsISD::Lo";
168 case MipsISD::GPRel: return "MipsISD::GPRel";
169 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
170 case MipsISD::Ret: return "MipsISD::Ret";
171 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
172 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
173 case MipsISD::FPCmp: return "MipsISD::FPCmp";
174 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
175 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
176 case MipsISD::FPRound: return "MipsISD::FPRound";
177 case MipsISD::MAdd: return "MipsISD::MAdd";
178 case MipsISD::MAddu: return "MipsISD::MAddu";
179 case MipsISD::MSub: return "MipsISD::MSub";
180 case MipsISD::MSubu: return "MipsISD::MSubu";
181 case MipsISD::DivRem: return "MipsISD::DivRem";
182 case MipsISD::DivRemU: return "MipsISD::DivRemU";
183 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
184 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
185 case MipsISD::Wrapper: return "MipsISD::Wrapper";
186 case MipsISD::Sync: return "MipsISD::Sync";
187 case MipsISD::Ext: return "MipsISD::Ext";
188 case MipsISD::Ins: return "MipsISD::Ins";
189 case MipsISD::LWL: return "MipsISD::LWL";
190 case MipsISD::LWR: return "MipsISD::LWR";
191 case MipsISD::SWL: return "MipsISD::SWL";
192 case MipsISD::SWR: return "MipsISD::SWR";
193 case MipsISD::LDL: return "MipsISD::LDL";
194 case MipsISD::LDR: return "MipsISD::LDR";
195 case MipsISD::SDL: return "MipsISD::SDL";
196 case MipsISD::SDR: return "MipsISD::SDR";
197 case MipsISD::EXTP: return "MipsISD::EXTP";
198 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
199 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
200 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
201 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
202 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
203 case MipsISD::SHILO: return "MipsISD::SHILO";
204 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
205 case MipsISD::MULT: return "MipsISD::MULT";
206 case MipsISD::MULTU: return "MipsISD::MULTU";
207 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSPDSP";
208 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
209 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
210 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
211 default: return NULL;
217 bool operator()(const char *s1, const char *s2) const
219 return strcmp(s1, s2) < 0;
223 std::set<const char*, ltstr> noHelperNeeded;
226 void MipsTargetLowering::SetMips16LibcallName
227 (RTLIB::Libcall l, const char *Name) {
228 setLibcallName(l, Name);
229 noHelperNeeded.insert(Name);
232 void MipsTargetLowering::setMips16HardFloatLibCalls() {
233 SetMips16LibcallName(RTLIB::ADD_F32, "__mips16_addsf3");
234 SetMips16LibcallName(RTLIB::ADD_F64, "__mips16_adddf3");
235 SetMips16LibcallName(RTLIB::SUB_F32, "__mips16_subsf3");
236 SetMips16LibcallName(RTLIB::SUB_F64, "__mips16_subdf3");
237 SetMips16LibcallName(RTLIB::MUL_F32, "__mips16_mulsf3");
238 SetMips16LibcallName(RTLIB::MUL_F64, "__mips16_muldf3");
239 SetMips16LibcallName(RTLIB::DIV_F32, "__mips16_divsf3");
240 SetMips16LibcallName(RTLIB::DIV_F64, "__mips16_divdf3");
241 SetMips16LibcallName(RTLIB::FPEXT_F32_F64, "__mips16_extendsfdf2");
242 SetMips16LibcallName(RTLIB::FPROUND_F64_F32, "__mips16_truncdfsf2");
243 SetMips16LibcallName(RTLIB::FPTOSINT_F32_I32, "__mips16_fix_truncsfsi");
244 SetMips16LibcallName(RTLIB::FPTOSINT_F64_I32, "__mips16_fix_truncdfsi");
245 SetMips16LibcallName(RTLIB::SINTTOFP_I32_F32, "__mips16_floatsisf");
246 SetMips16LibcallName(RTLIB::SINTTOFP_I32_F64, "__mips16_floatsidf");
247 SetMips16LibcallName(RTLIB::UINTTOFP_I32_F32, "__mips16_floatunsisf");
248 SetMips16LibcallName(RTLIB::UINTTOFP_I32_F64, "__mips16_floatunsidf");
249 SetMips16LibcallName(RTLIB::OEQ_F32, "__mips16_eqsf2");
250 SetMips16LibcallName(RTLIB::OEQ_F64, "__mips16_eqdf2");
251 SetMips16LibcallName(RTLIB::UNE_F32, "__mips16_nesf2");
252 SetMips16LibcallName(RTLIB::UNE_F64, "__mips16_nedf2");
253 SetMips16LibcallName(RTLIB::OGE_F32, "__mips16_gesf2");
254 SetMips16LibcallName(RTLIB::OGE_F64, "__mips16_gedf2");
255 SetMips16LibcallName(RTLIB::OLT_F32, "__mips16_ltsf2");
256 SetMips16LibcallName(RTLIB::OLT_F64, "__mips16_ltdf2");
257 SetMips16LibcallName(RTLIB::OLE_F32, "__mips16_lesf2");
258 SetMips16LibcallName(RTLIB::OLE_F64, "__mips16_ledf2");
259 SetMips16LibcallName(RTLIB::OGT_F32, "__mips16_gtsf2");
260 SetMips16LibcallName(RTLIB::OGT_F64, "__mips16_gtdf2");
261 SetMips16LibcallName(RTLIB::UO_F32, "__mips16_unordsf2");
262 SetMips16LibcallName(RTLIB::UO_F64, "__mips16_unorddf2");
263 SetMips16LibcallName(RTLIB::O_F32, "__mips16_unordsf2");
264 SetMips16LibcallName(RTLIB::O_F64, "__mips16_unorddf2");
268 MipsTargetLowering(MipsTargetMachine &TM)
269 : TargetLowering(TM, new MipsTargetObjectFile()),
270 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
271 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
272 IsO32(Subtarget->isABI_O32()) {
274 // Mips does not have i1 type, so use i32 for
275 // setcc operations results (slt, sgt, ...).
276 setBooleanContents(ZeroOrOneBooleanContent);
277 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
279 // Set up the register classes
280 addRegisterClass(MVT::i32, &Mips::CPURegsRegClass);
283 addRegisterClass(MVT::i64, &Mips::CPU64RegsRegClass);
285 if (Subtarget->inMips16Mode()) {
286 addRegisterClass(MVT::i32, &Mips::CPU16RegsRegClass);
288 setMips16HardFloatLibCalls();
291 if (Subtarget->hasDSP()) {
292 MVT::SimpleValueType VecTys[2] = {MVT::v2i16, MVT::v4i8};
294 for (unsigned i = 0; i < array_lengthof(VecTys); ++i) {
295 addRegisterClass(VecTys[i], &Mips::DSPRegsRegClass);
297 // Expand all builtin opcodes.
298 for (unsigned Opc = 0; Opc < ISD::BUILTIN_OP_END; ++Opc)
299 setOperationAction(Opc, VecTys[i], Expand);
301 setOperationAction(ISD::LOAD, VecTys[i], Legal);
302 setOperationAction(ISD::STORE, VecTys[i], Legal);
303 setOperationAction(ISD::BITCAST, VecTys[i], Legal);
307 if (!TM.Options.UseSoftFloat) {
308 addRegisterClass(MVT::f32, &Mips::FGR32RegClass);
310 // When dealing with single precision only, use libcalls
311 if (!Subtarget->isSingleFloat()) {
313 addRegisterClass(MVT::f64, &Mips::FGR64RegClass);
315 addRegisterClass(MVT::f64, &Mips::AFGR64RegClass);
319 // Load extented operations for i1 types must be promoted
320 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
321 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
322 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
324 // MIPS doesn't have extending float->double load/store
325 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
326 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
328 // Used by legalize types to correctly generate the setcc result.
329 // Without this, every float setcc comes with a AND/OR with the result,
330 // we don't want this, since the fpcmp result goes to a flag register,
331 // which is used implicitly by brcond and select operations.
332 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
334 // Mips Custom Operations
335 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
336 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
337 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
338 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
339 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
340 setOperationAction(ISD::SELECT, MVT::f32, Custom);
341 setOperationAction(ISD::SELECT, MVT::f64, Custom);
342 setOperationAction(ISD::SELECT, MVT::i32, Custom);
343 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
344 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
345 setOperationAction(ISD::SETCC, MVT::f32, Custom);
346 setOperationAction(ISD::SETCC, MVT::f64, Custom);
347 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
348 setOperationAction(ISD::VASTART, MVT::Other, Custom);
349 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
350 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
351 if (Subtarget->inMips16Mode()) {
352 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
353 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
356 setOperationAction(ISD::MEMBARRIER, MVT::Other, Custom);
357 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
359 if (!Subtarget->inMips16Mode()) {
360 setOperationAction(ISD::LOAD, MVT::i32, Custom);
361 setOperationAction(ISD::STORE, MVT::i32, Custom);
364 if (!TM.Options.NoNaNsFPMath) {
365 setOperationAction(ISD::FABS, MVT::f32, Custom);
366 setOperationAction(ISD::FABS, MVT::f64, Custom);
370 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
371 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
372 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
373 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
374 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
375 setOperationAction(ISD::SELECT, MVT::i64, Custom);
376 setOperationAction(ISD::LOAD, MVT::i64, Custom);
377 setOperationAction(ISD::STORE, MVT::i64, Custom);
381 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
382 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
383 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
386 setOperationAction(ISD::ADD, MVT::i32, Custom);
388 setOperationAction(ISD::ADD, MVT::i64, Custom);
390 setOperationAction(ISD::SDIV, MVT::i32, Expand);
391 setOperationAction(ISD::SREM, MVT::i32, Expand);
392 setOperationAction(ISD::UDIV, MVT::i32, Expand);
393 setOperationAction(ISD::UREM, MVT::i32, Expand);
394 setOperationAction(ISD::SDIV, MVT::i64, Expand);
395 setOperationAction(ISD::SREM, MVT::i64, Expand);
396 setOperationAction(ISD::UDIV, MVT::i64, Expand);
397 setOperationAction(ISD::UREM, MVT::i64, Expand);
399 // Operations not directly supported by Mips.
400 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
401 setOperationAction(ISD::BR_CC, MVT::Other, Expand);
402 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
403 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
404 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
405 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
406 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
407 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
408 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
409 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
410 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
411 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
412 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
413 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
414 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
415 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
416 setOperationAction(ISD::ROTL, MVT::i32, Expand);
417 setOperationAction(ISD::ROTL, MVT::i64, Expand);
418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
419 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
421 if (!Subtarget->hasMips32r2())
422 setOperationAction(ISD::ROTR, MVT::i32, Expand);
424 if (!Subtarget->hasMips64r2())
425 setOperationAction(ISD::ROTR, MVT::i64, Expand);
427 setOperationAction(ISD::FSIN, MVT::f32, Expand);
428 setOperationAction(ISD::FSIN, MVT::f64, Expand);
429 setOperationAction(ISD::FCOS, MVT::f32, Expand);
430 setOperationAction(ISD::FCOS, MVT::f64, Expand);
431 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
432 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
433 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
434 setOperationAction(ISD::FPOW, MVT::f32, Expand);
435 setOperationAction(ISD::FPOW, MVT::f64, Expand);
436 setOperationAction(ISD::FLOG, MVT::f32, Expand);
437 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
438 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
439 setOperationAction(ISD::FEXP, MVT::f32, Expand);
440 setOperationAction(ISD::FMA, MVT::f32, Expand);
441 setOperationAction(ISD::FMA, MVT::f64, Expand);
442 setOperationAction(ISD::FREM, MVT::f32, Expand);
443 setOperationAction(ISD::FREM, MVT::f64, Expand);
445 if (!TM.Options.NoNaNsFPMath) {
446 setOperationAction(ISD::FNEG, MVT::f32, Expand);
447 setOperationAction(ISD::FNEG, MVT::f64, Expand);
450 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
451 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
452 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
453 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
455 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
457 setOperationAction(ISD::VAARG, MVT::Other, Expand);
458 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
459 setOperationAction(ISD::VAEND, MVT::Other, Expand);
461 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
462 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
464 // Use the default for now
465 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
466 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
468 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
469 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
470 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
471 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
473 if (Subtarget->inMips16Mode()) {
474 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
475 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
476 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
477 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
478 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
479 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
480 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
481 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
482 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
483 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
484 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
485 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
488 setInsertFencesForAtomic(true);
490 if (!Subtarget->hasSEInReg()) {
491 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
492 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
495 if (!Subtarget->hasBitCount()) {
496 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
497 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
500 if (!Subtarget->hasSwap()) {
501 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
502 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
506 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
507 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
508 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
509 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
512 setTargetDAGCombine(ISD::ADDE);
513 setTargetDAGCombine(ISD::SUBE);
514 setTargetDAGCombine(ISD::SDIVREM);
515 setTargetDAGCombine(ISD::UDIVREM);
516 setTargetDAGCombine(ISD::SELECT);
517 setTargetDAGCombine(ISD::AND);
518 setTargetDAGCombine(ISD::OR);
519 setTargetDAGCombine(ISD::ADD);
521 setMinFunctionAlignment(HasMips64 ? 3 : 2);
523 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
524 computeRegisterProperties();
526 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
527 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
529 MaxStoresPerMemcpy = 16;
533 MipsTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const {
534 MVT::SimpleValueType SVT = VT.getSimpleVT().SimpleTy;
536 if (Subtarget->inMips16Mode())
550 EVT MipsTargetLowering::getSetCCResultType(EVT VT) const {
553 return VT.changeVectorElementTypeToInteger();
557 // Transforms a subgraph in CurDAG if the following pattern is found:
558 // (addc multLo, Lo0), (adde multHi, Hi0),
560 // multHi/Lo: product of multiplication
561 // Lo0: initial value of Lo register
562 // Hi0: initial value of Hi register
563 // Return true if pattern matching was successful.
564 static bool SelectMadd(SDNode *ADDENode, SelectionDAG *CurDAG) {
565 // ADDENode's second operand must be a flag output of an ADDC node in order
566 // for the matching to be successful.
567 SDNode *ADDCNode = ADDENode->getOperand(2).getNode();
569 if (ADDCNode->getOpcode() != ISD::ADDC)
572 SDValue MultHi = ADDENode->getOperand(0);
573 SDValue MultLo = ADDCNode->getOperand(0);
574 SDNode *MultNode = MultHi.getNode();
575 unsigned MultOpc = MultHi.getOpcode();
577 // MultHi and MultLo must be generated by the same node,
578 if (MultLo.getNode() != MultNode)
581 // and it must be a multiplication.
582 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
585 // MultLo amd MultHi must be the first and second output of MultNode
587 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
590 // Transform this to a MADD only if ADDENode and ADDCNode are the only users
591 // of the values of MultNode, in which case MultNode will be removed in later
593 // If there exist users other than ADDENode or ADDCNode, this function returns
594 // here, which will result in MultNode being mapped to a single MULT
595 // instruction node rather than a pair of MULT and MADD instructions being
597 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
600 SDValue Chain = CurDAG->getEntryNode();
601 DebugLoc dl = ADDENode->getDebugLoc();
603 // create MipsMAdd(u) node
604 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
606 SDValue MAdd = CurDAG->getNode(MultOpc, dl, MVT::Glue,
607 MultNode->getOperand(0),// Factor 0
608 MultNode->getOperand(1),// Factor 1
609 ADDCNode->getOperand(1),// Lo0
610 ADDENode->getOperand(1));// Hi0
612 // create CopyFromReg nodes
613 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
615 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
617 CopyFromLo.getValue(2));
619 // replace uses of adde and addc here
620 if (!SDValue(ADDCNode, 0).use_empty())
621 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
623 if (!SDValue(ADDENode, 0).use_empty())
624 CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
630 // Transforms a subgraph in CurDAG if the following pattern is found:
631 // (addc Lo0, multLo), (sube Hi0, multHi),
633 // multHi/Lo: product of multiplication
634 // Lo0: initial value of Lo register
635 // Hi0: initial value of Hi register
636 // Return true if pattern matching was successful.
637 static bool SelectMsub(SDNode *SUBENode, SelectionDAG *CurDAG) {
638 // SUBENode's second operand must be a flag output of an SUBC node in order
639 // for the matching to be successful.
640 SDNode *SUBCNode = SUBENode->getOperand(2).getNode();
642 if (SUBCNode->getOpcode() != ISD::SUBC)
645 SDValue MultHi = SUBENode->getOperand(1);
646 SDValue MultLo = SUBCNode->getOperand(1);
647 SDNode *MultNode = MultHi.getNode();
648 unsigned MultOpc = MultHi.getOpcode();
650 // MultHi and MultLo must be generated by the same node,
651 if (MultLo.getNode() != MultNode)
654 // and it must be a multiplication.
655 if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
658 // MultLo amd MultHi must be the first and second output of MultNode
660 if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
663 // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
664 // of the values of MultNode, in which case MultNode will be removed in later
666 // If there exist users other than SUBENode or SUBCNode, this function returns
667 // here, which will result in MultNode being mapped to a single MULT
668 // instruction node rather than a pair of MULT and MSUB instructions being
670 if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
673 SDValue Chain = CurDAG->getEntryNode();
674 DebugLoc dl = SUBENode->getDebugLoc();
676 // create MipsSub(u) node
677 MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
679 SDValue MSub = CurDAG->getNode(MultOpc, dl, MVT::Glue,
680 MultNode->getOperand(0),// Factor 0
681 MultNode->getOperand(1),// Factor 1
682 SUBCNode->getOperand(0),// Lo0
683 SUBENode->getOperand(0));// Hi0
685 // create CopyFromReg nodes
686 SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
688 SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
690 CopyFromLo.getValue(2));
692 // replace uses of sube and subc here
693 if (!SDValue(SUBCNode, 0).use_empty())
694 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
696 if (!SDValue(SUBENode, 0).use_empty())
697 CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
702 static SDValue PerformADDECombine(SDNode *N, SelectionDAG &DAG,
703 TargetLowering::DAGCombinerInfo &DCI,
704 const MipsSubtarget *Subtarget) {
705 if (DCI.isBeforeLegalize())
708 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
710 return SDValue(N, 0);
715 static SDValue PerformSUBECombine(SDNode *N, SelectionDAG &DAG,
716 TargetLowering::DAGCombinerInfo &DCI,
717 const MipsSubtarget *Subtarget) {
718 if (DCI.isBeforeLegalize())
721 if (Subtarget->hasMips32() && N->getValueType(0) == MVT::i32 &&
723 return SDValue(N, 0);
728 static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG &DAG,
729 TargetLowering::DAGCombinerInfo &DCI,
730 const MipsSubtarget *Subtarget) {
731 if (DCI.isBeforeLegalizeOps())
734 EVT Ty = N->getValueType(0);
735 unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
736 unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
737 unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
739 DebugLoc dl = N->getDebugLoc();
741 SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
742 N->getOperand(0), N->getOperand(1));
743 SDValue InChain = DAG.getEntryNode();
744 SDValue InGlue = DivRem;
747 if (N->hasAnyUseOfValue(0)) {
748 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, LO, Ty,
750 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
751 InChain = CopyFromLo.getValue(1);
752 InGlue = CopyFromLo.getValue(2);
756 if (N->hasAnyUseOfValue(1)) {
757 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
759 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
765 static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
767 default: llvm_unreachable("Unknown fp condition code!");
769 case ISD::SETOEQ: return Mips::FCOND_OEQ;
770 case ISD::SETUNE: return Mips::FCOND_UNE;
772 case ISD::SETOLT: return Mips::FCOND_OLT;
774 case ISD::SETOGT: return Mips::FCOND_OGT;
776 case ISD::SETOLE: return Mips::FCOND_OLE;
778 case ISD::SETOGE: return Mips::FCOND_OGE;
779 case ISD::SETULT: return Mips::FCOND_ULT;
780 case ISD::SETULE: return Mips::FCOND_ULE;
781 case ISD::SETUGT: return Mips::FCOND_UGT;
782 case ISD::SETUGE: return Mips::FCOND_UGE;
783 case ISD::SETUO: return Mips::FCOND_UN;
784 case ISD::SETO: return Mips::FCOND_OR;
786 case ISD::SETONE: return Mips::FCOND_ONE;
787 case ISD::SETUEQ: return Mips::FCOND_UEQ;
792 // Returns true if condition code has to be inverted.
793 static bool InvertFPCondCode(Mips::CondCode CC) {
794 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
797 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
798 "Illegal Condition Code");
803 // Creates and returns an FPCmp node from a setcc node.
804 // Returns Op if setcc is not a floating point comparison.
805 static SDValue CreateFPCmp(SelectionDAG &DAG, const SDValue &Op) {
806 // must be a SETCC node
807 if (Op.getOpcode() != ISD::SETCC)
810 SDValue LHS = Op.getOperand(0);
812 if (!LHS.getValueType().isFloatingPoint())
815 SDValue RHS = Op.getOperand(1);
816 DebugLoc dl = Op.getDebugLoc();
818 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
819 // node if necessary.
820 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
822 return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
823 DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
826 // Creates and returns a CMovFPT/F node.
827 static SDValue CreateCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
828 SDValue False, DebugLoc DL) {
829 bool invert = InvertFPCondCode((Mips::CondCode)
830 cast<ConstantSDNode>(Cond.getOperand(2))
833 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
834 True.getValueType(), True, False, Cond);
837 static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
838 TargetLowering::DAGCombinerInfo &DCI,
839 const MipsSubtarget *Subtarget) {
840 if (DCI.isBeforeLegalizeOps())
843 SDValue SetCC = N->getOperand(0);
845 if ((SetCC.getOpcode() != ISD::SETCC) ||
846 !SetCC.getOperand(0).getValueType().isInteger())
849 SDValue False = N->getOperand(2);
850 EVT FalseTy = False.getValueType();
852 if (!FalseTy.isInteger())
855 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
857 if (!CN || CN->getZExtValue())
860 const DebugLoc DL = N->getDebugLoc();
861 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
862 SDValue True = N->getOperand(1);
864 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
865 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
867 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
870 static SDValue PerformANDCombine(SDNode *N, SelectionDAG &DAG,
871 TargetLowering::DAGCombinerInfo &DCI,
872 const MipsSubtarget *Subtarget) {
873 // Pattern match EXT.
874 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
875 // => ext $dst, $src, size, pos
876 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
879 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
880 unsigned ShiftRightOpc = ShiftRight.getOpcode();
882 // Op's first operand must be a shift right.
883 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
886 // The second operand of the shift must be an immediate.
888 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
891 uint64_t Pos = CN->getZExtValue();
892 uint64_t SMPos, SMSize;
894 // Op's second operand must be a shifted mask.
895 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
896 !IsShiftedMask(CN->getZExtValue(), SMPos, SMSize))
899 // Return if the shifted mask does not start at bit 0 or the sum of its size
900 // and Pos exceeds the word's size.
901 EVT ValTy = N->getValueType(0);
902 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
905 return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
906 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
907 DAG.getConstant(SMSize, MVT::i32));
910 static SDValue PerformORCombine(SDNode *N, SelectionDAG &DAG,
911 TargetLowering::DAGCombinerInfo &DCI,
912 const MipsSubtarget *Subtarget) {
913 // Pattern match INS.
914 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
915 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
916 // => ins $dst, $src, size, pos, $src1
917 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
920 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
921 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
924 // See if Op's first operand matches (and $src1 , mask0).
925 if (And0.getOpcode() != ISD::AND)
928 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
929 !IsShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
932 // See if Op's second operand matches (and (shl $src, pos), mask1).
933 if (And1.getOpcode() != ISD::AND)
936 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
937 !IsShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
940 // The shift masks must have the same position and size.
941 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
944 SDValue Shl = And1.getOperand(0);
945 if (Shl.getOpcode() != ISD::SHL)
948 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
951 unsigned Shamt = CN->getZExtValue();
953 // Return if the shift amount and the first bit position of mask are not the
955 EVT ValTy = N->getValueType(0);
956 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
959 return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
960 DAG.getConstant(SMPos0, MVT::i32),
961 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
964 static SDValue PerformADDCombine(SDNode *N, SelectionDAG &DAG,
965 TargetLowering::DAGCombinerInfo &DCI,
966 const MipsSubtarget *Subtarget) {
967 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
969 if (DCI.isBeforeLegalizeOps())
972 SDValue Add = N->getOperand(1);
974 if (Add.getOpcode() != ISD::ADD)
977 SDValue Lo = Add.getOperand(1);
979 if ((Lo.getOpcode() != MipsISD::Lo) ||
980 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
983 EVT ValTy = N->getValueType(0);
984 DebugLoc DL = N->getDebugLoc();
986 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
988 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
991 SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
993 SelectionDAG &DAG = DCI.DAG;
994 unsigned opc = N->getOpcode();
999 return PerformADDECombine(N, DAG, DCI, Subtarget);
1001 return PerformSUBECombine(N, DAG, DCI, Subtarget);
1004 return PerformDivRemCombine(N, DAG, DCI, Subtarget);
1006 return PerformSELECTCombine(N, DAG, DCI, Subtarget);
1008 return PerformANDCombine(N, DAG, DCI, Subtarget);
1010 return PerformORCombine(N, DAG, DCI, Subtarget);
1012 return PerformADDCombine(N, DAG, DCI, Subtarget);
1019 MipsTargetLowering::LowerOperationWrapper(SDNode *N,
1020 SmallVectorImpl<SDValue> &Results,
1021 SelectionDAG &DAG) const {
1022 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
1024 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
1025 Results.push_back(Res.getValue(I));
1029 MipsTargetLowering::ReplaceNodeResults(SDNode *N,
1030 SmallVectorImpl<SDValue> &Results,
1031 SelectionDAG &DAG) const {
1032 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
1034 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
1035 Results.push_back(Res.getValue(I));
1038 SDValue MipsTargetLowering::
1039 LowerOperation(SDValue Op, SelectionDAG &DAG) const
1041 switch (Op.getOpcode())
1043 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
1044 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
1045 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1046 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
1047 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
1048 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
1049 case ISD::SELECT: return LowerSELECT(Op, DAG);
1050 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1051 case ISD::SETCC: return LowerSETCC(Op, DAG);
1052 case ISD::VASTART: return LowerVASTART(Op, DAG);
1053 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
1054 case ISD::FABS: return LowerFABS(Op, DAG);
1055 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
1056 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1057 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
1058 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op, DAG);
1059 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG);
1060 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
1061 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG, true);
1062 case ISD::SRL_PARTS: return LowerShiftRightParts(Op, DAG, false);
1063 case ISD::LOAD: return LowerLOAD(Op, DAG);
1064 case ISD::STORE: return LowerSTORE(Op, DAG);
1065 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1066 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
1067 case ISD::ADD: return LowerADD(Op, DAG);
1072 //===----------------------------------------------------------------------===//
1073 // Lower helper functions
1074 //===----------------------------------------------------------------------===//
1076 // AddLiveIn - This helper function adds the specified physical register to the
1077 // MachineFunction as a live in value. It also creates a corresponding
1078 // virtual register for it.
1080 AddLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
1082 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1083 MF.getRegInfo().addLiveIn(PReg, VReg);
1087 // Get fp branch code (not opcode) from condition code.
1088 static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
1089 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
1090 return Mips::BRANCH_T;
1092 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
1093 "Invalid CondCode.");
1095 return Mips::BRANCH_F;
1099 static MachineBasicBlock* ExpandCondMov(MachineInstr *MI, MachineBasicBlock *BB,
1101 const MipsSubtarget *Subtarget,
1102 const TargetInstrInfo *TII,
1103 bool isFPCmp, unsigned Opc) {
1104 // There is no need to expand CMov instructions if target has
1105 // conditional moves.
1106 if (Subtarget->hasCondMov())
1109 // To "insert" a SELECT_CC instruction, we actually have to insert the
1110 // diamond control-flow pattern. The incoming instruction knows the
1111 // destination vreg to set, the condition code register to branch on, the
1112 // true/false values to select between, and a branch opcode to use.
1113 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1114 MachineFunction::iterator It = BB;
1121 // bNE r1, r0, copy1MBB
1122 // fallthrough --> copy0MBB
1123 MachineBasicBlock *thisMBB = BB;
1124 MachineFunction *F = BB->getParent();
1125 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1126 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1127 F->insert(It, copy0MBB);
1128 F->insert(It, sinkMBB);
1130 // Transfer the remainder of BB and its successor edges to sinkMBB.
1131 sinkMBB->splice(sinkMBB->begin(), BB,
1132 llvm::next(MachineBasicBlock::iterator(MI)),
1134 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1136 // Next, add the true and fallthrough blocks as its successors.
1137 BB->addSuccessor(copy0MBB);
1138 BB->addSuccessor(sinkMBB);
1140 // Emit the right instruction according to the type of the operands compared
1142 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
1144 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
1145 .addReg(Mips::ZERO).addMBB(sinkMBB);
1148 // %FalseValue = ...
1149 // # fallthrough to sinkMBB
1152 // Update machine-CFG edges
1153 BB->addSuccessor(sinkMBB);
1156 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
1161 BuildMI(*BB, BB->begin(), dl,
1162 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1163 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
1164 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
1166 BuildMI(*BB, BB->begin(), dl,
1167 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1168 .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
1169 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
1171 MI->eraseFromParent(); // The pseudo instruction is gone now.
1177 MipsTargetLowering::EmitBPOSGE32(MachineInstr *MI, MachineBasicBlock *BB) const{
1179 // bposge32_pseudo $vr0
1189 // $vr0 = phi($vr2, $fbb, $vr1, $tbb)
1191 MachineRegisterInfo &RegInfo = BB->getParent()->getRegInfo();
1192 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1193 const TargetRegisterClass *RC = &Mips::CPURegsRegClass;
1194 DebugLoc DL = MI->getDebugLoc();
1195 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1196 MachineFunction::iterator It = llvm::next(MachineFunction::iterator(BB));
1197 MachineFunction *F = BB->getParent();
1198 MachineBasicBlock *FBB = F->CreateMachineBasicBlock(LLVM_BB);
1199 MachineBasicBlock *TBB = F->CreateMachineBasicBlock(LLVM_BB);
1200 MachineBasicBlock *Sink = F->CreateMachineBasicBlock(LLVM_BB);
1203 F->insert(It, Sink);
1205 // Transfer the remainder of BB and its successor edges to Sink.
1206 Sink->splice(Sink->begin(), BB, llvm::next(MachineBasicBlock::iterator(MI)),
1208 Sink->transferSuccessorsAndUpdatePHIs(BB);
1211 BB->addSuccessor(FBB);
1212 BB->addSuccessor(TBB);
1213 FBB->addSuccessor(Sink);
1214 TBB->addSuccessor(Sink);
1216 // Insert the real bposge32 instruction to $BB.
1217 BuildMI(BB, DL, TII->get(Mips::BPOSGE32)).addMBB(TBB);
1220 unsigned VR2 = RegInfo.createVirtualRegister(RC);
1221 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::ADDiu), VR2)
1222 .addReg(Mips::ZERO).addImm(0);
1223 BuildMI(*FBB, FBB->end(), DL, TII->get(Mips::B)).addMBB(Sink);
1226 unsigned VR1 = RegInfo.createVirtualRegister(RC);
1227 BuildMI(*TBB, TBB->end(), DL, TII->get(Mips::ADDiu), VR1)
1228 .addReg(Mips::ZERO).addImm(1);
1230 // Insert phi function to $Sink.
1231 BuildMI(*Sink, Sink->begin(), DL, TII->get(Mips::PHI),
1232 MI->getOperand(0).getReg())
1233 .addReg(VR2).addMBB(FBB).addReg(VR1).addMBB(TBB);
1235 MI->eraseFromParent(); // The pseudo instruction is gone now.
1239 MachineBasicBlock *MipsTargetLowering::EmitSel16(unsigned Opc, MachineInstr *MI,
1240 MachineBasicBlock *BB) const {
1241 if (DontExpandCondPseudos16)
1243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1244 DebugLoc dl = MI->getDebugLoc();
1245 // To "insert" a SELECT_CC instruction, we actually have to insert the
1246 // diamond control-flow pattern. The incoming instruction knows the
1247 // destination vreg to set, the condition code register to branch on, the
1248 // true/false values to select between, and a branch opcode to use.
1249 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1250 MachineFunction::iterator It = BB;
1257 // bNE r1, r0, copy1MBB
1258 // fallthrough --> copy0MBB
1259 MachineBasicBlock *thisMBB = BB;
1260 MachineFunction *F = BB->getParent();
1261 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1262 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1263 F->insert(It, copy0MBB);
1264 F->insert(It, sinkMBB);
1266 // Transfer the remainder of BB and its successor edges to sinkMBB.
1267 sinkMBB->splice(sinkMBB->begin(), BB,
1268 llvm::next(MachineBasicBlock::iterator(MI)),
1270 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1272 // Next, add the true and fallthrough blocks as its successors.
1273 BB->addSuccessor(copy0MBB);
1274 BB->addSuccessor(sinkMBB);
1276 BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(3).getReg())
1280 // %FalseValue = ...
1281 // # fallthrough to sinkMBB
1284 // Update machine-CFG edges
1285 BB->addSuccessor(sinkMBB);
1288 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
1292 BuildMI(*BB, BB->begin(), dl,
1293 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1294 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
1295 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
1297 MI->eraseFromParent(); // The pseudo instruction is gone now.
1302 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1303 MachineBasicBlock *BB) const {
1304 switch (MI->getOpcode()) {
1306 llvm_unreachable("Unexpected instr type to insert");
1307 case Mips::ATOMIC_LOAD_ADD_I8:
1308 case Mips::ATOMIC_LOAD_ADD_I8_P8:
1309 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
1310 case Mips::ATOMIC_LOAD_ADD_I16:
1311 case Mips::ATOMIC_LOAD_ADD_I16_P8:
1312 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
1313 case Mips::ATOMIC_LOAD_ADD_I32:
1314 case Mips::ATOMIC_LOAD_ADD_I32_P8:
1315 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
1316 case Mips::ATOMIC_LOAD_ADD_I64:
1317 case Mips::ATOMIC_LOAD_ADD_I64_P8:
1318 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
1320 case Mips::ATOMIC_LOAD_AND_I8:
1321 case Mips::ATOMIC_LOAD_AND_I8_P8:
1322 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
1323 case Mips::ATOMIC_LOAD_AND_I16:
1324 case Mips::ATOMIC_LOAD_AND_I16_P8:
1325 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
1326 case Mips::ATOMIC_LOAD_AND_I32:
1327 case Mips::ATOMIC_LOAD_AND_I32_P8:
1328 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
1329 case Mips::ATOMIC_LOAD_AND_I64:
1330 case Mips::ATOMIC_LOAD_AND_I64_P8:
1331 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
1333 case Mips::ATOMIC_LOAD_OR_I8:
1334 case Mips::ATOMIC_LOAD_OR_I8_P8:
1335 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
1336 case Mips::ATOMIC_LOAD_OR_I16:
1337 case Mips::ATOMIC_LOAD_OR_I16_P8:
1338 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
1339 case Mips::ATOMIC_LOAD_OR_I32:
1340 case Mips::ATOMIC_LOAD_OR_I32_P8:
1341 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
1342 case Mips::ATOMIC_LOAD_OR_I64:
1343 case Mips::ATOMIC_LOAD_OR_I64_P8:
1344 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
1346 case Mips::ATOMIC_LOAD_XOR_I8:
1347 case Mips::ATOMIC_LOAD_XOR_I8_P8:
1348 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
1349 case Mips::ATOMIC_LOAD_XOR_I16:
1350 case Mips::ATOMIC_LOAD_XOR_I16_P8:
1351 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
1352 case Mips::ATOMIC_LOAD_XOR_I32:
1353 case Mips::ATOMIC_LOAD_XOR_I32_P8:
1354 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
1355 case Mips::ATOMIC_LOAD_XOR_I64:
1356 case Mips::ATOMIC_LOAD_XOR_I64_P8:
1357 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
1359 case Mips::ATOMIC_LOAD_NAND_I8:
1360 case Mips::ATOMIC_LOAD_NAND_I8_P8:
1361 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
1362 case Mips::ATOMIC_LOAD_NAND_I16:
1363 case Mips::ATOMIC_LOAD_NAND_I16_P8:
1364 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
1365 case Mips::ATOMIC_LOAD_NAND_I32:
1366 case Mips::ATOMIC_LOAD_NAND_I32_P8:
1367 return EmitAtomicBinary(MI, BB, 4, 0, true);
1368 case Mips::ATOMIC_LOAD_NAND_I64:
1369 case Mips::ATOMIC_LOAD_NAND_I64_P8:
1370 return EmitAtomicBinary(MI, BB, 8, 0, true);
1372 case Mips::ATOMIC_LOAD_SUB_I8:
1373 case Mips::ATOMIC_LOAD_SUB_I8_P8:
1374 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
1375 case Mips::ATOMIC_LOAD_SUB_I16:
1376 case Mips::ATOMIC_LOAD_SUB_I16_P8:
1377 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
1378 case Mips::ATOMIC_LOAD_SUB_I32:
1379 case Mips::ATOMIC_LOAD_SUB_I32_P8:
1380 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
1381 case Mips::ATOMIC_LOAD_SUB_I64:
1382 case Mips::ATOMIC_LOAD_SUB_I64_P8:
1383 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
1385 case Mips::ATOMIC_SWAP_I8:
1386 case Mips::ATOMIC_SWAP_I8_P8:
1387 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
1388 case Mips::ATOMIC_SWAP_I16:
1389 case Mips::ATOMIC_SWAP_I16_P8:
1390 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
1391 case Mips::ATOMIC_SWAP_I32:
1392 case Mips::ATOMIC_SWAP_I32_P8:
1393 return EmitAtomicBinary(MI, BB, 4, 0);
1394 case Mips::ATOMIC_SWAP_I64:
1395 case Mips::ATOMIC_SWAP_I64_P8:
1396 return EmitAtomicBinary(MI, BB, 8, 0);
1398 case Mips::ATOMIC_CMP_SWAP_I8:
1399 case Mips::ATOMIC_CMP_SWAP_I8_P8:
1400 return EmitAtomicCmpSwapPartword(MI, BB, 1);
1401 case Mips::ATOMIC_CMP_SWAP_I16:
1402 case Mips::ATOMIC_CMP_SWAP_I16_P8:
1403 return EmitAtomicCmpSwapPartword(MI, BB, 2);
1404 case Mips::ATOMIC_CMP_SWAP_I32:
1405 case Mips::ATOMIC_CMP_SWAP_I32_P8:
1406 return EmitAtomicCmpSwap(MI, BB, 4);
1407 case Mips::ATOMIC_CMP_SWAP_I64:
1408 case Mips::ATOMIC_CMP_SWAP_I64_P8:
1409 return EmitAtomicCmpSwap(MI, BB, 8);
1410 case Mips::BPOSGE32_PSEUDO:
1411 return EmitBPOSGE32(MI, BB);
1413 return EmitSel16(Mips::BeqzRxImm16, MI, BB);
1415 return EmitSel16(Mips::BnezRxImm16, MI, BB);
1419 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1420 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1422 MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
1423 unsigned Size, unsigned BinOpcode,
1425 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
1427 MachineFunction *MF = BB->getParent();
1428 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1429 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1430 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1431 DebugLoc dl = MI->getDebugLoc();
1432 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1435 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1436 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1443 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1444 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1447 ZERO = Mips::ZERO_64;
1451 unsigned OldVal = MI->getOperand(0).getReg();
1452 unsigned Ptr = MI->getOperand(1).getReg();
1453 unsigned Incr = MI->getOperand(2).getReg();
1455 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1456 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1457 unsigned Success = RegInfo.createVirtualRegister(RC);
1459 // insert new blocks after the current block
1460 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1461 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1462 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1463 MachineFunction::iterator It = BB;
1465 MF->insert(It, loopMBB);
1466 MF->insert(It, exitMBB);
1468 // Transfer the remainder of BB and its successor edges to exitMBB.
1469 exitMBB->splice(exitMBB->begin(), BB,
1470 llvm::next(MachineBasicBlock::iterator(MI)),
1472 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1476 // fallthrough --> loopMBB
1477 BB->addSuccessor(loopMBB);
1478 loopMBB->addSuccessor(loopMBB);
1479 loopMBB->addSuccessor(exitMBB);
1482 // ll oldval, 0(ptr)
1483 // <binop> storeval, oldval, incr
1484 // sc success, storeval, 0(ptr)
1485 // beq success, $0, loopMBB
1487 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
1489 // and andres, oldval, incr
1490 // nor storeval, $0, andres
1491 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1492 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
1493 } else if (BinOpcode) {
1494 // <binop> storeval, oldval, incr
1495 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
1499 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1500 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
1502 MI->eraseFromParent(); // The instruction is gone now.
1508 MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
1509 MachineBasicBlock *BB,
1510 unsigned Size, unsigned BinOpcode,
1512 assert((Size == 1 || Size == 2) &&
1513 "Unsupported size for EmitAtomicBinaryPartial.");
1515 MachineFunction *MF = BB->getParent();
1516 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1517 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1518 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1519 DebugLoc dl = MI->getDebugLoc();
1520 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1521 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1523 unsigned Dest = MI->getOperand(0).getReg();
1524 unsigned Ptr = MI->getOperand(1).getReg();
1525 unsigned Incr = MI->getOperand(2).getReg();
1527 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1528 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1529 unsigned Mask = RegInfo.createVirtualRegister(RC);
1530 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1531 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1532 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1533 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
1534 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1535 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1536 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1537 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1538 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
1539 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1540 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1541 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1542 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1543 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1544 unsigned Success = RegInfo.createVirtualRegister(RC);
1546 // insert new blocks after the current block
1547 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1548 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1549 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1550 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1551 MachineFunction::iterator It = BB;
1553 MF->insert(It, loopMBB);
1554 MF->insert(It, sinkMBB);
1555 MF->insert(It, exitMBB);
1557 // Transfer the remainder of BB and its successor edges to exitMBB.
1558 exitMBB->splice(exitMBB->begin(), BB,
1559 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
1560 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1562 BB->addSuccessor(loopMBB);
1563 loopMBB->addSuccessor(loopMBB);
1564 loopMBB->addSuccessor(sinkMBB);
1565 sinkMBB->addSuccessor(exitMBB);
1568 // addiu masklsb2,$0,-4 # 0xfffffffc
1569 // and alignedaddr,ptr,masklsb2
1570 // andi ptrlsb2,ptr,3
1571 // sll shiftamt,ptrlsb2,3
1572 // ori maskupper,$0,255 # 0xff
1573 // sll mask,maskupper,shiftamt
1574 // nor mask2,$0,mask
1575 // sll incr2,incr,shiftamt
1577 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1578 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1579 .addReg(Mips::ZERO).addImm(-4);
1580 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1581 .addReg(Ptr).addReg(MaskLSB2);
1582 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1583 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1584 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1585 .addReg(Mips::ZERO).addImm(MaskImm);
1586 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1587 .addReg(ShiftAmt).addReg(MaskUpper);
1588 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1589 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
1591 // atomic.load.binop
1593 // ll oldval,0(alignedaddr)
1594 // binop binopres,oldval,incr2
1595 // and newval,binopres,mask
1596 // and maskedoldval0,oldval,mask2
1597 // or storeval,maskedoldval0,newval
1598 // sc success,storeval,0(alignedaddr)
1599 // beq success,$0,loopMBB
1603 // ll oldval,0(alignedaddr)
1604 // and newval,incr2,mask
1605 // and maskedoldval0,oldval,mask2
1606 // or storeval,maskedoldval0,newval
1607 // sc success,storeval,0(alignedaddr)
1608 // beq success,$0,loopMBB
1611 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1613 // and andres, oldval, incr2
1614 // nor binopres, $0, andres
1615 // and newval, binopres, mask
1616 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1617 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1618 .addReg(Mips::ZERO).addReg(AndRes);
1619 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1620 } else if (BinOpcode) {
1621 // <binop> binopres, oldval, incr2
1622 // and newval, binopres, mask
1623 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1624 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1625 } else {// atomic.swap
1626 // and newval, incr2, mask
1627 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
1630 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1631 .addReg(OldVal).addReg(Mask2);
1632 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1633 .addReg(MaskedOldVal0).addReg(NewVal);
1634 BuildMI(BB, dl, TII->get(SC), Success)
1635 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1636 BuildMI(BB, dl, TII->get(Mips::BEQ))
1637 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
1640 // and maskedoldval1,oldval,mask
1641 // srl srlres,maskedoldval1,shiftamt
1642 // sll sllres,srlres,24
1643 // sra dest,sllres,24
1645 int64_t ShiftImm = (Size == 1) ? 24 : 16;
1647 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1648 .addReg(OldVal).addReg(Mask);
1649 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1650 .addReg(ShiftAmt).addReg(MaskedOldVal1);
1651 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1652 .addReg(SrlRes).addImm(ShiftImm);
1653 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
1654 .addReg(SllRes).addImm(ShiftImm);
1656 MI->eraseFromParent(); // The instruction is gone now.
1662 MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
1663 MachineBasicBlock *BB,
1664 unsigned Size) const {
1665 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1667 MachineFunction *MF = BB->getParent();
1668 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1669 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1670 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1671 DebugLoc dl = MI->getDebugLoc();
1672 unsigned LL, SC, ZERO, BNE, BEQ;
1675 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1676 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1682 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1683 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1684 ZERO = Mips::ZERO_64;
1689 unsigned Dest = MI->getOperand(0).getReg();
1690 unsigned Ptr = MI->getOperand(1).getReg();
1691 unsigned OldVal = MI->getOperand(2).getReg();
1692 unsigned NewVal = MI->getOperand(3).getReg();
1694 unsigned Success = RegInfo.createVirtualRegister(RC);
1696 // insert new blocks after the current block
1697 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1698 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1699 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1700 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1701 MachineFunction::iterator It = BB;
1703 MF->insert(It, loop1MBB);
1704 MF->insert(It, loop2MBB);
1705 MF->insert(It, exitMBB);
1707 // Transfer the remainder of BB and its successor edges to exitMBB.
1708 exitMBB->splice(exitMBB->begin(), BB,
1709 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
1710 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1714 // fallthrough --> loop1MBB
1715 BB->addSuccessor(loop1MBB);
1716 loop1MBB->addSuccessor(exitMBB);
1717 loop1MBB->addSuccessor(loop2MBB);
1718 loop2MBB->addSuccessor(loop1MBB);
1719 loop2MBB->addSuccessor(exitMBB);
1723 // bne dest, oldval, exitMBB
1725 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1726 BuildMI(BB, dl, TII->get(BNE))
1727 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1730 // sc success, newval, 0(ptr)
1731 // beq success, $0, loop1MBB
1733 BuildMI(BB, dl, TII->get(SC), Success)
1734 .addReg(NewVal).addReg(Ptr).addImm(0);
1735 BuildMI(BB, dl, TII->get(BEQ))
1736 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1738 MI->eraseFromParent(); // The instruction is gone now.
1744 MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
1745 MachineBasicBlock *BB,
1746 unsigned Size) const {
1747 assert((Size == 1 || Size == 2) &&
1748 "Unsupported size for EmitAtomicCmpSwapPartial.");
1750 MachineFunction *MF = BB->getParent();
1751 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1752 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1753 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1754 DebugLoc dl = MI->getDebugLoc();
1755 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1756 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1758 unsigned Dest = MI->getOperand(0).getReg();
1759 unsigned Ptr = MI->getOperand(1).getReg();
1760 unsigned CmpVal = MI->getOperand(2).getReg();
1761 unsigned NewVal = MI->getOperand(3).getReg();
1763 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1764 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1765 unsigned Mask = RegInfo.createVirtualRegister(RC);
1766 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1767 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1768 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1769 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1770 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1771 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1772 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1773 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1774 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1775 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1776 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1777 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1778 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1779 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1780 unsigned Success = RegInfo.createVirtualRegister(RC);
1782 // insert new blocks after the current block
1783 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1784 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1785 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1786 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1787 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1788 MachineFunction::iterator It = BB;
1790 MF->insert(It, loop1MBB);
1791 MF->insert(It, loop2MBB);
1792 MF->insert(It, sinkMBB);
1793 MF->insert(It, exitMBB);
1795 // Transfer the remainder of BB and its successor edges to exitMBB.
1796 exitMBB->splice(exitMBB->begin(), BB,
1797 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
1798 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1800 BB->addSuccessor(loop1MBB);
1801 loop1MBB->addSuccessor(sinkMBB);
1802 loop1MBB->addSuccessor(loop2MBB);
1803 loop2MBB->addSuccessor(loop1MBB);
1804 loop2MBB->addSuccessor(sinkMBB);
1805 sinkMBB->addSuccessor(exitMBB);
1807 // FIXME: computation of newval2 can be moved to loop2MBB.
1809 // addiu masklsb2,$0,-4 # 0xfffffffc
1810 // and alignedaddr,ptr,masklsb2
1811 // andi ptrlsb2,ptr,3
1812 // sll shiftamt,ptrlsb2,3
1813 // ori maskupper,$0,255 # 0xff
1814 // sll mask,maskupper,shiftamt
1815 // nor mask2,$0,mask
1816 // andi maskedcmpval,cmpval,255
1817 // sll shiftedcmpval,maskedcmpval,shiftamt
1818 // andi maskednewval,newval,255
1819 // sll shiftednewval,maskednewval,shiftamt
1820 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1821 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1822 .addReg(Mips::ZERO).addImm(-4);
1823 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1824 .addReg(Ptr).addReg(MaskLSB2);
1825 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1826 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1827 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1828 .addReg(Mips::ZERO).addImm(MaskImm);
1829 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1830 .addReg(ShiftAmt).addReg(MaskUpper);
1831 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1832 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
1833 .addReg(CmpVal).addImm(MaskImm);
1834 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
1835 .addReg(ShiftAmt).addReg(MaskedCmpVal);
1836 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
1837 .addReg(NewVal).addImm(MaskImm);
1838 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
1839 .addReg(ShiftAmt).addReg(MaskedNewVal);
1842 // ll oldval,0(alginedaddr)
1843 // and maskedoldval0,oldval,mask
1844 // bne maskedoldval0,shiftedcmpval,sinkMBB
1846 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1847 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1848 .addReg(OldVal).addReg(Mask);
1849 BuildMI(BB, dl, TII->get(Mips::BNE))
1850 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1853 // and maskedoldval1,oldval,mask2
1854 // or storeval,maskedoldval1,shiftednewval
1855 // sc success,storeval,0(alignedaddr)
1856 // beq success,$0,loop1MBB
1858 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1859 .addReg(OldVal).addReg(Mask2);
1860 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1861 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1862 BuildMI(BB, dl, TII->get(SC), Success)
1863 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1864 BuildMI(BB, dl, TII->get(Mips::BEQ))
1865 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1868 // srl srlres,maskedoldval0,shiftamt
1869 // sll sllres,srlres,24
1870 // sra dest,sllres,24
1872 int64_t ShiftImm = (Size == 1) ? 24 : 16;
1874 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1875 .addReg(ShiftAmt).addReg(MaskedOldVal0);
1876 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1877 .addReg(SrlRes).addImm(ShiftImm);
1878 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
1879 .addReg(SllRes).addImm(ShiftImm);
1881 MI->eraseFromParent(); // The instruction is gone now.
1886 //===----------------------------------------------------------------------===//
1887 // Misc Lower Operation implementation
1888 //===----------------------------------------------------------------------===//
1889 SDValue MipsTargetLowering::
1890 LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
1892 // The first operand is the chain, the second is the condition, the third is
1893 // the block to branch to if the condition is true.
1894 SDValue Chain = Op.getOperand(0);
1895 SDValue Dest = Op.getOperand(2);
1896 DebugLoc dl = Op.getDebugLoc();
1898 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1900 // Return if flag is not set by a floating point comparison.
1901 if (CondRes.getOpcode() != MipsISD::FPCmp)
1904 SDValue CCNode = CondRes.getOperand(2);
1906 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1907 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
1909 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
1913 SDValue MipsTargetLowering::
1914 LowerSELECT(SDValue Op, SelectionDAG &DAG) const
1916 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
1918 // Return if flag is not set by a floating point comparison.
1919 if (Cond.getOpcode() != MipsISD::FPCmp)
1922 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1926 SDValue MipsTargetLowering::
1927 LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
1929 DebugLoc DL = Op.getDebugLoc();
1930 EVT Ty = Op.getOperand(0).getValueType();
1931 SDValue Cond = DAG.getNode(ISD::SETCC, DL, getSetCCResultType(Ty),
1932 Op.getOperand(0), Op.getOperand(1),
1935 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1939 SDValue MipsTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1940 SDValue Cond = CreateFPCmp(DAG, Op);
1942 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1943 "Floating point operand expected.");
1945 SDValue True = DAG.getConstant(1, MVT::i32);
1946 SDValue False = DAG.getConstant(0, MVT::i32);
1948 return CreateCMovFP(DAG, Cond, True, False, Op.getDebugLoc());
1951 SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1952 SelectionDAG &DAG) const {
1953 // FIXME there isn't actually debug info here
1954 DebugLoc dl = Op.getDebugLoc();
1955 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1957 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
1958 const MipsTargetObjectFile &TLOF =
1959 (const MipsTargetObjectFile&)getObjFileLowering();
1961 // %gp_rel relocation
1962 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1963 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1965 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl,
1966 DAG.getVTList(MVT::i32), &GA, 1);
1967 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
1968 return DAG.getNode(ISD::ADD, dl, MVT::i32, GPReg, GPRelNode);
1971 // %hi/%lo relocation
1972 return getAddrNonPIC(Op, DAG);
1975 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1976 return getAddrLocal(Op, DAG, HasMips64);
1979 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
1980 MipsII::MO_GOT_LO16);
1982 return getAddrGlobal(Op, DAG,
1983 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
1986 SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1987 SelectionDAG &DAG) const {
1988 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1989 return getAddrNonPIC(Op, DAG);
1991 return getAddrLocal(Op, DAG, HasMips64);
1994 SDValue MipsTargetLowering::
1995 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
1997 // If the relocation model is PIC, use the General Dynamic TLS Model or
1998 // Local Dynamic TLS model, otherwise use the Initial Exec or
1999 // Local Exec TLS Model.
2001 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2002 DebugLoc dl = GA->getDebugLoc();
2003 const GlobalValue *GV = GA->getGlobal();
2004 EVT PtrVT = getPointerTy();
2006 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2008 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
2009 // General Dynamic and Local Dynamic TLS Model.
2010 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2013 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
2014 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT,
2015 GetGlobalReg(DAG, PtrVT), TGA);
2016 unsigned PtrSize = PtrVT.getSizeInBits();
2017 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2019 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
2023 Entry.Node = Argument;
2025 Args.push_back(Entry);
2027 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
2028 false, false, false, false, 0, CallingConv::C,
2029 /*isTailCall=*/false, /*doesNotRet=*/false,
2030 /*isReturnValueUsed=*/true,
2031 TlsGetAddr, Args, DAG, dl);
2032 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2034 SDValue Ret = CallResult.first;
2036 if (model != TLSModel::LocalDynamic)
2039 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2040 MipsII::MO_DTPREL_HI);
2041 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
2042 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2043 MipsII::MO_DTPREL_LO);
2044 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
2045 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
2046 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
2050 if (model == TLSModel::InitialExec) {
2051 // Initial Exec TLS Model
2052 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2053 MipsII::MO_GOTTPREL);
2054 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, GetGlobalReg(DAG, PtrVT),
2056 Offset = DAG.getLoad(PtrVT, dl,
2057 DAG.getEntryNode(), TGA, MachinePointerInfo(),
2058 false, false, false, 0);
2060 // Local Exec TLS Model
2061 assert(model == TLSModel::LocalExec);
2062 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2063 MipsII::MO_TPREL_HI);
2064 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2065 MipsII::MO_TPREL_LO);
2066 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
2067 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
2068 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
2071 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
2072 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
2075 SDValue MipsTargetLowering::
2076 LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
2078 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
2079 return getAddrNonPIC(Op, DAG);
2081 return getAddrLocal(Op, DAG, HasMips64);
2084 SDValue MipsTargetLowering::
2085 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
2087 // gp_rel relocation
2088 // FIXME: we should reference the constant pool using small data sections,
2089 // but the asm printer currently doesn't support this feature without
2090 // hacking it. This feature should come soon so we can uncomment the
2092 //if (IsInSmallSection(C->getType())) {
2093 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
2094 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
2095 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
2097 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
2098 return getAddrNonPIC(Op, DAG);
2100 return getAddrLocal(Op, DAG, HasMips64);
2103 SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
2104 MachineFunction &MF = DAG.getMachineFunction();
2105 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2107 DebugLoc dl = Op.getDebugLoc();
2108 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2111 // vastart just stores the address of the VarArgsFrameIndex slot into the
2112 // memory location argument.
2113 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2114 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
2115 MachinePointerInfo(SV), false, false, 0);
2118 static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2119 EVT TyX = Op.getOperand(0).getValueType();
2120 EVT TyY = Op.getOperand(1).getValueType();
2121 SDValue Const1 = DAG.getConstant(1, MVT::i32);
2122 SDValue Const31 = DAG.getConstant(31, MVT::i32);
2123 DebugLoc DL = Op.getDebugLoc();
2126 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2128 SDValue X = (TyX == MVT::f32) ?
2129 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2130 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2132 SDValue Y = (TyY == MVT::f32) ?
2133 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2134 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2138 // ext E, Y, 31, 1 ; extract bit31 of Y
2139 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2140 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2141 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2144 // srl SrlX, SllX, 1
2146 // sll SllY, SrlX, 31
2147 // or Or, SrlX, SllY
2148 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2149 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2150 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2151 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2152 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2155 if (TyX == MVT::f32)
2156 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2158 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2159 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
2160 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2163 static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2164 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2165 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2166 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
2167 SDValue Const1 = DAG.getConstant(1, MVT::i32);
2168 DebugLoc DL = Op.getDebugLoc();
2170 // Bitcast to integer nodes.
2171 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2172 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
2175 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2176 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2177 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
2178 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
2180 if (WidthX > WidthY)
2181 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2182 else if (WidthY > WidthX)
2183 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
2185 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2186 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
2187 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2190 // (d)sll SllX, X, 1
2191 // (d)srl SrlX, SllX, 1
2192 // (d)srl SrlY, Y, width(Y)-1
2193 // (d)sll SllY, SrlX, width(Y)-1
2194 // or Or, SrlX, SllY
2195 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2196 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2197 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2198 DAG.getConstant(WidthY - 1, MVT::i32));
2200 if (WidthX > WidthY)
2201 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2202 else if (WidthY > WidthX)
2203 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2205 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2206 DAG.getConstant(WidthX - 1, MVT::i32));
2207 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2208 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
2212 MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
2213 if (Subtarget->hasMips64())
2214 return LowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
2216 return LowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
2219 static SDValue LowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2220 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
2221 DebugLoc DL = Op.getDebugLoc();
2223 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2225 SDValue X = (Op.getValueType() == MVT::f32) ?
2226 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2227 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2232 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
2233 DAG.getRegister(Mips::ZERO, MVT::i32),
2234 DAG.getConstant(31, MVT::i32), Const1, X);
2236 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2237 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2240 if (Op.getValueType() == MVT::f32)
2241 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
2243 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2244 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
2245 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2248 static SDValue LowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2249 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
2250 DebugLoc DL = Op.getDebugLoc();
2252 // Bitcast to integer node.
2253 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
2257 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
2258 DAG.getRegister(Mips::ZERO_64, MVT::i64),
2259 DAG.getConstant(63, MVT::i32), Const1, X);
2261 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
2262 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
2265 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
2269 MipsTargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
2270 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
2271 return LowerFABS64(Op, DAG, Subtarget->hasMips32r2());
2273 return LowerFABS32(Op, DAG, Subtarget->hasMips32r2());
2276 SDValue MipsTargetLowering::
2277 LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
2279 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2280 "Frame address can only be determined for current frame.");
2282 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2283 MFI->setFrameAddressIsTaken(true);
2284 EVT VT = Op.getValueType();
2285 DebugLoc dl = Op.getDebugLoc();
2286 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
2287 IsN64 ? Mips::FP_64 : Mips::FP, VT);
2291 SDValue MipsTargetLowering::LowerRETURNADDR(SDValue Op,
2292 SelectionDAG &DAG) const {
2294 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2295 "Return address can be determined only for current frame.");
2297 MachineFunction &MF = DAG.getMachineFunction();
2298 MachineFrameInfo *MFI = MF.getFrameInfo();
2299 MVT VT = Op.getSimpleValueType();
2300 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
2301 MFI->setReturnAddressIsTaken(true);
2303 // Return RA, which contains the return address. Mark it an implicit live-in.
2304 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
2305 return DAG.getCopyFromReg(DAG.getEntryNode(), Op.getDebugLoc(), Reg, VT);
2308 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2309 // generated from __builtin_eh_return (offset, handler)
2310 // The effect of this is to adjust the stack pointer by "offset"
2311 // and then branch to "handler".
2312 SDValue MipsTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
2314 MachineFunction &MF = DAG.getMachineFunction();
2315 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2317 MipsFI->setCallsEhReturn();
2318 SDValue Chain = Op.getOperand(0);
2319 SDValue Offset = Op.getOperand(1);
2320 SDValue Handler = Op.getOperand(2);
2321 DebugLoc DL = Op.getDebugLoc();
2322 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2324 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2325 // EH_RETURN nodes, so that instructions are emitted back-to-back.
2326 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
2327 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
2328 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2329 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2330 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2331 DAG.getRegister(OffsetReg, Ty),
2332 DAG.getRegister(AddrReg, getPointerTy()),
2336 // TODO: set SType according to the desired memory barrier behavior.
2338 MipsTargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const {
2340 DebugLoc dl = Op.getDebugLoc();
2341 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
2342 DAG.getConstant(SType, MVT::i32));
2345 SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
2346 SelectionDAG &DAG) const {
2347 // FIXME: Need pseudo-fence for 'singlethread' fences
2348 // FIXME: Set SType for weaker fences where supported/appropriate.
2350 DebugLoc dl = Op.getDebugLoc();
2351 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
2352 DAG.getConstant(SType, MVT::i32));
2355 SDValue MipsTargetLowering::LowerShiftLeftParts(SDValue Op,
2356 SelectionDAG &DAG) const {
2357 DebugLoc DL = Op.getDebugLoc();
2358 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2359 SDValue Shamt = Op.getOperand(2);
2362 // lo = (shl lo, shamt)
2363 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2366 // hi = (shl lo, shamt[4:0])
2367 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2368 DAG.getConstant(-1, MVT::i32));
2369 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2370 DAG.getConstant(1, MVT::i32));
2371 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2373 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2374 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2375 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2376 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2377 DAG.getConstant(0x20, MVT::i32));
2378 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2379 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
2380 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2382 SDValue Ops[2] = {Lo, Hi};
2383 return DAG.getMergeValues(Ops, 2, DL);
2386 SDValue MipsTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2388 DebugLoc DL = Op.getDebugLoc();
2389 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2390 SDValue Shamt = Op.getOperand(2);
2393 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2395 // hi = (sra hi, shamt)
2397 // hi = (srl hi, shamt)
2400 // lo = (sra hi, shamt[4:0])
2401 // hi = (sra hi, 31)
2403 // lo = (srl hi, shamt[4:0])
2405 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2406 DAG.getConstant(-1, MVT::i32));
2407 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2408 DAG.getConstant(1, MVT::i32));
2409 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2410 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2411 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2412 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2414 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2415 DAG.getConstant(0x20, MVT::i32));
2416 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2417 DAG.getConstant(31, MVT::i32));
2418 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2419 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2420 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2423 SDValue Ops[2] = {Lo, Hi};
2424 return DAG.getMergeValues(Ops, 2, DL);
2427 static SDValue CreateLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2428 SDValue Chain, SDValue Src, unsigned Offset) {
2429 SDValue Ptr = LD->getBasePtr();
2430 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2431 EVT BasePtrVT = Ptr.getValueType();
2432 DebugLoc DL = LD->getDebugLoc();
2433 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2436 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2437 DAG.getConstant(Offset, BasePtrVT));
2439 SDValue Ops[] = { Chain, Ptr, Src };
2440 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2441 LD->getMemOperand());
2444 // Expand an unaligned 32 or 64-bit integer load node.
2445 SDValue MipsTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2446 LoadSDNode *LD = cast<LoadSDNode>(Op);
2447 EVT MemVT = LD->getMemoryVT();
2449 // Return if load is aligned or if MemVT is neither i32 nor i64.
2450 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2451 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2454 bool IsLittle = Subtarget->isLittle();
2455 EVT VT = Op.getValueType();
2456 ISD::LoadExtType ExtType = LD->getExtensionType();
2457 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2459 assert((VT == MVT::i32) || (VT == MVT::i64));
2462 // (set dst, (i64 (load baseptr)))
2464 // (set tmp, (ldl (add baseptr, 7), undef))
2465 // (set dst, (ldr baseptr, tmp))
2466 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2467 SDValue LDL = CreateLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2469 return CreateLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2473 SDValue LWL = CreateLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2475 SDValue LWR = CreateLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2479 // (set dst, (i32 (load baseptr))) or
2480 // (set dst, (i64 (sextload baseptr))) or
2481 // (set dst, (i64 (extload baseptr)))
2483 // (set tmp, (lwl (add baseptr, 3), undef))
2484 // (set dst, (lwr baseptr, tmp))
2485 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2486 (ExtType == ISD::EXTLOAD))
2489 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2492 // (set dst, (i64 (zextload baseptr)))
2494 // (set tmp0, (lwl (add baseptr, 3), undef))
2495 // (set tmp1, (lwr baseptr, tmp0))
2496 // (set tmp2, (shl tmp1, 32))
2497 // (set dst, (srl tmp2, 32))
2498 DebugLoc DL = LD->getDebugLoc();
2499 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2500 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2501 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2502 SDValue Ops[] = { SRL, LWR.getValue(1) };
2503 return DAG.getMergeValues(Ops, 2, DL);
2506 static SDValue CreateStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2507 SDValue Chain, unsigned Offset) {
2508 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2509 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2510 DebugLoc DL = SD->getDebugLoc();
2511 SDVTList VTList = DAG.getVTList(MVT::Other);
2514 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2515 DAG.getConstant(Offset, BasePtrVT));
2517 SDValue Ops[] = { Chain, Value, Ptr };
2518 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2519 SD->getMemOperand());
2522 // Expand an unaligned 32 or 64-bit integer store node.
2523 SDValue MipsTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2524 StoreSDNode *SD = cast<StoreSDNode>(Op);
2525 EVT MemVT = SD->getMemoryVT();
2527 // Return if store is aligned or if MemVT is neither i32 nor i64.
2528 if ((SD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2529 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2532 bool IsLittle = Subtarget->isLittle();
2533 SDValue Value = SD->getValue(), Chain = SD->getChain();
2534 EVT VT = Value.getValueType();
2537 // (store val, baseptr) or
2538 // (truncstore val, baseptr)
2540 // (swl val, (add baseptr, 3))
2541 // (swr val, baseptr)
2542 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2543 SDValue SWL = CreateStoreLR(MipsISD::SWL, DAG, SD, Chain,
2545 return CreateStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2548 assert(VT == MVT::i64);
2551 // (store val, baseptr)
2553 // (sdl val, (add baseptr, 7))
2554 // (sdr val, baseptr)
2555 SDValue SDL = CreateStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2556 return CreateStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2559 // This function expands mips intrinsic nodes which have 64-bit input operands
2560 // or output values.
2562 // out64 = intrinsic-node in64
2564 // lo = copy (extract-element (in64, 0))
2565 // hi = copy (extract-element (in64, 1))
2566 // mips-specific-node
2569 // out64 = merge-values (v0, v1)
2571 static SDValue LowerDSPIntr(SDValue Op, SelectionDAG &DAG,
2572 unsigned Opc, bool HasI64In, bool HasI64Out) {
2573 DebugLoc DL = Op.getDebugLoc();
2574 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
2575 SDValue Chain = HasChainIn ? Op->getOperand(0) : DAG.getEntryNode();
2576 SmallVector<SDValue, 3> Ops;
2579 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
2580 Op->getOperand(1 + HasChainIn),
2581 DAG.getConstant(0, MVT::i32));
2582 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
2583 Op->getOperand(1 + HasChainIn),
2584 DAG.getConstant(1, MVT::i32));
2586 Chain = DAG.getCopyToReg(Chain, DL, Mips::LO, InLo, SDValue());
2587 Chain = DAG.getCopyToReg(Chain, DL, Mips::HI, InHi, Chain.getValue(1));
2589 Ops.push_back(Chain);
2590 Ops.append(Op->op_begin() + HasChainIn + 2, Op->op_end());
2591 Ops.push_back(Chain.getValue(1));
2593 Ops.push_back(Chain);
2594 Ops.append(Op->op_begin() + HasChainIn + 1, Op->op_end());
2598 return DAG.getNode(Opc, DL, Op->value_begin(), Op->getNumValues(),
2599 Ops.begin(), Ops.size());
2601 SDValue Intr = DAG.getNode(Opc, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2602 Ops.begin(), Ops.size());
2603 SDValue OutLo = DAG.getCopyFromReg(Intr.getValue(0), DL, Mips::LO, MVT::i32,
2605 SDValue OutHi = DAG.getCopyFromReg(OutLo.getValue(1), DL, Mips::HI, MVT::i32,
2607 SDValue Out = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, OutLo, OutHi);
2612 SDValue Vals[] = { Out, OutHi.getValue(1) };
2613 return DAG.getMergeValues(Vals, 2, DL);
2616 SDValue MipsTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2617 SelectionDAG &DAG) const {
2618 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
2621 case Intrinsic::mips_shilo:
2622 return LowerDSPIntr(Op, DAG, MipsISD::SHILO, true, true);
2623 case Intrinsic::mips_dpau_h_qbl:
2624 return LowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL, true, true);
2625 case Intrinsic::mips_dpau_h_qbr:
2626 return LowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR, true, true);
2627 case Intrinsic::mips_dpsu_h_qbl:
2628 return LowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL, true, true);
2629 case Intrinsic::mips_dpsu_h_qbr:
2630 return LowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR, true, true);
2631 case Intrinsic::mips_dpa_w_ph:
2632 return LowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH, true, true);
2633 case Intrinsic::mips_dps_w_ph:
2634 return LowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH, true, true);
2635 case Intrinsic::mips_dpax_w_ph:
2636 return LowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH, true, true);
2637 case Intrinsic::mips_dpsx_w_ph:
2638 return LowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH, true, true);
2639 case Intrinsic::mips_mulsa_w_ph:
2640 return LowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH, true, true);
2641 case Intrinsic::mips_mult:
2642 return LowerDSPIntr(Op, DAG, MipsISD::MULT, false, true);
2643 case Intrinsic::mips_multu:
2644 return LowerDSPIntr(Op, DAG, MipsISD::MULTU, false, true);
2645 case Intrinsic::mips_madd:
2646 return LowerDSPIntr(Op, DAG, MipsISD::MADD_DSP, true, true);
2647 case Intrinsic::mips_maddu:
2648 return LowerDSPIntr(Op, DAG, MipsISD::MADDU_DSP, true, true);
2649 case Intrinsic::mips_msub:
2650 return LowerDSPIntr(Op, DAG, MipsISD::MSUB_DSP, true, true);
2651 case Intrinsic::mips_msubu:
2652 return LowerDSPIntr(Op, DAG, MipsISD::MSUBU_DSP, true, true);
2656 SDValue MipsTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2657 SelectionDAG &DAG) const {
2658 switch (cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue()) {
2661 case Intrinsic::mips_extp:
2662 return LowerDSPIntr(Op, DAG, MipsISD::EXTP, true, false);
2663 case Intrinsic::mips_extpdp:
2664 return LowerDSPIntr(Op, DAG, MipsISD::EXTPDP, true, false);
2665 case Intrinsic::mips_extr_w:
2666 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_W, true, false);
2667 case Intrinsic::mips_extr_r_w:
2668 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W, true, false);
2669 case Intrinsic::mips_extr_rs_w:
2670 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W, true, false);
2671 case Intrinsic::mips_extr_s_h:
2672 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H, true, false);
2673 case Intrinsic::mips_mthlip:
2674 return LowerDSPIntr(Op, DAG, MipsISD::MTHLIP, true, true);
2675 case Intrinsic::mips_mulsaq_s_w_ph:
2676 return LowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH, true, true);
2677 case Intrinsic::mips_maq_s_w_phl:
2678 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL, true, true);
2679 case Intrinsic::mips_maq_s_w_phr:
2680 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR, true, true);
2681 case Intrinsic::mips_maq_sa_w_phl:
2682 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL, true, true);
2683 case Intrinsic::mips_maq_sa_w_phr:
2684 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR, true, true);
2685 case Intrinsic::mips_dpaq_s_w_ph:
2686 return LowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH, true, true);
2687 case Intrinsic::mips_dpsq_s_w_ph:
2688 return LowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH, true, true);
2689 case Intrinsic::mips_dpaq_sa_l_w:
2690 return LowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W, true, true);
2691 case Intrinsic::mips_dpsq_sa_l_w:
2692 return LowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W, true, true);
2693 case Intrinsic::mips_dpaqx_s_w_ph:
2694 return LowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH, true, true);
2695 case Intrinsic::mips_dpaqx_sa_w_ph:
2696 return LowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH, true, true);
2697 case Intrinsic::mips_dpsqx_s_w_ph:
2698 return LowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH, true, true);
2699 case Intrinsic::mips_dpsqx_sa_w_ph:
2700 return LowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH, true, true);
2704 SDValue MipsTargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
2705 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2706 || cast<ConstantSDNode>
2707 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2708 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2712 // (add (frameaddr 0), (frame_to_args_offset))
2713 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2714 // (add FrameObject, 0)
2715 // where FrameObject is a fixed StackObject with offset 0 which points to
2716 // the old stack pointer.
2717 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2718 EVT ValTy = Op->getValueType(0);
2719 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2720 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2721 return DAG.getNode(ISD::ADD, Op->getDebugLoc(), ValTy, InArgsAddr,
2722 DAG.getConstant(0, ValTy));
2725 //===----------------------------------------------------------------------===//
2726 // Calling Convention Implementation
2727 //===----------------------------------------------------------------------===//
2729 //===----------------------------------------------------------------------===//
2730 // TODO: Implement a generic logic using tblgen that can support this.
2731 // Mips O32 ABI rules:
2733 // i32 - Passed in A0, A1, A2, A3 and stack
2734 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
2735 // an argument. Otherwise, passed in A1, A2, A3 and stack.
2736 // f64 - Only passed in two aliased f32 registers if no int reg has been used
2737 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2738 // not used, it must be shadowed. If only A3 is avaiable, shadow it and
2741 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2742 //===----------------------------------------------------------------------===//
2744 static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
2745 MVT LocVT, CCValAssign::LocInfo LocInfo,
2746 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2748 static const unsigned IntRegsSize=4, FloatRegsSize=2;
2750 static const uint16_t IntRegs[] = {
2751 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2753 static const uint16_t F32Regs[] = {
2754 Mips::F12, Mips::F14
2756 static const uint16_t F64Regs[] = {
2760 // Do not process byval args here.
2761 if (ArgFlags.isByVal())
2764 // Promote i8 and i16
2765 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2767 if (ArgFlags.isSExt())
2768 LocInfo = CCValAssign::SExt;
2769 else if (ArgFlags.isZExt())
2770 LocInfo = CCValAssign::ZExt;
2772 LocInfo = CCValAssign::AExt;
2777 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2778 // is true: function is vararg, argument is 3rd or higher, there is previous
2779 // argument which is not f32 or f64.
2780 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2781 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
2782 unsigned OrigAlign = ArgFlags.getOrigAlign();
2783 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
2785 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
2786 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2787 // If this is the first part of an i64 arg,
2788 // the allocated register must be either A0 or A2.
2789 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2790 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2792 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2793 // Allocate int register and shadow next int register. If first
2794 // available register is Mips::A1 or Mips::A3, shadow it too.
2795 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2796 if (Reg == Mips::A1 || Reg == Mips::A3)
2797 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2798 State.AllocateReg(IntRegs, IntRegsSize);
2800 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2801 // we are guaranteed to find an available float register
2802 if (ValVT == MVT::f32) {
2803 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2804 // Shadow int register
2805 State.AllocateReg(IntRegs, IntRegsSize);
2807 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2808 // Shadow int registers
2809 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2810 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2811 State.AllocateReg(IntRegs, IntRegsSize);
2812 State.AllocateReg(IntRegs, IntRegsSize);
2815 llvm_unreachable("Cannot handle this ValVT.");
2818 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2820 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
2822 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2827 #include "MipsGenCallingConv.inc"
2829 //===----------------------------------------------------------------------===//
2830 // Call Calling Convention Implementation
2831 //===----------------------------------------------------------------------===//
2833 static const unsigned O32IntRegsSize = 4;
2835 // Return next O32 integer argument register.
2836 static unsigned getNextIntArgReg(unsigned Reg) {
2837 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2838 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2841 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
2842 /// for tail call optimization.
2843 bool MipsTargetLowering::
2844 IsEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
2845 unsigned NextStackOffset,
2846 const MipsFunctionInfo& FI) const {
2847 if (!EnableMipsTailCalls)
2850 // No tail call optimization for mips16.
2851 if (Subtarget->inMips16Mode())
2854 // Return false if either the callee or caller has a byval argument.
2855 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
2858 // Return true if the callee's argument area is no larger than the
2860 return NextStackOffset <= FI.getIncomingArgSize();
2864 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2865 SDValue Chain, SDValue Arg, DebugLoc DL,
2866 bool IsTailCall, SelectionDAG &DAG) const {
2868 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2869 DAG.getIntPtrConstant(Offset));
2870 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2874 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2875 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2876 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2877 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2878 /*isVolatile=*/ true, false, 0);
2882 // The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
2883 // cleaner way to do all of this but it will have to wait until the traditional
2884 // gcc mechanism is completed.
2886 // For Pic, in order for Mips16 code to call Mips32 code which according the abi
2887 // have either arguments or returned values placed in floating point registers,
2888 // we use a set of helper functions. (This includes functions which return type
2889 // complex which on Mips are returned in a pair of floating point registers).
2891 // This is an encoding that we inherited from gcc.
2892 // In Mips traditional O32, N32 ABI, floating point numbers are passed in
2893 // floating point argument registers 1,2 only when the first and optionally
2894 // the second arguments are float (sf) or double (df).
2895 // For Mips16 we are only concerned with the situations where floating point
2896 // arguments are being passed in floating point registers by the ABI, because
2897 // Mips16 mode code cannot execute floating point instructions to load those
2898 // values and hence helper functions are needed.
2899 // The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
2900 // the helper function suffixs for these are:
2901 // 0, 1, 5, 9, 2, 6, 10
2902 // this suffix can then be calculated as follows:
2903 // for a given argument Arg:
2904 // Arg1x, Arg2x = 1 : Arg is sf
2906 // 0: Arg is neither sf or df
2907 // So this stub is the string for number Arg1x + Arg2x*4.
2908 // However not all numbers between 0 and 10 are possible, we check anyway and
2909 // assert if the impossible exists.
2912 unsigned int MipsTargetLowering::getMips16HelperFunctionStubNumber
2913 (ArgListTy &Args) const {
2914 unsigned int resultNum = 0;
2915 if (Args.size() >= 1) {
2916 Type *t = Args[0].Ty;
2917 if (t->isFloatTy()) {
2920 else if (t->isDoubleTy()) {
2925 if (Args.size() >=2) {
2926 Type *t = Args[1].Ty;
2927 if (t->isFloatTy()) {
2930 else if (t->isDoubleTy()) {
2939 // prefixs are attached to stub numbers depending on the return type .
2940 // return type: float sf_
2942 // single complex sc_
2943 // double complext dc_
2947 // The full name of a helper function is__mips16_call_stub +
2948 // return type dependent prefix + stub number
2951 // This is something that probably should be in a different source file and
2952 // perhaps done differently but my main purpose is to not waste runtime
2953 // on something that we can enumerate in the source. Another possibility is
2954 // to have a python script to generate these mapping tables. This will do
2955 // for now. There are a whole series of helper function mapping arrays, one
2956 // for each return type class as outlined above. There there are 11 possible
2957 // entries. Ones with 0 are ones which should never be selected
2959 // All the arrays are similar except for ones which return neither
2960 // sf, df, sc, dc, in which only care about ones which have sf or df as a
2963 #define P_ "__mips16_call_stub_"
2964 #define MAX_STUB_NUMBER 10
2965 #define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
2966 #define T P "0" , T1
2968 static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
2972 static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
2976 static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
2980 static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
2984 static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
2990 const char* MipsTargetLowering::
2991 getMips16HelperFunction
2992 (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
2993 const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
2995 const unsigned int maxStubNum = 10;
2996 assert(stubNum <= maxStubNum);
2997 const bool validStubNum[maxStubNum+1] =
2998 {true, true, true, false, false, true, true, false, false, true, true};
2999 assert(validStubNum[stubNum]);
3002 if (RetTy->isFloatTy()) {
3003 result = sfMips16Helper[stubNum];
3005 else if (RetTy ->isDoubleTy()) {
3006 result = dfMips16Helper[stubNum];
3008 else if (RetTy->isStructTy()) {
3009 // check if it's complex
3010 if (RetTy->getNumContainedTypes() == 2) {
3011 if ((RetTy->getContainedType(0)->isFloatTy()) &&
3012 (RetTy->getContainedType(1)->isFloatTy())) {
3013 result = scMips16Helper[stubNum];
3015 else if ((RetTy->getContainedType(0)->isDoubleTy()) &&
3016 (RetTy->getContainedType(1)->isDoubleTy())) {
3017 result = dcMips16Helper[stubNum];
3020 llvm_unreachable("Uncovered condition");
3024 llvm_unreachable("Uncovered condition");
3032 result = vMips16Helper[stubNum];
3038 /// LowerCall - functions arguments are copied from virtual regs to
3039 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
3041 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
3042 SmallVectorImpl<SDValue> &InVals) const {
3043 SelectionDAG &DAG = CLI.DAG;
3044 DebugLoc &dl = CLI.DL;
3045 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
3046 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
3047 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
3048 SDValue Chain = CLI.Chain;
3049 SDValue Callee = CLI.Callee;
3050 bool &isTailCall = CLI.IsTailCall;
3051 CallingConv::ID CallConv = CLI.CallConv;
3052 bool isVarArg = CLI.IsVarArg;
3054 const char* mips16HelperFunction = 0;
3055 bool needMips16Helper = false;
3057 if (Subtarget->inMips16Mode() && getTargetMachine().Options.UseSoftFloat &&
3060 // currently we don't have symbols tagged with the mips16 or mips32
3061 // qualifier so we will assume that we don't know what kind it is.
3062 // and generate the helper
3064 bool lookupHelper = true;
3065 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3066 if (noHelperNeeded.find(S->getSymbol()) != noHelperNeeded.end()) {
3067 lookupHelper = false;
3070 if (lookupHelper) mips16HelperFunction =
3071 getMips16HelperFunction(CLI.RetTy, CLI.Args, needMips16Helper);
3074 MachineFunction &MF = DAG.getMachineFunction();
3075 MachineFrameInfo *MFI = MF.getFrameInfo();
3076 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
3077 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
3079 // Analyze operands of the call, assigning locations to each operand.
3080 SmallVector<CCValAssign, 16> ArgLocs;
3081 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3082 getTargetMachine(), ArgLocs, *DAG.getContext());
3083 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
3085 MipsCCInfo.analyzeCallOperands(Outs, isVarArg);
3087 // Get a count of how many bytes are to be pushed on the stack.
3088 unsigned NextStackOffset = CCInfo.getNextStackOffset();
3090 // Check if it's really possible to do a tail call.
3093 IsEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
3094 *MF.getInfo<MipsFunctionInfo>());
3099 // Chain is the output chain of the last Load/Store or CopyToReg node.
3100 // ByValChain is the output chain of the last Memcpy node created for copying
3101 // byval arguments to the stack.
3102 unsigned StackAlignment = TFL->getStackAlignment();
3103 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
3104 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
3107 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal);
3109 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl,
3110 IsN64 ? Mips::SP_64 : Mips::SP,
3113 // With EABI is it possible to have 16 args on registers.
3114 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
3115 SmallVector<SDValue, 8> MemOpChains;
3116 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
3118 // Walk the register/memloc assignments, inserting copies/loads.
3119 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3120 SDValue Arg = OutVals[i];
3121 CCValAssign &VA = ArgLocs[i];
3122 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
3123 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3126 if (Flags.isByVal()) {
3127 assert(Flags.getByValSize() &&
3128 "ByVal args of size 0 should have been ignored by front-end.");
3129 assert(ByValArg != MipsCCInfo.byval_end());
3130 assert(!isTailCall &&
3131 "Do not tail-call optimize if there is a byval argument.");
3132 passByValArg(Chain, dl, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
3133 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
3138 // Promote the value if needed.
3139 switch (VA.getLocInfo()) {
3140 default: llvm_unreachable("Unknown loc info!");
3141 case CCValAssign::Full:
3142 if (VA.isRegLoc()) {
3143 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
3144 (ValVT == MVT::f64 && LocVT == MVT::i64))
3145 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
3146 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
3147 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
3148 Arg, DAG.getConstant(0, MVT::i32));
3149 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
3150 Arg, DAG.getConstant(1, MVT::i32));
3151 if (!Subtarget->isLittle())
3153 unsigned LocRegLo = VA.getLocReg();
3154 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
3155 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3156 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
3161 case CCValAssign::SExt:
3162 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
3164 case CCValAssign::ZExt:
3165 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
3167 case CCValAssign::AExt:
3168 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
3172 // Arguments that can be passed on register must be kept at
3173 // RegsToPass vector
3174 if (VA.isRegLoc()) {
3175 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3179 // Register can't get to this point...
3180 assert(VA.isMemLoc());
3182 // emit ISD::STORE whichs stores the
3183 // parameter value to a stack Location
3184 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
3185 Chain, Arg, dl, isTailCall, DAG));
3188 // Transform all store nodes into one single node because all store
3189 // nodes are independent of each other.
3190 if (!MemOpChains.empty())
3191 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
3192 &MemOpChains[0], MemOpChains.size());
3194 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3195 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3196 // node so that legalize doesn't hack it.
3197 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
3198 bool GlobalOrExternal = false, InternalLinkage = false;
3201 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3203 InternalLinkage = G->getGlobal()->hasInternalLinkage();
3205 if (InternalLinkage)
3206 Callee = getAddrLocal(Callee, DAG, HasMips64);
3208 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
3209 MipsII::MO_CALL_LO16);
3211 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
3213 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
3214 MipsII::MO_NO_FLAG);
3215 GlobalOrExternal = true;
3217 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3218 if (!IsN64 && !IsPIC) // !N64 && static
3219 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
3220 MipsII::MO_NO_FLAG);
3222 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
3223 MipsII::MO_CALL_LO16);
3225 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_DISP);
3227 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
3229 GlobalOrExternal = true;
3232 SDValue JumpTarget = Callee;
3234 // T9 should contain the address of the callee function if
3235 // -reloction-model=pic or it is an indirect call.
3236 if (IsPICCall || !GlobalOrExternal) {
3237 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
3238 unsigned V0Reg = Mips::V0;
3239 if (needMips16Helper) {
3240 RegsToPass.push_front(std::make_pair(V0Reg, Callee));
3241 JumpTarget = DAG.getExternalSymbol(
3242 mips16HelperFunction, getPointerTy());
3243 JumpTarget = getAddrGlobal(JumpTarget, DAG, MipsII::MO_GOT);
3246 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
3248 if (!Subtarget->inMips16Mode())
3249 JumpTarget = SDValue();
3253 // Insert node "GP copy globalreg" before call to function.
3255 // R_MIPS_CALL* operators (emitted when non-internal functions are called
3256 // in PIC mode) allow symbols to be resolved via lazy binding.
3257 // The lazy binding stub requires GP to point to the GOT.
3258 if (IsPICCall && !InternalLinkage) {
3259 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
3260 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
3261 RegsToPass.push_back(std::make_pair(GPReg, GetGlobalReg(DAG, Ty)));
3264 // Build a sequence of copy-to-reg nodes chained together with token
3265 // chain and flag operands which copy the outgoing args into registers.
3266 // The InFlag in necessary since all emitted instructions must be
3270 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3271 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3272 RegsToPass[i].second, InFlag);
3273 InFlag = Chain.getValue(1);
3276 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
3277 // = Chain, Callee, Reg#1, Reg#2, ...
3279 // Returns a chain & a flag for retval copy to use.
3280 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3281 SmallVector<SDValue, 8> Ops(1, Chain);
3283 if (JumpTarget.getNode())
3284 Ops.push_back(JumpTarget);
3286 // Add argument registers to the end of the list so that they are
3287 // known live into the call.
3288 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3289 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3290 RegsToPass[i].second.getValueType()));
3292 // Add a register mask operand representing the call-preserved registers.
3293 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3294 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3295 assert(Mask && "Missing call preserved mask for calling convention");
3296 Ops.push_back(DAG.getRegisterMask(Mask));
3298 if (InFlag.getNode())
3299 Ops.push_back(InFlag);
3302 return DAG.getNode(MipsISD::TailCall, dl, MVT::Other, &Ops[0], Ops.size());
3304 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
3305 InFlag = Chain.getValue(1);
3307 // Create the CALLSEQ_END node.
3308 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
3309 DAG.getIntPtrConstant(0, true), InFlag);
3310 InFlag = Chain.getValue(1);
3312 // Handle result values, copying them out of physregs into vregs that we
3314 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3315 Ins, dl, DAG, InVals);
3318 /// LowerCallResult - Lower the result values of a call into the
3319 /// appropriate copies out of appropriate physical registers.
3321 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
3322 CallingConv::ID CallConv, bool isVarArg,
3323 const SmallVectorImpl<ISD::InputArg> &Ins,
3324 DebugLoc dl, SelectionDAG &DAG,
3325 SmallVectorImpl<SDValue> &InVals) const {
3326 // Assign locations to each value returned by this call.
3327 SmallVector<CCValAssign, 16> RVLocs;
3328 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3329 getTargetMachine(), RVLocs, *DAG.getContext());
3331 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
3333 // Copy all of the result registers out of their specified physreg.
3334 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3335 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
3336 RVLocs[i].getValVT(), InFlag).getValue(1);
3337 InFlag = Chain.getValue(2);
3338 InVals.push_back(Chain.getValue(0));
3344 //===----------------------------------------------------------------------===//
3345 // Formal Arguments Calling Convention Implementation
3346 //===----------------------------------------------------------------------===//
3347 /// LowerFormalArguments - transform physical registers into virtual registers
3348 /// and generate load operations for arguments places on the stack.
3350 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
3351 CallingConv::ID CallConv,
3353 const SmallVectorImpl<ISD::InputArg> &Ins,
3354 DebugLoc dl, SelectionDAG &DAG,
3355 SmallVectorImpl<SDValue> &InVals)
3357 MachineFunction &MF = DAG.getMachineFunction();
3358 MachineFrameInfo *MFI = MF.getFrameInfo();
3359 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3361 MipsFI->setVarArgsFrameIndex(0);
3363 // Used with vargs to acumulate store chains.
3364 std::vector<SDValue> OutChains;
3366 // Assign locations to all of the incoming arguments.
3367 SmallVector<CCValAssign, 16> ArgLocs;
3368 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3369 getTargetMachine(), ArgLocs, *DAG.getContext());
3370 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
3372 MipsCCInfo.analyzeFormalArguments(Ins);
3373 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
3374 MipsCCInfo.hasByValArg());
3376 Function::const_arg_iterator FuncArg =
3377 DAG.getMachineFunction().getFunction()->arg_begin();
3378 unsigned CurArgIdx = 0;
3379 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
3381 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3382 CCValAssign &VA = ArgLocs[i];
3383 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
3384 CurArgIdx = Ins[i].OrigArgIndex;
3385 EVT ValVT = VA.getValVT();
3386 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3387 bool IsRegLoc = VA.isRegLoc();
3389 if (Flags.isByVal()) {
3390 assert(Flags.getByValSize() &&
3391 "ByVal args of size 0 should have been ignored by front-end.");
3392 assert(ByValArg != MipsCCInfo.byval_end());
3393 copyByValRegs(Chain, dl, OutChains, DAG, Flags, InVals, &*FuncArg,
3394 MipsCCInfo, *ByValArg);
3399 // Arguments stored on registers
3401 EVT RegVT = VA.getLocVT();
3402 unsigned ArgReg = VA.getLocReg();
3403 const TargetRegisterClass *RC;
3405 if (RegVT == MVT::i32)
3406 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
3407 &Mips::CPURegsRegClass;
3408 else if (RegVT == MVT::i64)
3409 RC = &Mips::CPU64RegsRegClass;
3410 else if (RegVT == MVT::f32)
3411 RC = &Mips::FGR32RegClass;
3412 else if (RegVT == MVT::f64)
3413 RC = HasMips64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
3415 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
3417 // Transform the arguments stored on
3418 // physical registers into virtual ones
3419 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3420 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
3422 // If this is an 8 or 16-bit value, it has been passed promoted
3423 // to 32 bits. Insert an assert[sz]ext to capture this, then
3424 // truncate to the right size.
3425 if (VA.getLocInfo() != CCValAssign::Full) {
3426 unsigned Opcode = 0;
3427 if (VA.getLocInfo() == CCValAssign::SExt)
3428 Opcode = ISD::AssertSext;
3429 else if (VA.getLocInfo() == CCValAssign::ZExt)
3430 Opcode = ISD::AssertZext;
3432 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
3433 DAG.getValueType(ValVT));
3434 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
3437 // Handle floating point arguments passed in integer registers.
3438 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
3439 (RegVT == MVT::i64 && ValVT == MVT::f64))
3440 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
3441 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
3442 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
3443 getNextIntArgReg(ArgReg), RC);
3444 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
3445 if (!Subtarget->isLittle())
3446 std::swap(ArgValue, ArgValue2);
3447 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
3448 ArgValue, ArgValue2);
3451 InVals.push_back(ArgValue);
3452 } else { // VA.isRegLoc()
3455 assert(VA.isMemLoc());
3457 // The stack pointer offset is relative to the caller stack frame.
3458 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
3459 VA.getLocMemOffset(), true);
3461 // Create load nodes to retrieve arguments from the stack
3462 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3463 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
3464 MachinePointerInfo::getFixedStack(FI),
3465 false, false, false, 0));
3469 // The mips ABIs for returning structs by value requires that we copy
3470 // the sret argument into $v0 for the return. Save the argument into
3471 // a virtual register so that we can access it from the return points.
3472 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3473 unsigned Reg = MipsFI->getSRetReturnReg();
3475 Reg = MF.getRegInfo().
3476 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
3477 MipsFI->setSRetReturnReg(Reg);
3479 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
3480 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
3484 writeVarArgRegs(OutChains, MipsCCInfo, Chain, dl, DAG);
3486 // All stores are grouped in one node to allow the matching between
3487 // the size of Ins and InVals. This only happens when on varg functions
3488 if (!OutChains.empty()) {
3489 OutChains.push_back(Chain);
3490 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
3491 &OutChains[0], OutChains.size());
3497 //===----------------------------------------------------------------------===//
3498 // Return Value Calling Convention Implementation
3499 //===----------------------------------------------------------------------===//
3502 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3503 MachineFunction &MF, bool isVarArg,
3504 const SmallVectorImpl<ISD::OutputArg> &Outs,
3505 LLVMContext &Context) const {
3506 SmallVector<CCValAssign, 16> RVLocs;
3507 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
3509 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3513 MipsTargetLowering::LowerReturn(SDValue Chain,
3514 CallingConv::ID CallConv, bool isVarArg,
3515 const SmallVectorImpl<ISD::OutputArg> &Outs,
3516 const SmallVectorImpl<SDValue> &OutVals,
3517 DebugLoc dl, SelectionDAG &DAG) const {
3519 // CCValAssign - represent the assignment of
3520 // the return value to a location
3521 SmallVector<CCValAssign, 16> RVLocs;
3523 // CCState - Info about the registers and stack slot.
3524 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3525 getTargetMachine(), RVLocs, *DAG.getContext());
3527 // Analize return values.
3528 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
3531 SmallVector<SDValue, 4> RetOps(1, Chain);
3533 // Copy the result values into the output registers.
3534 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3535 CCValAssign &VA = RVLocs[i];
3536 assert(VA.isRegLoc() && "Can only return in registers!");
3538 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
3540 // Guarantee that all emitted copies are stuck together with flags.
3541 Flag = Chain.getValue(1);
3542 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3545 // The mips ABIs for returning structs by value requires that we copy
3546 // the sret argument into $v0 for the return. We saved the argument into
3547 // a virtual register in the entry block, so now we copy the value out
3549 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3550 MachineFunction &MF = DAG.getMachineFunction();
3551 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3552 unsigned Reg = MipsFI->getSRetReturnReg();
3555 llvm_unreachable("sret virtual register not created in the entry block");
3556 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
3557 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
3559 Chain = DAG.getCopyToReg(Chain, dl, V0, Val, Flag);
3560 Flag = Chain.getValue(1);
3561 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
3564 RetOps[0] = Chain; // Update chain.
3566 // Add the flag if we have it.
3568 RetOps.push_back(Flag);
3570 // Return on Mips is always a "jr $ra"
3571 return DAG.getNode(MipsISD::Ret, dl, MVT::Other, &RetOps[0], RetOps.size());
3574 //===----------------------------------------------------------------------===//
3575 // Mips Inline Assembly Support
3576 //===----------------------------------------------------------------------===//
3578 /// getConstraintType - Given a constraint letter, return the type of
3579 /// constraint it is for this target.
3580 MipsTargetLowering::ConstraintType MipsTargetLowering::
3581 getConstraintType(const std::string &Constraint) const
3583 // Mips specific constrainy
3584 // GCC config/mips/constraints.md
3586 // 'd' : An address register. Equivalent to r
3587 // unless generating MIPS16 code.
3588 // 'y' : Equivalent to r; retained for
3589 // backwards compatibility.
3590 // 'c' : A register suitable for use in an indirect
3591 // jump. This will always be $25 for -mabicalls.
3592 // 'l' : The lo register. 1 word storage.
3593 // 'x' : The hilo register pair. Double word storage.
3594 if (Constraint.size() == 1) {
3595 switch (Constraint[0]) {
3603 return C_RegisterClass;
3606 return TargetLowering::getConstraintType(Constraint);
3609 /// Examine constraint type and operand type and determine a weight value.
3610 /// This object must already have been set up with the operand type
3611 /// and the current alternative constraint selected.
3612 TargetLowering::ConstraintWeight
3613 MipsTargetLowering::getSingleConstraintMatchWeight(
3614 AsmOperandInfo &info, const char *constraint) const {
3615 ConstraintWeight weight = CW_Invalid;
3616 Value *CallOperandVal = info.CallOperandVal;
3617 // If we don't have a value, we can't do a match,
3618 // but allow it at the lowest weight.
3619 if (CallOperandVal == NULL)
3621 Type *type = CallOperandVal->getType();
3622 // Look at the constraint type.
3623 switch (*constraint) {
3625 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3629 if (type->isIntegerTy())
3630 weight = CW_Register;
3633 if (type->isFloatTy())
3634 weight = CW_Register;
3636 case 'c': // $25 for indirect jumps
3637 case 'l': // lo register
3638 case 'x': // hilo register pair
3639 if (type->isIntegerTy())
3640 weight = CW_SpecificReg;
3642 case 'I': // signed 16 bit immediate
3643 case 'J': // integer zero
3644 case 'K': // unsigned 16 bit immediate
3645 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3646 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3647 case 'O': // signed 15 bit immediate (+- 16383)
3648 case 'P': // immediate in the range of 65535 to 1 (inclusive)
3649 if (isa<ConstantInt>(CallOperandVal))
3650 weight = CW_Constant;
3656 /// Given a register class constraint, like 'r', if this corresponds directly
3657 /// to an LLVM register class, return a register of 0 and the register class
3659 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
3660 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
3662 if (Constraint.size() == 1) {
3663 switch (Constraint[0]) {
3664 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3665 case 'y': // Same as 'r'. Exists for compatibility.
3667 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3668 if (Subtarget->inMips16Mode())
3669 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
3670 return std::make_pair(0U, &Mips::CPURegsRegClass);
3672 if (VT == MVT::i64 && !HasMips64)
3673 return std::make_pair(0U, &Mips::CPURegsRegClass);
3674 if (VT == MVT::i64 && HasMips64)
3675 return std::make_pair(0U, &Mips::CPU64RegsRegClass);
3676 // This will generate an error message
3677 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
3680 return std::make_pair(0U, &Mips::FGR32RegClass);
3681 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
3682 if (Subtarget->isFP64bit())
3683 return std::make_pair(0U, &Mips::FGR64RegClass);
3684 return std::make_pair(0U, &Mips::AFGR64RegClass);
3687 case 'c': // register suitable for indirect jump
3689 return std::make_pair((unsigned)Mips::T9, &Mips::CPURegsRegClass);
3690 assert(VT == MVT::i64 && "Unexpected type.");
3691 return std::make_pair((unsigned)Mips::T9_64, &Mips::CPU64RegsRegClass);
3692 case 'l': // register suitable for indirect jump
3694 return std::make_pair((unsigned)Mips::LO, &Mips::HILORegClass);
3695 return std::make_pair((unsigned)Mips::LO64, &Mips::HILO64RegClass);
3696 case 'x': // register suitable for indirect jump
3697 // Fixme: Not triggering the use of both hi and low
3698 // This will generate an error message
3699 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
3702 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3705 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3706 /// vector. If it is invalid, don't add anything to Ops.
3707 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3708 std::string &Constraint,
3709 std::vector<SDValue>&Ops,
3710 SelectionDAG &DAG) const {
3711 SDValue Result(0, 0);
3713 // Only support length 1 constraints for now.
3714 if (Constraint.length() > 1) return;
3716 char ConstraintLetter = Constraint[0];
3717 switch (ConstraintLetter) {
3718 default: break; // This will fall through to the generic implementation
3719 case 'I': // Signed 16 bit constant
3720 // If this fails, the parent routine will give an error
3721 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3722 EVT Type = Op.getValueType();
3723 int64_t Val = C->getSExtValue();
3724 if (isInt<16>(Val)) {
3725 Result = DAG.getTargetConstant(Val, Type);
3730 case 'J': // integer zero
3731 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3732 EVT Type = Op.getValueType();
3733 int64_t Val = C->getZExtValue();
3735 Result = DAG.getTargetConstant(0, Type);
3740 case 'K': // unsigned 16 bit immediate
3741 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3742 EVT Type = Op.getValueType();
3743 uint64_t Val = (uint64_t)C->getZExtValue();
3744 if (isUInt<16>(Val)) {
3745 Result = DAG.getTargetConstant(Val, Type);
3750 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3751 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3752 EVT Type = Op.getValueType();
3753 int64_t Val = C->getSExtValue();
3754 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3755 Result = DAG.getTargetConstant(Val, Type);
3760 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3761 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3762 EVT Type = Op.getValueType();
3763 int64_t Val = C->getSExtValue();
3764 if ((Val >= -65535) && (Val <= -1)) {
3765 Result = DAG.getTargetConstant(Val, Type);
3770 case 'O': // signed 15 bit immediate
3771 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3772 EVT Type = Op.getValueType();
3773 int64_t Val = C->getSExtValue();
3774 if ((isInt<15>(Val))) {
3775 Result = DAG.getTargetConstant(Val, Type);
3780 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3781 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3782 EVT Type = Op.getValueType();
3783 int64_t Val = C->getSExtValue();
3784 if ((Val <= 65535) && (Val >= 1)) {
3785 Result = DAG.getTargetConstant(Val, Type);
3792 if (Result.getNode()) {
3793 Ops.push_back(Result);
3797 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3801 MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
3802 // No global is ever allowed as a base.
3807 case 0: // "r+i" or just "i", depending on HasBaseReg.
3810 if (!AM.HasBaseReg) // allow "r+i".
3812 return false; // disallow "r+r" or "r+r+i".
3821 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3822 // The Mips target isn't yet aware of offsets.
3826 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
3828 bool IsMemset, bool ZeroMemset,
3830 MachineFunction &MF) const {
3831 if (Subtarget->hasMips64())
3837 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3838 if (VT != MVT::f32 && VT != MVT::f64)
3840 if (Imm.isNegZero())
3842 return Imm.isZero();
3845 unsigned MipsTargetLowering::getJumpTableEncoding() const {
3847 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
3849 return TargetLowering::getJumpTableEncoding();
3852 MipsTargetLowering::MipsCC::MipsCC(CallingConv::ID CC, bool IsO32_,
3854 : CCInfo(Info), CallConv(CC), IsO32(IsO32_) {
3855 // Pre-allocate reserved argument area.
3856 CCInfo.AllocateStack(reservedArgArea(), 1);
3859 void MipsTargetLowering::MipsCC::
3860 analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
3862 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3863 "CallingConv::Fast shouldn't be used for vararg functions.");
3865 unsigned NumOpnds = Args.size();
3866 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
3868 for (unsigned I = 0; I != NumOpnds; ++I) {
3869 MVT ArgVT = Args[I].VT;
3870 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3873 if (ArgFlags.isByVal()) {
3874 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3878 if (IsVarArg && !Args[I].IsFixed)
3879 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
3881 R = FixedFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
3885 dbgs() << "Call operand #" << I << " has unhandled type "
3886 << EVT(ArgVT).getEVTString();
3888 llvm_unreachable(0);
3893 void MipsTargetLowering::MipsCC::
3894 analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args) {
3895 unsigned NumArgs = Args.size();
3896 llvm::CCAssignFn *FixedFn = fixedArgFn();
3898 for (unsigned I = 0; I != NumArgs; ++I) {
3899 MVT ArgVT = Args[I].VT;
3900 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3902 if (ArgFlags.isByVal()) {
3903 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3907 if (!FixedFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo))
3911 dbgs() << "Formal Arg #" << I << " has unhandled type "
3912 << EVT(ArgVT).getEVTString();
3914 llvm_unreachable(0);
3919 MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3921 CCValAssign::LocInfo LocInfo,
3922 ISD::ArgFlagsTy ArgFlags) {
3923 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3925 struct ByValArgInfo ByVal;
3926 unsigned RegSize = regSize();
3927 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3928 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3931 if (useRegsForByval())
3932 allocateRegs(ByVal, ByValSize, Align);
3934 // Allocate space on caller's stack.
3935 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3937 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3939 ByValArgs.push_back(ByVal);
3942 unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3943 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3946 unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3947 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3950 const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3951 return IsO32 ? O32IntRegs : Mips64IntRegs;
3954 llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3955 if (CallConv == CallingConv::Fast)
3956 return CC_Mips_FastCC;
3958 return IsO32 ? CC_MipsO32 : CC_MipsN;
3961 llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
3962 return IsO32 ? CC_MipsO32 : CC_MipsN_VarArg;
3965 const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3966 return IsO32 ? O32IntRegs : Mips64DPRegs;
3969 void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3972 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3973 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
3974 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3975 "Byval argument's size and alignment should be a multiple of"
3978 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3980 // If Align > RegSize, the first arg register must be even.
3981 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3982 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3986 // Mark the registers allocated.
3987 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3988 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3989 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3992 void MipsTargetLowering::
3993 copyByValRegs(SDValue Chain, DebugLoc DL, std::vector<SDValue> &OutChains,
3994 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3995 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3996 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3997 MachineFunction &MF = DAG.getMachineFunction();
3998 MachineFrameInfo *MFI = MF.getFrameInfo();
3999 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
4000 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4004 FrameObjOffset = (int)CC.reservedArgArea() -
4005 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
4007 FrameObjOffset = ByVal.Address;
4009 // Create frame object.
4010 EVT PtrTy = getPointerTy();
4011 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
4012 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4013 InVals.push_back(FIN);
4018 // Copy arg registers.
4019 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
4020 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4022 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
4023 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
4024 unsigned VReg = AddLiveIn(MF, ArgReg, RC);
4025 unsigned Offset = I * CC.regSize();
4026 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
4027 DAG.getConstant(Offset, PtrTy));
4028 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
4029 StorePtr, MachinePointerInfo(FuncArg, Offset),
4031 OutChains.push_back(Store);
4035 // Copy byVal arg to registers and stack.
4036 void MipsTargetLowering::
4037 passByValArg(SDValue Chain, DebugLoc DL,
4038 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
4039 SmallVector<SDValue, 8> &MemOpChains, SDValue StackPtr,
4040 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
4041 const MipsCC &CC, const ByValArgInfo &ByVal,
4042 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
4043 unsigned ByValSize = Flags.getByValSize();
4044 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
4045 unsigned RegSize = CC.regSize();
4046 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
4047 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
4049 if (ByVal.NumRegs) {
4050 const uint16_t *ArgRegs = CC.intArgRegs();
4051 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
4054 // Copy words to registers.
4055 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
4056 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4057 DAG.getConstant(Offset, PtrTy));
4058 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
4059 MachinePointerInfo(), false, false, false,
4061 MemOpChains.push_back(LoadVal.getValue(1));
4062 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
4063 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4066 // Return if the struct has been fully copied.
4067 if (ByValSize == Offset)
4070 // Copy the remainder of the byval argument with sub-word loads and shifts.
4071 if (LeftoverBytes) {
4072 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
4073 "Size of the remainder should be smaller than RegSize.");
4076 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
4077 Offset < ByValSize; LoadSize /= 2) {
4078 unsigned RemSize = ByValSize - Offset;
4080 if (RemSize < LoadSize)
4084 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4085 DAG.getConstant(Offset, PtrTy));
4087 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
4088 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
4089 false, false, Alignment);
4090 MemOpChains.push_back(LoadVal.getValue(1));
4092 // Shift the loaded value.
4096 Shamt = TotalSizeLoaded;
4098 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
4100 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
4101 DAG.getConstant(Shamt, MVT::i32));
4104 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4109 TotalSizeLoaded += LoadSize;
4110 Alignment = std::min(Alignment, LoadSize);
4113 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
4114 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4119 // Copy remainder of byval arg to it with memcpy.
4120 unsigned MemCpySize = ByValSize - Offset;
4121 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4122 DAG.getConstant(Offset, PtrTy));
4123 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
4124 DAG.getIntPtrConstant(ByVal.Address));
4125 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
4126 DAG.getConstant(MemCpySize, PtrTy), Alignment,
4127 /*isVolatile=*/false, /*AlwaysInline=*/false,
4128 MachinePointerInfo(0), MachinePointerInfo(0));
4129 MemOpChains.push_back(Chain);
4133 MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
4134 const MipsCC &CC, SDValue Chain,
4135 DebugLoc DL, SelectionDAG &DAG) const {
4136 unsigned NumRegs = CC.numIntArgRegs();
4137 const uint16_t *ArgRegs = CC.intArgRegs();
4138 const CCState &CCInfo = CC.getCCInfo();
4139 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
4140 unsigned RegSize = CC.regSize();
4141 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
4142 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4143 MachineFunction &MF = DAG.getMachineFunction();
4144 MachineFrameInfo *MFI = MF.getFrameInfo();
4145 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4147 // Offset of the first variable argument from stack pointer.
4151 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
4154 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
4156 // Record the frame index of the first variable argument
4157 // which is a value necessary to VASTART.
4158 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
4159 MipsFI->setVarArgsFrameIndex(FI);
4161 // Copy the integer registers that have not been used for argument passing
4162 // to the argument register save area. For O32, the save area is allocated
4163 // in the caller's stack frame, while for N32/64, it is allocated in the
4164 // callee's stack frame.
4165 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
4166 unsigned Reg = AddLiveIn(MF, ArgRegs[I], RC);
4167 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
4168 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
4169 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
4170 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
4171 MachinePointerInfo(), false, false, 0);
4172 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
4173 OutChains.push_back(Store);