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"
15 #include "MipsISelLowering.h"
16 #include "InstPrinter/MipsInstPrinter.h"
17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "MipsMachineFunction.h"
19 #include "MipsSubtarget.h"
20 #include "MipsTargetMachine.h"
21 #include "MipsTargetObjectFile.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringSwitch.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/GlobalVariable.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
42 STATISTIC(NumTailCalls, "Number of tail calls");
45 LargeGOT("mxgot", cl::Hidden,
46 cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
49 NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
50 cl::desc("MIPS: Don't trap on integer division by zero."),
53 static const uint16_t O32IntRegs[4] = {
54 Mips::A0, Mips::A1, Mips::A2, Mips::A3
57 static const uint16_t Mips64IntRegs[8] = {
58 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
59 Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
62 static const uint16_t Mips64DPRegs[8] = {
63 Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
64 Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
67 // If I is a shifted mask, set the size (Size) and the first bit of the
68 // mask (Pos), and return true.
69 // For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
70 static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
71 if (!isShiftedMask_64(I))
74 Size = CountPopulation_64(I);
75 Pos = countTrailingZeros(I);
79 SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
80 MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
81 return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
84 SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
86 unsigned Flag) const {
87 return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
90 SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
92 unsigned Flag) const {
93 return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
96 SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
98 unsigned Flag) const {
99 return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
102 SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
104 unsigned Flag) const {
105 return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
108 SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
110 unsigned Flag) const {
111 return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
112 N->getOffset(), Flag);
115 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
117 case MipsISD::JmpLink: return "MipsISD::JmpLink";
118 case MipsISD::JmpLinkMM: return "MipsISD::JmpLinkMM";
119 case MipsISD::TailCall: return "MipsISD::TailCall";
120 case MipsISD::Hi: return "MipsISD::Hi";
121 case MipsISD::Lo: return "MipsISD::Lo";
122 case MipsISD::GPRel: return "MipsISD::GPRel";
123 case MipsISD::ThreadPointer: return "MipsISD::ThreadPointer";
124 case MipsISD::Ret: return "MipsISD::Ret";
125 case MipsISD::EH_RETURN: return "MipsISD::EH_RETURN";
126 case MipsISD::FPBrcond: return "MipsISD::FPBrcond";
127 case MipsISD::FPCmp: return "MipsISD::FPCmp";
128 case MipsISD::CMovFP_T: return "MipsISD::CMovFP_T";
129 case MipsISD::CMovFP_F: return "MipsISD::CMovFP_F";
130 case MipsISD::TruncIntFP: return "MipsISD::TruncIntFP";
131 case MipsISD::MFHI: return "MipsISD::MFHI";
132 case MipsISD::MFLO: return "MipsISD::MFLO";
133 case MipsISD::MTLOHI: return "MipsISD::MTLOHI";
134 case MipsISD::Mult: return "MipsISD::Mult";
135 case MipsISD::Multu: return "MipsISD::Multu";
136 case MipsISD::MAdd: return "MipsISD::MAdd";
137 case MipsISD::MAddu: return "MipsISD::MAddu";
138 case MipsISD::MSub: return "MipsISD::MSub";
139 case MipsISD::MSubu: return "MipsISD::MSubu";
140 case MipsISD::DivRem: return "MipsISD::DivRem";
141 case MipsISD::DivRemU: return "MipsISD::DivRemU";
142 case MipsISD::DivRem16: return "MipsISD::DivRem16";
143 case MipsISD::DivRemU16: return "MipsISD::DivRemU16";
144 case MipsISD::BuildPairF64: return "MipsISD::BuildPairF64";
145 case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
146 case MipsISD::Wrapper: return "MipsISD::Wrapper";
147 case MipsISD::Sync: return "MipsISD::Sync";
148 case MipsISD::Ext: return "MipsISD::Ext";
149 case MipsISD::Ins: return "MipsISD::Ins";
150 case MipsISD::LWL: return "MipsISD::LWL";
151 case MipsISD::LWR: return "MipsISD::LWR";
152 case MipsISD::SWL: return "MipsISD::SWL";
153 case MipsISD::SWR: return "MipsISD::SWR";
154 case MipsISD::LDL: return "MipsISD::LDL";
155 case MipsISD::LDR: return "MipsISD::LDR";
156 case MipsISD::SDL: return "MipsISD::SDL";
157 case MipsISD::SDR: return "MipsISD::SDR";
158 case MipsISD::EXTP: return "MipsISD::EXTP";
159 case MipsISD::EXTPDP: return "MipsISD::EXTPDP";
160 case MipsISD::EXTR_S_H: return "MipsISD::EXTR_S_H";
161 case MipsISD::EXTR_W: return "MipsISD::EXTR_W";
162 case MipsISD::EXTR_R_W: return "MipsISD::EXTR_R_W";
163 case MipsISD::EXTR_RS_W: return "MipsISD::EXTR_RS_W";
164 case MipsISD::SHILO: return "MipsISD::SHILO";
165 case MipsISD::MTHLIP: return "MipsISD::MTHLIP";
166 case MipsISD::MULT: return "MipsISD::MULT";
167 case MipsISD::MULTU: return "MipsISD::MULTU";
168 case MipsISD::MADD_DSP: return "MipsISD::MADD_DSP";
169 case MipsISD::MADDU_DSP: return "MipsISD::MADDU_DSP";
170 case MipsISD::MSUB_DSP: return "MipsISD::MSUB_DSP";
171 case MipsISD::MSUBU_DSP: return "MipsISD::MSUBU_DSP";
172 case MipsISD::SHLL_DSP: return "MipsISD::SHLL_DSP";
173 case MipsISD::SHRA_DSP: return "MipsISD::SHRA_DSP";
174 case MipsISD::SHRL_DSP: return "MipsISD::SHRL_DSP";
175 case MipsISD::SETCC_DSP: return "MipsISD::SETCC_DSP";
176 case MipsISD::SELECT_CC_DSP: return "MipsISD::SELECT_CC_DSP";
177 case MipsISD::VALL_ZERO: return "MipsISD::VALL_ZERO";
178 case MipsISD::VANY_ZERO: return "MipsISD::VANY_ZERO";
179 case MipsISD::VALL_NONZERO: return "MipsISD::VALL_NONZERO";
180 case MipsISD::VANY_NONZERO: return "MipsISD::VANY_NONZERO";
181 case MipsISD::VCEQ: return "MipsISD::VCEQ";
182 case MipsISD::VCLE_S: return "MipsISD::VCLE_S";
183 case MipsISD::VCLE_U: return "MipsISD::VCLE_U";
184 case MipsISD::VCLT_S: return "MipsISD::VCLT_S";
185 case MipsISD::VCLT_U: return "MipsISD::VCLT_U";
186 case MipsISD::VSMAX: return "MipsISD::VSMAX";
187 case MipsISD::VSMIN: return "MipsISD::VSMIN";
188 case MipsISD::VUMAX: return "MipsISD::VUMAX";
189 case MipsISD::VUMIN: return "MipsISD::VUMIN";
190 case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
191 case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
192 case MipsISD::VNOR: return "MipsISD::VNOR";
193 case MipsISD::VSHF: return "MipsISD::VSHF";
194 case MipsISD::SHF: return "MipsISD::SHF";
195 case MipsISD::ILVEV: return "MipsISD::ILVEV";
196 case MipsISD::ILVOD: return "MipsISD::ILVOD";
197 case MipsISD::ILVL: return "MipsISD::ILVL";
198 case MipsISD::ILVR: return "MipsISD::ILVR";
199 case MipsISD::PCKEV: return "MipsISD::PCKEV";
200 case MipsISD::PCKOD: return "MipsISD::PCKOD";
201 default: return NULL;
206 MipsTargetLowering(MipsTargetMachine &TM)
207 : TargetLowering(TM, new MipsTargetObjectFile()),
208 Subtarget(&TM.getSubtarget<MipsSubtarget>()),
209 HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
210 IsO32(Subtarget->isABI_O32()) {
211 // Mips does not have i1 type, so use i32 for
212 // setcc operations results (slt, sgt, ...).
213 setBooleanContents(ZeroOrOneBooleanContent);
214 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
216 // Load extented operations for i1 types must be promoted
217 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
218 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
219 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
221 // MIPS doesn't have extending float->double load/store
222 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
223 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
225 // Used by legalize types to correctly generate the setcc result.
226 // Without this, every float setcc comes with a AND/OR with the result,
227 // we don't want this, since the fpcmp result goes to a flag register,
228 // which is used implicitly by brcond and select operations.
229 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
231 // Mips Custom Operations
232 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
233 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
234 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
235 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
236 setOperationAction(ISD::JumpTable, MVT::i32, Custom);
237 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
238 setOperationAction(ISD::SELECT, MVT::f32, Custom);
239 setOperationAction(ISD::SELECT, MVT::f64, Custom);
240 setOperationAction(ISD::SELECT, MVT::i32, Custom);
241 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
242 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
243 setOperationAction(ISD::SETCC, MVT::f32, Custom);
244 setOperationAction(ISD::SETCC, MVT::f64, Custom);
245 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
246 setOperationAction(ISD::VASTART, MVT::Other, Custom);
247 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
248 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
249 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
251 if (!TM.Options.NoNaNsFPMath) {
252 setOperationAction(ISD::FABS, MVT::f32, Custom);
253 setOperationAction(ISD::FABS, MVT::f64, Custom);
257 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
258 setOperationAction(ISD::BlockAddress, MVT::i64, Custom);
259 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
260 setOperationAction(ISD::JumpTable, MVT::i64, Custom);
261 setOperationAction(ISD::ConstantPool, MVT::i64, Custom);
262 setOperationAction(ISD::SELECT, MVT::i64, Custom);
263 setOperationAction(ISD::LOAD, MVT::i64, Custom);
264 setOperationAction(ISD::STORE, MVT::i64, Custom);
265 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
269 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
270 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
271 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
274 setOperationAction(ISD::ADD, MVT::i32, Custom);
276 setOperationAction(ISD::ADD, MVT::i64, Custom);
278 setOperationAction(ISD::SDIV, MVT::i32, Expand);
279 setOperationAction(ISD::SREM, MVT::i32, Expand);
280 setOperationAction(ISD::UDIV, MVT::i32, Expand);
281 setOperationAction(ISD::UREM, MVT::i32, Expand);
282 setOperationAction(ISD::SDIV, MVT::i64, Expand);
283 setOperationAction(ISD::SREM, MVT::i64, Expand);
284 setOperationAction(ISD::UDIV, MVT::i64, Expand);
285 setOperationAction(ISD::UREM, MVT::i64, Expand);
287 // Operations not directly supported by Mips.
288 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
289 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
290 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
291 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
292 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
293 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
294 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
295 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
296 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
297 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
298 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
299 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
300 setOperationAction(ISD::CTTZ, MVT::i32, Expand);
301 setOperationAction(ISD::CTTZ, MVT::i64, Expand);
302 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
303 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
304 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
305 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
306 setOperationAction(ISD::ROTL, MVT::i32, Expand);
307 setOperationAction(ISD::ROTL, MVT::i64, Expand);
308 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
309 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
311 if (!Subtarget->hasMips32r2())
312 setOperationAction(ISD::ROTR, MVT::i32, Expand);
314 if (!Subtarget->hasMips64r2())
315 setOperationAction(ISD::ROTR, MVT::i64, Expand);
317 setOperationAction(ISD::FSIN, MVT::f32, Expand);
318 setOperationAction(ISD::FSIN, MVT::f64, Expand);
319 setOperationAction(ISD::FCOS, MVT::f32, Expand);
320 setOperationAction(ISD::FCOS, MVT::f64, Expand);
321 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
322 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
323 setOperationAction(ISD::FPOWI, MVT::f32, Expand);
324 setOperationAction(ISD::FPOW, MVT::f32, Expand);
325 setOperationAction(ISD::FPOW, MVT::f64, Expand);
326 setOperationAction(ISD::FLOG, MVT::f32, Expand);
327 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
328 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
329 setOperationAction(ISD::FEXP, MVT::f32, Expand);
330 setOperationAction(ISD::FMA, MVT::f32, Expand);
331 setOperationAction(ISD::FMA, MVT::f64, Expand);
332 setOperationAction(ISD::FREM, MVT::f32, Expand);
333 setOperationAction(ISD::FREM, MVT::f64, Expand);
335 if (!TM.Options.NoNaNsFPMath) {
336 setOperationAction(ISD::FNEG, MVT::f32, Expand);
337 setOperationAction(ISD::FNEG, MVT::f64, Expand);
340 setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
342 setOperationAction(ISD::VAARG, MVT::Other, Expand);
343 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
344 setOperationAction(ISD::VAEND, MVT::Other, Expand);
346 // Use the default for now
347 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
348 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
350 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand);
351 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand);
352 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand);
353 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand);
355 setInsertFencesForAtomic(true);
357 if (!Subtarget->hasSEInReg()) {
358 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
359 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
362 if (!Subtarget->hasBitCount()) {
363 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
364 setOperationAction(ISD::CTLZ, MVT::i64, Expand);
367 if (!Subtarget->hasSwap()) {
368 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
369 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
373 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
374 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
375 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
376 setTruncStoreAction(MVT::i64, MVT::i32, Custom);
379 setOperationAction(ISD::TRAP, MVT::Other, Legal);
381 setTargetDAGCombine(ISD::SDIVREM);
382 setTargetDAGCombine(ISD::UDIVREM);
383 setTargetDAGCombine(ISD::SELECT);
384 setTargetDAGCombine(ISD::AND);
385 setTargetDAGCombine(ISD::OR);
386 setTargetDAGCombine(ISD::ADD);
388 setMinFunctionAlignment(HasMips64 ? 3 : 2);
390 setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
392 setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
393 setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
395 MaxStoresPerMemcpy = 16;
397 isMicroMips = Subtarget->inMicroMipsMode();
400 const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
401 if (TM.getSubtargetImpl()->inMips16Mode())
402 return llvm::createMips16TargetLowering(TM);
404 return llvm::createMipsSETargetLowering(TM);
407 EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
410 return VT.changeVectorElementTypeToInteger();
413 static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
414 TargetLowering::DAGCombinerInfo &DCI,
415 const MipsSubtarget *Subtarget) {
416 if (DCI.isBeforeLegalizeOps())
419 EVT Ty = N->getValueType(0);
420 unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
421 unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
422 unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
426 SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
427 N->getOperand(0), N->getOperand(1));
428 SDValue InChain = DAG.getEntryNode();
429 SDValue InGlue = DivRem;
432 if (N->hasAnyUseOfValue(0)) {
433 SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
435 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
436 InChain = CopyFromLo.getValue(1);
437 InGlue = CopyFromLo.getValue(2);
441 if (N->hasAnyUseOfValue(1)) {
442 SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
444 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
450 static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
452 default: llvm_unreachable("Unknown fp condition code!");
454 case ISD::SETOEQ: return Mips::FCOND_OEQ;
455 case ISD::SETUNE: return Mips::FCOND_UNE;
457 case ISD::SETOLT: return Mips::FCOND_OLT;
459 case ISD::SETOGT: return Mips::FCOND_OGT;
461 case ISD::SETOLE: return Mips::FCOND_OLE;
463 case ISD::SETOGE: return Mips::FCOND_OGE;
464 case ISD::SETULT: return Mips::FCOND_ULT;
465 case ISD::SETULE: return Mips::FCOND_ULE;
466 case ISD::SETUGT: return Mips::FCOND_UGT;
467 case ISD::SETUGE: return Mips::FCOND_UGE;
468 case ISD::SETUO: return Mips::FCOND_UN;
469 case ISD::SETO: return Mips::FCOND_OR;
471 case ISD::SETONE: return Mips::FCOND_ONE;
472 case ISD::SETUEQ: return Mips::FCOND_UEQ;
477 /// This function returns true if the floating point conditional branches and
478 /// conditional moves which use condition code CC should be inverted.
479 static bool invertFPCondCodeUser(Mips::CondCode CC) {
480 if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
483 assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
484 "Illegal Condition Code");
489 // Creates and returns an FPCmp node from a setcc node.
490 // Returns Op if setcc is not a floating point comparison.
491 static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
492 // must be a SETCC node
493 if (Op.getOpcode() != ISD::SETCC)
496 SDValue LHS = Op.getOperand(0);
498 if (!LHS.getValueType().isFloatingPoint())
501 SDValue RHS = Op.getOperand(1);
504 // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
505 // node if necessary.
506 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
508 return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
509 DAG.getConstant(condCodeToFCC(CC), MVT::i32));
512 // Creates and returns a CMovFPT/F node.
513 static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
514 SDValue False, SDLoc DL) {
515 ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
516 bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
517 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
519 return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
520 True.getValueType(), True, FCC0, False, Cond);
523 static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
524 TargetLowering::DAGCombinerInfo &DCI,
525 const MipsSubtarget *Subtarget) {
526 if (DCI.isBeforeLegalizeOps())
529 SDValue SetCC = N->getOperand(0);
531 if ((SetCC.getOpcode() != ISD::SETCC) ||
532 !SetCC.getOperand(0).getValueType().isInteger())
535 SDValue False = N->getOperand(2);
536 EVT FalseTy = False.getValueType();
538 if (!FalseTy.isInteger())
541 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
543 // If the RHS (False) is 0, we swap the order of the operands
544 // of ISD::SELECT (obviously also inverting the condition) so that we can
545 // take advantage of conditional moves using the $0 register.
547 // return (a != 0) ? x : 0;
555 if (!FalseC->getZExtValue()) {
556 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
557 SDValue True = N->getOperand(1);
559 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
560 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
562 return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
565 // If both operands are integer constants there's a possibility that we
566 // can do some interesting optimizations.
567 SDValue True = N->getOperand(1);
568 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
570 if (!TrueC || !True.getValueType().isInteger())
573 // We'll also ignore MVT::i64 operands as this optimizations proves
574 // to be ineffective because of the required sign extensions as the result
575 // of a SETCC operator is always MVT::i32 for non-vector types.
576 if (True.getValueType() == MVT::i64)
579 int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
581 // 1) (a < x) ? y : y-1
583 // addiu $reg2, $reg1, y-1
585 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
587 // 2) (a < x) ? y-1 : y
589 // xor $reg1, $reg1, 1
590 // addiu $reg2, $reg1, y-1
592 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
593 SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
594 SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
595 return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
598 // Couldn't optimize.
602 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
603 TargetLowering::DAGCombinerInfo &DCI,
604 const MipsSubtarget *Subtarget) {
605 // Pattern match EXT.
606 // $dst = and ((sra or srl) $src , pos), (2**size - 1)
607 // => ext $dst, $src, size, pos
608 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
611 SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
612 unsigned ShiftRightOpc = ShiftRight.getOpcode();
614 // Op's first operand must be a shift right.
615 if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
618 // The second operand of the shift must be an immediate.
620 if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
623 uint64_t Pos = CN->getZExtValue();
624 uint64_t SMPos, SMSize;
626 // Op's second operand must be a shifted mask.
627 if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
628 !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
631 // Return if the shifted mask does not start at bit 0 or the sum of its size
632 // and Pos exceeds the word's size.
633 EVT ValTy = N->getValueType(0);
634 if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
637 return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
638 ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
639 DAG.getConstant(SMSize, MVT::i32));
642 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
643 TargetLowering::DAGCombinerInfo &DCI,
644 const MipsSubtarget *Subtarget) {
645 // Pattern match INS.
646 // $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
647 // where mask1 = (2**size - 1) << pos, mask0 = ~mask1
648 // => ins $dst, $src, size, pos, $src1
649 if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
652 SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
653 uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
656 // See if Op's first operand matches (and $src1 , mask0).
657 if (And0.getOpcode() != ISD::AND)
660 if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
661 !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
664 // See if Op's second operand matches (and (shl $src, pos), mask1).
665 if (And1.getOpcode() != ISD::AND)
668 if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
669 !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
672 // The shift masks must have the same position and size.
673 if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
676 SDValue Shl = And1.getOperand(0);
677 if (Shl.getOpcode() != ISD::SHL)
680 if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
683 unsigned Shamt = CN->getZExtValue();
685 // Return if the shift amount and the first bit position of mask are not the
687 EVT ValTy = N->getValueType(0);
688 if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
691 return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
692 DAG.getConstant(SMPos0, MVT::i32),
693 DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
696 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
697 TargetLowering::DAGCombinerInfo &DCI,
698 const MipsSubtarget *Subtarget) {
699 // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
701 if (DCI.isBeforeLegalizeOps())
704 SDValue Add = N->getOperand(1);
706 if (Add.getOpcode() != ISD::ADD)
709 SDValue Lo = Add.getOperand(1);
711 if ((Lo.getOpcode() != MipsISD::Lo) ||
712 (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
715 EVT ValTy = N->getValueType(0);
718 SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
720 return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
723 SDValue MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
725 SelectionDAG &DAG = DCI.DAG;
726 unsigned Opc = N->getOpcode();
732 return performDivRemCombine(N, DAG, DCI, Subtarget);
734 return performSELECTCombine(N, DAG, DCI, Subtarget);
736 return performANDCombine(N, DAG, DCI, Subtarget);
738 return performORCombine(N, DAG, DCI, Subtarget);
740 return performADDCombine(N, DAG, DCI, Subtarget);
747 MipsTargetLowering::LowerOperationWrapper(SDNode *N,
748 SmallVectorImpl<SDValue> &Results,
749 SelectionDAG &DAG) const {
750 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
752 for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
753 Results.push_back(Res.getValue(I));
757 MipsTargetLowering::ReplaceNodeResults(SDNode *N,
758 SmallVectorImpl<SDValue> &Results,
759 SelectionDAG &DAG) const {
760 return LowerOperationWrapper(N, Results, DAG);
763 SDValue MipsTargetLowering::
764 LowerOperation(SDValue Op, SelectionDAG &DAG) const
766 switch (Op.getOpcode())
768 case ISD::BR_JT: return lowerBR_JT(Op, DAG);
769 case ISD::BRCOND: return lowerBRCOND(Op, DAG);
770 case ISD::ConstantPool: return lowerConstantPool(Op, DAG);
771 case ISD::GlobalAddress: return lowerGlobalAddress(Op, DAG);
772 case ISD::BlockAddress: return lowerBlockAddress(Op, DAG);
773 case ISD::GlobalTLSAddress: return lowerGlobalTLSAddress(Op, DAG);
774 case ISD::JumpTable: return lowerJumpTable(Op, DAG);
775 case ISD::SELECT: return lowerSELECT(Op, DAG);
776 case ISD::SELECT_CC: return lowerSELECT_CC(Op, DAG);
777 case ISD::SETCC: return lowerSETCC(Op, DAG);
778 case ISD::VASTART: return lowerVASTART(Op, DAG);
779 case ISD::FCOPYSIGN: return lowerFCOPYSIGN(Op, DAG);
780 case ISD::FABS: return lowerFABS(Op, DAG);
781 case ISD::FRAMEADDR: return lowerFRAMEADDR(Op, DAG);
782 case ISD::RETURNADDR: return lowerRETURNADDR(Op, DAG);
783 case ISD::EH_RETURN: return lowerEH_RETURN(Op, DAG);
784 case ISD::ATOMIC_FENCE: return lowerATOMIC_FENCE(Op, DAG);
785 case ISD::SHL_PARTS: return lowerShiftLeftParts(Op, DAG);
786 case ISD::SRA_PARTS: return lowerShiftRightParts(Op, DAG, true);
787 case ISD::SRL_PARTS: return lowerShiftRightParts(Op, DAG, false);
788 case ISD::LOAD: return lowerLOAD(Op, DAG);
789 case ISD::STORE: return lowerSTORE(Op, DAG);
790 case ISD::ADD: return lowerADD(Op, DAG);
791 case ISD::FP_TO_SINT: return lowerFP_TO_SINT(Op, DAG);
796 //===----------------------------------------------------------------------===//
797 // Lower helper functions
798 //===----------------------------------------------------------------------===//
800 // addLiveIn - This helper function adds the specified physical register to the
801 // MachineFunction as a live in value. It also creates a corresponding
802 // virtual register for it.
804 addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
806 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
807 MF.getRegInfo().addLiveIn(PReg, VReg);
811 static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
812 MachineBasicBlock &MBB,
813 const TargetInstrInfo &TII,
818 // Insert instruction "teq $divisor_reg, $zero, 7".
819 MachineBasicBlock::iterator I(MI);
820 MachineInstrBuilder MIB;
821 MachineOperand &Divisor = MI->getOperand(2);
822 MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
823 .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
824 .addReg(Mips::ZERO).addImm(7);
826 // Use the 32-bit sub-register if this is a 64-bit division.
828 MIB->getOperand(0).setSubReg(Mips::sub_32);
830 // Clear Divisor's kill flag.
831 Divisor.setIsKill(false);
836 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
837 MachineBasicBlock *BB) const {
838 switch (MI->getOpcode()) {
840 llvm_unreachable("Unexpected instr type to insert");
841 case Mips::ATOMIC_LOAD_ADD_I8:
842 return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
843 case Mips::ATOMIC_LOAD_ADD_I16:
844 return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
845 case Mips::ATOMIC_LOAD_ADD_I32:
846 return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
847 case Mips::ATOMIC_LOAD_ADD_I64:
848 return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
850 case Mips::ATOMIC_LOAD_AND_I8:
851 return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
852 case Mips::ATOMIC_LOAD_AND_I16:
853 return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
854 case Mips::ATOMIC_LOAD_AND_I32:
855 return emitAtomicBinary(MI, BB, 4, Mips::AND);
856 case Mips::ATOMIC_LOAD_AND_I64:
857 return emitAtomicBinary(MI, BB, 8, Mips::AND64);
859 case Mips::ATOMIC_LOAD_OR_I8:
860 return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
861 case Mips::ATOMIC_LOAD_OR_I16:
862 return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
863 case Mips::ATOMIC_LOAD_OR_I32:
864 return emitAtomicBinary(MI, BB, 4, Mips::OR);
865 case Mips::ATOMIC_LOAD_OR_I64:
866 return emitAtomicBinary(MI, BB, 8, Mips::OR64);
868 case Mips::ATOMIC_LOAD_XOR_I8:
869 return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
870 case Mips::ATOMIC_LOAD_XOR_I16:
871 return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
872 case Mips::ATOMIC_LOAD_XOR_I32:
873 return emitAtomicBinary(MI, BB, 4, Mips::XOR);
874 case Mips::ATOMIC_LOAD_XOR_I64:
875 return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
877 case Mips::ATOMIC_LOAD_NAND_I8:
878 return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
879 case Mips::ATOMIC_LOAD_NAND_I16:
880 return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
881 case Mips::ATOMIC_LOAD_NAND_I32:
882 return emitAtomicBinary(MI, BB, 4, 0, true);
883 case Mips::ATOMIC_LOAD_NAND_I64:
884 return emitAtomicBinary(MI, BB, 8, 0, true);
886 case Mips::ATOMIC_LOAD_SUB_I8:
887 return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
888 case Mips::ATOMIC_LOAD_SUB_I16:
889 return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
890 case Mips::ATOMIC_LOAD_SUB_I32:
891 return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
892 case Mips::ATOMIC_LOAD_SUB_I64:
893 return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
895 case Mips::ATOMIC_SWAP_I8:
896 return emitAtomicBinaryPartword(MI, BB, 1, 0);
897 case Mips::ATOMIC_SWAP_I16:
898 return emitAtomicBinaryPartword(MI, BB, 2, 0);
899 case Mips::ATOMIC_SWAP_I32:
900 return emitAtomicBinary(MI, BB, 4, 0);
901 case Mips::ATOMIC_SWAP_I64:
902 return emitAtomicBinary(MI, BB, 8, 0);
904 case Mips::ATOMIC_CMP_SWAP_I8:
905 return emitAtomicCmpSwapPartword(MI, BB, 1);
906 case Mips::ATOMIC_CMP_SWAP_I16:
907 return emitAtomicCmpSwapPartword(MI, BB, 2);
908 case Mips::ATOMIC_CMP_SWAP_I32:
909 return emitAtomicCmpSwap(MI, BB, 4);
910 case Mips::ATOMIC_CMP_SWAP_I64:
911 return emitAtomicCmpSwap(MI, BB, 8);
912 case Mips::PseudoSDIV:
913 case Mips::PseudoUDIV:
914 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
915 case Mips::PseudoDSDIV:
916 case Mips::PseudoDUDIV:
917 return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
921 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
922 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
924 MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
925 unsigned Size, unsigned BinOpcode,
927 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
929 MachineFunction *MF = BB->getParent();
930 MachineRegisterInfo &RegInfo = MF->getRegInfo();
931 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
932 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
933 DebugLoc DL = MI->getDebugLoc();
934 unsigned LL, SC, AND, NOR, ZERO, BEQ;
937 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
938 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
949 ZERO = Mips::ZERO_64;
953 unsigned OldVal = MI->getOperand(0).getReg();
954 unsigned Ptr = MI->getOperand(1).getReg();
955 unsigned Incr = MI->getOperand(2).getReg();
957 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
958 unsigned AndRes = RegInfo.createVirtualRegister(RC);
959 unsigned Success = RegInfo.createVirtualRegister(RC);
961 // insert new blocks after the current block
962 const BasicBlock *LLVM_BB = BB->getBasicBlock();
963 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
964 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
965 MachineFunction::iterator It = BB;
967 MF->insert(It, loopMBB);
968 MF->insert(It, exitMBB);
970 // Transfer the remainder of BB and its successor edges to exitMBB.
971 exitMBB->splice(exitMBB->begin(), BB,
972 std::next(MachineBasicBlock::iterator(MI)), BB->end());
973 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
977 // fallthrough --> loopMBB
978 BB->addSuccessor(loopMBB);
979 loopMBB->addSuccessor(loopMBB);
980 loopMBB->addSuccessor(exitMBB);
984 // <binop> storeval, oldval, incr
985 // sc success, storeval, 0(ptr)
986 // beq success, $0, loopMBB
988 BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
990 // and andres, oldval, incr
991 // nor storeval, $0, andres
992 BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
993 BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
994 } else if (BinOpcode) {
995 // <binop> storeval, oldval, incr
996 BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
1000 BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1001 BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
1003 MI->eraseFromParent(); // The instruction is gone now.
1009 MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
1010 MachineBasicBlock *BB,
1011 unsigned Size, unsigned BinOpcode,
1013 assert((Size == 1 || Size == 2) &&
1014 "Unsupported size for EmitAtomicBinaryPartial.");
1016 MachineFunction *MF = BB->getParent();
1017 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1018 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1019 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1020 DebugLoc DL = MI->getDebugLoc();
1022 unsigned Dest = MI->getOperand(0).getReg();
1023 unsigned Ptr = MI->getOperand(1).getReg();
1024 unsigned Incr = MI->getOperand(2).getReg();
1026 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1027 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1028 unsigned Mask = RegInfo.createVirtualRegister(RC);
1029 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1030 unsigned NewVal = RegInfo.createVirtualRegister(RC);
1031 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1032 unsigned Incr2 = RegInfo.createVirtualRegister(RC);
1033 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1034 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1035 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1036 unsigned AndRes = RegInfo.createVirtualRegister(RC);
1037 unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
1038 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1039 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1040 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1041 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1042 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1043 unsigned Success = RegInfo.createVirtualRegister(RC);
1045 // insert new blocks after the current block
1046 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1047 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1048 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1049 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1050 MachineFunction::iterator It = BB;
1052 MF->insert(It, loopMBB);
1053 MF->insert(It, sinkMBB);
1054 MF->insert(It, exitMBB);
1056 // Transfer the remainder of BB and its successor edges to exitMBB.
1057 exitMBB->splice(exitMBB->begin(), BB,
1058 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1059 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1061 BB->addSuccessor(loopMBB);
1062 loopMBB->addSuccessor(loopMBB);
1063 loopMBB->addSuccessor(sinkMBB);
1064 sinkMBB->addSuccessor(exitMBB);
1067 // addiu masklsb2,$0,-4 # 0xfffffffc
1068 // and alignedaddr,ptr,masklsb2
1069 // andi ptrlsb2,ptr,3
1070 // sll shiftamt,ptrlsb2,3
1071 // ori maskupper,$0,255 # 0xff
1072 // sll mask,maskupper,shiftamt
1073 // nor mask2,$0,mask
1074 // sll incr2,incr,shiftamt
1076 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1077 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
1078 .addReg(Mips::ZERO).addImm(-4);
1079 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
1080 .addReg(Ptr).addReg(MaskLSB2);
1081 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1082 if (Subtarget->isLittle()) {
1083 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1085 unsigned Off = RegInfo.createVirtualRegister(RC);
1086 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1087 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1088 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1090 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1091 .addReg(Mips::ZERO).addImm(MaskImm);
1092 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1093 .addReg(MaskUpper).addReg(ShiftAmt);
1094 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1095 BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
1097 // atomic.load.binop
1099 // ll oldval,0(alignedaddr)
1100 // binop binopres,oldval,incr2
1101 // and newval,binopres,mask
1102 // and maskedoldval0,oldval,mask2
1103 // or storeval,maskedoldval0,newval
1104 // sc success,storeval,0(alignedaddr)
1105 // beq success,$0,loopMBB
1109 // ll oldval,0(alignedaddr)
1110 // and newval,incr2,mask
1111 // and maskedoldval0,oldval,mask2
1112 // or storeval,maskedoldval0,newval
1113 // sc success,storeval,0(alignedaddr)
1114 // beq success,$0,loopMBB
1117 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1119 // and andres, oldval, incr2
1120 // nor binopres, $0, andres
1121 // and newval, binopres, mask
1122 BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1123 BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
1124 .addReg(Mips::ZERO).addReg(AndRes);
1125 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1126 } else if (BinOpcode) {
1127 // <binop> binopres, oldval, incr2
1128 // and newval, binopres, mask
1129 BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1130 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1131 } else { // atomic.swap
1132 // and newval, incr2, mask
1133 BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
1136 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1137 .addReg(OldVal).addReg(Mask2);
1138 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1139 .addReg(MaskedOldVal0).addReg(NewVal);
1140 BuildMI(BB, DL, TII->get(Mips::SC), Success)
1141 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1142 BuildMI(BB, DL, TII->get(Mips::BEQ))
1143 .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
1146 // and maskedoldval1,oldval,mask
1147 // srl srlres,maskedoldval1,shiftamt
1148 // sll sllres,srlres,24
1149 // sra dest,sllres,24
1151 int64_t ShiftImm = (Size == 1) ? 24 : 16;
1153 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1154 .addReg(OldVal).addReg(Mask);
1155 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1156 .addReg(MaskedOldVal1).addReg(ShiftAmt);
1157 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
1158 .addReg(SrlRes).addImm(ShiftImm);
1159 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
1160 .addReg(SllRes).addImm(ShiftImm);
1162 MI->eraseFromParent(); // The instruction is gone now.
1167 MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1168 MachineBasicBlock *BB,
1169 unsigned Size) const {
1170 assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1172 MachineFunction *MF = BB->getParent();
1173 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1174 const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1175 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1176 DebugLoc DL = MI->getDebugLoc();
1177 unsigned LL, SC, ZERO, BNE, BEQ;
1180 LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1181 SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1188 ZERO = Mips::ZERO_64;
1193 unsigned Dest = MI->getOperand(0).getReg();
1194 unsigned Ptr = MI->getOperand(1).getReg();
1195 unsigned OldVal = MI->getOperand(2).getReg();
1196 unsigned NewVal = MI->getOperand(3).getReg();
1198 unsigned Success = RegInfo.createVirtualRegister(RC);
1200 // insert new blocks after the current block
1201 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1202 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1203 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1204 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1205 MachineFunction::iterator It = BB;
1207 MF->insert(It, loop1MBB);
1208 MF->insert(It, loop2MBB);
1209 MF->insert(It, exitMBB);
1211 // Transfer the remainder of BB and its successor edges to exitMBB.
1212 exitMBB->splice(exitMBB->begin(), BB,
1213 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1214 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1218 // fallthrough --> loop1MBB
1219 BB->addSuccessor(loop1MBB);
1220 loop1MBB->addSuccessor(exitMBB);
1221 loop1MBB->addSuccessor(loop2MBB);
1222 loop2MBB->addSuccessor(loop1MBB);
1223 loop2MBB->addSuccessor(exitMBB);
1227 // bne dest, oldval, exitMBB
1229 BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1230 BuildMI(BB, DL, TII->get(BNE))
1231 .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1234 // sc success, newval, 0(ptr)
1235 // beq success, $0, loop1MBB
1237 BuildMI(BB, DL, TII->get(SC), Success)
1238 .addReg(NewVal).addReg(Ptr).addImm(0);
1239 BuildMI(BB, DL, TII->get(BEQ))
1240 .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1242 MI->eraseFromParent(); // The instruction is gone now.
1248 MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
1249 MachineBasicBlock *BB,
1250 unsigned Size) const {
1251 assert((Size == 1 || Size == 2) &&
1252 "Unsupported size for EmitAtomicCmpSwapPartial.");
1254 MachineFunction *MF = BB->getParent();
1255 MachineRegisterInfo &RegInfo = MF->getRegInfo();
1256 const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1257 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1258 DebugLoc DL = MI->getDebugLoc();
1260 unsigned Dest = MI->getOperand(0).getReg();
1261 unsigned Ptr = MI->getOperand(1).getReg();
1262 unsigned CmpVal = MI->getOperand(2).getReg();
1263 unsigned NewVal = MI->getOperand(3).getReg();
1265 unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1266 unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1267 unsigned Mask = RegInfo.createVirtualRegister(RC);
1268 unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1269 unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1270 unsigned OldVal = RegInfo.createVirtualRegister(RC);
1271 unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1272 unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1273 unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1274 unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1275 unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1276 unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1277 unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1278 unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1279 unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1280 unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1281 unsigned SllRes = RegInfo.createVirtualRegister(RC);
1282 unsigned Success = RegInfo.createVirtualRegister(RC);
1284 // insert new blocks after the current block
1285 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1286 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1287 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1288 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1289 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1290 MachineFunction::iterator It = BB;
1292 MF->insert(It, loop1MBB);
1293 MF->insert(It, loop2MBB);
1294 MF->insert(It, sinkMBB);
1295 MF->insert(It, exitMBB);
1297 // Transfer the remainder of BB and its successor edges to exitMBB.
1298 exitMBB->splice(exitMBB->begin(), BB,
1299 std::next(MachineBasicBlock::iterator(MI)), BB->end());
1300 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1302 BB->addSuccessor(loop1MBB);
1303 loop1MBB->addSuccessor(sinkMBB);
1304 loop1MBB->addSuccessor(loop2MBB);
1305 loop2MBB->addSuccessor(loop1MBB);
1306 loop2MBB->addSuccessor(sinkMBB);
1307 sinkMBB->addSuccessor(exitMBB);
1309 // FIXME: computation of newval2 can be moved to loop2MBB.
1311 // addiu masklsb2,$0,-4 # 0xfffffffc
1312 // and alignedaddr,ptr,masklsb2
1313 // andi ptrlsb2,ptr,3
1314 // sll shiftamt,ptrlsb2,3
1315 // ori maskupper,$0,255 # 0xff
1316 // sll mask,maskupper,shiftamt
1317 // nor mask2,$0,mask
1318 // andi maskedcmpval,cmpval,255
1319 // sll shiftedcmpval,maskedcmpval,shiftamt
1320 // andi maskednewval,newval,255
1321 // sll shiftednewval,maskednewval,shiftamt
1322 int64_t MaskImm = (Size == 1) ? 255 : 65535;
1323 BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
1324 .addReg(Mips::ZERO).addImm(-4);
1325 BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
1326 .addReg(Ptr).addReg(MaskLSB2);
1327 BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1328 if (Subtarget->isLittle()) {
1329 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1331 unsigned Off = RegInfo.createVirtualRegister(RC);
1332 BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1333 .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1334 BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1336 BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1337 .addReg(Mips::ZERO).addImm(MaskImm);
1338 BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1339 .addReg(MaskUpper).addReg(ShiftAmt);
1340 BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1341 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
1342 .addReg(CmpVal).addImm(MaskImm);
1343 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
1344 .addReg(MaskedCmpVal).addReg(ShiftAmt);
1345 BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
1346 .addReg(NewVal).addImm(MaskImm);
1347 BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
1348 .addReg(MaskedNewVal).addReg(ShiftAmt);
1351 // ll oldval,0(alginedaddr)
1352 // and maskedoldval0,oldval,mask
1353 // bne maskedoldval0,shiftedcmpval,sinkMBB
1355 BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1356 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1357 .addReg(OldVal).addReg(Mask);
1358 BuildMI(BB, DL, TII->get(Mips::BNE))
1359 .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1362 // and maskedoldval1,oldval,mask2
1363 // or storeval,maskedoldval1,shiftednewval
1364 // sc success,storeval,0(alignedaddr)
1365 // beq success,$0,loop1MBB
1367 BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1368 .addReg(OldVal).addReg(Mask2);
1369 BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1370 .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1371 BuildMI(BB, DL, TII->get(Mips::SC), Success)
1372 .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1373 BuildMI(BB, DL, TII->get(Mips::BEQ))
1374 .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1377 // srl srlres,maskedoldval0,shiftamt
1378 // sll sllres,srlres,24
1379 // sra dest,sllres,24
1381 int64_t ShiftImm = (Size == 1) ? 24 : 16;
1383 BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1384 .addReg(MaskedOldVal0).addReg(ShiftAmt);
1385 BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
1386 .addReg(SrlRes).addImm(ShiftImm);
1387 BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
1388 .addReg(SllRes).addImm(ShiftImm);
1390 MI->eraseFromParent(); // The instruction is gone now.
1395 //===----------------------------------------------------------------------===//
1396 // Misc Lower Operation implementation
1397 //===----------------------------------------------------------------------===//
1398 SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
1399 SDValue Chain = Op.getOperand(0);
1400 SDValue Table = Op.getOperand(1);
1401 SDValue Index = Op.getOperand(2);
1403 EVT PTy = getPointerTy();
1404 unsigned EntrySize =
1405 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1407 Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1408 DAG.getConstant(EntrySize, PTy));
1409 SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1411 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1412 Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1413 MachinePointerInfo::getJumpTable(), MemVT, false, false,
1415 Chain = Addr.getValue(1);
1417 if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
1418 // For PIC, the sequence is:
1419 // BRIND(load(Jumptable + index) + RelocBase)
1420 // RelocBase can be JumpTable, GOT or some sort of global base.
1421 Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1422 getPICJumpTableRelocBase(Table, DAG));
1425 return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1428 SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1429 // The first operand is the chain, the second is the condition, the third is
1430 // the block to branch to if the condition is true.
1431 SDValue Chain = Op.getOperand(0);
1432 SDValue Dest = Op.getOperand(2);
1435 SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
1437 // Return if flag is not set by a floating point comparison.
1438 if (CondRes.getOpcode() != MipsISD::FPCmp)
1441 SDValue CCNode = CondRes.getOperand(2);
1443 (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1444 unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1445 SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
1446 SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
1447 return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
1448 FCC0, Dest, CondRes);
1451 SDValue MipsTargetLowering::
1452 lowerSELECT(SDValue Op, SelectionDAG &DAG) const
1454 SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
1456 // Return if flag is not set by a floating point comparison.
1457 if (Cond.getOpcode() != MipsISD::FPCmp)
1460 return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1464 SDValue MipsTargetLowering::
1465 lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
1468 EVT Ty = Op.getOperand(0).getValueType();
1469 SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1470 getSetCCResultType(*DAG.getContext(), Ty),
1471 Op.getOperand(0), Op.getOperand(1),
1474 return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1478 SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1479 SDValue Cond = createFPCmp(DAG, Op);
1481 assert(Cond.getOpcode() == MipsISD::FPCmp &&
1482 "Floating point operand expected.");
1484 SDValue True = DAG.getConstant(1, MVT::i32);
1485 SDValue False = DAG.getConstant(0, MVT::i32);
1487 return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
1490 SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
1491 SelectionDAG &DAG) const {
1492 // FIXME there isn't actually debug info here
1494 EVT Ty = Op.getValueType();
1495 GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1496 const GlobalValue *GV = N->getGlobal();
1498 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
1499 const MipsTargetObjectFile &TLOF =
1500 (const MipsTargetObjectFile&)getObjFileLowering();
1502 // %gp_rel relocation
1503 if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1504 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
1506 SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
1507 DAG.getVTList(MVT::i32), &GA, 1);
1508 SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
1509 return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
1512 // %hi/%lo relocation
1513 return getAddrNonPIC(N, Ty, DAG);
1516 if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1517 return getAddrLocal(N, Ty, DAG, HasMips64);
1520 return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
1521 MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1522 MachinePointerInfo::getGOT());
1524 return getAddrGlobal(N, Ty, DAG,
1525 HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16,
1526 DAG.getEntryNode(), MachinePointerInfo::getGOT());
1529 SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
1530 SelectionDAG &DAG) const {
1531 BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1532 EVT Ty = Op.getValueType();
1534 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1535 return getAddrNonPIC(N, Ty, DAG);
1537 return getAddrLocal(N, Ty, DAG, HasMips64);
1540 SDValue MipsTargetLowering::
1541 lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
1543 // If the relocation model is PIC, use the General Dynamic TLS Model or
1544 // Local Dynamic TLS model, otherwise use the Initial Exec or
1545 // Local Exec TLS Model.
1547 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1549 const GlobalValue *GV = GA->getGlobal();
1550 EVT PtrVT = getPointerTy();
1552 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1554 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1555 // General Dynamic and Local Dynamic TLS Model.
1556 unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1559 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1560 SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1561 getGlobalReg(DAG, PtrVT), TGA);
1562 unsigned PtrSize = PtrVT.getSizeInBits();
1563 IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1565 SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
1569 Entry.Node = Argument;
1571 Args.push_back(Entry);
1573 TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
1574 false, false, false, false, 0, CallingConv::C,
1575 /*IsTailCall=*/false, /*doesNotRet=*/false,
1576 /*isReturnValueUsed=*/true,
1577 TlsGetAddr, Args, DAG, DL);
1578 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
1580 SDValue Ret = CallResult.first;
1582 if (model != TLSModel::LocalDynamic)
1585 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1586 MipsII::MO_DTPREL_HI);
1587 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1588 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1589 MipsII::MO_DTPREL_LO);
1590 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1591 SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1592 return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
1596 if (model == TLSModel::InitialExec) {
1597 // Initial Exec TLS Model
1598 SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1599 MipsII::MO_GOTTPREL);
1600 TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
1602 Offset = DAG.getLoad(PtrVT, DL,
1603 DAG.getEntryNode(), TGA, MachinePointerInfo(),
1604 false, false, false, 0);
1606 // Local Exec TLS Model
1607 assert(model == TLSModel::LocalExec);
1608 SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1609 MipsII::MO_TPREL_HI);
1610 SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1611 MipsII::MO_TPREL_LO);
1612 SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1613 SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1614 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1617 SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1618 return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
1621 SDValue MipsTargetLowering::
1622 lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
1624 JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1625 EVT Ty = Op.getValueType();
1627 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1628 return getAddrNonPIC(N, Ty, DAG);
1630 return getAddrLocal(N, Ty, DAG, HasMips64);
1633 SDValue MipsTargetLowering::
1634 lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
1636 // gp_rel relocation
1637 // FIXME: we should reference the constant pool using small data sections,
1638 // but the asm printer currently doesn't support this feature without
1639 // hacking it. This feature should come soon so we can uncomment the
1641 //if (IsInSmallSection(C->getType())) {
1642 // SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1643 // SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1644 // ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
1645 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1646 EVT Ty = Op.getValueType();
1648 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
1649 return getAddrNonPIC(N, Ty, DAG);
1651 return getAddrLocal(N, Ty, DAG, HasMips64);
1654 SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1655 MachineFunction &MF = DAG.getMachineFunction();
1656 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1659 SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1662 // vastart just stores the address of the VarArgsFrameIndex slot into the
1663 // memory location argument.
1664 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1665 return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
1666 MachinePointerInfo(SV), false, false, 0);
1669 static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1670 bool HasExtractInsert) {
1671 EVT TyX = Op.getOperand(0).getValueType();
1672 EVT TyY = Op.getOperand(1).getValueType();
1673 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1674 SDValue Const31 = DAG.getConstant(31, MVT::i32);
1678 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1680 SDValue X = (TyX == MVT::f32) ?
1681 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1682 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1684 SDValue Y = (TyY == MVT::f32) ?
1685 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1686 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1689 if (HasExtractInsert) {
1690 // ext E, Y, 31, 1 ; extract bit31 of Y
1691 // ins X, E, 31, 1 ; insert extracted bit at bit31 of X
1692 SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1693 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1696 // srl SrlX, SllX, 1
1698 // sll SllY, SrlX, 31
1699 // or Or, SrlX, SllY
1700 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1701 SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1702 SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1703 SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1704 Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1707 if (TyX == MVT::f32)
1708 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1710 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1711 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1712 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1715 static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1716 bool HasExtractInsert) {
1717 unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1718 unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1719 EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1720 SDValue Const1 = DAG.getConstant(1, MVT::i32);
1723 // Bitcast to integer nodes.
1724 SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1725 SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
1727 if (HasExtractInsert) {
1728 // ext E, Y, width(Y) - 1, 1 ; extract bit width(Y)-1 of Y
1729 // ins X, E, width(X) - 1, 1 ; insert extracted bit at bit width(X)-1 of X
1730 SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1731 DAG.getConstant(WidthY - 1, MVT::i32), Const1);
1733 if (WidthX > WidthY)
1734 E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1735 else if (WidthY > WidthX)
1736 E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
1738 SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1739 DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1740 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1743 // (d)sll SllX, X, 1
1744 // (d)srl SrlX, SllX, 1
1745 // (d)srl SrlY, Y, width(Y)-1
1746 // (d)sll SllY, SrlX, width(Y)-1
1747 // or Or, SrlX, SllY
1748 SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1749 SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1750 SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1751 DAG.getConstant(WidthY - 1, MVT::i32));
1753 if (WidthX > WidthY)
1754 SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1755 else if (WidthY > WidthX)
1756 SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1758 SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1759 DAG.getConstant(WidthX - 1, MVT::i32));
1760 SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1761 return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
1765 MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
1766 if (Subtarget->hasMips64())
1767 return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasExtractInsert());
1769 return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasExtractInsert());
1772 static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG,
1773 bool HasExtractInsert) {
1774 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
1777 // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1779 SDValue X = (Op.getValueType() == MVT::f32) ?
1780 DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1781 DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1785 if (HasExtractInsert)
1786 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
1787 DAG.getRegister(Mips::ZERO, MVT::i32),
1788 DAG.getConstant(31, MVT::i32), Const1, X);
1790 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1791 Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1794 if (Op.getValueType() == MVT::f32)
1795 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
1797 SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1798 Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1799 return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1802 static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG,
1803 bool HasExtractInsert) {
1804 SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
1807 // Bitcast to integer node.
1808 SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
1811 if (HasExtractInsert)
1812 Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
1813 DAG.getRegister(Mips::ZERO_64, MVT::i64),
1814 DAG.getConstant(63, MVT::i32), Const1, X);
1816 SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
1817 Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
1820 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
1824 MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
1825 if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
1826 return lowerFABS64(Op, DAG, Subtarget->hasExtractInsert());
1828 return lowerFABS32(Op, DAG, Subtarget->hasExtractInsert());
1831 SDValue MipsTargetLowering::
1832 lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
1834 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1835 "Frame address can only be determined for current frame.");
1837 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1838 MFI->setFrameAddressIsTaken(true);
1839 EVT VT = Op.getValueType();
1841 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
1842 IsN64 ? Mips::FP_64 : Mips::FP, VT);
1846 SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
1847 SelectionDAG &DAG) const {
1848 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1852 assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1853 "Return address can be determined only for current frame.");
1855 MachineFunction &MF = DAG.getMachineFunction();
1856 MachineFrameInfo *MFI = MF.getFrameInfo();
1857 MVT VT = Op.getSimpleValueType();
1858 unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
1859 MFI->setReturnAddressIsTaken(true);
1861 // Return RA, which contains the return address. Mark it an implicit live-in.
1862 unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
1863 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
1866 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1867 // generated from __builtin_eh_return (offset, handler)
1868 // The effect of this is to adjust the stack pointer by "offset"
1869 // and then branch to "handler".
1870 SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
1872 MachineFunction &MF = DAG.getMachineFunction();
1873 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1875 MipsFI->setCallsEhReturn();
1876 SDValue Chain = Op.getOperand(0);
1877 SDValue Offset = Op.getOperand(1);
1878 SDValue Handler = Op.getOperand(2);
1880 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
1882 // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1883 // EH_RETURN nodes, so that instructions are emitted back-to-back.
1884 unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
1885 unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
1886 Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1887 Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1888 return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1889 DAG.getRegister(OffsetReg, Ty),
1890 DAG.getRegister(AddrReg, getPointerTy()),
1894 SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
1895 SelectionDAG &DAG) const {
1896 // FIXME: Need pseudo-fence for 'singlethread' fences
1897 // FIXME: Set SType for weaker fences where supported/appropriate.
1900 return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
1901 DAG.getConstant(SType, MVT::i32));
1904 SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
1905 SelectionDAG &DAG) const {
1907 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1908 SDValue Shamt = Op.getOperand(2);
1911 // lo = (shl lo, shamt)
1912 // hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1915 // hi = (shl lo, shamt[4:0])
1916 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1917 DAG.getConstant(-1, MVT::i32));
1918 SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1919 DAG.getConstant(1, MVT::i32));
1920 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1922 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1923 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1924 SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1925 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1926 DAG.getConstant(0x20, MVT::i32));
1927 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1928 DAG.getConstant(0, MVT::i32), ShiftLeftLo);
1929 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1931 SDValue Ops[2] = {Lo, Hi};
1932 return DAG.getMergeValues(Ops, 2, DL);
1935 SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
1938 SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1939 SDValue Shamt = Op.getOperand(2);
1942 // lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1944 // hi = (sra hi, shamt)
1946 // hi = (srl hi, shamt)
1949 // lo = (sra hi, shamt[4:0])
1950 // hi = (sra hi, 31)
1952 // lo = (srl hi, shamt[4:0])
1954 SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1955 DAG.getConstant(-1, MVT::i32));
1956 SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1957 DAG.getConstant(1, MVT::i32));
1958 SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1959 SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1960 SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1961 SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1963 SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1964 DAG.getConstant(0x20, MVT::i32));
1965 SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1966 DAG.getConstant(31, MVT::i32));
1967 Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1968 Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1969 IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1972 SDValue Ops[2] = {Lo, Hi};
1973 return DAG.getMergeValues(Ops, 2, DL);
1976 static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
1977 SDValue Chain, SDValue Src, unsigned Offset) {
1978 SDValue Ptr = LD->getBasePtr();
1979 EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
1980 EVT BasePtrVT = Ptr.getValueType();
1982 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
1985 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
1986 DAG.getConstant(Offset, BasePtrVT));
1988 SDValue Ops[] = { Chain, Ptr, Src };
1989 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
1990 LD->getMemOperand());
1993 // Expand an unaligned 32 or 64-bit integer load node.
1994 SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1995 LoadSDNode *LD = cast<LoadSDNode>(Op);
1996 EVT MemVT = LD->getMemoryVT();
1998 // Return if load is aligned or if MemVT is neither i32 nor i64.
1999 if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2000 ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2003 bool IsLittle = Subtarget->isLittle();
2004 EVT VT = Op.getValueType();
2005 ISD::LoadExtType ExtType = LD->getExtensionType();
2006 SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2008 assert((VT == MVT::i32) || (VT == MVT::i64));
2011 // (set dst, (i64 (load baseptr)))
2013 // (set tmp, (ldl (add baseptr, 7), undef))
2014 // (set dst, (ldr baseptr, tmp))
2015 if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2016 SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2018 return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2022 SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2024 SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2028 // (set dst, (i32 (load baseptr))) or
2029 // (set dst, (i64 (sextload baseptr))) or
2030 // (set dst, (i64 (extload baseptr)))
2032 // (set tmp, (lwl (add baseptr, 3), undef))
2033 // (set dst, (lwr baseptr, tmp))
2034 if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2035 (ExtType == ISD::EXTLOAD))
2038 assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2041 // (set dst, (i64 (zextload baseptr)))
2043 // (set tmp0, (lwl (add baseptr, 3), undef))
2044 // (set tmp1, (lwr baseptr, tmp0))
2045 // (set tmp2, (shl tmp1, 32))
2046 // (set dst, (srl tmp2, 32))
2048 SDValue Const32 = DAG.getConstant(32, MVT::i32);
2049 SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2050 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2051 SDValue Ops[] = { SRL, LWR.getValue(1) };
2052 return DAG.getMergeValues(Ops, 2, DL);
2055 static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2056 SDValue Chain, unsigned Offset) {
2057 SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2058 EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2060 SDVTList VTList = DAG.getVTList(MVT::Other);
2063 Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2064 DAG.getConstant(Offset, BasePtrVT));
2066 SDValue Ops[] = { Chain, Value, Ptr };
2067 return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
2068 SD->getMemOperand());
2071 // Expand an unaligned 32 or 64-bit integer store node.
2072 static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2074 SDValue Value = SD->getValue(), Chain = SD->getChain();
2075 EVT VT = Value.getValueType();
2078 // (store val, baseptr) or
2079 // (truncstore val, baseptr)
2081 // (swl val, (add baseptr, 3))
2082 // (swr val, baseptr)
2083 if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2084 SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
2086 return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2089 assert(VT == MVT::i64);
2092 // (store val, baseptr)
2094 // (sdl val, (add baseptr, 7))
2095 // (sdr val, baseptr)
2096 SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2097 return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2100 // Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2101 static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2102 SDValue Val = SD->getValue();
2104 if (Val.getOpcode() != ISD::FP_TO_SINT)
2107 EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
2108 SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
2111 return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
2112 SD->getPointerInfo(), SD->isVolatile(),
2113 SD->isNonTemporal(), SD->getAlignment());
2116 SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2117 StoreSDNode *SD = cast<StoreSDNode>(Op);
2118 EVT MemVT = SD->getMemoryVT();
2120 // Lower unaligned integer stores.
2121 if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2122 ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2123 return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2125 return lowerFP_TO_SINT_STORE(SD, DAG);
2128 SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
2129 if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2130 || cast<ConstantSDNode>
2131 (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2132 || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2136 // (add (frameaddr 0), (frame_to_args_offset))
2137 // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2138 // (add FrameObject, 0)
2139 // where FrameObject is a fixed StackObject with offset 0 which points to
2140 // the old stack pointer.
2141 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2142 EVT ValTy = Op->getValueType(0);
2143 int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2144 SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2145 return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
2146 DAG.getConstant(0, ValTy));
2149 SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2150 SelectionDAG &DAG) const {
2151 EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
2152 SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
2154 return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
2157 //===----------------------------------------------------------------------===//
2158 // Calling Convention Implementation
2159 //===----------------------------------------------------------------------===//
2161 //===----------------------------------------------------------------------===//
2162 // TODO: Implement a generic logic using tblgen that can support this.
2163 // Mips O32 ABI rules:
2165 // i32 - Passed in A0, A1, A2, A3 and stack
2166 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
2167 // an argument. Otherwise, passed in A1, A2, A3 and stack.
2168 // f64 - Only passed in two aliased f32 registers if no int reg has been used
2169 // yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2170 // not used, it must be shadowed. If only A3 is avaiable, shadow it and
2173 // For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2174 //===----------------------------------------------------------------------===//
2176 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2177 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2178 CCState &State, const uint16_t *F64Regs) {
2180 static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
2182 static const uint16_t IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2183 static const uint16_t F32Regs[] = { Mips::F12, Mips::F14 };
2185 // Do not process byval args here.
2186 if (ArgFlags.isByVal())
2189 // Promote i8 and i16
2190 if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2192 if (ArgFlags.isSExt())
2193 LocInfo = CCValAssign::SExt;
2194 else if (ArgFlags.isZExt())
2195 LocInfo = CCValAssign::ZExt;
2197 LocInfo = CCValAssign::AExt;
2202 // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2203 // is true: function is vararg, argument is 3rd or higher, there is previous
2204 // argument which is not f32 or f64.
2205 bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2206 || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
2207 unsigned OrigAlign = ArgFlags.getOrigAlign();
2208 bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
2210 if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
2211 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2212 // If this is the first part of an i64 arg,
2213 // the allocated register must be either A0 or A2.
2214 if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2215 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2217 } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2218 // Allocate int register and shadow next int register. If first
2219 // available register is Mips::A1 or Mips::A3, shadow it too.
2220 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2221 if (Reg == Mips::A1 || Reg == Mips::A3)
2222 Reg = State.AllocateReg(IntRegs, IntRegsSize);
2223 State.AllocateReg(IntRegs, IntRegsSize);
2225 } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2226 // we are guaranteed to find an available float register
2227 if (ValVT == MVT::f32) {
2228 Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2229 // Shadow int register
2230 State.AllocateReg(IntRegs, IntRegsSize);
2232 Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2233 // Shadow int registers
2234 unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2235 if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2236 State.AllocateReg(IntRegs, IntRegsSize);
2237 State.AllocateReg(IntRegs, IntRegsSize);
2240 llvm_unreachable("Cannot handle this ValVT.");
2243 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2245 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
2247 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2252 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2253 MVT LocVT, CCValAssign::LocInfo LocInfo,
2254 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2255 static const uint16_t F64Regs[] = { Mips::D6, Mips::D7 };
2257 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2260 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2261 MVT LocVT, CCValAssign::LocInfo LocInfo,
2262 ISD::ArgFlagsTy ArgFlags, CCState &State) {
2263 static const uint16_t F64Regs[] = { Mips::D12_64, Mips::D14_64 };
2265 return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2268 #include "MipsGenCallingConv.inc"
2270 //===----------------------------------------------------------------------===//
2271 // Call Calling Convention Implementation
2272 //===----------------------------------------------------------------------===//
2274 // Return next O32 integer argument register.
2275 static unsigned getNextIntArgReg(unsigned Reg) {
2276 assert((Reg == Mips::A0) || (Reg == Mips::A2));
2277 return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2281 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2282 SDValue Chain, SDValue Arg, SDLoc DL,
2283 bool IsTailCall, SelectionDAG &DAG) const {
2285 SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2286 DAG.getIntPtrConstant(Offset));
2287 return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2291 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2292 int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2293 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2294 return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2295 /*isVolatile=*/ true, false, 0);
2298 void MipsTargetLowering::
2299 getOpndList(SmallVectorImpl<SDValue> &Ops,
2300 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2301 bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2302 CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2303 // Insert node "GP copy globalreg" before call to function.
2305 // R_MIPS_CALL* operators (emitted when non-internal functions are called
2306 // in PIC mode) allow symbols to be resolved via lazy binding.
2307 // The lazy binding stub requires GP to point to the GOT.
2308 if (IsPICCall && !InternalLinkage) {
2309 unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
2310 EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
2311 RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2314 // Build a sequence of copy-to-reg nodes chained together with token
2315 // chain and flag operands which copy the outgoing args into registers.
2316 // The InFlag in necessary since all emitted instructions must be
2320 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2321 Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2322 RegsToPass[i].second, InFlag);
2323 InFlag = Chain.getValue(1);
2326 // Add argument registers to the end of the list so that they are
2327 // known live into the call.
2328 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2329 Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2330 RegsToPass[i].second.getValueType()));
2332 // Add a register mask operand representing the call-preserved registers.
2333 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2334 const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2335 assert(Mask && "Missing call preserved mask for calling convention");
2336 if (Subtarget->inMips16HardFloat()) {
2337 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2338 llvm::StringRef Sym = G->getGlobal()->getName();
2339 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2340 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
2341 Mask = MipsRegisterInfo::getMips16RetHelperMask();
2345 Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2347 if (InFlag.getNode())
2348 Ops.push_back(InFlag);
2351 /// LowerCall - functions arguments are copied from virtual regs to
2352 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
2354 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
2355 SmallVectorImpl<SDValue> &InVals) const {
2356 SelectionDAG &DAG = CLI.DAG;
2358 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2359 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
2360 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
2361 SDValue Chain = CLI.Chain;
2362 SDValue Callee = CLI.Callee;
2363 bool &IsTailCall = CLI.IsTailCall;
2364 CallingConv::ID CallConv = CLI.CallConv;
2365 bool IsVarArg = CLI.IsVarArg;
2367 MachineFunction &MF = DAG.getMachineFunction();
2368 MachineFrameInfo *MFI = MF.getFrameInfo();
2369 const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
2370 MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2371 bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
2373 // Analyze operands of the call, assigning locations to each operand.
2374 SmallVector<CCValAssign, 16> ArgLocs;
2375 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
2376 getTargetMachine(), ArgLocs, *DAG.getContext());
2377 MipsCC::SpecialCallingConvType SpecialCallingConv =
2378 getSpecialCallingConv(Callee);
2379 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo,
2380 SpecialCallingConv);
2382 MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
2383 Subtarget->mipsSEUsesSoftFloat(),
2384 Callee.getNode(), CLI.Args);
2386 // Get a count of how many bytes are to be pushed on the stack.
2387 unsigned NextStackOffset = CCInfo.getNextStackOffset();
2389 // Check if it's really possible to do a tail call.
2392 isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
2393 *MF.getInfo<MipsFunctionInfo>());
2398 // Chain is the output chain of the last Load/Store or CopyToReg node.
2399 // ByValChain is the output chain of the last Memcpy node created for copying
2400 // byval arguments to the stack.
2401 unsigned StackAlignment = TFL->getStackAlignment();
2402 NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
2403 SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2406 Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
2408 SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
2409 IsN64 ? Mips::SP_64 : Mips::SP,
2412 // With EABI is it possible to have 16 args on registers.
2413 std::deque< std::pair<unsigned, SDValue> > RegsToPass;
2414 SmallVector<SDValue, 8> MemOpChains;
2415 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
2417 // Walk the register/memloc assignments, inserting copies/loads.
2418 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2419 SDValue Arg = OutVals[i];
2420 CCValAssign &VA = ArgLocs[i];
2421 MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
2422 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2425 if (Flags.isByVal()) {
2426 assert(Flags.getByValSize() &&
2427 "ByVal args of size 0 should have been ignored by front-end.");
2428 assert(ByValArg != MipsCCInfo.byval_end());
2429 assert(!IsTailCall &&
2430 "Do not tail-call optimize if there is a byval argument.");
2431 passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
2432 MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2437 // Promote the value if needed.
2438 switch (VA.getLocInfo()) {
2439 default: llvm_unreachable("Unknown loc info!");
2440 case CCValAssign::Full:
2441 if (VA.isRegLoc()) {
2442 if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2443 (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2444 (ValVT == MVT::i64 && LocVT == MVT::f64))
2445 Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2446 else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
2447 SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2448 Arg, DAG.getConstant(0, MVT::i32));
2449 SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2450 Arg, DAG.getConstant(1, MVT::i32));
2451 if (!Subtarget->isLittle())
2453 unsigned LocRegLo = VA.getLocReg();
2454 unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2455 RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2456 RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
2461 case CCValAssign::SExt:
2462 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
2464 case CCValAssign::ZExt:
2465 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
2467 case CCValAssign::AExt:
2468 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
2472 // Arguments that can be passed on register must be kept at
2473 // RegsToPass vector
2474 if (VA.isRegLoc()) {
2475 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2479 // Register can't get to this point...
2480 assert(VA.isMemLoc());
2482 // emit ISD::STORE whichs stores the
2483 // parameter value to a stack Location
2484 MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
2485 Chain, Arg, DL, IsTailCall, DAG));
2488 // Transform all store nodes into one single node because all store
2489 // nodes are independent of each other.
2490 if (!MemOpChains.empty())
2491 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
2492 &MemOpChains[0], MemOpChains.size());
2494 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2495 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2496 // node so that legalize doesn't hack it.
2497 bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
2498 bool GlobalOrExternal = false, InternalLinkage = false;
2500 EVT Ty = Callee.getValueType();
2502 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2504 const GlobalValue *Val = G->getGlobal();
2505 InternalLinkage = Val->hasInternalLinkage();
2507 if (InternalLinkage)
2508 Callee = getAddrLocal(G, Ty, DAG, HasMips64);
2510 Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
2511 MipsII::MO_CALL_LO16, Chain,
2512 FuncInfo->callPtrInfo(Val));
2514 Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2515 FuncInfo->callPtrInfo(Val));
2517 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
2518 MipsII::MO_NO_FLAG);
2519 GlobalOrExternal = true;
2521 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2522 const char *Sym = S->getSymbol();
2524 if (!IsN64 && !IsPIC) // !N64 && static
2525 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
2526 MipsII::MO_NO_FLAG);
2528 Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
2529 MipsII::MO_CALL_LO16, Chain,
2530 FuncInfo->callPtrInfo(Sym));
2532 Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2533 FuncInfo->callPtrInfo(Sym));
2535 GlobalOrExternal = true;
2538 SmallVector<SDValue, 8> Ops(1, Chain);
2539 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2541 getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2542 CLI, Callee, Chain);
2545 return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
2547 MipsISD::NodeType JmpLink = isMicroMips ? MipsISD::JmpLinkMM
2549 Chain = DAG.getNode(JmpLink, DL, NodeTys, &Ops[0], Ops.size());
2550 SDValue InFlag = Chain.getValue(1);
2552 // Create the CALLSEQ_END node.
2553 Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
2554 DAG.getIntPtrConstant(0, true), InFlag, DL);
2555 InFlag = Chain.getValue(1);
2557 // Handle result values, copying them out of physregs into vregs that we
2559 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2560 Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
2563 /// LowerCallResult - Lower the result values of a call into the
2564 /// appropriate copies out of appropriate physical registers.
2566 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
2567 CallingConv::ID CallConv, bool IsVarArg,
2568 const SmallVectorImpl<ISD::InputArg> &Ins,
2569 SDLoc DL, SelectionDAG &DAG,
2570 SmallVectorImpl<SDValue> &InVals,
2571 const SDNode *CallNode,
2572 const Type *RetTy) const {
2573 // Assign locations to each value returned by this call.
2574 SmallVector<CCValAssign, 16> RVLocs;
2575 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
2576 getTargetMachine(), RVLocs, *DAG.getContext());
2577 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
2579 MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
2582 // Copy all of the result registers out of their specified physreg.
2583 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2584 SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
2585 RVLocs[i].getLocVT(), InFlag);
2586 Chain = Val.getValue(1);
2587 InFlag = Val.getValue(2);
2589 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
2590 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
2592 InVals.push_back(Val);
2598 //===----------------------------------------------------------------------===//
2599 // Formal Arguments Calling Convention Implementation
2600 //===----------------------------------------------------------------------===//
2601 /// LowerFormalArguments - transform physical registers into virtual registers
2602 /// and generate load operations for arguments places on the stack.
2604 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
2605 CallingConv::ID CallConv,
2607 const SmallVectorImpl<ISD::InputArg> &Ins,
2608 SDLoc DL, SelectionDAG &DAG,
2609 SmallVectorImpl<SDValue> &InVals)
2611 MachineFunction &MF = DAG.getMachineFunction();
2612 MachineFrameInfo *MFI = MF.getFrameInfo();
2613 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2615 MipsFI->setVarArgsFrameIndex(0);
2617 // Used with vargs to acumulate store chains.
2618 std::vector<SDValue> OutChains;
2620 // Assign locations to all of the incoming arguments.
2621 SmallVector<CCValAssign, 16> ArgLocs;
2622 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
2623 getTargetMachine(), ArgLocs, *DAG.getContext());
2624 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
2625 Function::const_arg_iterator FuncArg =
2626 DAG.getMachineFunction().getFunction()->arg_begin();
2627 bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
2629 MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
2630 MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2631 MipsCCInfo.hasByValArg());
2633 unsigned CurArgIdx = 0;
2634 MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
2636 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2637 CCValAssign &VA = ArgLocs[i];
2638 std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2639 CurArgIdx = Ins[i].OrigArgIndex;
2640 EVT ValVT = VA.getValVT();
2641 ISD::ArgFlagsTy Flags = Ins[i].Flags;
2642 bool IsRegLoc = VA.isRegLoc();
2644 if (Flags.isByVal()) {
2645 assert(Flags.getByValSize() &&
2646 "ByVal args of size 0 should have been ignored by front-end.");
2647 assert(ByValArg != MipsCCInfo.byval_end());
2648 copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
2649 MipsCCInfo, *ByValArg);
2654 // Arguments stored on registers
2656 MVT RegVT = VA.getLocVT();
2657 unsigned ArgReg = VA.getLocReg();
2658 const TargetRegisterClass *RC = getRegClassFor(RegVT);
2660 // Transform the arguments stored on
2661 // physical registers into virtual ones
2662 unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2663 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
2665 // If this is an 8 or 16-bit value, it has been passed promoted
2666 // to 32 bits. Insert an assert[sz]ext to capture this, then
2667 // truncate to the right size.
2668 if (VA.getLocInfo() != CCValAssign::Full) {
2669 unsigned Opcode = 0;
2670 if (VA.getLocInfo() == CCValAssign::SExt)
2671 Opcode = ISD::AssertSext;
2672 else if (VA.getLocInfo() == CCValAssign::ZExt)
2673 Opcode = ISD::AssertZext;
2675 ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
2676 DAG.getValueType(ValVT));
2677 ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
2680 // Handle floating point arguments passed in integer registers and
2681 // long double arguments passed in floating point registers.
2682 if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2683 (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2684 (RegVT == MVT::f64 && ValVT == MVT::i64))
2685 ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
2686 else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
2687 unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
2688 getNextIntArgReg(ArgReg), RC);
2689 SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
2690 if (!Subtarget->isLittle())
2691 std::swap(ArgValue, ArgValue2);
2692 ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
2693 ArgValue, ArgValue2);
2696 InVals.push_back(ArgValue);
2697 } else { // VA.isRegLoc()
2700 assert(VA.isMemLoc());
2702 // The stack pointer offset is relative to the caller stack frame.
2703 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
2704 VA.getLocMemOffset(), true);
2706 // Create load nodes to retrieve arguments from the stack
2707 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2708 SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2709 MachinePointerInfo::getFixedStack(FI),
2710 false, false, false, 0);
2711 InVals.push_back(Load);
2712 OutChains.push_back(Load.getValue(1));
2716 // The mips ABIs for returning structs by value requires that we copy
2717 // the sret argument into $v0 for the return. Save the argument into
2718 // a virtual register so that we can access it from the return points.
2719 if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2720 unsigned Reg = MipsFI->getSRetReturnReg();
2722 Reg = MF.getRegInfo().
2723 createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
2724 MipsFI->setSRetReturnReg(Reg);
2726 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
2727 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
2731 writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
2733 // All stores are grouped in one node to allow the matching between
2734 // the size of Ins and InVals. This only happens when on varg functions
2735 if (!OutChains.empty()) {
2736 OutChains.push_back(Chain);
2737 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
2738 &OutChains[0], OutChains.size());
2744 //===----------------------------------------------------------------------===//
2745 // Return Value Calling Convention Implementation
2746 //===----------------------------------------------------------------------===//
2749 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2750 MachineFunction &MF, bool IsVarArg,
2751 const SmallVectorImpl<ISD::OutputArg> &Outs,
2752 LLVMContext &Context) const {
2753 SmallVector<CCValAssign, 16> RVLocs;
2754 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
2756 return CCInfo.CheckReturn(Outs, RetCC_Mips);
2760 MipsTargetLowering::LowerReturn(SDValue Chain,
2761 CallingConv::ID CallConv, bool IsVarArg,
2762 const SmallVectorImpl<ISD::OutputArg> &Outs,
2763 const SmallVectorImpl<SDValue> &OutVals,
2764 SDLoc DL, SelectionDAG &DAG) const {
2765 // CCValAssign - represent the assignment of
2766 // the return value to a location
2767 SmallVector<CCValAssign, 16> RVLocs;
2768 MachineFunction &MF = DAG.getMachineFunction();
2770 // CCState - Info about the registers and stack slot.
2771 CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
2773 MipsCC MipsCCInfo(CallConv, IsO32, Subtarget->isFP64bit(), CCInfo);
2775 // Analyze return values.
2776 MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
2777 MF.getFunction()->getReturnType());
2780 SmallVector<SDValue, 4> RetOps(1, Chain);
2782 // Copy the result values into the output registers.
2783 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2784 SDValue Val = OutVals[i];
2785 CCValAssign &VA = RVLocs[i];
2786 assert(VA.isRegLoc() && "Can only return in registers!");
2788 if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
2789 Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
2791 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
2793 // Guarantee that all emitted copies are stuck together with flags.
2794 Flag = Chain.getValue(1);
2795 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2798 // The mips ABIs for returning structs by value requires that we copy
2799 // the sret argument into $v0 for the return. We saved the argument into
2800 // a virtual register in the entry block, so now we copy the value out
2802 if (MF.getFunction()->hasStructRetAttr()) {
2803 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2804 unsigned Reg = MipsFI->getSRetReturnReg();
2807 llvm_unreachable("sret virtual register not created in the entry block");
2808 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
2809 unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
2811 Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
2812 Flag = Chain.getValue(1);
2813 RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
2816 RetOps[0] = Chain; // Update chain.
2818 // Add the flag if we have it.
2820 RetOps.push_back(Flag);
2822 // Return on Mips is always a "jr $ra"
2823 return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
2826 //===----------------------------------------------------------------------===//
2827 // Mips Inline Assembly Support
2828 //===----------------------------------------------------------------------===//
2830 /// getConstraintType - Given a constraint letter, return the type of
2831 /// constraint it is for this target.
2832 MipsTargetLowering::ConstraintType MipsTargetLowering::
2833 getConstraintType(const std::string &Constraint) const
2835 // Mips specific constraints
2836 // GCC config/mips/constraints.md
2838 // 'd' : An address register. Equivalent to r
2839 // unless generating MIPS16 code.
2840 // 'y' : Equivalent to r; retained for
2841 // backwards compatibility.
2842 // 'c' : A register suitable for use in an indirect
2843 // jump. This will always be $25 for -mabicalls.
2844 // 'l' : The lo register. 1 word storage.
2845 // 'x' : The hilo register pair. Double word storage.
2846 if (Constraint.size() == 1) {
2847 switch (Constraint[0]) {
2855 return C_RegisterClass;
2860 return TargetLowering::getConstraintType(Constraint);
2863 /// Examine constraint type and operand type and determine a weight value.
2864 /// This object must already have been set up with the operand type
2865 /// and the current alternative constraint selected.
2866 TargetLowering::ConstraintWeight
2867 MipsTargetLowering::getSingleConstraintMatchWeight(
2868 AsmOperandInfo &info, const char *constraint) const {
2869 ConstraintWeight weight = CW_Invalid;
2870 Value *CallOperandVal = info.CallOperandVal;
2871 // If we don't have a value, we can't do a match,
2872 // but allow it at the lowest weight.
2873 if (CallOperandVal == NULL)
2875 Type *type = CallOperandVal->getType();
2876 // Look at the constraint type.
2877 switch (*constraint) {
2879 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2883 if (type->isIntegerTy())
2884 weight = CW_Register;
2886 case 'f': // FPU or MSA register
2887 if (Subtarget->hasMSA() && type->isVectorTy() &&
2888 cast<VectorType>(type)->getBitWidth() == 128)
2889 weight = CW_Register;
2890 else if (type->isFloatTy())
2891 weight = CW_Register;
2893 case 'c': // $25 for indirect jumps
2894 case 'l': // lo register
2895 case 'x': // hilo register pair
2896 if (type->isIntegerTy())
2897 weight = CW_SpecificReg;
2899 case 'I': // signed 16 bit immediate
2900 case 'J': // integer zero
2901 case 'K': // unsigned 16 bit immediate
2902 case 'L': // signed 32 bit immediate where lower 16 bits are 0
2903 case 'N': // immediate in the range of -65535 to -1 (inclusive)
2904 case 'O': // signed 15 bit immediate (+- 16383)
2905 case 'P': // immediate in the range of 65535 to 1 (inclusive)
2906 if (isa<ConstantInt>(CallOperandVal))
2907 weight = CW_Constant;
2916 /// This is a helper function to parse a physical register string and split it
2917 /// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2918 /// that is returned indicates whether parsing was successful. The second flag
2919 /// is true if the numeric part exists.
2920 static std::pair<bool, bool>
2921 parsePhysicalReg(const StringRef &C, std::string &Prefix,
2922 unsigned long long &Reg) {
2923 if (C.front() != '{' || C.back() != '}')
2924 return std::make_pair(false, false);
2926 // Search for the first numeric character.
2927 StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2928 I = std::find_if(B, E, std::ptr_fun(isdigit));
2930 Prefix.assign(B, I - B);
2932 // The second flag is set to false if no numeric characters were found.
2934 return std::make_pair(true, false);
2936 // Parse the numeric characters.
2937 return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2941 std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2942 parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2943 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2944 const TargetRegisterClass *RC;
2946 unsigned long long Reg;
2948 std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2951 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2953 if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2954 // No numeric characters follow "hi" or "lo".
2956 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2958 RC = TRI->getRegClass(Prefix == "hi" ?
2959 Mips::HI32RegClassID : Mips::LO32RegClassID);
2960 return std::make_pair(*(RC->begin()), RC);
2961 } else if (Prefix.compare(0, 4, "$msa") == 0) {
2962 // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
2964 // No numeric characters follow the name.
2966 return std::make_pair((unsigned)0, (const TargetRegisterClass *)0);
2968 Reg = StringSwitch<unsigned long long>(Prefix)
2969 .Case("$msair", Mips::MSAIR)
2970 .Case("$msacsr", Mips::MSACSR)
2971 .Case("$msaaccess", Mips::MSAAccess)
2972 .Case("$msasave", Mips::MSASave)
2973 .Case("$msamodify", Mips::MSAModify)
2974 .Case("$msarequest", Mips::MSARequest)
2975 .Case("$msamap", Mips::MSAMap)
2976 .Case("$msaunmap", Mips::MSAUnmap)
2980 return std::make_pair((unsigned)0, (const TargetRegisterClass *)0);
2982 RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
2983 return std::make_pair(Reg, RC);
2987 return std::make_pair((unsigned)0, (const TargetRegisterClass*)0);
2989 if (Prefix == "$f") { // Parse $f0-$f31.
2990 // If the size of FP registers is 64-bit or Reg is an even number, select
2991 // the 64-bit register class. Otherwise, select the 32-bit register class.
2992 if (VT == MVT::Other)
2993 VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
2995 RC = getRegClassFor(VT);
2997 if (RC == &Mips::AFGR64RegClass) {
2998 assert(Reg % 2 == 0);
3001 } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
3002 RC = TRI->getRegClass(Mips::FCCRegClassID);
3003 else if (Prefix == "$w") { // Parse $w0-$w31.
3004 RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
3005 } else { // Parse $0-$31.
3006 assert(Prefix == "$");
3007 RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3010 assert(Reg < RC->getNumRegs());
3011 return std::make_pair(*(RC->begin() + Reg), RC);
3014 /// Given a register class constraint, like 'r', if this corresponds directly
3015 /// to an LLVM register class, return a register of 0 and the register class
3017 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
3018 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
3020 if (Constraint.size() == 1) {
3021 switch (Constraint[0]) {
3022 case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3023 case 'y': // Same as 'r'. Exists for compatibility.
3025 if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3026 if (Subtarget->inMips16Mode())
3027 return std::make_pair(0U, &Mips::CPU16RegsRegClass);
3028 return std::make_pair(0U, &Mips::GPR32RegClass);
3030 if (VT == MVT::i64 && !HasMips64)
3031 return std::make_pair(0U, &Mips::GPR32RegClass);
3032 if (VT == MVT::i64 && HasMips64)
3033 return std::make_pair(0U, &Mips::GPR64RegClass);
3034 // This will generate an error message
3035 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
3036 case 'f': // FPU or MSA register
3037 if (VT == MVT::v16i8)
3038 return std::make_pair(0U, &Mips::MSA128BRegClass);
3039 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3040 return std::make_pair(0U, &Mips::MSA128HRegClass);
3041 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3042 return std::make_pair(0U, &Mips::MSA128WRegClass);
3043 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3044 return std::make_pair(0U, &Mips::MSA128DRegClass);
3045 else if (VT == MVT::f32)
3046 return std::make_pair(0U, &Mips::FGR32RegClass);
3047 else if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
3048 if (Subtarget->isFP64bit())
3049 return std::make_pair(0U, &Mips::FGR64RegClass);
3050 return std::make_pair(0U, &Mips::AFGR64RegClass);
3053 case 'c': // register suitable for indirect jump
3055 return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
3056 assert(VT == MVT::i64 && "Unexpected type.");
3057 return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
3058 case 'l': // register suitable for indirect jump
3060 return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3061 return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
3062 case 'x': // register suitable for indirect jump
3063 // Fixme: Not triggering the use of both hi and low
3064 // This will generate an error message
3065 return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
3069 std::pair<unsigned, const TargetRegisterClass *> R;
3070 R = parseRegForInlineAsmConstraint(Constraint, VT);
3075 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3078 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3079 /// vector. If it is invalid, don't add anything to Ops.
3080 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3081 std::string &Constraint,
3082 std::vector<SDValue>&Ops,
3083 SelectionDAG &DAG) const {
3084 SDValue Result(0, 0);
3086 // Only support length 1 constraints for now.
3087 if (Constraint.length() > 1) return;
3089 char ConstraintLetter = Constraint[0];
3090 switch (ConstraintLetter) {
3091 default: break; // This will fall through to the generic implementation
3092 case 'I': // Signed 16 bit constant
3093 // If this fails, the parent routine will give an error
3094 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3095 EVT Type = Op.getValueType();
3096 int64_t Val = C->getSExtValue();
3097 if (isInt<16>(Val)) {
3098 Result = DAG.getTargetConstant(Val, Type);
3103 case 'J': // integer zero
3104 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3105 EVT Type = Op.getValueType();
3106 int64_t Val = C->getZExtValue();
3108 Result = DAG.getTargetConstant(0, Type);
3113 case 'K': // unsigned 16 bit immediate
3114 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3115 EVT Type = Op.getValueType();
3116 uint64_t Val = (uint64_t)C->getZExtValue();
3117 if (isUInt<16>(Val)) {
3118 Result = DAG.getTargetConstant(Val, Type);
3123 case 'L': // signed 32 bit immediate where lower 16 bits are 0
3124 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3125 EVT Type = Op.getValueType();
3126 int64_t Val = C->getSExtValue();
3127 if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3128 Result = DAG.getTargetConstant(Val, Type);
3133 case 'N': // immediate in the range of -65535 to -1 (inclusive)
3134 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3135 EVT Type = Op.getValueType();
3136 int64_t Val = C->getSExtValue();
3137 if ((Val >= -65535) && (Val <= -1)) {
3138 Result = DAG.getTargetConstant(Val, Type);
3143 case 'O': // signed 15 bit immediate
3144 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3145 EVT Type = Op.getValueType();
3146 int64_t Val = C->getSExtValue();
3147 if ((isInt<15>(Val))) {
3148 Result = DAG.getTargetConstant(Val, Type);
3153 case 'P': // immediate in the range of 1 to 65535 (inclusive)
3154 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3155 EVT Type = Op.getValueType();
3156 int64_t Val = C->getSExtValue();
3157 if ((Val <= 65535) && (Val >= 1)) {
3158 Result = DAG.getTargetConstant(Val, Type);
3165 if (Result.getNode()) {
3166 Ops.push_back(Result);
3170 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3173 bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3175 // No global is ever allowed as a base.
3180 case 0: // "r+i" or just "i", depending on HasBaseReg.
3183 if (!AM.HasBaseReg) // allow "r+i".
3185 return false; // disallow "r+r" or "r+r+i".
3194 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3195 // The Mips target isn't yet aware of offsets.
3199 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
3201 bool IsMemset, bool ZeroMemset,
3203 MachineFunction &MF) const {
3204 if (Subtarget->hasMips64())
3210 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3211 if (VT != MVT::f32 && VT != MVT::f64)
3213 if (Imm.isNegZero())
3215 return Imm.isZero();
3218 unsigned MipsTargetLowering::getJumpTableEncoding() const {
3220 return MachineJumpTableInfo::EK_GPRel64BlockAddress;
3222 return TargetLowering::getJumpTableEncoding();
3225 /// This function returns true if CallSym is a long double emulation routine.
3226 static bool isF128SoftLibCall(const char *CallSym) {
3227 const char *const LibCalls[] =
3228 {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3229 "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3230 "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3231 "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3232 "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3233 "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3234 "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3235 "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3238 const char *const *End = LibCalls + array_lengthof(LibCalls);
3240 // Check that LibCalls is sorted alphabetically.
3241 MipsTargetLowering::LTStr Comp;
3244 for (const char *const *I = LibCalls; I < End - 1; ++I)
3245 assert(Comp(*I, *(I + 1)));
3248 return std::binary_search(LibCalls, End, CallSym, Comp);
3251 /// This function returns true if Ty is fp128 or i128 which was originally a
3253 static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3254 if (Ty->isFP128Ty())
3257 const ExternalSymbolSDNode *ES =
3258 dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3260 // If the Ty is i128 and the function being called is a long double emulation
3261 // routine, then the original type is f128.
3262 return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3265 MipsTargetLowering::MipsCC::SpecialCallingConvType
3266 MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3267 MipsCC::SpecialCallingConvType SpecialCallingConv =
3268 MipsCC::NoSpecialCallingConv;;
3269 if (Subtarget->inMips16HardFloat()) {
3270 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3271 llvm::StringRef Sym = G->getGlobal()->getName();
3272 Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3273 if (F && F->hasFnAttribute("__Mips16RetHelper")) {
3274 SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3278 return SpecialCallingConv;
3281 MipsTargetLowering::MipsCC::MipsCC(
3282 CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
3283 MipsCC::SpecialCallingConvType SpecialCallingConv_)
3284 : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
3285 SpecialCallingConv(SpecialCallingConv_){
3286 // Pre-allocate reserved argument area.
3287 CCInfo.AllocateStack(reservedArgArea(), 1);
3291 void MipsTargetLowering::MipsCC::
3292 analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
3293 bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3294 std::vector<ArgListEntry> &FuncArgs) {
3295 assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3296 "CallingConv::Fast shouldn't be used for vararg functions.");
3298 unsigned NumOpnds = Args.size();
3299 llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
3301 for (unsigned I = 0; I != NumOpnds; ++I) {
3302 MVT ArgVT = Args[I].VT;
3303 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3306 if (ArgFlags.isByVal()) {
3307 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3311 if (IsVarArg && !Args[I].IsFixed)
3312 R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
3314 MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3316 R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3321 dbgs() << "Call operand #" << I << " has unhandled type "
3322 << EVT(ArgVT).getEVTString();
3324 llvm_unreachable(0);
3329 void MipsTargetLowering::MipsCC::
3330 analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3331 bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
3332 unsigned NumArgs = Args.size();
3333 llvm::CCAssignFn *FixedFn = fixedArgFn();
3334 unsigned CurArgIdx = 0;
3336 for (unsigned I = 0; I != NumArgs; ++I) {
3337 MVT ArgVT = Args[I].VT;
3338 ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3339 std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3340 CurArgIdx = Args[I].OrigArgIndex;
3342 if (ArgFlags.isByVal()) {
3343 handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3347 MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
3349 if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
3353 dbgs() << "Formal Arg #" << I << " has unhandled type "
3354 << EVT(ArgVT).getEVTString();
3356 llvm_unreachable(0);
3360 template<typename Ty>
3361 void MipsTargetLowering::MipsCC::
3362 analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3363 const SDNode *CallNode, const Type *RetTy) const {
3366 if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3367 Fn = RetCC_F128Soft;
3371 for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3372 MVT VT = RetVals[I].VT;
3373 ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3374 MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3376 if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
3378 dbgs() << "Call result #" << I << " has unhandled type "
3379 << EVT(VT).getEVTString() << '\n';
3381 llvm_unreachable(0);
3386 void MipsTargetLowering::MipsCC::
3387 analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3388 const SDNode *CallNode, const Type *RetTy) const {
3389 analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3392 void MipsTargetLowering::MipsCC::
3393 analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3394 const Type *RetTy) const {
3395 analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
3398 void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3400 CCValAssign::LocInfo LocInfo,
3401 ISD::ArgFlagsTy ArgFlags) {
3402 assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3404 struct ByValArgInfo ByVal;
3405 unsigned RegSize = regSize();
3406 unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3407 unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3410 if (useRegsForByval())
3411 allocateRegs(ByVal, ByValSize, Align);
3413 // Allocate space on caller's stack.
3414 ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3416 CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3418 ByValArgs.push_back(ByVal);
3421 unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3422 return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3425 unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3426 return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3429 const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
3430 return IsO32 ? O32IntRegs : Mips64IntRegs;
3433 llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3434 if (CallConv == CallingConv::Fast)
3435 return CC_Mips_FastCC;
3437 if (SpecialCallingConv == Mips16RetHelperConv)
3438 return CC_Mips16RetHelper;
3439 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
3442 llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
3443 return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
3446 const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
3447 return IsO32 ? O32IntRegs : Mips64DPRegs;
3450 void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3453 unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3454 const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
3455 assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3456 "Byval argument's size and alignment should be a multiple of"
3459 ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3461 // If Align > RegSize, the first arg register must be even.
3462 if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3463 CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3467 // Mark the registers allocated.
3468 for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3469 ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3470 CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3473 MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3474 const SDNode *CallNode,
3475 bool IsSoftFloat) const {
3476 if (IsSoftFloat || IsO32)
3479 // Check if the original type was fp128.
3480 if (originalTypeIsF128(OrigTy, CallNode)) {
3481 assert(VT == MVT::i64);
3488 void MipsTargetLowering::
3489 copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
3490 SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3491 SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3492 const MipsCC &CC, const ByValArgInfo &ByVal) const {
3493 MachineFunction &MF = DAG.getMachineFunction();
3494 MachineFrameInfo *MFI = MF.getFrameInfo();
3495 unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3496 unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3500 FrameObjOffset = (int)CC.reservedArgArea() -
3501 (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3503 FrameObjOffset = ByVal.Address;
3505 // Create frame object.
3506 EVT PtrTy = getPointerTy();
3507 int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3508 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3509 InVals.push_back(FIN);
3514 // Copy arg registers.
3515 MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
3516 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3518 for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3519 unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
3520 unsigned VReg = addLiveIn(MF, ArgReg, RC);
3521 unsigned Offset = I * CC.regSize();
3522 SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3523 DAG.getConstant(Offset, PtrTy));
3524 SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3525 StorePtr, MachinePointerInfo(FuncArg, Offset),
3527 OutChains.push_back(Store);
3531 // Copy byVal arg to registers and stack.
3532 void MipsTargetLowering::
3533 passByValArg(SDValue Chain, SDLoc DL,
3534 std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
3535 SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
3536 MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3537 const MipsCC &CC, const ByValArgInfo &ByVal,
3538 const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3539 unsigned ByValSize = Flags.getByValSize();
3540 unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
3541 unsigned RegSize = CC.regSize();
3542 unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
3543 EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
3545 if (ByVal.NumRegs) {
3546 const uint16_t *ArgRegs = CC.intArgRegs();
3547 bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
3550 // Copy words to registers.
3551 for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
3552 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3553 DAG.getConstant(Offset, PtrTy));
3554 SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3555 MachinePointerInfo(), false, false, false,
3557 MemOpChains.push_back(LoadVal.getValue(1));
3558 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3559 RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3562 // Return if the struct has been fully copied.
3563 if (ByValSize == Offset)
3566 // Copy the remainder of the byval argument with sub-word loads and shifts.
3567 if (LeftoverBytes) {
3568 assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
3569 "Size of the remainder should be smaller than RegSize.");
3572 for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
3573 Offset < ByValSize; LoadSize /= 2) {
3574 unsigned RemSize = ByValSize - Offset;
3576 if (RemSize < LoadSize)
3580 SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3581 DAG.getConstant(Offset, PtrTy));
3583 DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
3584 MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
3585 false, false, Alignment);
3586 MemOpChains.push_back(LoadVal.getValue(1));
3588 // Shift the loaded value.
3592 Shamt = TotalSizeLoaded;
3594 Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
3596 SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3597 DAG.getConstant(Shamt, MVT::i32));
3600 Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3605 TotalSizeLoaded += LoadSize;
3606 Alignment = std::min(Alignment, LoadSize);
3609 unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3610 RegsToPass.push_back(std::make_pair(ArgReg, Val));
3615 // Copy remainder of byval arg to it with memcpy.
3616 unsigned MemCpySize = ByValSize - Offset;
3617 SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3618 DAG.getConstant(Offset, PtrTy));
3619 SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3620 DAG.getIntPtrConstant(ByVal.Address));
3621 Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3622 Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
3623 MachinePointerInfo(0), MachinePointerInfo(0));
3624 MemOpChains.push_back(Chain);
3627 void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3628 const MipsCC &CC, SDValue Chain,
3629 SDLoc DL, SelectionDAG &DAG) const {
3630 unsigned NumRegs = CC.numIntArgRegs();
3631 const uint16_t *ArgRegs = CC.intArgRegs();
3632 const CCState &CCInfo = CC.getCCInfo();
3633 unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3634 unsigned RegSize = CC.regSize();
3635 MVT RegTy = MVT::getIntegerVT(RegSize * 8);
3636 const TargetRegisterClass *RC = getRegClassFor(RegTy);
3637 MachineFunction &MF = DAG.getMachineFunction();
3638 MachineFrameInfo *MFI = MF.getFrameInfo();
3639 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3641 // Offset of the first variable argument from stack pointer.
3645 VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3647 VaArgOffset = (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3649 // Record the frame index of the first variable argument
3650 // which is a value necessary to VASTART.
3651 int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3652 MipsFI->setVarArgsFrameIndex(FI);
3654 // Copy the integer registers that have not been used for argument passing
3655 // to the argument register save area. For O32, the save area is allocated
3656 // in the caller's stack frame, while for N32/64, it is allocated in the
3657 // callee's stack frame.
3658 for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
3659 unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
3660 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3661 FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3662 SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3663 SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3664 MachinePointerInfo(), false, false, 0);
3665 cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
3666 OutChains.push_back(Store);