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.
1301 MachineBasicBlock *MipsTargetLowering::EmitSelT16
1302 (unsigned Opc1, unsigned Opc2,
1303 MachineInstr *MI, MachineBasicBlock *BB) const {
1304 if (DontExpandCondPseudos16)
1306 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1307 DebugLoc dl = MI->getDebugLoc();
1308 // To "insert" a SELECT_CC instruction, we actually have to insert the
1309 // diamond control-flow pattern. The incoming instruction knows the
1310 // destination vreg to set, the condition code register to branch on, the
1311 // true/false values to select between, and a branch opcode to use.
1312 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1313 MachineFunction::iterator It = BB;
1320 // bNE r1, r0, copy1MBB
1321 // fallthrough --> copy0MBB
1322 MachineBasicBlock *thisMBB = BB;
1323 MachineFunction *F = BB->getParent();
1324 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1325 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1326 F->insert(It, copy0MBB);
1327 F->insert(It, sinkMBB);
1329 // Transfer the remainder of BB and its successor edges to sinkMBB.
1330 sinkMBB->splice(sinkMBB->begin(), BB,
1331 llvm::next(MachineBasicBlock::iterator(MI)),
1333 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1335 // Next, add the true and fallthrough blocks as its successors.
1336 BB->addSuccessor(copy0MBB);
1337 BB->addSuccessor(sinkMBB);
1339 BuildMI(BB, dl, TII->get(Opc2)).addReg(MI->getOperand(3).getReg())
1340 .addReg(MI->getOperand(4).getReg());
1341 BuildMI(BB, dl, TII->get(Opc1)).addMBB(sinkMBB);
1344 // %FalseValue = ...
1345 // # fallthrough to sinkMBB
1348 // Update machine-CFG edges
1349 BB->addSuccessor(sinkMBB);
1352 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
1356 BuildMI(*BB, BB->begin(), dl,
1357 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1358 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
1359 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
1361 MI->eraseFromParent(); // The pseudo instruction is gone now.
1367 MachineBasicBlock *MipsTargetLowering::EmitSeliT16
1368 (unsigned Opc1, unsigned Opc2,
1369 MachineInstr *MI, MachineBasicBlock *BB) const {
1370 if (DontExpandCondPseudos16)
1372 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1373 DebugLoc dl = MI->getDebugLoc();
1374 // To "insert" a SELECT_CC instruction, we actually have to insert the
1375 // diamond control-flow pattern. The incoming instruction knows the
1376 // destination vreg to set, the condition code register to branch on, the
1377 // true/false values to select between, and a branch opcode to use.
1378 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1379 MachineFunction::iterator It = BB;
1386 // bNE r1, r0, copy1MBB
1387 // fallthrough --> copy0MBB
1388 MachineBasicBlock *thisMBB = BB;
1389 MachineFunction *F = BB->getParent();
1390 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1391 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
1392 F->insert(It, copy0MBB);
1393 F->insert(It, sinkMBB);
1395 // Transfer the remainder of BB and its successor edges to sinkMBB.
1396 sinkMBB->splice(sinkMBB->begin(), BB,
1397 llvm::next(MachineBasicBlock::iterator(MI)),
1399 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1401 // Next, add the true and fallthrough blocks as its successors.
1402 BB->addSuccessor(copy0MBB);
1403 BB->addSuccessor(sinkMBB);
1405 BuildMI(BB, dl, TII->get(Opc2)).addReg(MI->getOperand(3).getReg())
1406 .addImm(MI->getOperand(4).getImm());
1407 BuildMI(BB, dl, TII->get(Opc1)).addMBB(sinkMBB);
1410 // %FalseValue = ...
1411 // # fallthrough to sinkMBB
1414 // Update machine-CFG edges
1415 BB->addSuccessor(sinkMBB);
1418 // %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
1422 BuildMI(*BB, BB->begin(), dl,
1423 TII->get(Mips::PHI), MI->getOperand(0).getReg())
1424 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB)
1425 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB);
1427 MI->eraseFromParent(); // The pseudo instruction is gone now.
1434 *MipsTargetLowering::EmitFEXT_T8I816_ins(unsigned BtOpc, unsigned CmpOpc,
1436 MachineBasicBlock *BB) const {
1437 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1438 unsigned regX = MI->getOperand(0).getReg();
1439 unsigned regY = MI->getOperand(1).getReg();
1440 MachineBasicBlock *target = MI->getOperand(2).getMBB();
1441 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(CmpOpc)).addReg(regX).addReg(regY);
1442 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(BtOpc)).addMBB(target);
1443 MI->eraseFromParent(); // The pseudo instruction is gone now.
1448 MachineBasicBlock *MipsTargetLowering::EmitFEXT_T8I8I16_ins(
1449 unsigned BtOpc, unsigned CmpiOpc, unsigned CmpiXOpc,
1450 MachineInstr *MI, MachineBasicBlock *BB) const {
1451 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1452 unsigned regX = MI->getOperand(0).getReg();
1453 int64_t imm = MI->getOperand(1).getImm();
1454 MachineBasicBlock *target = MI->getOperand(2).getMBB();
1458 else if (isUInt<16>(imm))
1461 llvm_unreachable("immediate field not usable");
1462 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(CmpOpc)).addReg(regX).addImm(imm);
1463 BuildMI(*BB, MI, MI->getDebugLoc(), TII->get(BtOpc)).addMBB(target);
1464 MI->eraseFromParent(); // The pseudo instruction is gone now.
1469 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1470 MachineBasicBlock *BB) const {
1471 switch (MI->getOpcode()) {
1473 llvm_unreachable("Unexpected instr type to insert");
1474 case Mips::ATOMIC_LOAD_ADD_I8:
1475 case Mips::ATOMIC_LOAD_ADD_I8_P8:
1476 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
1477 case Mips::ATOMIC_LOAD_ADD_I16:
1478 case Mips::ATOMIC_LOAD_ADD_I16_P8:
1479 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
1480 case Mips::ATOMIC_LOAD_ADD_I32:
1481 case Mips::ATOMIC_LOAD_ADD_I32_P8:
1482 return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
1483 case Mips::ATOMIC_LOAD_ADD_I64:
1484 case Mips::ATOMIC_LOAD_ADD_I64_P8:
1485 return EmitAtomicBinary(MI, BB, 8, Mips::DADDu);
1487 case Mips::ATOMIC_LOAD_AND_I8:
1488 case Mips::ATOMIC_LOAD_AND_I8_P8:
1489 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
1490 case Mips::ATOMIC_LOAD_AND_I16:
1491 case Mips::ATOMIC_LOAD_AND_I16_P8:
1492 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
1493 case Mips::ATOMIC_LOAD_AND_I32:
1494 case Mips::ATOMIC_LOAD_AND_I32_P8:
1495 return EmitAtomicBinary(MI, BB, 4, Mips::AND);
1496 case Mips::ATOMIC_LOAD_AND_I64:
1497 case Mips::ATOMIC_LOAD_AND_I64_P8:
1498 return EmitAtomicBinary(MI, BB, 8, Mips::AND64);
1500 case Mips::ATOMIC_LOAD_OR_I8:
1501 case Mips::ATOMIC_LOAD_OR_I8_P8:
1502 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
1503 case Mips::ATOMIC_LOAD_OR_I16:
1504 case Mips::ATOMIC_LOAD_OR_I16_P8:
1505 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
1506 case Mips::ATOMIC_LOAD_OR_I32:
1507 case Mips::ATOMIC_LOAD_OR_I32_P8:
1508 return EmitAtomicBinary(MI, BB, 4, Mips::OR);
1509 case Mips::ATOMIC_LOAD_OR_I64:
1510 case Mips::ATOMIC_LOAD_OR_I64_P8:
1511 return EmitAtomicBinary(MI, BB, 8, Mips::OR64);
1513 case Mips::ATOMIC_LOAD_XOR_I8:
1514 case Mips::ATOMIC_LOAD_XOR_I8_P8:
1515 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
1516 case Mips::ATOMIC_LOAD_XOR_I16:
1517 case Mips::ATOMIC_LOAD_XOR_I16_P8:
1518 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
1519 case Mips::ATOMIC_LOAD_XOR_I32:
1520 case Mips::ATOMIC_LOAD_XOR_I32_P8:
1521 return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
1522 case Mips::ATOMIC_LOAD_XOR_I64:
1523 case Mips::ATOMIC_LOAD_XOR_I64_P8:
1524 return EmitAtomicBinary(MI, BB, 8, Mips::XOR64);
1526 case Mips::ATOMIC_LOAD_NAND_I8:
1527 case Mips::ATOMIC_LOAD_NAND_I8_P8:
1528 return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
1529 case Mips::ATOMIC_LOAD_NAND_I16:
1530 case Mips::ATOMIC_LOAD_NAND_I16_P8:
1531 return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
1532 case Mips::ATOMIC_LOAD_NAND_I32:
1533 case Mips::ATOMIC_LOAD_NAND_I32_P8:
1534 return EmitAtomicBinary(MI, BB, 4, 0, true);
1535 case Mips::ATOMIC_LOAD_NAND_I64:
1536 case Mips::ATOMIC_LOAD_NAND_I64_P8:
1537 return EmitAtomicBinary(MI, BB, 8, 0, true);
1539 case Mips::ATOMIC_LOAD_SUB_I8:
1540 case Mips::ATOMIC_LOAD_SUB_I8_P8:
1541 return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
1542 case Mips::ATOMIC_LOAD_SUB_I16:
1543 case Mips::ATOMIC_LOAD_SUB_I16_P8:
1544 return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
1545 case Mips::ATOMIC_LOAD_SUB_I32:
1546 case Mips::ATOMIC_LOAD_SUB_I32_P8:
1547 return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
1548 case Mips::ATOMIC_LOAD_SUB_I64:
1549 case Mips::ATOMIC_LOAD_SUB_I64_P8:
1550 return EmitAtomicBinary(MI, BB, 8, Mips::DSUBu);
1552 case Mips::ATOMIC_SWAP_I8:
1553 case Mips::ATOMIC_SWAP_I8_P8:
1554 return EmitAtomicBinaryPartword(MI, BB, 1, 0);
1555 case Mips::ATOMIC_SWAP_I16:
1556 case Mips::ATOMIC_SWAP_I16_P8:
1557 return EmitAtomicBinaryPartword(MI, BB, 2, 0);
1558 case Mips::ATOMIC_SWAP_I32:
1559 case Mips::ATOMIC_SWAP_I32_P8:
1560 return EmitAtomicBinary(MI, BB, 4, 0);
1561 case Mips::ATOMIC_SWAP_I64:
1562 case Mips::ATOMIC_SWAP_I64_P8:
1563 return EmitAtomicBinary(MI, BB, 8, 0);
1565 case Mips::ATOMIC_CMP_SWAP_I8:
1566 case Mips::ATOMIC_CMP_SWAP_I8_P8:
1567 return EmitAtomicCmpSwapPartword(MI, BB, 1);
1568 case Mips::ATOMIC_CMP_SWAP_I16:
1569 case Mips::ATOMIC_CMP_SWAP_I16_P8:
1570 return EmitAtomicCmpSwapPartword(MI, BB, 2);
1571 case Mips::ATOMIC_CMP_SWAP_I32:
1572 case Mips::ATOMIC_CMP_SWAP_I32_P8:
1573 return EmitAtomicCmpSwap(MI, BB, 4);
1574 case Mips::ATOMIC_CMP_SWAP_I64:
1575 case Mips::ATOMIC_CMP_SWAP_I64_P8:
1576 return EmitAtomicCmpSwap(MI, BB, 8);
1577 case Mips::BPOSGE32_PSEUDO:
1578 return EmitBPOSGE32(MI, BB);
1580 return EmitSel16(Mips::BeqzRxImm16, MI, BB);
1582 return EmitSel16(Mips::BnezRxImm16, MI, BB);
1583 case Mips::SelTBteqZCmpi:
1584 return EmitSeliT16(Mips::BteqzX16, Mips::CmpiRxImmX16, MI, BB);
1585 case Mips::SelTBteqZSlti:
1586 return EmitSeliT16(Mips::BteqzX16, Mips::SltiRxImmX16, MI, BB);
1587 case Mips::SelTBteqZSltiu:
1588 return EmitSeliT16(Mips::BteqzX16, Mips::SltiuRxImmX16, MI, BB);
1589 case Mips::SelTBtneZCmpi:
1590 return EmitSeliT16(Mips::BtnezX16, Mips::CmpiRxImmX16, MI, BB);
1591 case Mips::SelTBtneZSlti:
1592 return EmitSeliT16(Mips::BtnezX16, Mips::SltiRxImmX16, MI, BB);
1593 case Mips::SelTBtneZSltiu:
1594 return EmitSeliT16(Mips::BtnezX16, Mips::SltiuRxImmX16, MI, BB);
1595 case Mips::SelTBteqZCmp:
1596 return EmitSelT16(Mips::BteqzX16, Mips::CmpRxRy16, MI, BB);
1597 case Mips::SelTBteqZSlt:
1598 return EmitSelT16(Mips::BteqzX16, Mips::SltRxRy16, MI, BB);
1599 case Mips::SelTBteqZSltu:
1600 return EmitSelT16(Mips::BteqzX16, Mips::SltuRxRy16, MI, BB);
1601 case Mips::SelTBtneZCmp:
1602 return EmitSelT16(Mips::BtnezX16, Mips::CmpRxRy16, MI, BB);
1603 case Mips::SelTBtneZSlt:
1604 return EmitSelT16(Mips::BtnezX16, Mips::SltRxRy16, MI, BB);
1605 case Mips::SelTBtneZSltu:
1606 return EmitSelT16(Mips::BtnezX16, Mips::SltuRxRy16, MI, BB);
1607 case Mips::BteqzT8CmpX16:
1608 return EmitFEXT_T8I816_ins(Mips::BteqzX16, Mips::CmpRxRy16, MI, BB);
1609 case Mips::BteqzT8SltX16:
1610 return EmitFEXT_T8I816_ins(Mips::BteqzX16, Mips::SltRxRy16, MI, BB);
1611 case Mips::BteqzT8SltuX16:
1612 // TBD: figure out a way to get this or remove the instruction
1614 return EmitFEXT_T8I816_ins(Mips::BteqzX16, Mips::SltuRxRy16, MI, BB);
1615 case Mips::BtnezT8CmpX16:
1616 return EmitFEXT_T8I816_ins(Mips::BtnezX16, Mips::CmpRxRy16, MI, BB);
1617 case Mips::BtnezT8SltX16:
1618 return EmitFEXT_T8I816_ins(Mips::BtnezX16, Mips::SltRxRy16, MI, BB);
1619 case Mips::BtnezT8SltuX16:
1620 // TBD: figure out a way to get this or remove the instruction
1622 return EmitFEXT_T8I816_ins(Mips::BtnezX16, Mips::SltuRxRy16, MI, BB);
1623 case Mips::BteqzT8CmpiX16: return EmitFEXT_T8I8I16_ins(
1624 Mips::BteqzX16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, MI, BB);
1625 case Mips::BteqzT8SltiX16: return EmitFEXT_T8I8I16_ins(
1626 Mips::BteqzX16, Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
1627 case Mips::BteqzT8SltiuX16: return EmitFEXT_T8I8I16_ins(
1628 Mips::BteqzX16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
1629 case Mips::BtnezT8CmpiX16: return EmitFEXT_T8I8I16_ins(
1630 Mips::BtnezX16, Mips::CmpiRxImm16, Mips::CmpiRxImmX16, MI, BB);
1631 case Mips::BtnezT8SltiX16: return EmitFEXT_T8I8I16_ins(
1632 Mips::BtnezX16, Mips::SltiRxImm16, Mips::SltiRxImmX16, MI, BB);
1633 case Mips::BtnezT8SltiuX16: return EmitFEXT_T8I8I16_ins(
1634 Mips::BtnezX16, Mips::SltiuRxImm16, Mips::SltiuRxImmX16, MI, BB);
1639 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
1640 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
1642 MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
1643 unsigned Size, unsigned BinOpcode,
1645 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
1647 MachineFunction *MF = BB->getParent();
1648 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1649 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1650 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1651 DebugLoc dl = MI->getDebugLoc();
1652 unsigned LL, SC, AND, NOR, ZERO, BEQ;
1655 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1656 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1663 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1664 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1667 ZERO = Mips::ZERO_64;
1671 unsigned OldVal = MI->getOperand(0).getReg();
1672 unsigned Ptr = MI->getOperand(1).getReg();
1673 unsigned Incr = MI->getOperand(2).getReg();
1675 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1676 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1677 unsigned Success = RegInfo.createVirtualRegister(RC);
1679 // insert new blocks after the current block
1680 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1681 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1682 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1683 MachineFunction::iterator It = BB;
1685 MF->insert(It, loopMBB);
1686 MF->insert(It, exitMBB);
1688 // Transfer the remainder of BB and its successor edges to exitMBB.
1689 exitMBB->splice(exitMBB->begin(), BB,
1690 llvm::next(MachineBasicBlock::iterator(MI)),
1692 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1696 // fallthrough --> loopMBB
1697 BB->addSuccessor(loopMBB);
1698 loopMBB->addSuccessor(loopMBB);
1699 loopMBB->addSuccessor(exitMBB);
1702 // ll oldval, 0(ptr)
1703 // <binop> storeval, oldval, incr
1704 // sc success, storeval, 0(ptr)
1705 // beq success, $0, loopMBB
1707 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
1709 // and andres, oldval, incr
1710 // nor storeval, $0, andres
1711 BuildMI(BB, dl, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1712 BuildMI(BB, dl, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
1713 } else if (BinOpcode) {
1714 // <binop> storeval, oldval, incr
1715 BuildMI(BB, dl, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
1719 BuildMI(BB, dl, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1720 BuildMI(BB, dl, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
1722 MI->eraseFromParent(); // The instruction is gone now.
1728 MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
1729 MachineBasicBlock *BB,
1730 unsigned Size, unsigned BinOpcode,
1732 assert((Size == 1 || Size == 2) &&
1733 "Unsupported size for EmitAtomicBinaryPartial.");
1735 MachineFunction *MF = BB->getParent();
1736 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1737 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1738 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1739 DebugLoc dl = MI->getDebugLoc();
1740 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1741 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1743 unsigned Dest = MI->getOperand(0).getReg();
1744 unsigned Ptr = MI->getOperand(1).getReg();
1745 unsigned Incr = MI->getOperand(2).getReg();
1747 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1748 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1749 unsigned Mask = RegInfo.createVirtualRegister(RC);
1750 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1751 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1752 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1753 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
1754 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1755 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1756 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1757 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1758 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
1759 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1760 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1761 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1762 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1763 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1764 unsigned Success = RegInfo.createVirtualRegister(RC);
1766 // insert new blocks after the current block
1767 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1768 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1769 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1770 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1771 MachineFunction::iterator It = BB;
1773 MF->insert(It, loopMBB);
1774 MF->insert(It, sinkMBB);
1775 MF->insert(It, exitMBB);
1777 // Transfer the remainder of BB and its successor edges to exitMBB.
1778 exitMBB->splice(exitMBB->begin(), BB,
1779 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
1780 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1782 BB->addSuccessor(loopMBB);
1783 loopMBB->addSuccessor(loopMBB);
1784 loopMBB->addSuccessor(sinkMBB);
1785 sinkMBB->addSuccessor(exitMBB);
1788 // addiu masklsb2,$0,-4 # 0xfffffffc
1789 // and alignedaddr,ptr,masklsb2
1790 // andi ptrlsb2,ptr,3
1791 // sll shiftamt,ptrlsb2,3
1792 // ori maskupper,$0,255 # 0xff
1793 // sll mask,maskupper,shiftamt
1794 // nor mask2,$0,mask
1795 // sll incr2,incr,shiftamt
1797 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1798 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
1799 .addReg(Mips::ZERO).addImm(-4);
1800 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
1801 .addReg(Ptr).addReg(MaskLSB2);
1802 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1803 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1804 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
1805 .addReg(Mips::ZERO).addImm(MaskImm);
1806 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
1807 .addReg(ShiftAmt).addReg(MaskUpper);
1808 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1809 BuildMI(BB, dl, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
1811 // atomic.load.binop
1813 // ll oldval,0(alignedaddr)
1814 // binop binopres,oldval,incr2
1815 // and newval,binopres,mask
1816 // and maskedoldval0,oldval,mask2
1817 // or storeval,maskedoldval0,newval
1818 // sc success,storeval,0(alignedaddr)
1819 // beq success,$0,loopMBB
1823 // ll oldval,0(alignedaddr)
1824 // and newval,incr2,mask
1825 // and maskedoldval0,oldval,mask2
1826 // or storeval,maskedoldval0,newval
1827 // sc success,storeval,0(alignedaddr)
1828 // beq success,$0,loopMBB
1831 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
1833 // and andres, oldval, incr2
1834 // nor binopres, $0, andres
1835 // and newval, binopres, mask
1836 BuildMI(BB, dl, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1837 BuildMI(BB, dl, TII->get(Mips::NOR), BinOpRes)
1838 .addReg(Mips::ZERO).addReg(AndRes);
1839 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1840 } else if (BinOpcode) {
1841 // <binop> binopres, oldval, incr2
1842 // and newval, binopres, mask
1843 BuildMI(BB, dl, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1844 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1845 } else {// atomic.swap
1846 // and newval, incr2, mask
1847 BuildMI(BB, dl, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
1850 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
1851 .addReg(OldVal).addReg(Mask2);
1852 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
1853 .addReg(MaskedOldVal0).addReg(NewVal);
1854 BuildMI(BB, dl, TII->get(SC), Success)
1855 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1856 BuildMI(BB, dl, TII->get(Mips::BEQ))
1857 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
1860 // and maskedoldval1,oldval,mask
1861 // srl srlres,maskedoldval1,shiftamt
1862 // sll sllres,srlres,24
1863 // sra dest,sllres,24
1865 int64_t ShiftImm = (Size == 1) ? 24 : 16;
1867 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
1868 .addReg(OldVal).addReg(Mask);
1869 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
1870 .addReg(ShiftAmt).addReg(MaskedOldVal1);
1871 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
1872 .addReg(SrlRes).addImm(ShiftImm);
1873 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
1874 .addReg(SllRes).addImm(ShiftImm);
1876 MI->eraseFromParent(); // The instruction is gone now.
1882 MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
1883 MachineBasicBlock *BB,
1884 unsigned Size) const {
1885 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1887 MachineFunction *MF = BB->getParent();
1888 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1889 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1890 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1891 DebugLoc dl = MI->getDebugLoc();
1892 unsigned LL, SC, ZERO, BNE, BEQ;
1895 LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1896 SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1902 LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
1903 SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
1904 ZERO = Mips::ZERO_64;
1909 unsigned Dest = MI->getOperand(0).getReg();
1910 unsigned Ptr = MI->getOperand(1).getReg();
1911 unsigned OldVal = MI->getOperand(2).getReg();
1912 unsigned NewVal = MI->getOperand(3).getReg();
1914 unsigned Success = RegInfo.createVirtualRegister(RC);
1916 // insert new blocks after the current block
1917 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1918 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1919 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1920 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1921 MachineFunction::iterator It = BB;
1923 MF->insert(It, loop1MBB);
1924 MF->insert(It, loop2MBB);
1925 MF->insert(It, exitMBB);
1927 // Transfer the remainder of BB and its successor edges to exitMBB.
1928 exitMBB->splice(exitMBB->begin(), BB,
1929 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
1930 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1934 // fallthrough --> loop1MBB
1935 BB->addSuccessor(loop1MBB);
1936 loop1MBB->addSuccessor(exitMBB);
1937 loop1MBB->addSuccessor(loop2MBB);
1938 loop2MBB->addSuccessor(loop1MBB);
1939 loop2MBB->addSuccessor(exitMBB);
1943 // bne dest, oldval, exitMBB
1945 BuildMI(BB, dl, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1946 BuildMI(BB, dl, TII->get(BNE))
1947 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1950 // sc success, newval, 0(ptr)
1951 // beq success, $0, loop1MBB
1953 BuildMI(BB, dl, TII->get(SC), Success)
1954 .addReg(NewVal).addReg(Ptr).addImm(0);
1955 BuildMI(BB, dl, TII->get(BEQ))
1956 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1958 MI->eraseFromParent(); // The instruction is gone now.
1964 MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
1965 MachineBasicBlock *BB,
1966 unsigned Size) const {
1967 assert((Size == 1 || Size == 2) &&
1968 "Unsupported size for EmitAtomicCmpSwapPartial.");
1970 MachineFunction *MF = BB->getParent();
1971 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1972 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1973 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1974 DebugLoc dl = MI->getDebugLoc();
1975 unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
1976 unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
1978 unsigned Dest = MI->getOperand(0).getReg();
1979 unsigned Ptr = MI->getOperand(1).getReg();
1980 unsigned CmpVal = MI->getOperand(2).getReg();
1981 unsigned NewVal = MI->getOperand(3).getReg();
1983 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1984 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1985 unsigned Mask = RegInfo.createVirtualRegister(RC);
1986 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1987 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1988 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1989 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1990 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1991 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1992 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1993 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1994 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1995 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1996 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1997 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1998 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1999 unsigned SllRes = RegInfo.createVirtualRegister(RC);
2000 unsigned Success = RegInfo.createVirtualRegister(RC);
2002 // insert new blocks after the current block
2003 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2004 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
2005 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
2006 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
2007 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
2008 MachineFunction::iterator It = BB;
2010 MF->insert(It, loop1MBB);
2011 MF->insert(It, loop2MBB);
2012 MF->insert(It, sinkMBB);
2013 MF->insert(It, exitMBB);
2015 // Transfer the remainder of BB and its successor edges to exitMBB.
2016 exitMBB->splice(exitMBB->begin(), BB,
2017 llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
2018 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
2020 BB->addSuccessor(loop1MBB);
2021 loop1MBB->addSuccessor(sinkMBB);
2022 loop1MBB->addSuccessor(loop2MBB);
2023 loop2MBB->addSuccessor(loop1MBB);
2024 loop2MBB->addSuccessor(sinkMBB);
2025 sinkMBB->addSuccessor(exitMBB);
2027 // FIXME: computation of newval2 can be moved to loop2MBB.
2029 // addiu masklsb2,$0,-4 # 0xfffffffc
2030 // and alignedaddr,ptr,masklsb2
2031 // andi ptrlsb2,ptr,3
2032 // sll shiftamt,ptrlsb2,3
2033 // ori maskupper,$0,255 # 0xff
2034 // sll mask,maskupper,shiftamt
2035 // nor mask2,$0,mask
2036 // andi maskedcmpval,cmpval,255
2037 // sll shiftedcmpval,maskedcmpval,shiftamt
2038 // andi maskednewval,newval,255
2039 // sll shiftednewval,maskednewval,shiftamt
2040 int64_t MaskImm = (Size == 1) ? 255 : 65535;
2041 BuildMI(BB, dl, TII->get(Mips::ADDiu), MaskLSB2)
2042 .addReg(Mips::ZERO).addImm(-4);
2043 BuildMI(BB, dl, TII->get(Mips::AND), AlignedAddr)
2044 .addReg(Ptr).addReg(MaskLSB2);
2045 BuildMI(BB, dl, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
2046 BuildMI(BB, dl, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
2047 BuildMI(BB, dl, TII->get(Mips::ORi), MaskUpper)
2048 .addReg(Mips::ZERO).addImm(MaskImm);
2049 BuildMI(BB, dl, TII->get(Mips::SLLV), Mask)
2050 .addReg(ShiftAmt).addReg(MaskUpper);
2051 BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
2052 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedCmpVal)
2053 .addReg(CmpVal).addImm(MaskImm);
2054 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedCmpVal)
2055 .addReg(ShiftAmt).addReg(MaskedCmpVal);
2056 BuildMI(BB, dl, TII->get(Mips::ANDi), MaskedNewVal)
2057 .addReg(NewVal).addImm(MaskImm);
2058 BuildMI(BB, dl, TII->get(Mips::SLLV), ShiftedNewVal)
2059 .addReg(ShiftAmt).addReg(MaskedNewVal);
2062 // ll oldval,0(alginedaddr)
2063 // and maskedoldval0,oldval,mask
2064 // bne maskedoldval0,shiftedcmpval,sinkMBB
2066 BuildMI(BB, dl, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
2067 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal0)
2068 .addReg(OldVal).addReg(Mask);
2069 BuildMI(BB, dl, TII->get(Mips::BNE))
2070 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
2073 // and maskedoldval1,oldval,mask2
2074 // or storeval,maskedoldval1,shiftednewval
2075 // sc success,storeval,0(alignedaddr)
2076 // beq success,$0,loop1MBB
2078 BuildMI(BB, dl, TII->get(Mips::AND), MaskedOldVal1)
2079 .addReg(OldVal).addReg(Mask2);
2080 BuildMI(BB, dl, TII->get(Mips::OR), StoreVal)
2081 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
2082 BuildMI(BB, dl, TII->get(SC), Success)
2083 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
2084 BuildMI(BB, dl, TII->get(Mips::BEQ))
2085 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
2088 // srl srlres,maskedoldval0,shiftamt
2089 // sll sllres,srlres,24
2090 // sra dest,sllres,24
2092 int64_t ShiftImm = (Size == 1) ? 24 : 16;
2094 BuildMI(BB, dl, TII->get(Mips::SRLV), SrlRes)
2095 .addReg(ShiftAmt).addReg(MaskedOldVal0);
2096 BuildMI(BB, dl, TII->get(Mips::SLL), SllRes)
2097 .addReg(SrlRes).addImm(ShiftImm);
2098 BuildMI(BB, dl, TII->get(Mips::SRA), Dest)
2099 .addReg(SllRes).addImm(ShiftImm);
2101 MI->eraseFromParent(); // The instruction is gone now.
2106 //===----------------------------------------------------------------------===//
2107 // Misc Lower Operation implementation
2108 //===----------------------------------------------------------------------===//
2109 SDValue MipsTargetLowering::
2110 LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
2112 // The first operand is the chain, the second is the condition, the third is
2113 // the block to branch to if the condition is true.
2114 SDValue Chain = Op.getOperand(0);
2115 SDValue Dest = Op.getOperand(2);
2116 DebugLoc dl = Op.getDebugLoc();
2118 SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
2120 // Return if flag is not set by a floating point comparison.
2121 if (CondRes.getOpcode() != MipsISD::FPCmp)
2124 SDValue CCNode = CondRes.getOperand(2);
2126 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
2127 SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
2129 return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
2133 SDValue MipsTargetLowering::
2134 LowerSELECT(SDValue Op, SelectionDAG &DAG) const
2136 SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
2138 // Return if flag is not set by a floating point comparison.
2139 if (Cond.getOpcode() != MipsISD::FPCmp)
2142 return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
2146 SDValue MipsTargetLowering::
2147 LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
2149 DebugLoc DL = Op.getDebugLoc();
2150 EVT Ty = Op.getOperand(0).getValueType();
2151 SDValue Cond = DAG.getNode(ISD::SETCC, DL, getSetCCResultType(Ty),
2152 Op.getOperand(0), Op.getOperand(1),
2155 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
2159 SDValue MipsTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
2160 SDValue Cond = CreateFPCmp(DAG, Op);
2162 assert(Cond.getOpcode() == MipsISD::FPCmp &&
2163 "Floating point operand expected.");
2165 SDValue True = DAG.getConstant(1, MVT::i32);
2166 SDValue False = DAG.getConstant(0, MVT::i32);
2168 return CreateCMovFP(DAG, Cond, True, False, Op.getDebugLoc());
2171 SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
2172 SelectionDAG &DAG) const {
2173 // FIXME there isn't actually debug info here
2174 DebugLoc dl = Op.getDebugLoc();
2175 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2177 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
2178 const MipsTargetObjectFile &TLOF =
2179 (const MipsTargetObjectFile&)getObjFileLowering();
2181 // %gp_rel relocation
2182 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
2183 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
2185 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl,
2186 DAG.getVTList(MVT::i32), &GA, 1);
2187 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
2188 return DAG.getNode(ISD::ADD, dl, MVT::i32, GPReg, GPRelNode);
2191 // %hi/%lo relocation
2192 return getAddrNonPIC(Op, DAG);
2195 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
2196 return getAddrLocal(Op, DAG, HasMips64);
2199 return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
2200 MipsII::MO_GOT_LO16);
2202 return getAddrGlobal(Op, DAG,
2203 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
2206 SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
2207 SelectionDAG &DAG) const {
2208 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
2209 return getAddrNonPIC(Op, DAG);
2211 return getAddrLocal(Op, DAG, HasMips64);
2214 SDValue MipsTargetLowering::
2215 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
2217 // If the relocation model is PIC, use the General Dynamic TLS Model or
2218 // Local Dynamic TLS model, otherwise use the Initial Exec or
2219 // Local Exec TLS Model.
2221 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
2222 DebugLoc dl = GA->getDebugLoc();
2223 const GlobalValue *GV = GA->getGlobal();
2224 EVT PtrVT = getPointerTy();
2226 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
2228 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
2229 // General Dynamic and Local Dynamic TLS Model.
2230 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
2233 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, Flag);
2234 SDValue Argument = DAG.getNode(MipsISD::Wrapper, dl, PtrVT,
2235 GetGlobalReg(DAG, PtrVT), TGA);
2236 unsigned PtrSize = PtrVT.getSizeInBits();
2237 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
2239 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
2243 Entry.Node = Argument;
2245 Args.push_back(Entry);
2247 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
2248 false, false, false, false, 0, CallingConv::C,
2249 /*isTailCall=*/false, /*doesNotRet=*/false,
2250 /*isReturnValueUsed=*/true,
2251 TlsGetAddr, Args, DAG, dl);
2252 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
2254 SDValue Ret = CallResult.first;
2256 if (model != TLSModel::LocalDynamic)
2259 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2260 MipsII::MO_DTPREL_HI);
2261 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
2262 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2263 MipsII::MO_DTPREL_LO);
2264 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
2265 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Ret);
2266 return DAG.getNode(ISD::ADD, dl, PtrVT, Add, Lo);
2270 if (model == TLSModel::InitialExec) {
2271 // Initial Exec TLS Model
2272 SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2273 MipsII::MO_GOTTPREL);
2274 TGA = DAG.getNode(MipsISD::Wrapper, dl, PtrVT, GetGlobalReg(DAG, PtrVT),
2276 Offset = DAG.getLoad(PtrVT, dl,
2277 DAG.getEntryNode(), TGA, MachinePointerInfo(),
2278 false, false, false, 0);
2280 // Local Exec TLS Model
2281 assert(model == TLSModel::LocalExec);
2282 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2283 MipsII::MO_TPREL_HI);
2284 SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
2285 MipsII::MO_TPREL_LO);
2286 SDValue Hi = DAG.getNode(MipsISD::Hi, dl, PtrVT, TGAHi);
2287 SDValue Lo = DAG.getNode(MipsISD::Lo, dl, PtrVT, TGALo);
2288 Offset = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
2291 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
2292 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
2295 SDValue MipsTargetLowering::
2296 LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
2298 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
2299 return getAddrNonPIC(Op, DAG);
2301 return getAddrLocal(Op, DAG, HasMips64);
2304 SDValue MipsTargetLowering::
2305 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
2307 // gp_rel relocation
2308 // FIXME: we should reference the constant pool using small data sections,
2309 // but the asm printer currently doesn't support this feature without
2310 // hacking it. This feature should come soon so we can uncomment the
2312 //if (IsInSmallSection(C->getType())) {
2313 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
2314 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
2315 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
2317 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
2318 return getAddrNonPIC(Op, DAG);
2320 return getAddrLocal(Op, DAG, HasMips64);
2323 SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
2324 MachineFunction &MF = DAG.getMachineFunction();
2325 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2327 DebugLoc dl = Op.getDebugLoc();
2328 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
2331 // vastart just stores the address of the VarArgsFrameIndex slot into the
2332 // memory location argument.
2333 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2334 return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
2335 MachinePointerInfo(SV), false, false, 0);
2338 static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2339 EVT TyX = Op.getOperand(0).getValueType();
2340 EVT TyY = Op.getOperand(1).getValueType();
2341 SDValue Const1 = DAG.getConstant(1, MVT::i32);
2342 SDValue Const31 = DAG.getConstant(31, MVT::i32);
2343 DebugLoc DL = Op.getDebugLoc();
2346 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2348 SDValue X = (TyX == MVT::f32) ?
2349 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2350 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2352 SDValue Y = (TyY == MVT::f32) ?
2353 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
2354 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
2358 // ext E, Y, 31, 1 ; extract bit31 of Y
2359 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
2360 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
2361 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
2364 // srl SrlX, SllX, 1
2366 // sll SllY, SrlX, 31
2367 // or Or, SrlX, SllY
2368 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2369 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2370 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
2371 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
2372 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
2375 if (TyX == MVT::f32)
2376 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
2378 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2379 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
2380 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2383 static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2384 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
2385 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
2386 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
2387 SDValue Const1 = DAG.getConstant(1, MVT::i32);
2388 DebugLoc DL = Op.getDebugLoc();
2390 // Bitcast to integer nodes.
2391 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
2392 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
2395 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
2396 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
2397 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
2398 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
2400 if (WidthX > WidthY)
2401 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
2402 else if (WidthY > WidthX)
2403 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
2405 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
2406 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
2407 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
2410 // (d)sll SllX, X, 1
2411 // (d)srl SrlX, SllX, 1
2412 // (d)srl SrlY, Y, width(Y)-1
2413 // (d)sll SllY, SrlX, width(Y)-1
2414 // or Or, SrlX, SllY
2415 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
2416 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
2417 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
2418 DAG.getConstant(WidthY - 1, MVT::i32));
2420 if (WidthX > WidthY)
2421 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
2422 else if (WidthY > WidthX)
2423 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
2425 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
2426 DAG.getConstant(WidthX - 1, MVT::i32));
2427 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
2428 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
2432 MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
2433 if (Subtarget->hasMips64())
2434 return LowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
2436 return LowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
2439 static SDValue LowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2440 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
2441 DebugLoc DL = Op.getDebugLoc();
2443 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
2445 SDValue X = (Op.getValueType() == MVT::f32) ?
2446 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
2447 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
2452 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
2453 DAG.getRegister(Mips::ZERO, MVT::i32),
2454 DAG.getConstant(31, MVT::i32), Const1, X);
2456 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
2457 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
2460 if (Op.getValueType() == MVT::f32)
2461 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
2463 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2464 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
2465 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
2468 static SDValue LowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
2469 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
2470 DebugLoc DL = Op.getDebugLoc();
2472 // Bitcast to integer node.
2473 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
2477 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
2478 DAG.getRegister(Mips::ZERO_64, MVT::i64),
2479 DAG.getConstant(63, MVT::i32), Const1, X);
2481 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
2482 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
2485 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
2489 MipsTargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const {
2490 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
2491 return LowerFABS64(Op, DAG, Subtarget->hasMips32r2());
2493 return LowerFABS32(Op, DAG, Subtarget->hasMips32r2());
2496 SDValue MipsTargetLowering::
2497 LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
2499 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2500 "Frame address can only be determined for current frame.");
2502 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2503 MFI->setFrameAddressIsTaken(true);
2504 EVT VT = Op.getValueType();
2505 DebugLoc dl = Op.getDebugLoc();
2506 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
2507 IsN64 ? Mips::FP_64 : Mips::FP, VT);
2511 SDValue MipsTargetLowering::LowerRETURNADDR(SDValue Op,
2512 SelectionDAG &DAG) const {
2514 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
2515 "Return address can be determined only for current frame.");
2517 MachineFunction &MF = DAG.getMachineFunction();
2518 MachineFrameInfo *MFI = MF.getFrameInfo();
2519 MVT VT = Op.getSimpleValueType();
2520 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
2521 MFI->setReturnAddressIsTaken(true);
2523 // Return RA, which contains the return address. Mark it an implicit live-in.
2524 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
2525 return DAG.getCopyFromReg(DAG.getEntryNode(), Op.getDebugLoc(), Reg, VT);
2528 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is
2529 // generated from __builtin_eh_return (offset, handler)
2530 // The effect of this is to adjust the stack pointer by "offset"
2531 // and then branch to "handler".
2532 SDValue MipsTargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
2534 MachineFunction &MF = DAG.getMachineFunction();
2535 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2537 MipsFI->setCallsEhReturn();
2538 SDValue Chain = Op.getOperand(0);
2539 SDValue Offset = Op.getOperand(1);
2540 SDValue Handler = Op.getOperand(2);
2541 DebugLoc DL = Op.getDebugLoc();
2542 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2544 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
2545 // EH_RETURN nodes, so that instructions are emitted back-to-back.
2546 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
2547 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
2548 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
2549 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
2550 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
2551 DAG.getRegister(OffsetReg, Ty),
2552 DAG.getRegister(AddrReg, getPointerTy()),
2556 // TODO: set SType according to the desired memory barrier behavior.
2558 MipsTargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const {
2560 DebugLoc dl = Op.getDebugLoc();
2561 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
2562 DAG.getConstant(SType, MVT::i32));
2565 SDValue MipsTargetLowering::LowerATOMIC_FENCE(SDValue Op,
2566 SelectionDAG &DAG) const {
2567 // FIXME: Need pseudo-fence for 'singlethread' fences
2568 // FIXME: Set SType for weaker fences where supported/appropriate.
2570 DebugLoc dl = Op.getDebugLoc();
2571 return DAG.getNode(MipsISD::Sync, dl, MVT::Other, Op.getOperand(0),
2572 DAG.getConstant(SType, MVT::i32));
2575 SDValue MipsTargetLowering::LowerShiftLeftParts(SDValue Op,
2576 SelectionDAG &DAG) const {
2577 DebugLoc DL = Op.getDebugLoc();
2578 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2579 SDValue Shamt = Op.getOperand(2);
2582 // lo = (shl lo, shamt)
2583 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
2586 // hi = (shl lo, shamt[4:0])
2587 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2588 DAG.getConstant(-1, MVT::i32));
2589 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
2590 DAG.getConstant(1, MVT::i32));
2591 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
2593 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
2594 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2595 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
2596 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2597 DAG.getConstant(0x20, MVT::i32));
2598 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2599 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
2600 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
2602 SDValue Ops[2] = {Lo, Hi};
2603 return DAG.getMergeValues(Ops, 2, DL);
2606 SDValue MipsTargetLowering::LowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
2608 DebugLoc DL = Op.getDebugLoc();
2609 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
2610 SDValue Shamt = Op.getOperand(2);
2613 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
2615 // hi = (sra hi, shamt)
2617 // hi = (srl hi, shamt)
2620 // lo = (sra hi, shamt[4:0])
2621 // hi = (sra hi, 31)
2623 // lo = (srl hi, shamt[4:0])
2625 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
2626 DAG.getConstant(-1, MVT::i32));
2627 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
2628 DAG.getConstant(1, MVT::i32));
2629 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
2630 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
2631 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
2632 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2634 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
2635 DAG.getConstant(0x20, MVT::i32));
2636 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
2637 DAG.getConstant(31, MVT::i32));
2638 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
2639 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
2640 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
2643 SDValue Ops[2] = {Lo, Hi};
2644 return DAG.getMergeValues(Ops, 2, DL);
2647 static SDValue CreateLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2648 SDValue Chain, SDValue Src, unsigned Offset) {
2649 SDValue Ptr = LD->getBasePtr();
2650 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2651 EVT BasePtrVT = Ptr.getValueType();
2652 DebugLoc DL = LD->getDebugLoc();
2653 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2656 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2657 DAG.getConstant(Offset, BasePtrVT));
2659 SDValue Ops[] = { Chain, Ptr, Src };
2660 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2661 LD->getMemOperand());
2664 // Expand an unaligned 32 or 64-bit integer load node.
2665 SDValue MipsTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2666 LoadSDNode *LD = cast<LoadSDNode>(Op);
2667 EVT MemVT = LD->getMemoryVT();
2669 // Return if load is aligned or if MemVT is neither i32 nor i64.
2670 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2671 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2674 bool IsLittle = Subtarget->isLittle();
2675 EVT VT = Op.getValueType();
2676 ISD::LoadExtType ExtType = LD->getExtensionType();
2677 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2679 assert((VT == MVT::i32) || (VT == MVT::i64));
2682 // (set dst, (i64 (load baseptr)))
2684 // (set tmp, (ldl (add baseptr, 7), undef))
2685 // (set dst, (ldr baseptr, tmp))
2686 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2687 SDValue LDL = CreateLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2689 return CreateLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2693 SDValue LWL = CreateLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2695 SDValue LWR = CreateLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2699 // (set dst, (i32 (load baseptr))) or
2700 // (set dst, (i64 (sextload baseptr))) or
2701 // (set dst, (i64 (extload baseptr)))
2703 // (set tmp, (lwl (add baseptr, 3), undef))
2704 // (set dst, (lwr baseptr, tmp))
2705 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2706 (ExtType == ISD::EXTLOAD))
2709 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2712 // (set dst, (i64 (zextload baseptr)))
2714 // (set tmp0, (lwl (add baseptr, 3), undef))
2715 // (set tmp1, (lwr baseptr, tmp0))
2716 // (set tmp2, (shl tmp1, 32))
2717 // (set dst, (srl tmp2, 32))
2718 DebugLoc DL = LD->getDebugLoc();
2719 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2720 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2721 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2722 SDValue Ops[] = { SRL, LWR.getValue(1) };
2723 return DAG.getMergeValues(Ops, 2, DL);
2726 static SDValue CreateStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2727 SDValue Chain, unsigned Offset) {
2728 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2729 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2730 DebugLoc DL = SD->getDebugLoc();
2731 SDVTList VTList = DAG.getVTList(MVT::Other);
2734 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2735 DAG.getConstant(Offset, BasePtrVT));
2737 SDValue Ops[] = { Chain, Value, Ptr };
2738 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2739 SD->getMemOperand());
2742 // Expand an unaligned 32 or 64-bit integer store node.
2743 SDValue MipsTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2744 StoreSDNode *SD = cast<StoreSDNode>(Op);
2745 EVT MemVT = SD->getMemoryVT();
2747 // Return if store is aligned or if MemVT is neither i32 nor i64.
2748 if ((SD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2749 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2752 bool IsLittle = Subtarget->isLittle();
2753 SDValue Value = SD->getValue(), Chain = SD->getChain();
2754 EVT VT = Value.getValueType();
2757 // (store val, baseptr) or
2758 // (truncstore val, baseptr)
2760 // (swl val, (add baseptr, 3))
2761 // (swr val, baseptr)
2762 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2763 SDValue SWL = CreateStoreLR(MipsISD::SWL, DAG, SD, Chain,
2765 return CreateStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2768 assert(VT == MVT::i64);
2771 // (store val, baseptr)
2773 // (sdl val, (add baseptr, 7))
2774 // (sdr val, baseptr)
2775 SDValue SDL = CreateStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2776 return CreateStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2779 // This function expands mips intrinsic nodes which have 64-bit input operands
2780 // or output values.
2782 // out64 = intrinsic-node in64
2784 // lo = copy (extract-element (in64, 0))
2785 // hi = copy (extract-element (in64, 1))
2786 // mips-specific-node
2789 // out64 = merge-values (v0, v1)
2791 static SDValue LowerDSPIntr(SDValue Op, SelectionDAG &DAG,
2792 unsigned Opc, bool HasI64In, bool HasI64Out) {
2793 DebugLoc DL = Op.getDebugLoc();
2794 bool HasChainIn = Op->getOperand(0).getValueType() == MVT::Other;
2795 SDValue Chain = HasChainIn ? Op->getOperand(0) : DAG.getEntryNode();
2796 SmallVector<SDValue, 3> Ops;
2799 SDValue InLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
2800 Op->getOperand(1 + HasChainIn),
2801 DAG.getConstant(0, MVT::i32));
2802 SDValue InHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
2803 Op->getOperand(1 + HasChainIn),
2804 DAG.getConstant(1, MVT::i32));
2806 Chain = DAG.getCopyToReg(Chain, DL, Mips::LO, InLo, SDValue());
2807 Chain = DAG.getCopyToReg(Chain, DL, Mips::HI, InHi, Chain.getValue(1));
2809 Ops.push_back(Chain);
2810 Ops.append(Op->op_begin() + HasChainIn + 2, Op->op_end());
2811 Ops.push_back(Chain.getValue(1));
2813 Ops.push_back(Chain);
2814 Ops.append(Op->op_begin() + HasChainIn + 1, Op->op_end());
2818 return DAG.getNode(Opc, DL, Op->value_begin(), Op->getNumValues(),
2819 Ops.begin(), Ops.size());
2821 SDValue Intr = DAG.getNode(Opc, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2822 Ops.begin(), Ops.size());
2823 SDValue OutLo = DAG.getCopyFromReg(Intr.getValue(0), DL, Mips::LO, MVT::i32,
2825 SDValue OutHi = DAG.getCopyFromReg(OutLo.getValue(1), DL, Mips::HI, MVT::i32,
2827 SDValue Out = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, OutLo, OutHi);
2832 SDValue Vals[] = { Out, OutHi.getValue(1) };
2833 return DAG.getMergeValues(Vals, 2, DL);
2836 SDValue MipsTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2837 SelectionDAG &DAG) const {
2838 switch (cast<ConstantSDNode>(Op->getOperand(0))->getZExtValue()) {
2841 case Intrinsic::mips_shilo:
2842 return LowerDSPIntr(Op, DAG, MipsISD::SHILO, true, true);
2843 case Intrinsic::mips_dpau_h_qbl:
2844 return LowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBL, true, true);
2845 case Intrinsic::mips_dpau_h_qbr:
2846 return LowerDSPIntr(Op, DAG, MipsISD::DPAU_H_QBR, true, true);
2847 case Intrinsic::mips_dpsu_h_qbl:
2848 return LowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBL, true, true);
2849 case Intrinsic::mips_dpsu_h_qbr:
2850 return LowerDSPIntr(Op, DAG, MipsISD::DPSU_H_QBR, true, true);
2851 case Intrinsic::mips_dpa_w_ph:
2852 return LowerDSPIntr(Op, DAG, MipsISD::DPA_W_PH, true, true);
2853 case Intrinsic::mips_dps_w_ph:
2854 return LowerDSPIntr(Op, DAG, MipsISD::DPS_W_PH, true, true);
2855 case Intrinsic::mips_dpax_w_ph:
2856 return LowerDSPIntr(Op, DAG, MipsISD::DPAX_W_PH, true, true);
2857 case Intrinsic::mips_dpsx_w_ph:
2858 return LowerDSPIntr(Op, DAG, MipsISD::DPSX_W_PH, true, true);
2859 case Intrinsic::mips_mulsa_w_ph:
2860 return LowerDSPIntr(Op, DAG, MipsISD::MULSA_W_PH, true, true);
2861 case Intrinsic::mips_mult:
2862 return LowerDSPIntr(Op, DAG, MipsISD::MULT, false, true);
2863 case Intrinsic::mips_multu:
2864 return LowerDSPIntr(Op, DAG, MipsISD::MULTU, false, true);
2865 case Intrinsic::mips_madd:
2866 return LowerDSPIntr(Op, DAG, MipsISD::MADD_DSP, true, true);
2867 case Intrinsic::mips_maddu:
2868 return LowerDSPIntr(Op, DAG, MipsISD::MADDU_DSP, true, true);
2869 case Intrinsic::mips_msub:
2870 return LowerDSPIntr(Op, DAG, MipsISD::MSUB_DSP, true, true);
2871 case Intrinsic::mips_msubu:
2872 return LowerDSPIntr(Op, DAG, MipsISD::MSUBU_DSP, true, true);
2876 SDValue MipsTargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2877 SelectionDAG &DAG) const {
2878 switch (cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue()) {
2881 case Intrinsic::mips_extp:
2882 return LowerDSPIntr(Op, DAG, MipsISD::EXTP, true, false);
2883 case Intrinsic::mips_extpdp:
2884 return LowerDSPIntr(Op, DAG, MipsISD::EXTPDP, true, false);
2885 case Intrinsic::mips_extr_w:
2886 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_W, true, false);
2887 case Intrinsic::mips_extr_r_w:
2888 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_R_W, true, false);
2889 case Intrinsic::mips_extr_rs_w:
2890 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_RS_W, true, false);
2891 case Intrinsic::mips_extr_s_h:
2892 return LowerDSPIntr(Op, DAG, MipsISD::EXTR_S_H, true, false);
2893 case Intrinsic::mips_mthlip:
2894 return LowerDSPIntr(Op, DAG, MipsISD::MTHLIP, true, true);
2895 case Intrinsic::mips_mulsaq_s_w_ph:
2896 return LowerDSPIntr(Op, DAG, MipsISD::MULSAQ_S_W_PH, true, true);
2897 case Intrinsic::mips_maq_s_w_phl:
2898 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHL, true, true);
2899 case Intrinsic::mips_maq_s_w_phr:
2900 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_S_W_PHR, true, true);
2901 case Intrinsic::mips_maq_sa_w_phl:
2902 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHL, true, true);
2903 case Intrinsic::mips_maq_sa_w_phr:
2904 return LowerDSPIntr(Op, DAG, MipsISD::MAQ_SA_W_PHR, true, true);
2905 case Intrinsic::mips_dpaq_s_w_ph:
2906 return LowerDSPIntr(Op, DAG, MipsISD::DPAQ_S_W_PH, true, true);
2907 case Intrinsic::mips_dpsq_s_w_ph:
2908 return LowerDSPIntr(Op, DAG, MipsISD::DPSQ_S_W_PH, true, true);
2909 case Intrinsic::mips_dpaq_sa_l_w:
2910 return LowerDSPIntr(Op, DAG, MipsISD::DPAQ_SA_L_W, true, true);
2911 case Intrinsic::mips_dpsq_sa_l_w:
2912 return LowerDSPIntr(Op, DAG, MipsISD::DPSQ_SA_L_W, true, true);
2913 case Intrinsic::mips_dpaqx_s_w_ph:
2914 return LowerDSPIntr(Op, DAG, MipsISD::DPAQX_S_W_PH, true, true);
2915 case Intrinsic::mips_dpaqx_sa_w_ph:
2916 return LowerDSPIntr(Op, DAG, MipsISD::DPAQX_SA_W_PH, true, true);
2917 case Intrinsic::mips_dpsqx_s_w_ph:
2918 return LowerDSPIntr(Op, DAG, MipsISD::DPSQX_S_W_PH, true, true);
2919 case Intrinsic::mips_dpsqx_sa_w_ph:
2920 return LowerDSPIntr(Op, DAG, MipsISD::DPSQX_SA_W_PH, true, true);
2924 SDValue MipsTargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) const {
2925 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2926 || cast<ConstantSDNode>
2927 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2928 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2932 // (add (frameaddr 0), (frame_to_args_offset))
2933 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2934 // (add FrameObject, 0)
2935 // where FrameObject is a fixed StackObject with offset 0 which points to
2936 // the old stack pointer.
2937 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2938 EVT ValTy = Op->getValueType(0);
2939 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2940 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2941 return DAG.getNode(ISD::ADD, Op->getDebugLoc(), ValTy, InArgsAddr,
2942 DAG.getConstant(0, ValTy));
2945 //===----------------------------------------------------------------------===//
2946 // Calling Convention Implementation
2947 //===----------------------------------------------------------------------===//
2949 //===----------------------------------------------------------------------===//
2950 // TODO: Implement a generic logic using tblgen that can support this.
2951 // Mips O32 ABI rules:
2953 // i32 - Passed in A0, A1, A2, A3 and stack
2954 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
2955 // an argument. Otherwise, passed in A1, A2, A3 and stack.
2956 // f64 - Only passed in two aliased f32 registers if no int reg has been used
2957 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2958 // not used, it must be shadowed. If only A3 is avaiable, shadow it and
2961 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2962 //===----------------------------------------------------------------------===//
2964 static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
2965 MVT LocVT, CCValAssign::LocInfo LocInfo,
2966 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2968 static const unsigned IntRegsSize=4, FloatRegsSize=2;
2970 static const uint16_t IntRegs[] = {
2971 Mips::A0, Mips::A1, Mips::A2, Mips::A3
2973 static const uint16_t F32Regs[] = {
2974 Mips::F12, Mips::F14
2976 static const uint16_t F64Regs[] = {
2980 // Do not process byval args here.
2981 if (ArgFlags.isByVal())
2984 // Promote i8 and i16
2985 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2987 if (ArgFlags.isSExt())
2988 LocInfo = CCValAssign::SExt;
2989 else if (ArgFlags.isZExt())
2990 LocInfo = CCValAssign::ZExt;
2992 LocInfo = CCValAssign::AExt;
2997 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2998 // is true: function is vararg, argument is 3rd or higher, there is previous
2999 // argument which is not f32 or f64.
3000 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
3001 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
3002 unsigned OrigAlign = ArgFlags.getOrigAlign();
3003 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
3005 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
3006 Reg = State.AllocateReg(IntRegs, IntRegsSize);
3007 // If this is the first part of an i64 arg,
3008 // the allocated register must be either A0 or A2.
3009 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
3010 Reg = State.AllocateReg(IntRegs, IntRegsSize);
3012 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
3013 // Allocate int register and shadow next int register. If first
3014 // available register is Mips::A1 or Mips::A3, shadow it too.
3015 Reg = State.AllocateReg(IntRegs, IntRegsSize);
3016 if (Reg == Mips::A1 || Reg == Mips::A3)
3017 Reg = State.AllocateReg(IntRegs, IntRegsSize);
3018 State.AllocateReg(IntRegs, IntRegsSize);
3020 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
3021 // we are guaranteed to find an available float register
3022 if (ValVT == MVT::f32) {
3023 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
3024 // Shadow int register
3025 State.AllocateReg(IntRegs, IntRegsSize);
3027 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
3028 // Shadow int registers
3029 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
3030 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
3031 State.AllocateReg(IntRegs, IntRegsSize);
3032 State.AllocateReg(IntRegs, IntRegsSize);
3035 llvm_unreachable("Cannot handle this ValVT.");
3038 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
3040 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
3042 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
3047 #include "MipsGenCallingConv.inc"
3049 //===----------------------------------------------------------------------===//
3050 // Call Calling Convention Implementation
3051 //===----------------------------------------------------------------------===//
3053 static const unsigned O32IntRegsSize = 4;
3055 // Return next O32 integer argument register.
3056 static unsigned getNextIntArgReg(unsigned Reg) {
3057 assert((Reg == Mips::A0) || (Reg == Mips::A2));
3058 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
3061 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
3062 /// for tail call optimization.
3063 bool MipsTargetLowering::
3064 IsEligibleForTailCallOptimization(const MipsCC &MipsCCInfo,
3065 unsigned NextStackOffset,
3066 const MipsFunctionInfo& FI) const {
3067 if (!EnableMipsTailCalls)
3070 // No tail call optimization for mips16.
3071 if (Subtarget->inMips16Mode())
3074 // Return false if either the callee or caller has a byval argument.
3075 if (MipsCCInfo.hasByValArg() || FI.hasByvalArg())
3078 // Return true if the callee's argument area is no larger than the
3080 return NextStackOffset <= FI.getIncomingArgSize();
3084 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
3085 SDValue Chain, SDValue Arg, DebugLoc DL,
3086 bool IsTailCall, SelectionDAG &DAG) const {
3088 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
3089 DAG.getIntPtrConstant(Offset));
3090 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
3094 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3095 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
3096 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3097 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
3098 /*isVolatile=*/ true, false, 0);
3102 // The Mips16 hard float is a crazy quilt inherited from gcc. I have a much
3103 // cleaner way to do all of this but it will have to wait until the traditional
3104 // gcc mechanism is completed.
3106 // For Pic, in order for Mips16 code to call Mips32 code which according the abi
3107 // have either arguments or returned values placed in floating point registers,
3108 // we use a set of helper functions. (This includes functions which return type
3109 // complex which on Mips are returned in a pair of floating point registers).
3111 // This is an encoding that we inherited from gcc.
3112 // In Mips traditional O32, N32 ABI, floating point numbers are passed in
3113 // floating point argument registers 1,2 only when the first and optionally
3114 // the second arguments are float (sf) or double (df).
3115 // For Mips16 we are only concerned with the situations where floating point
3116 // arguments are being passed in floating point registers by the ABI, because
3117 // Mips16 mode code cannot execute floating point instructions to load those
3118 // values and hence helper functions are needed.
3119 // The possibilities are (), (sf), (sf, sf), (sf, df), (df), (df, sf), (df, df)
3120 // the helper function suffixs for these are:
3121 // 0, 1, 5, 9, 2, 6, 10
3122 // this suffix can then be calculated as follows:
3123 // for a given argument Arg:
3124 // Arg1x, Arg2x = 1 : Arg is sf
3126 // 0: Arg is neither sf or df
3127 // So this stub is the string for number Arg1x + Arg2x*4.
3128 // However not all numbers between 0 and 10 are possible, we check anyway and
3129 // assert if the impossible exists.
3132 unsigned int MipsTargetLowering::getMips16HelperFunctionStubNumber
3133 (ArgListTy &Args) const {
3134 unsigned int resultNum = 0;
3135 if (Args.size() >= 1) {
3136 Type *t = Args[0].Ty;
3137 if (t->isFloatTy()) {
3140 else if (t->isDoubleTy()) {
3145 if (Args.size() >=2) {
3146 Type *t = Args[1].Ty;
3147 if (t->isFloatTy()) {
3150 else if (t->isDoubleTy()) {
3159 // prefixs are attached to stub numbers depending on the return type .
3160 // return type: float sf_
3162 // single complex sc_
3163 // double complext dc_
3167 // The full name of a helper function is__mips16_call_stub +
3168 // return type dependent prefix + stub number
3171 // This is something that probably should be in a different source file and
3172 // perhaps done differently but my main purpose is to not waste runtime
3173 // on something that we can enumerate in the source. Another possibility is
3174 // to have a python script to generate these mapping tables. This will do
3175 // for now. There are a whole series of helper function mapping arrays, one
3176 // for each return type class as outlined above. There there are 11 possible
3177 // entries. Ones with 0 are ones which should never be selected
3179 // All the arrays are similar except for ones which return neither
3180 // sf, df, sc, dc, in which only care about ones which have sf or df as a
3183 #define P_ "__mips16_call_stub_"
3184 #define MAX_STUB_NUMBER 10
3185 #define T1 P "1", P "2", 0, 0, P "5", P "6", 0, 0, P "9", P "10"
3186 #define T P "0" , T1
3188 static char const * vMips16Helper[MAX_STUB_NUMBER+1] =
3192 static char const * sfMips16Helper[MAX_STUB_NUMBER+1] =
3196 static char const * dfMips16Helper[MAX_STUB_NUMBER+1] =
3200 static char const * scMips16Helper[MAX_STUB_NUMBER+1] =
3204 static char const * dcMips16Helper[MAX_STUB_NUMBER+1] =
3210 const char* MipsTargetLowering::
3211 getMips16HelperFunction
3212 (Type* RetTy, ArgListTy &Args, bool &needHelper) const {
3213 const unsigned int stubNum = getMips16HelperFunctionStubNumber(Args);
3215 const unsigned int maxStubNum = 10;
3216 assert(stubNum <= maxStubNum);
3217 const bool validStubNum[maxStubNum+1] =
3218 {true, true, true, false, false, true, true, false, false, true, true};
3219 assert(validStubNum[stubNum]);
3222 if (RetTy->isFloatTy()) {
3223 result = sfMips16Helper[stubNum];
3225 else if (RetTy ->isDoubleTy()) {
3226 result = dfMips16Helper[stubNum];
3228 else if (RetTy->isStructTy()) {
3229 // check if it's complex
3230 if (RetTy->getNumContainedTypes() == 2) {
3231 if ((RetTy->getContainedType(0)->isFloatTy()) &&
3232 (RetTy->getContainedType(1)->isFloatTy())) {
3233 result = scMips16Helper[stubNum];
3235 else if ((RetTy->getContainedType(0)->isDoubleTy()) &&
3236 (RetTy->getContainedType(1)->isDoubleTy())) {
3237 result = dcMips16Helper[stubNum];
3240 llvm_unreachable("Uncovered condition");
3244 llvm_unreachable("Uncovered condition");
3252 result = vMips16Helper[stubNum];
3258 /// LowerCall - functions arguments are copied from virtual regs to
3259 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
3261 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
3262 SmallVectorImpl<SDValue> &InVals) const {
3263 SelectionDAG &DAG = CLI.DAG;
3264 DebugLoc &dl = CLI.DL;
3265 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
3266 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
3267 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
3268 SDValue Chain = CLI.Chain;
3269 SDValue Callee = CLI.Callee;
3270 bool &isTailCall = CLI.IsTailCall;
3271 CallingConv::ID CallConv = CLI.CallConv;
3272 bool isVarArg = CLI.IsVarArg;
3274 const char* mips16HelperFunction = 0;
3275 bool needMips16Helper = false;
3277 if (Subtarget->inMips16Mode() && getTargetMachine().Options.UseSoftFloat &&
3280 // currently we don't have symbols tagged with the mips16 or mips32
3281 // qualifier so we will assume that we don't know what kind it is.
3282 // and generate the helper
3284 bool lookupHelper = true;
3285 if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3286 if (noHelperNeeded.find(S->getSymbol()) != noHelperNeeded.end()) {
3287 lookupHelper = false;
3290 if (lookupHelper) mips16HelperFunction =
3291 getMips16HelperFunction(CLI.RetTy, CLI.Args, needMips16Helper);
3294 MachineFunction &MF = DAG.getMachineFunction();
3295 MachineFrameInfo *MFI = MF.getFrameInfo();
3296 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
3297 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
3299 // Analyze operands of the call, assigning locations to each operand.
3300 SmallVector<CCValAssign, 16> ArgLocs;
3301 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3302 getTargetMachine(), ArgLocs, *DAG.getContext());
3303 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
3305 MipsCCInfo.analyzeCallOperands(Outs, isVarArg);
3307 // Get a count of how many bytes are to be pushed on the stack.
3308 unsigned NextStackOffset = CCInfo.getNextStackOffset();
3310 // Check if it's really possible to do a tail call.
3313 IsEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
3314 *MF.getInfo<MipsFunctionInfo>());
3319 // Chain is the output chain of the last Load/Store or CopyToReg node.
3320 // ByValChain is the output chain of the last Memcpy node created for copying
3321 // byval arguments to the stack.
3322 unsigned StackAlignment = TFL->getStackAlignment();
3323 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
3324 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
3327 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal);
3329 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl,
3330 IsN64 ? Mips::SP_64 : Mips::SP,
3333 // With EABI is it possible to have 16 args on registers.
3334 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
3335 SmallVector<SDValue, 8> MemOpChains;
3336 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
3338 // Walk the register/memloc assignments, inserting copies/loads.
3339 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3340 SDValue Arg = OutVals[i];
3341 CCValAssign &VA = ArgLocs[i];
3342 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
3343 ISD::ArgFlagsTy Flags = Outs[i].Flags;
3346 if (Flags.isByVal()) {
3347 assert(Flags.getByValSize() &&
3348 "ByVal args of size 0 should have been ignored by front-end.");
3349 assert(ByValArg != MipsCCInfo.byval_end());
3350 assert(!isTailCall &&
3351 "Do not tail-call optimize if there is a byval argument.");
3352 passByValArg(Chain, dl, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
3353 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
3358 // Promote the value if needed.
3359 switch (VA.getLocInfo()) {
3360 default: llvm_unreachable("Unknown loc info!");
3361 case CCValAssign::Full:
3362 if (VA.isRegLoc()) {
3363 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
3364 (ValVT == MVT::f64 && LocVT == MVT::i64))
3365 Arg = DAG.getNode(ISD::BITCAST, dl, LocVT, Arg);
3366 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
3367 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
3368 Arg, DAG.getConstant(0, MVT::i32));
3369 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
3370 Arg, DAG.getConstant(1, MVT::i32));
3371 if (!Subtarget->isLittle())
3373 unsigned LocRegLo = VA.getLocReg();
3374 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
3375 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
3376 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
3381 case CCValAssign::SExt:
3382 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, LocVT, Arg);
3384 case CCValAssign::ZExt:
3385 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, LocVT, Arg);
3387 case CCValAssign::AExt:
3388 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, LocVT, Arg);
3392 // Arguments that can be passed on register must be kept at
3393 // RegsToPass vector
3394 if (VA.isRegLoc()) {
3395 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3399 // Register can't get to this point...
3400 assert(VA.isMemLoc());
3402 // emit ISD::STORE whichs stores the
3403 // parameter value to a stack Location
3404 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
3405 Chain, Arg, dl, isTailCall, DAG));
3408 // Transform all store nodes into one single node because all store
3409 // nodes are independent of each other.
3410 if (!MemOpChains.empty())
3411 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
3412 &MemOpChains[0], MemOpChains.size());
3414 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
3415 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
3416 // node so that legalize doesn't hack it.
3417 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
3418 bool GlobalOrExternal = false, InternalLinkage = false;
3421 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3423 InternalLinkage = G->getGlobal()->hasInternalLinkage();
3425 if (InternalLinkage)
3426 Callee = getAddrLocal(Callee, DAG, HasMips64);
3428 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
3429 MipsII::MO_CALL_LO16);
3431 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
3433 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(), 0,
3434 MipsII::MO_NO_FLAG);
3435 GlobalOrExternal = true;
3437 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3438 if (!IsN64 && !IsPIC) // !N64 && static
3439 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
3440 MipsII::MO_NO_FLAG);
3442 Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
3443 MipsII::MO_CALL_LO16);
3445 Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
3447 GlobalOrExternal = true;
3450 SDValue JumpTarget = Callee;
3452 // T9 should contain the address of the callee function if
3453 // -reloction-model=pic or it is an indirect call.
3454 if (IsPICCall || !GlobalOrExternal) {
3455 unsigned T9Reg = IsN64 ? Mips::T9_64 : Mips::T9;
3456 unsigned V0Reg = Mips::V0;
3457 if (needMips16Helper) {
3458 RegsToPass.push_front(std::make_pair(V0Reg, Callee));
3459 JumpTarget = DAG.getExternalSymbol(
3460 mips16HelperFunction, getPointerTy());
3461 JumpTarget = getAddrGlobal(JumpTarget, DAG, MipsII::MO_GOT);
3464 RegsToPass.push_front(std::make_pair(T9Reg, Callee));
3466 if (!Subtarget->inMips16Mode())
3467 JumpTarget = SDValue();
3471 // Insert node "GP copy globalreg" before call to function.
3473 // R_MIPS_CALL* operators (emitted when non-internal functions are called
3474 // in PIC mode) allow symbols to be resolved via lazy binding.
3475 // The lazy binding stub requires GP to point to the GOT.
3476 if (IsPICCall && !InternalLinkage) {
3477 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
3478 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
3479 RegsToPass.push_back(std::make_pair(GPReg, GetGlobalReg(DAG, Ty)));
3482 // Build a sequence of copy-to-reg nodes chained together with token
3483 // chain and flag operands which copy the outgoing args into registers.
3484 // The InFlag in necessary since all emitted instructions must be
3488 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3489 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3490 RegsToPass[i].second, InFlag);
3491 InFlag = Chain.getValue(1);
3494 // MipsJmpLink = #chain, #target_address, #opt_in_flags...
3495 // = Chain, Callee, Reg#1, Reg#2, ...
3497 // Returns a chain & a flag for retval copy to use.
3498 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3499 SmallVector<SDValue, 8> Ops(1, Chain);
3501 if (JumpTarget.getNode())
3502 Ops.push_back(JumpTarget);
3504 // Add argument registers to the end of the list so that they are
3505 // known live into the call.
3506 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3507 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3508 RegsToPass[i].second.getValueType()));
3510 // Add a register mask operand representing the call-preserved registers.
3511 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
3512 const uint32_t *Mask = TRI->getCallPreservedMask(CallConv);
3513 assert(Mask && "Missing call preserved mask for calling convention");
3514 Ops.push_back(DAG.getRegisterMask(Mask));
3516 if (InFlag.getNode())
3517 Ops.push_back(InFlag);
3520 return DAG.getNode(MipsISD::TailCall, dl, MVT::Other, &Ops[0], Ops.size());
3522 Chain = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
3523 InFlag = Chain.getValue(1);
3525 // Create the CALLSEQ_END node.
3526 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
3527 DAG.getIntPtrConstant(0, true), InFlag);
3528 InFlag = Chain.getValue(1);
3530 // Handle result values, copying them out of physregs into vregs that we
3532 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3533 Ins, dl, DAG, InVals);
3536 /// LowerCallResult - Lower the result values of a call into the
3537 /// appropriate copies out of appropriate physical registers.
3539 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
3540 CallingConv::ID CallConv, bool isVarArg,
3541 const SmallVectorImpl<ISD::InputArg> &Ins,
3542 DebugLoc dl, SelectionDAG &DAG,
3543 SmallVectorImpl<SDValue> &InVals) const {
3544 // Assign locations to each value returned by this call.
3545 SmallVector<CCValAssign, 16> RVLocs;
3546 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3547 getTargetMachine(), RVLocs, *DAG.getContext());
3549 CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
3551 // Copy all of the result registers out of their specified physreg.
3552 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3553 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
3554 RVLocs[i].getValVT(), InFlag).getValue(1);
3555 InFlag = Chain.getValue(2);
3556 InVals.push_back(Chain.getValue(0));
3562 //===----------------------------------------------------------------------===//
3563 // Formal Arguments Calling Convention Implementation
3564 //===----------------------------------------------------------------------===//
3565 /// LowerFormalArguments - transform physical registers into virtual registers
3566 /// and generate load operations for arguments places on the stack.
3568 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
3569 CallingConv::ID CallConv,
3571 const SmallVectorImpl<ISD::InputArg> &Ins,
3572 DebugLoc dl, SelectionDAG &DAG,
3573 SmallVectorImpl<SDValue> &InVals)
3575 MachineFunction &MF = DAG.getMachineFunction();
3576 MachineFrameInfo *MFI = MF.getFrameInfo();
3577 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3579 MipsFI->setVarArgsFrameIndex(0);
3581 // Used with vargs to acumulate store chains.
3582 std::vector<SDValue> OutChains;
3584 // Assign locations to all of the incoming arguments.
3585 SmallVector<CCValAssign, 16> ArgLocs;
3586 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3587 getTargetMachine(), ArgLocs, *DAG.getContext());
3588 MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
3590 MipsCCInfo.analyzeFormalArguments(Ins);
3591 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
3592 MipsCCInfo.hasByValArg());
3594 Function::const_arg_iterator FuncArg =
3595 DAG.getMachineFunction().getFunction()->arg_begin();
3596 unsigned CurArgIdx = 0;
3597 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
3599 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3600 CCValAssign &VA = ArgLocs[i];
3601 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
3602 CurArgIdx = Ins[i].OrigArgIndex;
3603 EVT ValVT = VA.getValVT();
3604 ISD::ArgFlagsTy Flags = Ins[i].Flags;
3605 bool IsRegLoc = VA.isRegLoc();
3607 if (Flags.isByVal()) {
3608 assert(Flags.getByValSize() &&
3609 "ByVal args of size 0 should have been ignored by front-end.");
3610 assert(ByValArg != MipsCCInfo.byval_end());
3611 copyByValRegs(Chain, dl, OutChains, DAG, Flags, InVals, &*FuncArg,
3612 MipsCCInfo, *ByValArg);
3617 // Arguments stored on registers
3619 EVT RegVT = VA.getLocVT();
3620 unsigned ArgReg = VA.getLocReg();
3621 const TargetRegisterClass *RC;
3623 if (RegVT == MVT::i32)
3624 RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
3625 &Mips::CPURegsRegClass;
3626 else if (RegVT == MVT::i64)
3627 RC = &Mips::CPU64RegsRegClass;
3628 else if (RegVT == MVT::f32)
3629 RC = &Mips::FGR32RegClass;
3630 else if (RegVT == MVT::f64)
3631 RC = HasMips64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
3633 llvm_unreachable("RegVT not supported by FormalArguments Lowering");
3635 // Transform the arguments stored on
3636 // physical registers into virtual ones
3637 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
3638 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
3640 // If this is an 8 or 16-bit value, it has been passed promoted
3641 // to 32 bits. Insert an assert[sz]ext to capture this, then
3642 // truncate to the right size.
3643 if (VA.getLocInfo() != CCValAssign::Full) {
3644 unsigned Opcode = 0;
3645 if (VA.getLocInfo() == CCValAssign::SExt)
3646 Opcode = ISD::AssertSext;
3647 else if (VA.getLocInfo() == CCValAssign::ZExt)
3648 Opcode = ISD::AssertZext;
3650 ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
3651 DAG.getValueType(ValVT));
3652 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, ValVT, ArgValue);
3655 // Handle floating point arguments passed in integer registers.
3656 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
3657 (RegVT == MVT::i64 && ValVT == MVT::f64))
3658 ArgValue = DAG.getNode(ISD::BITCAST, dl, ValVT, ArgValue);
3659 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
3660 unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
3661 getNextIntArgReg(ArgReg), RC);
3662 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
3663 if (!Subtarget->isLittle())
3664 std::swap(ArgValue, ArgValue2);
3665 ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
3666 ArgValue, ArgValue2);
3669 InVals.push_back(ArgValue);
3670 } else { // VA.isRegLoc()
3673 assert(VA.isMemLoc());
3675 // The stack pointer offset is relative to the caller stack frame.
3676 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
3677 VA.getLocMemOffset(), true);
3679 // Create load nodes to retrieve arguments from the stack
3680 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3681 InVals.push_back(DAG.getLoad(ValVT, dl, Chain, FIN,
3682 MachinePointerInfo::getFixedStack(FI),
3683 false, false, false, 0));
3687 // The mips ABIs for returning structs by value requires that we copy
3688 // the sret argument into $v0 for the return. Save the argument into
3689 // a virtual register so that we can access it from the return points.
3690 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3691 unsigned Reg = MipsFI->getSRetReturnReg();
3693 Reg = MF.getRegInfo().
3694 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
3695 MipsFI->setSRetReturnReg(Reg);
3697 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
3698 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
3702 writeVarArgRegs(OutChains, MipsCCInfo, Chain, dl, DAG);
3704 // All stores are grouped in one node to allow the matching between
3705 // the size of Ins and InVals. This only happens when on varg functions
3706 if (!OutChains.empty()) {
3707 OutChains.push_back(Chain);
3708 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
3709 &OutChains[0], OutChains.size());
3715 //===----------------------------------------------------------------------===//
3716 // Return Value Calling Convention Implementation
3717 //===----------------------------------------------------------------------===//
3720 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
3721 MachineFunction &MF, bool isVarArg,
3722 const SmallVectorImpl<ISD::OutputArg> &Outs,
3723 LLVMContext &Context) const {
3724 SmallVector<CCValAssign, 16> RVLocs;
3725 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(),
3727 return CCInfo.CheckReturn(Outs, RetCC_Mips);
3731 MipsTargetLowering::LowerReturn(SDValue Chain,
3732 CallingConv::ID CallConv, bool isVarArg,
3733 const SmallVectorImpl<ISD::OutputArg> &Outs,
3734 const SmallVectorImpl<SDValue> &OutVals,
3735 DebugLoc dl, SelectionDAG &DAG) const {
3737 // CCValAssign - represent the assignment of
3738 // the return value to a location
3739 SmallVector<CCValAssign, 16> RVLocs;
3741 // CCState - Info about the registers and stack slot.
3742 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
3743 getTargetMachine(), RVLocs, *DAG.getContext());
3745 // Analize return values.
3746 CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
3749 SmallVector<SDValue, 4> RetOps(1, Chain);
3751 // Copy the result values into the output registers.
3752 for (unsigned i = 0; i != RVLocs.size(); ++i) {
3753 CCValAssign &VA = RVLocs[i];
3754 assert(VA.isRegLoc() && "Can only return in registers!");
3756 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), OutVals[i], Flag);
3758 // Guarantee that all emitted copies are stuck together with flags.
3759 Flag = Chain.getValue(1);
3760 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
3763 // The mips ABIs for returning structs by value requires that we copy
3764 // the sret argument into $v0 for the return. We saved the argument into
3765 // a virtual register in the entry block, so now we copy the value out
3767 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
3768 MachineFunction &MF = DAG.getMachineFunction();
3769 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3770 unsigned Reg = MipsFI->getSRetReturnReg();
3773 llvm_unreachable("sret virtual register not created in the entry block");
3774 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
3775 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
3777 Chain = DAG.getCopyToReg(Chain, dl, V0, Val, Flag);
3778 Flag = Chain.getValue(1);
3779 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
3782 RetOps[0] = Chain; // Update chain.
3784 // Add the flag if we have it.
3786 RetOps.push_back(Flag);
3788 // Return on Mips is always a "jr $ra"
3789 return DAG.getNode(MipsISD::Ret, dl, MVT::Other, &RetOps[0], RetOps.size());
3792 //===----------------------------------------------------------------------===//
3793 // Mips Inline Assembly Support
3794 //===----------------------------------------------------------------------===//
3796 /// getConstraintType - Given a constraint letter, return the type of
3797 /// constraint it is for this target.
3798 MipsTargetLowering::ConstraintType MipsTargetLowering::
3799 getConstraintType(const std::string &Constraint) const
3801 // Mips specific constrainy
3802 // GCC config/mips/constraints.md
3804 // 'd' : An address register. Equivalent to r
3805 // unless generating MIPS16 code.
3806 // 'y' : Equivalent to r; retained for
3807 // backwards compatibility.
3808 // 'c' : A register suitable for use in an indirect
3809 // jump. This will always be $25 for -mabicalls.
3810 // 'l' : The lo register. 1 word storage.
3811 // 'x' : The hilo register pair. Double word storage.
3812 if (Constraint.size() == 1) {
3813 switch (Constraint[0]) {
3821 return C_RegisterClass;
3824 return TargetLowering::getConstraintType(Constraint);
3827 /// Examine constraint type and operand type and determine a weight value.
3828 /// This object must already have been set up with the operand type
3829 /// and the current alternative constraint selected.
3830 TargetLowering::ConstraintWeight
3831 MipsTargetLowering::getSingleConstraintMatchWeight(
3832 AsmOperandInfo &info, const char *constraint) const {
3833 ConstraintWeight weight = CW_Invalid;
3834 Value *CallOperandVal = info.CallOperandVal;
3835 // If we don't have a value, we can't do a match,
3836 // but allow it at the lowest weight.
3837 if (CallOperandVal == NULL)
3839 Type *type = CallOperandVal->getType();
3840 // Look at the constraint type.
3841 switch (*constraint) {
3843 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3847 if (type->isIntegerTy())
3848 weight = CW_Register;
3851 if (type->isFloatTy())
3852 weight = CW_Register;
3854 case 'c': // $25 for indirect jumps
3855 case 'l': // lo register
3856 case 'x': // hilo register pair
3857 if (type->isIntegerTy())
3858 weight = CW_SpecificReg;
3860 case 'I': // signed 16 bit immediate
3861 case 'J': // integer zero
3862 case 'K': // unsigned 16 bit immediate
3863 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3864 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3865 case 'O': // signed 15 bit immediate (+- 16383)
3866 case 'P': // immediate in the range of 65535 to 1 (inclusive)
3867 if (isa<ConstantInt>(CallOperandVal))
3868 weight = CW_Constant;
3874 /// Given a register class constraint, like 'r', if this corresponds directly
3875 /// to an LLVM register class, return a register of 0 and the register class
3877 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
3878 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
3880 if (Constraint.size() == 1) {
3881 switch (Constraint[0]) {
3882 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3883 case 'y': // Same as 'r'. Exists for compatibility.
3885 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3886 if (Subtarget->inMips16Mode())
3887 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
3888 return std::make_pair(0U, &Mips::CPURegsRegClass);
3890 if (VT == MVT::i64 && !HasMips64)
3891 return std::make_pair(0U, &Mips::CPURegsRegClass);
3892 if (VT == MVT::i64 && HasMips64)
3893 return std::make_pair(0U, &Mips::CPU64RegsRegClass);
3894 // This will generate an error message
3895 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
3898 return std::make_pair(0U, &Mips::FGR32RegClass);
3899 if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
3900 if (Subtarget->isFP64bit())
3901 return std::make_pair(0U, &Mips::FGR64RegClass);
3902 return std::make_pair(0U, &Mips::AFGR64RegClass);
3905 case 'c': // register suitable for indirect jump
3907 return std::make_pair((unsigned)Mips::T9, &Mips::CPURegsRegClass);
3908 assert(VT == MVT::i64 && "Unexpected type.");
3909 return std::make_pair((unsigned)Mips::T9_64, &Mips::CPU64RegsRegClass);
3910 case 'l': // register suitable for indirect jump
3912 return std::make_pair((unsigned)Mips::LO, &Mips::HILORegClass);
3913 return std::make_pair((unsigned)Mips::LO64, &Mips::HILO64RegClass);
3914 case 'x': // register suitable for indirect jump
3915 // Fixme: Not triggering the use of both hi and low
3916 // This will generate an error message
3917 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
3920 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3923 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3924 /// vector. If it is invalid, don't add anything to Ops.
3925 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3926 std::string &Constraint,
3927 std::vector<SDValue>&Ops,
3928 SelectionDAG &DAG) const {
3929 SDValue Result(0, 0);
3931 // Only support length 1 constraints for now.
3932 if (Constraint.length() > 1) return;
3934 char ConstraintLetter = Constraint[0];
3935 switch (ConstraintLetter) {
3936 default: break; // This will fall through to the generic implementation
3937 case 'I': // Signed 16 bit constant
3938 // If this fails, the parent routine will give an error
3939 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3940 EVT Type = Op.getValueType();
3941 int64_t Val = C->getSExtValue();
3942 if (isInt<16>(Val)) {
3943 Result = DAG.getTargetConstant(Val, Type);
3948 case 'J': // integer zero
3949 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3950 EVT Type = Op.getValueType();
3951 int64_t Val = C->getZExtValue();
3953 Result = DAG.getTargetConstant(0, Type);
3958 case 'K': // unsigned 16 bit immediate
3959 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3960 EVT Type = Op.getValueType();
3961 uint64_t Val = (uint64_t)C->getZExtValue();
3962 if (isUInt<16>(Val)) {
3963 Result = DAG.getTargetConstant(Val, Type);
3968 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3969 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3970 EVT Type = Op.getValueType();
3971 int64_t Val = C->getSExtValue();
3972 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3973 Result = DAG.getTargetConstant(Val, Type);
3978 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3979 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3980 EVT Type = Op.getValueType();
3981 int64_t Val = C->getSExtValue();
3982 if ((Val >= -65535) && (Val <= -1)) {
3983 Result = DAG.getTargetConstant(Val, Type);
3988 case 'O': // signed 15 bit immediate
3989 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3990 EVT Type = Op.getValueType();
3991 int64_t Val = C->getSExtValue();
3992 if ((isInt<15>(Val))) {
3993 Result = DAG.getTargetConstant(Val, Type);
3998 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3999 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4000 EVT Type = Op.getValueType();
4001 int64_t Val = C->getSExtValue();
4002 if ((Val <= 65535) && (Val >= 1)) {
4003 Result = DAG.getTargetConstant(Val, Type);
4010 if (Result.getNode()) {
4011 Ops.push_back(Result);
4015 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
4019 MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
4020 // No global is ever allowed as a base.
4025 case 0: // "r+i" or just "i", depending on HasBaseReg.
4028 if (!AM.HasBaseReg) // allow "r+i".
4030 return false; // disallow "r+r" or "r+r+i".
4039 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
4040 // The Mips target isn't yet aware of offsets.
4044 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
4046 bool IsMemset, bool ZeroMemset,
4048 MachineFunction &MF) const {
4049 if (Subtarget->hasMips64())
4055 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4056 if (VT != MVT::f32 && VT != MVT::f64)
4058 if (Imm.isNegZero())
4060 return Imm.isZero();
4063 unsigned MipsTargetLowering::getJumpTableEncoding() const {
4065 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
4067 return TargetLowering::getJumpTableEncoding();
4070 MipsTargetLowering::MipsCC::MipsCC(CallingConv::ID CC, bool IsO32_,
4072 : CCInfo(Info), CallConv(CC), IsO32(IsO32_) {
4073 // Pre-allocate reserved argument area.
4074 CCInfo.AllocateStack(reservedArgArea(), 1);
4077 void MipsTargetLowering::MipsCC::
4078 analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
4080 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
4081 "CallingConv::Fast shouldn't be used for vararg functions.");
4083 unsigned NumOpnds = Args.size();
4084 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
4086 for (unsigned I = 0; I != NumOpnds; ++I) {
4087 MVT ArgVT = Args[I].VT;
4088 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
4091 if (ArgFlags.isByVal()) {
4092 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
4096 if (IsVarArg && !Args[I].IsFixed)
4097 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
4099 R = FixedFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
4103 dbgs() << "Call operand #" << I << " has unhandled type "
4104 << EVT(ArgVT).getEVTString();
4106 llvm_unreachable(0);
4111 void MipsTargetLowering::MipsCC::
4112 analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args) {
4113 unsigned NumArgs = Args.size();
4114 llvm::CCAssignFn *FixedFn = fixedArgFn();
4116 for (unsigned I = 0; I != NumArgs; ++I) {
4117 MVT ArgVT = Args[I].VT;
4118 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
4120 if (ArgFlags.isByVal()) {
4121 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
4125 if (!FixedFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo))
4129 dbgs() << "Formal Arg #" << I << " has unhandled type "
4130 << EVT(ArgVT).getEVTString();
4132 llvm_unreachable(0);
4137 MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
4139 CCValAssign::LocInfo LocInfo,
4140 ISD::ArgFlagsTy ArgFlags) {
4141 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
4143 struct ByValArgInfo ByVal;
4144 unsigned RegSize = regSize();
4145 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
4146 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
4149 if (useRegsForByval())
4150 allocateRegs(ByVal, ByValSize, Align);
4152 // Allocate space on caller's stack.
4153 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
4155 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
4157 ByValArgs.push_back(ByVal);
4160 unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
4161 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
4164 unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
4165 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
4168 const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
4169 return IsO32 ? O32IntRegs : Mips64IntRegs;
4172 llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
4173 if (CallConv == CallingConv::Fast)
4174 return CC_Mips_FastCC;
4176 return IsO32 ? CC_MipsO32 : CC_MipsN;
4179 llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
4180 return IsO32 ? CC_MipsO32 : CC_MipsN_VarArg;
4183 const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
4184 return IsO32 ? O32IntRegs : Mips64DPRegs;
4187 void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
4190 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
4191 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
4192 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
4193 "Byval argument's size and alignment should be a multiple of"
4196 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
4198 // If Align > RegSize, the first arg register must be even.
4199 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
4200 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
4204 // Mark the registers allocated.
4205 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
4206 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
4207 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
4210 void MipsTargetLowering::
4211 copyByValRegs(SDValue Chain, DebugLoc DL, std::vector<SDValue> &OutChains,
4212 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
4213 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
4214 const MipsCC &CC, const ByValArgInfo &ByVal) const {
4215 MachineFunction &MF = DAG.getMachineFunction();
4216 MachineFrameInfo *MFI = MF.getFrameInfo();
4217 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
4218 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
4222 FrameObjOffset = (int)CC.reservedArgArea() -
4223 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
4225 FrameObjOffset = ByVal.Address;
4227 // Create frame object.
4228 EVT PtrTy = getPointerTy();
4229 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
4230 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4231 InVals.push_back(FIN);
4236 // Copy arg registers.
4237 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
4238 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4240 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
4241 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
4242 unsigned VReg = AddLiveIn(MF, ArgReg, RC);
4243 unsigned Offset = I * CC.regSize();
4244 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
4245 DAG.getConstant(Offset, PtrTy));
4246 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
4247 StorePtr, MachinePointerInfo(FuncArg, Offset),
4249 OutChains.push_back(Store);
4253 // Copy byVal arg to registers and stack.
4254 void MipsTargetLowering::
4255 passByValArg(SDValue Chain, DebugLoc DL,
4256 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
4257 SmallVector<SDValue, 8> &MemOpChains, SDValue StackPtr,
4258 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
4259 const MipsCC &CC, const ByValArgInfo &ByVal,
4260 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
4261 unsigned ByValSize = Flags.getByValSize();
4262 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
4263 unsigned RegSize = CC.regSize();
4264 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
4265 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
4267 if (ByVal.NumRegs) {
4268 const uint16_t *ArgRegs = CC.intArgRegs();
4269 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
4272 // Copy words to registers.
4273 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
4274 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4275 DAG.getConstant(Offset, PtrTy));
4276 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
4277 MachinePointerInfo(), false, false, false,
4279 MemOpChains.push_back(LoadVal.getValue(1));
4280 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
4281 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
4284 // Return if the struct has been fully copied.
4285 if (ByValSize == Offset)
4288 // Copy the remainder of the byval argument with sub-word loads and shifts.
4289 if (LeftoverBytes) {
4290 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
4291 "Size of the remainder should be smaller than RegSize.");
4294 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
4295 Offset < ByValSize; LoadSize /= 2) {
4296 unsigned RemSize = ByValSize - Offset;
4298 if (RemSize < LoadSize)
4302 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4303 DAG.getConstant(Offset, PtrTy));
4305 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
4306 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
4307 false, false, Alignment);
4308 MemOpChains.push_back(LoadVal.getValue(1));
4310 // Shift the loaded value.
4314 Shamt = TotalSizeLoaded;
4316 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
4318 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
4319 DAG.getConstant(Shamt, MVT::i32));
4322 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
4327 TotalSizeLoaded += LoadSize;
4328 Alignment = std::min(Alignment, LoadSize);
4331 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
4332 RegsToPass.push_back(std::make_pair(ArgReg, Val));
4337 // Copy remainder of byval arg to it with memcpy.
4338 unsigned MemCpySize = ByValSize - Offset;
4339 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
4340 DAG.getConstant(Offset, PtrTy));
4341 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
4342 DAG.getIntPtrConstant(ByVal.Address));
4343 Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
4344 DAG.getConstant(MemCpySize, PtrTy), Alignment,
4345 /*isVolatile=*/false, /*AlwaysInline=*/false,
4346 MachinePointerInfo(0), MachinePointerInfo(0));
4347 MemOpChains.push_back(Chain);
4351 MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
4352 const MipsCC &CC, SDValue Chain,
4353 DebugLoc DL, SelectionDAG &DAG) const {
4354 unsigned NumRegs = CC.numIntArgRegs();
4355 const uint16_t *ArgRegs = CC.intArgRegs();
4356 const CCState &CCInfo = CC.getCCInfo();
4357 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
4358 unsigned RegSize = CC.regSize();
4359 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
4360 const TargetRegisterClass *RC = getRegClassFor(RegTy);
4361 MachineFunction &MF = DAG.getMachineFunction();
4362 MachineFrameInfo *MFI = MF.getFrameInfo();
4363 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
4365 // Offset of the first variable argument from stack pointer.
4369 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
4372 (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
4374 // Record the frame index of the first variable argument
4375 // which is a value necessary to VASTART.
4376 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
4377 MipsFI->setVarArgsFrameIndex(FI);
4379 // Copy the integer registers that have not been used for argument passing
4380 // to the argument register save area. For O32, the save area is allocated
4381 // in the caller's stack frame, while for N32/64, it is allocated in the
4382 // callee's stack frame.
4383 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
4384 unsigned Reg = AddLiveIn(MF, ArgRegs[I], RC);
4385 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
4386 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
4387 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
4388 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
4389 MachinePointerInfo(), false, false, 0);
4390 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
4391 OutChains.push_back(Store);