1 //===-- MipsastISel.cpp - Mips FastISel implementation
2 //---------------------===//
4 #include "MipsCCState.h"
5 #include "MipsInstrInfo.h"
6 #include "MipsISelLowering.h"
7 #include "MipsMachineFunction.h"
8 #include "MipsRegisterInfo.h"
9 #include "MipsSubtarget.h"
10 #include "MipsTargetMachine.h"
11 #include "llvm/Analysis/TargetLibraryInfo.h"
12 #include "llvm/CodeGen/FastISel.h"
13 #include "llvm/CodeGen/FunctionLoweringInfo.h"
14 #include "llvm/CodeGen/MachineInstrBuilder.h"
15 #include "llvm/CodeGen/MachineRegisterInfo.h"
16 #include "llvm/IR/GetElementPtrTypeIterator.h"
17 #include "llvm/IR/GlobalAlias.h"
18 #include "llvm/IR/GlobalVariable.h"
19 #include "llvm/Target/TargetInstrInfo.h"
25 class MipsFastISel final : public FastISel {
27 // All possible address modes.
30 typedef enum { RegBase, FrameIndexBase } BaseKind;
41 const GlobalValue *GV;
44 // Innocuous defaults for our address.
45 Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; }
46 void setKind(BaseKind K) { Kind = K; }
47 BaseKind getKind() const { return Kind; }
48 bool isRegBase() const { return Kind == RegBase; }
49 bool isFIBase() const { return Kind == FrameIndexBase; }
50 void setReg(unsigned Reg) {
51 assert(isRegBase() && "Invalid base register access!");
54 unsigned getReg() const {
55 assert(isRegBase() && "Invalid base register access!");
58 void setFI(unsigned FI) {
59 assert(isFIBase() && "Invalid base frame index access!");
62 unsigned getFI() const {
63 assert(isFIBase() && "Invalid base frame index access!");
67 void setOffset(int64_t Offset_) { Offset = Offset_; }
68 int64_t getOffset() const { return Offset; }
69 void setGlobalValue(const GlobalValue *G) { GV = G; }
70 const GlobalValue *getGlobalValue() { return GV; }
73 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
74 /// make the right decision when generating code for different targets.
75 const TargetMachine &TM;
76 const MipsSubtarget *Subtarget;
77 const TargetInstrInfo &TII;
78 const TargetLowering &TLI;
79 MipsFunctionInfo *MFI;
81 // Convenience variables to avoid some queries.
84 bool fastLowerCall(CallLoweringInfo &CLI) override;
87 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
88 // floating point but not reject doing fast-isel in other
92 // Selection routines.
93 bool selectLogicalOp(const Instruction *I);
94 bool selectLoad(const Instruction *I);
95 bool selectStore(const Instruction *I);
96 bool selectBranch(const Instruction *I);
97 bool selectSelect(const Instruction *I);
98 bool selectCmp(const Instruction *I);
99 bool selectFPExt(const Instruction *I);
100 bool selectFPTrunc(const Instruction *I);
101 bool selectFPToInt(const Instruction *I, bool IsSigned);
102 bool selectRet(const Instruction *I);
103 bool selectTrunc(const Instruction *I);
104 bool selectIntExt(const Instruction *I);
105 bool selectShift(const Instruction *I);
106 bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
108 // Utility helper routines.
109 bool isTypeLegal(Type *Ty, MVT &VT);
110 bool isTypeSupported(Type *Ty, MVT &VT);
111 bool isLoadTypeLegal(Type *Ty, MVT &VT);
112 bool computeAddress(const Value *Obj, Address &Addr);
113 bool computeCallAddress(const Value *V, Address &Addr);
114 void simplifyAddress(Address &Addr);
116 // Emit helper routines.
117 bool emitCmp(unsigned DestReg, const CmpInst *CI);
118 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
119 unsigned Alignment = 0);
120 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
121 MachineMemOperand *MMO = nullptr);
122 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
123 unsigned Alignment = 0);
124 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
125 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
128 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
130 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
131 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
133 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
136 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
138 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
141 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
142 unsigned materializeGV(const GlobalValue *GV, MVT VT);
143 unsigned materializeInt(const Constant *C, MVT VT);
144 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
146 MachineInstrBuilder emitInst(unsigned Opc) {
147 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
149 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
150 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
153 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
154 unsigned MemReg, int64_t MemOffset) {
155 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
157 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
158 unsigned MemReg, int64_t MemOffset) {
159 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
162 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
163 const TargetRegisterClass *RC,
164 unsigned Op0, bool Op0IsKill,
165 unsigned Op1, bool Op1IsKill);
167 // for some reason, this default is not generated by tablegen
168 // so we explicitly generate it here.
170 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
171 unsigned Op0, bool Op0IsKill, uint64_t imm1,
172 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
176 // Call handling routines.
178 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
179 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
181 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
184 // Backend specific FastISel code.
185 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
186 const TargetLibraryInfo *libInfo)
187 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
188 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
189 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
190 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
191 Context = &funcInfo.Fn->getContext();
193 ((TM.getRelocationModel() == Reloc::PIC_) &&
194 ((Subtarget->hasMips32r2() || Subtarget->hasMips32()) &&
195 (static_cast<const MipsTargetMachine &>(TM).getABI().IsO32())));
196 UnsupportedFPMode = Subtarget->isFP64bit();
199 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
200 unsigned fastMaterializeConstant(const Constant *C) override;
201 bool fastSelectInstruction(const Instruction *I) override;
203 #include "MipsGenFastISel.inc"
205 } // end anonymous namespace.
207 static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
208 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
209 CCState &State) LLVM_ATTRIBUTE_UNUSED;
211 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
212 CCValAssign::LocInfo LocInfo,
213 ISD::ArgFlagsTy ArgFlags, CCState &State) {
214 llvm_unreachable("should not be called");
217 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
218 CCValAssign::LocInfo LocInfo,
219 ISD::ArgFlagsTy ArgFlags, CCState &State) {
220 llvm_unreachable("should not be called");
223 #include "MipsGenCallingConv.inc"
225 CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
229 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
230 const Value *LHS, const Value *RHS) {
231 // Canonicalize immediates to the RHS first.
232 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
236 if (ISDOpc == ISD::AND) {
238 } else if (ISDOpc == ISD::OR) {
240 } else if (ISDOpc == ISD::XOR) {
243 llvm_unreachable("unexpected opcode");
245 unsigned LHSReg = getRegForValue(LHS);
246 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
254 if (const auto *C = dyn_cast<ConstantInt>(RHS))
255 RHSReg = materializeInt(C, MVT::i32);
257 RHSReg = getRegForValue(RHS);
262 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
266 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
267 assert(TLI.getValueType(AI->getType(), true) == MVT::i32 &&
268 "Alloca should always return a pointer.");
270 DenseMap<const AllocaInst *, int>::iterator SI =
271 FuncInfo.StaticAllocaMap.find(AI);
273 if (SI != FuncInfo.StaticAllocaMap.end()) {
274 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
275 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
277 .addFrameIndex(SI->second)
285 unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
286 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
288 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
289 const ConstantInt *CI = cast<ConstantInt>(C);
291 if ((VT != MVT::i1) && CI->isNegative())
292 Imm = CI->getSExtValue();
294 Imm = CI->getZExtValue();
295 return materialize32BitInt(Imm, RC);
298 unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
299 const TargetRegisterClass *RC) {
300 unsigned ResultReg = createResultReg(RC);
302 if (isInt<16>(Imm)) {
303 unsigned Opc = Mips::ADDiu;
304 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
306 } else if (isUInt<16>(Imm)) {
307 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
310 unsigned Lo = Imm & 0xFFFF;
311 unsigned Hi = (Imm >> 16) & 0xFFFF;
313 // Both Lo and Hi have nonzero bits.
314 unsigned TmpReg = createResultReg(RC);
315 emitInst(Mips::LUi, TmpReg).addImm(Hi);
316 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
318 emitInst(Mips::LUi, ResultReg).addImm(Hi);
323 unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
324 if (UnsupportedFPMode)
326 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
327 if (VT == MVT::f32) {
328 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
329 unsigned DestReg = createResultReg(RC);
330 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
331 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
333 } else if (VT == MVT::f64) {
334 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
335 unsigned DestReg = createResultReg(RC);
336 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
338 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
339 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
345 unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
346 // For now 32-bit only.
349 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
350 unsigned DestReg = createResultReg(RC);
351 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
352 bool IsThreadLocal = GVar && GVar->isThreadLocal();
353 // TLS not supported at this time.
356 emitInst(Mips::LW, DestReg)
357 .addReg(MFI->getGlobalBaseReg())
358 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
359 if ((GV->hasInternalLinkage() ||
360 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
361 unsigned TempReg = createResultReg(RC);
362 emitInst(Mips::ADDiu, TempReg)
364 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
370 // Materialize a constant into a register, and return the register
371 // number (or zero if we failed to handle it).
372 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
373 EVT CEVT = TLI.getValueType(C->getType(), true);
375 // Only handle simple types.
376 if (!CEVT.isSimple())
378 MVT VT = CEVT.getSimpleVT();
380 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
381 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
382 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
383 return materializeGV(GV, VT);
384 else if (isa<ConstantInt>(C))
385 return materializeInt(C, VT);
390 bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
392 const User *U = nullptr;
393 unsigned Opcode = Instruction::UserOp1;
394 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
395 // Don't walk into other basic blocks unless the object is an alloca from
396 // another block, otherwise it may not have a virtual register assigned.
397 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
398 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
399 Opcode = I->getOpcode();
402 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
403 Opcode = C->getOpcode();
409 case Instruction::BitCast: {
410 // Look through bitcasts.
411 return computeAddress(U->getOperand(0), Addr);
413 case Instruction::GetElementPtr: {
414 Address SavedAddr = Addr;
415 uint64_t TmpOffset = Addr.getOffset();
416 // Iterate through the GEP folding the constants into offsets where
418 gep_type_iterator GTI = gep_type_begin(U);
419 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
421 const Value *Op = *i;
422 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
423 const StructLayout *SL = DL.getStructLayout(STy);
424 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
425 TmpOffset += SL->getElementOffset(Idx);
427 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
429 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
430 // Constant-offset addressing.
431 TmpOffset += CI->getSExtValue() * S;
434 if (canFoldAddIntoGEP(U, Op)) {
435 // A compatible add with a constant operand. Fold the constant.
437 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
438 TmpOffset += CI->getSExtValue() * S;
439 // Iterate on the other operand.
440 Op = cast<AddOperator>(Op)->getOperand(0);
444 goto unsupported_gep;
448 // Try to grab the base operand now.
449 Addr.setOffset(TmpOffset);
450 if (computeAddress(U->getOperand(0), Addr))
452 // We failed, restore everything and try the other options.
457 case Instruction::Alloca: {
458 const AllocaInst *AI = cast<AllocaInst>(Obj);
459 DenseMap<const AllocaInst *, int>::iterator SI =
460 FuncInfo.StaticAllocaMap.find(AI);
461 if (SI != FuncInfo.StaticAllocaMap.end()) {
462 Addr.setKind(Address::FrameIndexBase);
463 Addr.setFI(SI->second);
469 Addr.setReg(getRegForValue(Obj));
470 return Addr.getReg() != 0;
473 bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
474 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
475 if (GV && isa<Function>(GV) && cast<Function>(GV)->isIntrinsic())
479 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
480 Addr.setGlobalValue(GV);
486 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
487 EVT evt = TLI.getValueType(Ty, true);
488 // Only handle simple types.
489 if (evt == MVT::Other || !evt.isSimple())
491 VT = evt.getSimpleVT();
493 // Handle all legal types, i.e. a register that will directly hold this
495 return TLI.isTypeLegal(VT);
498 bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
499 if (Ty->isVectorTy())
502 if (isTypeLegal(Ty, VT))
505 // If this is a type than can be sign or zero-extended to a basic operation
506 // go ahead and accept it now.
507 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
513 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
514 if (isTypeLegal(Ty, VT))
516 // We will extend this in a later patch:
517 // If this is a type than can be sign or zero-extended to a basic operation
518 // go ahead and accept it now.
519 if (VT == MVT::i8 || VT == MVT::i16)
523 // Because of how EmitCmp is called with fast-isel, you can
524 // end up with redundant "andi" instructions after the sequences emitted below.
525 // We should try and solve this issue in the future.
527 bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
528 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
529 bool IsUnsigned = CI->isUnsigned();
530 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
533 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
536 CmpInst::Predicate P = CI->getPredicate();
541 case CmpInst::ICMP_EQ: {
542 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
543 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
544 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
547 case CmpInst::ICMP_NE: {
548 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
549 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
550 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
553 case CmpInst::ICMP_UGT: {
554 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
557 case CmpInst::ICMP_ULT: {
558 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
561 case CmpInst::ICMP_UGE: {
562 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
563 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
564 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
567 case CmpInst::ICMP_ULE: {
568 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
569 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
570 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
573 case CmpInst::ICMP_SGT: {
574 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
577 case CmpInst::ICMP_SLT: {
578 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
581 case CmpInst::ICMP_SGE: {
582 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
583 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
584 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
587 case CmpInst::ICMP_SLE: {
588 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
589 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
590 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
593 case CmpInst::FCMP_OEQ:
594 case CmpInst::FCMP_UNE:
595 case CmpInst::FCMP_OLT:
596 case CmpInst::FCMP_OLE:
597 case CmpInst::FCMP_OGT:
598 case CmpInst::FCMP_OGE: {
599 if (UnsupportedFPMode)
601 bool IsFloat = Left->getType()->isFloatTy();
602 bool IsDouble = Left->getType()->isDoubleTy();
603 if (!IsFloat && !IsDouble)
605 unsigned Opc, CondMovOpc;
607 case CmpInst::FCMP_OEQ:
608 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
609 CondMovOpc = Mips::MOVT_I;
611 case CmpInst::FCMP_UNE:
612 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
613 CondMovOpc = Mips::MOVF_I;
615 case CmpInst::FCMP_OLT:
616 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
617 CondMovOpc = Mips::MOVT_I;
619 case CmpInst::FCMP_OLE:
620 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
621 CondMovOpc = Mips::MOVT_I;
623 case CmpInst::FCMP_OGT:
624 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
625 CondMovOpc = Mips::MOVF_I;
627 case CmpInst::FCMP_OGE:
628 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
629 CondMovOpc = Mips::MOVF_I;
632 llvm_unreachable("Only switching of a subset of CCs.");
634 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
635 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
636 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
637 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
638 emitInst(Opc).addReg(LeftReg).addReg(RightReg).addReg(
639 Mips::FCC0, RegState::ImplicitDefine);
640 MachineInstrBuilder MI = emitInst(CondMovOpc, ResultReg)
643 .addReg(RegWithZero, RegState::Implicit);
644 MI->tieOperands(0, 3);
650 bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
651 unsigned Alignment) {
653 // more cases will be handled here in following patches.
656 switch (VT.SimpleTy) {
658 ResultReg = createResultReg(&Mips::GPR32RegClass);
663 ResultReg = createResultReg(&Mips::GPR32RegClass);
668 ResultReg = createResultReg(&Mips::GPR32RegClass);
673 if (UnsupportedFPMode)
675 ResultReg = createResultReg(&Mips::FGR32RegClass);
680 if (UnsupportedFPMode)
682 ResultReg = createResultReg(&Mips::AFGR64RegClass);
689 if (Addr.isRegBase()) {
690 simplifyAddress(Addr);
691 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
694 if (Addr.isFIBase()) {
695 unsigned FI = Addr.getFI();
697 unsigned Offset = Addr.getOffset();
698 MachineFrameInfo &MFI = *MF->getFrameInfo();
699 MachineMemOperand *MMO = MF->getMachineMemOperand(
700 MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
701 MFI.getObjectSize(FI), Align);
702 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
711 bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
712 unsigned Alignment) {
714 // more cases will be handled here in following patches.
717 switch (VT.SimpleTy) {
728 if (UnsupportedFPMode)
733 if (UnsupportedFPMode)
740 if (Addr.isRegBase()) {
741 simplifyAddress(Addr);
742 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
745 if (Addr.isFIBase()) {
746 unsigned FI = Addr.getFI();
748 unsigned Offset = Addr.getOffset();
749 MachineFrameInfo &MFI = *MF->getFrameInfo();
750 MachineMemOperand *MMO = MF->getMachineMemOperand(
751 MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
752 MFI.getObjectSize(FI), Align);
753 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
763 bool MipsFastISel::selectLogicalOp(const Instruction *I) {
765 if (!isTypeSupported(I->getType(), VT))
769 switch (I->getOpcode()) {
771 llvm_unreachable("Unexpected instruction.");
772 case Instruction::And:
773 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
775 case Instruction::Or:
776 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
778 case Instruction::Xor:
779 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
786 updateValueMap(I, ResultReg);
790 bool MipsFastISel::selectLoad(const Instruction *I) {
791 // Atomic loads need special handling.
792 if (cast<LoadInst>(I)->isAtomic())
795 // Verify we have a legal type before going any further.
797 if (!isLoadTypeLegal(I->getType(), VT))
800 // See if we can handle this address.
802 if (!computeAddress(I->getOperand(0), Addr))
806 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
808 updateValueMap(I, ResultReg);
812 bool MipsFastISel::selectStore(const Instruction *I) {
813 Value *Op0 = I->getOperand(0);
816 // Atomic stores need special handling.
817 if (cast<StoreInst>(I)->isAtomic())
820 // Verify we have a legal type before going any further.
822 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
825 // Get the value to be stored into a register.
826 SrcReg = getRegForValue(Op0);
830 // See if we can handle this address.
832 if (!computeAddress(I->getOperand(1), Addr))
835 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
841 // This can cause a redundant sltiu to be generated.
842 // FIXME: try and eliminate this in a future patch.
844 bool MipsFastISel::selectBranch(const Instruction *I) {
845 const BranchInst *BI = cast<BranchInst>(I);
846 MachineBasicBlock *BrBB = FuncInfo.MBB;
848 // TBB is the basic block for the case where the comparison is true.
849 // FBB is the basic block for the case where the comparison is false.
850 // if (cond) goto TBB
854 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
855 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
857 // For now, just try the simplest case where it's fed by a compare.
858 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
859 unsigned CondReg = createResultReg(&Mips::GPR32RegClass);
860 if (!emitCmp(CondReg, CI))
862 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
865 fastEmitBranch(FBB, DbgLoc);
866 FuncInfo.MBB->addSuccessor(TBB);
872 bool MipsFastISel::selectCmp(const Instruction *I) {
873 const CmpInst *CI = cast<CmpInst>(I);
874 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
875 if (!emitCmp(ResultReg, CI))
877 updateValueMap(I, ResultReg);
881 // Attempt to fast-select a floating-point extend instruction.
882 bool MipsFastISel::selectFPExt(const Instruction *I) {
883 if (UnsupportedFPMode)
885 Value *Src = I->getOperand(0);
886 EVT SrcVT = TLI.getValueType(Src->getType(), true);
887 EVT DestVT = TLI.getValueType(I->getType(), true);
889 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
893 getRegForValue(Src); // his must be a 32 bit floating point register class
894 // maybe we should handle this differently
898 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
899 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
900 updateValueMap(I, DestReg);
904 bool MipsFastISel::selectSelect(const Instruction *I) {
905 assert(isa<SelectInst>(I) && "Expected a select instruction.");
908 if (!isTypeSupported(I->getType(), VT))
912 const TargetRegisterClass *RC;
914 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
915 CondMovOpc = Mips::MOVN_I_I;
916 RC = &Mips::GPR32RegClass;
917 } else if (VT == MVT::f32) {
918 CondMovOpc = Mips::MOVN_I_S;
919 RC = &Mips::FGR32RegClass;
920 } else if (VT == MVT::f64) {
921 CondMovOpc = Mips::MOVN_I_D32;
922 RC = &Mips::AFGR64RegClass;
926 const SelectInst *SI = cast<SelectInst>(I);
927 const Value *Cond = SI->getCondition();
928 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
929 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
930 unsigned CondReg = getRegForValue(Cond);
932 if (!Src1Reg || !Src2Reg || !CondReg)
935 unsigned ResultReg = createResultReg(RC);
936 unsigned TempReg = createResultReg(RC);
938 if (!ResultReg || !TempReg)
941 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
942 emitInst(CondMovOpc, ResultReg)
943 .addReg(Src1Reg).addReg(CondReg).addReg(TempReg);
944 updateValueMap(I, ResultReg);
948 // Attempt to fast-select a floating-point truncate instruction.
949 bool MipsFastISel::selectFPTrunc(const Instruction *I) {
950 if (UnsupportedFPMode)
952 Value *Src = I->getOperand(0);
953 EVT SrcVT = TLI.getValueType(Src->getType(), true);
954 EVT DestVT = TLI.getValueType(I->getType(), true);
956 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
959 unsigned SrcReg = getRegForValue(Src);
963 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
967 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
968 updateValueMap(I, DestReg);
972 // Attempt to fast-select a floating-point-to-integer conversion.
973 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
974 if (UnsupportedFPMode)
978 return false; // We don't handle this case yet. There is no native
979 // instruction for this but it can be synthesized.
980 Type *DstTy = I->getType();
981 if (!isTypeLegal(DstTy, DstVT))
984 if (DstVT != MVT::i32)
987 Value *Src = I->getOperand(0);
988 Type *SrcTy = Src->getType();
989 if (!isTypeLegal(SrcTy, SrcVT))
992 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
995 unsigned SrcReg = getRegForValue(Src);
999 // Determine the opcode for the conversion, which takes place
1000 // entirely within FPRs.
1001 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1002 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
1005 if (SrcVT == MVT::f32)
1006 Opc = Mips::TRUNC_W_S;
1008 Opc = Mips::TRUNC_W_D32;
1010 // Generate the convert.
1011 emitInst(Opc, TempReg).addReg(SrcReg);
1013 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1015 updateValueMap(I, DestReg);
1019 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1020 SmallVectorImpl<MVT> &OutVTs,
1021 unsigned &NumBytes) {
1022 CallingConv::ID CC = CLI.CallConv;
1023 SmallVector<CCValAssign, 16> ArgLocs;
1024 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1025 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1026 // Get a count of how many bytes are to be pushed on the stack.
1027 NumBytes = CCInfo.getNextStackOffset();
1028 // This is the minimum argument area used for A0-A3.
1032 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16);
1033 // Process the args.
1035 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1036 CCValAssign &VA = ArgLocs[i];
1037 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1038 MVT ArgVT = OutVTs[VA.getValNo()];
1042 if (ArgVT == MVT::f32) {
1043 VA.convertToReg(Mips::F12);
1044 } else if (ArgVT == MVT::f64) {
1045 VA.convertToReg(Mips::D6);
1047 } else if (i == 1) {
1048 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1049 if (ArgVT == MVT::f32) {
1050 VA.convertToReg(Mips::F14);
1051 } else if (ArgVT == MVT::f64) {
1052 VA.convertToReg(Mips::D7);
1056 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1057 (ArgVT == MVT::i8)) &&
1059 switch (VA.getLocMemOffset()) {
1061 VA.convertToReg(Mips::A0);
1064 VA.convertToReg(Mips::A1);
1067 VA.convertToReg(Mips::A2);
1070 VA.convertToReg(Mips::A3);
1076 unsigned ArgReg = getRegForValue(ArgVal);
1080 // Handle arg promotion: SExt, ZExt, AExt.
1081 switch (VA.getLocInfo()) {
1082 case CCValAssign::Full:
1084 case CCValAssign::AExt:
1085 case CCValAssign::SExt: {
1086 MVT DestVT = VA.getLocVT();
1088 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1093 case CCValAssign::ZExt: {
1094 MVT DestVT = VA.getLocVT();
1096 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1102 llvm_unreachable("Unknown arg promotion!");
1105 // Now copy/store arg to correct locations.
1106 if (VA.isRegLoc() && !VA.needsCustom()) {
1107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1108 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1109 CLI.OutRegs.push_back(VA.getLocReg());
1110 } else if (VA.needsCustom()) {
1111 llvm_unreachable("Mips does not use custom args.");
1115 // FIXME: This path will currently return false. It was copied
1116 // from the AArch64 port and should be essentially fine for Mips too.
1117 // The work to finish up this path will be done in a follow-on patch.
1119 assert(VA.isMemLoc() && "Assuming store on stack.");
1120 // Don't emit stores for undef values.
1121 if (isa<UndefValue>(ArgVal))
1124 // Need to store on the stack.
1125 // FIXME: This alignment is incorrect but this path is disabled
1126 // for now (will return false). We need to determine the right alignment
1127 // based on the normal alignment for the underlying machine type.
1129 unsigned ArgSize = RoundUpToAlignment(ArgVT.getSizeInBits(), 4);
1131 unsigned BEAlign = 0;
1132 if (ArgSize < 8 && !Subtarget->isLittle())
1133 BEAlign = 8 - ArgSize;
1136 Addr.setKind(Address::RegBase);
1137 Addr.setReg(Mips::SP);
1138 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1140 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1141 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1142 MachinePointerInfo::getStack(Addr.getOffset()),
1143 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1145 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1146 return false; // can't store on the stack yet.
1153 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1154 unsigned NumBytes) {
1155 CallingConv::ID CC = CLI.CallConv;
1156 emitInst(Mips::ADJCALLSTACKUP).addImm(16);
1157 if (RetVT != MVT::isVoid) {
1158 SmallVector<CCValAssign, 16> RVLocs;
1159 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1160 CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips);
1162 // Only handle a single return value.
1163 if (RVLocs.size() != 1)
1165 // Copy all of the result registers out of their specified physreg.
1166 MVT CopyVT = RVLocs[0].getValVT();
1167 // Special handling for extended integers.
1168 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1171 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1174 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1175 TII.get(TargetOpcode::COPY),
1176 ResultReg).addReg(RVLocs[0].getLocReg());
1177 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1179 CLI.ResultReg = ResultReg;
1180 CLI.NumResultRegs = 1;
1185 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1186 CallingConv::ID CC = CLI.CallConv;
1187 bool IsTailCall = CLI.IsTailCall;
1188 bool IsVarArg = CLI.IsVarArg;
1189 const Value *Callee = CLI.Callee;
1190 // const char *SymName = CLI.SymName;
1192 // Allow SelectionDAG isel to handle tail calls.
1196 // Let SDISel handle vararg functions.
1200 // FIXME: Only handle *simple* calls for now.
1202 if (CLI.RetTy->isVoidTy())
1203 RetVT = MVT::isVoid;
1204 else if (!isTypeSupported(CLI.RetTy, RetVT))
1207 for (auto Flag : CLI.OutFlags)
1208 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1211 // Set up the argument vectors.
1212 SmallVector<MVT, 16> OutVTs;
1213 OutVTs.reserve(CLI.OutVals.size());
1215 for (auto *Val : CLI.OutVals) {
1217 if (!isTypeLegal(Val->getType(), VT) &&
1218 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1221 // We don't handle vector parameters yet.
1222 if (VT.isVector() || VT.getSizeInBits() > 64)
1225 OutVTs.push_back(VT);
1229 if (!computeCallAddress(Callee, Addr))
1232 // Handle the arguments now that we've gotten them.
1234 if (!processCallArgs(CLI, OutVTs, NumBytes))
1238 unsigned DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
1239 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1240 MachineInstrBuilder MIB =
1241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1242 Mips::RA).addReg(Mips::T9);
1244 // Add implicit physical register uses to the call.
1245 for (auto Reg : CLI.OutRegs)
1246 MIB.addReg(Reg, RegState::Implicit);
1248 // Add a register mask with the call-preserved registers.
1249 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1250 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
1254 // Finish off the call including any return values.
1255 return finishCall(CLI, RetVT, NumBytes);
1258 bool MipsFastISel::selectRet(const Instruction *I) {
1259 const Function &F = *I->getParent()->getParent();
1260 const ReturnInst *Ret = cast<ReturnInst>(I);
1262 if (!FuncInfo.CanLowerReturn)
1265 // Build a list of return value registers.
1266 SmallVector<unsigned, 4> RetRegs;
1268 if (Ret->getNumOperands() > 0) {
1269 CallingConv::ID CC = F.getCallingConv();
1270 SmallVector<ISD::OutputArg, 4> Outs;
1271 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1272 // Analyze operands of the call, assigning locations to each operand.
1273 SmallVector<CCValAssign, 16> ValLocs;
1274 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1276 CCAssignFn *RetCC = RetCC_Mips;
1277 CCInfo.AnalyzeReturn(Outs, RetCC);
1279 // Only handle a single return value for now.
1280 if (ValLocs.size() != 1)
1283 CCValAssign &VA = ValLocs[0];
1284 const Value *RV = Ret->getOperand(0);
1286 // Don't bother handling odd stuff for now.
1287 if ((VA.getLocInfo() != CCValAssign::Full) &&
1288 (VA.getLocInfo() != CCValAssign::BCvt))
1291 // Only handle register returns for now.
1295 unsigned Reg = getRegForValue(RV);
1299 unsigned SrcReg = Reg + VA.getValNo();
1300 unsigned DestReg = VA.getLocReg();
1301 // Avoid a cross-class copy. This is very unlikely.
1302 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1305 EVT RVEVT = TLI.getValueType(RV->getType());
1306 if (!RVEVT.isSimple())
1309 if (RVEVT.isVector())
1312 MVT RVVT = RVEVT.getSimpleVT();
1313 if (RVVT == MVT::f128)
1316 MVT DestVT = VA.getValVT();
1317 // Special handling for extended integers.
1318 if (RVVT != DestVT) {
1319 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1322 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1323 bool IsZExt = Outs[0].Flags.isZExt();
1324 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1331 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1332 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1334 // Add register to return instruction.
1335 RetRegs.push_back(VA.getLocReg());
1337 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1338 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1339 MIB.addReg(RetRegs[i], RegState::Implicit);
1343 bool MipsFastISel::selectTrunc(const Instruction *I) {
1344 // The high bits for a type smaller than the register size are assumed to be
1346 Value *Op = I->getOperand(0);
1349 SrcVT = TLI.getValueType(Op->getType(), true);
1350 DestVT = TLI.getValueType(I->getType(), true);
1352 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1354 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1357 unsigned SrcReg = getRegForValue(Op);
1361 // Because the high bits are undefined, a truncate doesn't generate
1363 updateValueMap(I, SrcReg);
1366 bool MipsFastISel::selectIntExt(const Instruction *I) {
1367 Type *DestTy = I->getType();
1368 Value *Src = I->getOperand(0);
1369 Type *SrcTy = Src->getType();
1371 bool isZExt = isa<ZExtInst>(I);
1372 unsigned SrcReg = getRegForValue(Src);
1376 EVT SrcEVT, DestEVT;
1377 SrcEVT = TLI.getValueType(SrcTy, true);
1378 DestEVT = TLI.getValueType(DestTy, true);
1379 if (!SrcEVT.isSimple())
1381 if (!DestEVT.isSimple())
1384 MVT SrcVT = SrcEVT.getSimpleVT();
1385 MVT DestVT = DestEVT.getSimpleVT();
1386 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1388 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1390 updateValueMap(I, ResultReg);
1393 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1396 switch (SrcVT.SimpleTy) {
1406 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1407 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1408 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1412 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1414 switch (SrcVT.SimpleTy) {
1418 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1421 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1427 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1429 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1431 if (Subtarget->hasMips32r2())
1432 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1433 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1436 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1438 switch (SrcVT.SimpleTy) {
1442 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(1);
1445 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xff);
1448 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xffff);
1454 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1455 unsigned DestReg, bool IsZExt) {
1456 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1457 // DestVT are odd things, so test to make sure that they are both types we can
1458 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1459 // bail out to SelectionDAG.
1460 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1461 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1464 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1465 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1468 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1470 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1471 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1472 return Success ? DestReg : 0;
1475 bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
1476 EVT DestEVT = TLI.getValueType(I->getType(), true);
1477 if (!DestEVT.isSimple())
1480 MVT DestVT = DestEVT.getSimpleVT();
1481 if (DestVT != MVT::i32)
1485 switch (ISDOpcode) {
1490 DivOpc = Mips::SDIV;
1494 DivOpc = Mips::UDIV;
1498 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1499 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1500 if (!Src0Reg || !Src1Reg)
1503 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1504 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1506 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1510 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1513 emitInst(MFOpc, ResultReg);
1515 updateValueMap(I, ResultReg);
1519 bool MipsFastISel::selectShift(const Instruction *I) {
1522 if (!isTypeSupported(I->getType(), RetVT))
1525 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1529 unsigned Opcode = I->getOpcode();
1530 const Value *Op0 = I->getOperand(0);
1531 unsigned Op0Reg = getRegForValue(Op0);
1535 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1536 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1537 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1541 MVT Op0MVT = TLI.getValueType(Op0->getType(), true).getSimpleVT();
1542 bool IsZExt = Opcode == Instruction::LShr;
1543 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1549 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1550 uint64_t ShiftVal = C->getZExtValue();
1554 llvm_unreachable("Unexpected instruction.");
1555 case Instruction::Shl:
1558 case Instruction::AShr:
1561 case Instruction::LShr:
1566 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1567 updateValueMap(I, ResultReg);
1571 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1577 llvm_unreachable("Unexpected instruction.");
1578 case Instruction::Shl:
1579 Opcode = Mips::SLLV;
1581 case Instruction::AShr:
1582 Opcode = Mips::SRAV;
1584 case Instruction::LShr:
1585 Opcode = Mips::SRLV;
1589 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1590 updateValueMap(I, ResultReg);
1594 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
1595 if (!TargetSupported)
1597 switch (I->getOpcode()) {
1600 case Instruction::Load:
1601 return selectLoad(I);
1602 case Instruction::Store:
1603 return selectStore(I);
1604 case Instruction::SDiv:
1605 if (!selectBinaryOp(I, ISD::SDIV))
1606 return selectDivRem(I, ISD::SDIV);
1608 case Instruction::UDiv:
1609 if (!selectBinaryOp(I, ISD::UDIV))
1610 return selectDivRem(I, ISD::UDIV);
1612 case Instruction::SRem:
1613 if (!selectBinaryOp(I, ISD::SREM))
1614 return selectDivRem(I, ISD::SREM);
1616 case Instruction::URem:
1617 if (!selectBinaryOp(I, ISD::UREM))
1618 return selectDivRem(I, ISD::UREM);
1620 case Instruction::Shl:
1621 case Instruction::LShr:
1622 case Instruction::AShr:
1623 return selectShift(I);
1624 case Instruction::And:
1625 case Instruction::Or:
1626 case Instruction::Xor:
1627 return selectLogicalOp(I);
1628 case Instruction::Br:
1629 return selectBranch(I);
1630 case Instruction::Ret:
1631 return selectRet(I);
1632 case Instruction::Trunc:
1633 return selectTrunc(I);
1634 case Instruction::ZExt:
1635 case Instruction::SExt:
1636 return selectIntExt(I);
1637 case Instruction::FPTrunc:
1638 return selectFPTrunc(I);
1639 case Instruction::FPExt:
1640 return selectFPExt(I);
1641 case Instruction::FPToSI:
1642 return selectFPToInt(I, /*isSigned*/ true);
1643 case Instruction::FPToUI:
1644 return selectFPToInt(I, /*isSigned*/ false);
1645 case Instruction::ICmp:
1646 case Instruction::FCmp:
1647 return selectCmp(I);
1648 case Instruction::Select:
1649 return selectSelect(I);
1654 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
1656 unsigned VReg = getRegForValue(V);
1659 MVT VMVT = TLI.getValueType(V->getType(), true).getSimpleVT();
1660 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
1661 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1662 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
1669 void MipsFastISel::simplifyAddress(Address &Addr) {
1670 if (!isInt<16>(Addr.getOffset())) {
1672 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
1673 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1674 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
1675 Addr.setReg(DestReg);
1680 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
1681 const TargetRegisterClass *RC,
1682 unsigned Op0, bool Op0IsKill,
1683 unsigned Op1, bool Op1IsKill) {
1684 // We treat the MUL instruction in a special way because it clobbers
1685 // the HI0 & LO0 registers. The TableGen definition of this instruction can
1686 // mark these registers only as implicitly defined. As a result, the
1687 // register allocator runs out of registers when this instruction is
1688 // followed by another instruction that defines the same registers too.
1689 // We can fix this by explicitly marking those registers as dead.
1690 if (MachineInstOpcode == Mips::MUL) {
1691 unsigned ResultReg = createResultReg(RC);
1692 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1693 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1694 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1695 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1696 .addReg(Op0, getKillRegState(Op0IsKill))
1697 .addReg(Op1, getKillRegState(Op1IsKill))
1698 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
1699 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
1703 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
1708 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
1709 const TargetLibraryInfo *libInfo) {
1710 return new MipsFastISel(funcInfo, libInfo);