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/MC/MCSymbol.h"
20 #include "llvm/Target/TargetInstrInfo.h"
26 class MipsFastISel final : public FastISel {
28 // All possible address modes.
31 typedef enum { RegBase, FrameIndexBase } BaseKind;
42 const GlobalValue *GV;
45 // Innocuous defaults for our address.
46 Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; }
47 void setKind(BaseKind K) { Kind = K; }
48 BaseKind getKind() const { return Kind; }
49 bool isRegBase() const { return Kind == RegBase; }
50 bool isFIBase() const { return Kind == FrameIndexBase; }
51 void setReg(unsigned Reg) {
52 assert(isRegBase() && "Invalid base register access!");
55 unsigned getReg() const {
56 assert(isRegBase() && "Invalid base register access!");
59 void setFI(unsigned FI) {
60 assert(isFIBase() && "Invalid base frame index access!");
63 unsigned getFI() const {
64 assert(isFIBase() && "Invalid base frame index access!");
68 void setOffset(int64_t Offset_) { Offset = Offset_; }
69 int64_t getOffset() const { return Offset; }
70 void setGlobalValue(const GlobalValue *G) { GV = G; }
71 const GlobalValue *getGlobalValue() { return GV; }
74 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
75 /// make the right decision when generating code for different targets.
76 const TargetMachine &TM;
77 const MipsSubtarget *Subtarget;
78 const TargetInstrInfo &TII;
79 const TargetLowering &TLI;
80 MipsFunctionInfo *MFI;
82 // Convenience variables to avoid some queries.
85 bool fastLowerCall(CallLoweringInfo &CLI) override;
86 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
89 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
90 // floating point but not reject doing fast-isel in other
94 // Selection routines.
95 bool selectLogicalOp(const Instruction *I);
96 bool selectLoad(const Instruction *I);
97 bool selectStore(const Instruction *I);
98 bool selectBranch(const Instruction *I);
99 bool selectSelect(const Instruction *I);
100 bool selectCmp(const Instruction *I);
101 bool selectFPExt(const Instruction *I);
102 bool selectFPTrunc(const Instruction *I);
103 bool selectFPToInt(const Instruction *I, bool IsSigned);
104 bool selectRet(const Instruction *I);
105 bool selectTrunc(const Instruction *I);
106 bool selectIntExt(const Instruction *I);
107 bool selectShift(const Instruction *I);
108 bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
110 // Utility helper routines.
111 bool isTypeLegal(Type *Ty, MVT &VT);
112 bool isTypeSupported(Type *Ty, MVT &VT);
113 bool isLoadTypeLegal(Type *Ty, MVT &VT);
114 bool computeAddress(const Value *Obj, Address &Addr);
115 bool computeCallAddress(const Value *V, Address &Addr);
116 void simplifyAddress(Address &Addr);
118 // Emit helper routines.
119 bool emitCmp(unsigned DestReg, const CmpInst *CI);
120 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
121 unsigned Alignment = 0);
122 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
123 MachineMemOperand *MMO = nullptr);
124 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
125 unsigned Alignment = 0);
126 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
127 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
130 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
132 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
133 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
135 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
138 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
140 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
143 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
144 unsigned materializeGV(const GlobalValue *GV, MVT VT);
145 unsigned materializeInt(const Constant *C, MVT VT);
146 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
147 unsigned materializeExternalCallSym(MCSymbol *Syn);
149 MachineInstrBuilder emitInst(unsigned Opc) {
150 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
152 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
153 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
156 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
157 unsigned MemReg, int64_t MemOffset) {
158 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
160 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
161 unsigned MemReg, int64_t MemOffset) {
162 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
165 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
166 const TargetRegisterClass *RC,
167 unsigned Op0, bool Op0IsKill,
168 unsigned Op1, bool Op1IsKill);
170 // for some reason, this default is not generated by tablegen
171 // so we explicitly generate it here.
173 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
174 unsigned Op0, bool Op0IsKill, uint64_t imm1,
175 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
179 // Call handling routines.
181 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
182 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
184 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
187 // Backend specific FastISel code.
188 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
189 const TargetLibraryInfo *libInfo)
190 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
191 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
192 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
193 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
194 Context = &funcInfo.Fn->getContext();
196 ((TM.getRelocationModel() == Reloc::PIC_) &&
197 ((Subtarget->hasMips32r2() || Subtarget->hasMips32()) &&
198 (static_cast<const MipsTargetMachine &>(TM).getABI().IsO32())));
199 UnsupportedFPMode = Subtarget->isFP64bit();
202 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
203 unsigned fastMaterializeConstant(const Constant *C) override;
204 bool fastSelectInstruction(const Instruction *I) override;
206 #include "MipsGenFastISel.inc"
208 } // end anonymous namespace.
210 static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
211 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
212 CCState &State) LLVM_ATTRIBUTE_UNUSED;
214 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
215 CCValAssign::LocInfo LocInfo,
216 ISD::ArgFlagsTy ArgFlags, CCState &State) {
217 llvm_unreachable("should not be called");
220 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
221 CCValAssign::LocInfo LocInfo,
222 ISD::ArgFlagsTy ArgFlags, CCState &State) {
223 llvm_unreachable("should not be called");
226 #include "MipsGenCallingConv.inc"
228 CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
232 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
233 const Value *LHS, const Value *RHS) {
234 // Canonicalize immediates to the RHS first.
235 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
239 if (ISDOpc == ISD::AND) {
241 } else if (ISDOpc == ISD::OR) {
243 } else if (ISDOpc == ISD::XOR) {
246 llvm_unreachable("unexpected opcode");
248 unsigned LHSReg = getRegForValue(LHS);
249 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
257 if (const auto *C = dyn_cast<ConstantInt>(RHS))
258 RHSReg = materializeInt(C, MVT::i32);
260 RHSReg = getRegForValue(RHS);
265 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
269 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
270 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i32 &&
271 "Alloca should always return a pointer.");
273 DenseMap<const AllocaInst *, int>::iterator SI =
274 FuncInfo.StaticAllocaMap.find(AI);
276 if (SI != FuncInfo.StaticAllocaMap.end()) {
277 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
280 .addFrameIndex(SI->second)
288 unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
289 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
291 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
292 const ConstantInt *CI = cast<ConstantInt>(C);
294 if ((VT != MVT::i1) && CI->isNegative())
295 Imm = CI->getSExtValue();
297 Imm = CI->getZExtValue();
298 return materialize32BitInt(Imm, RC);
301 unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
302 const TargetRegisterClass *RC) {
303 unsigned ResultReg = createResultReg(RC);
305 if (isInt<16>(Imm)) {
306 unsigned Opc = Mips::ADDiu;
307 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
309 } else if (isUInt<16>(Imm)) {
310 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
313 unsigned Lo = Imm & 0xFFFF;
314 unsigned Hi = (Imm >> 16) & 0xFFFF;
316 // Both Lo and Hi have nonzero bits.
317 unsigned TmpReg = createResultReg(RC);
318 emitInst(Mips::LUi, TmpReg).addImm(Hi);
319 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
321 emitInst(Mips::LUi, ResultReg).addImm(Hi);
326 unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
327 if (UnsupportedFPMode)
329 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
330 if (VT == MVT::f32) {
331 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
332 unsigned DestReg = createResultReg(RC);
333 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
334 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
336 } else if (VT == MVT::f64) {
337 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
338 unsigned DestReg = createResultReg(RC);
339 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
341 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
342 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
348 unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
349 // For now 32-bit only.
352 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
353 unsigned DestReg = createResultReg(RC);
354 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
355 bool IsThreadLocal = GVar && GVar->isThreadLocal();
356 // TLS not supported at this time.
359 emitInst(Mips::LW, DestReg)
360 .addReg(MFI->getGlobalBaseReg())
361 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
362 if ((GV->hasInternalLinkage() ||
363 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
364 unsigned TempReg = createResultReg(RC);
365 emitInst(Mips::ADDiu, TempReg)
367 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
373 unsigned MipsFastISel::materializeExternalCallSym(MCSymbol *Sym) {
374 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
375 unsigned DestReg = createResultReg(RC);
376 emitInst(Mips::LW, DestReg)
377 .addReg(MFI->getGlobalBaseReg())
378 .addSym(Sym, MipsII::MO_GOT);
382 // Materialize a constant into a register, and return the register
383 // number (or zero if we failed to handle it).
384 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
385 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
387 // Only handle simple types.
388 if (!CEVT.isSimple())
390 MVT VT = CEVT.getSimpleVT();
392 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
393 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
394 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
395 return materializeGV(GV, VT);
396 else if (isa<ConstantInt>(C))
397 return materializeInt(C, VT);
402 bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
404 const User *U = nullptr;
405 unsigned Opcode = Instruction::UserOp1;
406 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
407 // Don't walk into other basic blocks unless the object is an alloca from
408 // another block, otherwise it may not have a virtual register assigned.
409 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
410 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
411 Opcode = I->getOpcode();
414 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
415 Opcode = C->getOpcode();
421 case Instruction::BitCast: {
422 // Look through bitcasts.
423 return computeAddress(U->getOperand(0), Addr);
425 case Instruction::GetElementPtr: {
426 Address SavedAddr = Addr;
427 uint64_t TmpOffset = Addr.getOffset();
428 // Iterate through the GEP folding the constants into offsets where
430 gep_type_iterator GTI = gep_type_begin(U);
431 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
433 const Value *Op = *i;
434 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
435 const StructLayout *SL = DL.getStructLayout(STy);
436 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
437 TmpOffset += SL->getElementOffset(Idx);
439 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
441 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
442 // Constant-offset addressing.
443 TmpOffset += CI->getSExtValue() * S;
446 if (canFoldAddIntoGEP(U, Op)) {
447 // A compatible add with a constant operand. Fold the constant.
449 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
450 TmpOffset += CI->getSExtValue() * S;
451 // Iterate on the other operand.
452 Op = cast<AddOperator>(Op)->getOperand(0);
456 goto unsupported_gep;
460 // Try to grab the base operand now.
461 Addr.setOffset(TmpOffset);
462 if (computeAddress(U->getOperand(0), Addr))
464 // We failed, restore everything and try the other options.
469 case Instruction::Alloca: {
470 const AllocaInst *AI = cast<AllocaInst>(Obj);
471 DenseMap<const AllocaInst *, int>::iterator SI =
472 FuncInfo.StaticAllocaMap.find(AI);
473 if (SI != FuncInfo.StaticAllocaMap.end()) {
474 Addr.setKind(Address::FrameIndexBase);
475 Addr.setFI(SI->second);
481 Addr.setReg(getRegForValue(Obj));
482 return Addr.getReg() != 0;
485 bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
486 const User *U = nullptr;
487 unsigned Opcode = Instruction::UserOp1;
489 if (const auto *I = dyn_cast<Instruction>(V)) {
490 // Check if the value is defined in the same basic block. This information
491 // is crucial to know whether or not folding an operand is valid.
492 if (I->getParent() == FuncInfo.MBB->getBasicBlock()) {
493 Opcode = I->getOpcode();
496 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
497 Opcode = C->getOpcode();
504 case Instruction::BitCast:
505 // Look past bitcasts if its operand is in the same BB.
506 return computeCallAddress(U->getOperand(0), Addr);
508 case Instruction::IntToPtr:
509 // Look past no-op inttoptrs if its operand is in the same BB.
510 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
511 TLI.getPointerTy(DL))
512 return computeCallAddress(U->getOperand(0), Addr);
514 case Instruction::PtrToInt:
515 // Look past no-op ptrtoints if its operand is in the same BB.
516 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
517 return computeCallAddress(U->getOperand(0), Addr);
521 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
522 Addr.setGlobalValue(GV);
526 // If all else fails, try to materialize the value in a register.
527 if (!Addr.getGlobalValue()) {
528 Addr.setReg(getRegForValue(V));
529 return Addr.getReg() != 0;
535 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
536 EVT evt = TLI.getValueType(DL, Ty, true);
537 // Only handle simple types.
538 if (evt == MVT::Other || !evt.isSimple())
540 VT = evt.getSimpleVT();
542 // Handle all legal types, i.e. a register that will directly hold this
544 return TLI.isTypeLegal(VT);
547 bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
548 if (Ty->isVectorTy())
551 if (isTypeLegal(Ty, VT))
554 // If this is a type than can be sign or zero-extended to a basic operation
555 // go ahead and accept it now.
556 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
562 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
563 if (isTypeLegal(Ty, VT))
565 // We will extend this in a later patch:
566 // If this is a type than can be sign or zero-extended to a basic operation
567 // go ahead and accept it now.
568 if (VT == MVT::i8 || VT == MVT::i16)
572 // Because of how EmitCmp is called with fast-isel, you can
573 // end up with redundant "andi" instructions after the sequences emitted below.
574 // We should try and solve this issue in the future.
576 bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
577 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
578 bool IsUnsigned = CI->isUnsigned();
579 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
582 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
585 CmpInst::Predicate P = CI->getPredicate();
590 case CmpInst::ICMP_EQ: {
591 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
592 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
593 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
596 case CmpInst::ICMP_NE: {
597 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
598 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
599 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
602 case CmpInst::ICMP_UGT: {
603 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
606 case CmpInst::ICMP_ULT: {
607 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
610 case CmpInst::ICMP_UGE: {
611 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
612 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
613 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
616 case CmpInst::ICMP_ULE: {
617 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
618 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
619 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
622 case CmpInst::ICMP_SGT: {
623 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
626 case CmpInst::ICMP_SLT: {
627 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
630 case CmpInst::ICMP_SGE: {
631 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
632 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
633 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
636 case CmpInst::ICMP_SLE: {
637 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
638 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
639 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
642 case CmpInst::FCMP_OEQ:
643 case CmpInst::FCMP_UNE:
644 case CmpInst::FCMP_OLT:
645 case CmpInst::FCMP_OLE:
646 case CmpInst::FCMP_OGT:
647 case CmpInst::FCMP_OGE: {
648 if (UnsupportedFPMode)
650 bool IsFloat = Left->getType()->isFloatTy();
651 bool IsDouble = Left->getType()->isDoubleTy();
652 if (!IsFloat && !IsDouble)
654 unsigned Opc, CondMovOpc;
656 case CmpInst::FCMP_OEQ:
657 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
658 CondMovOpc = Mips::MOVT_I;
660 case CmpInst::FCMP_UNE:
661 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
662 CondMovOpc = Mips::MOVF_I;
664 case CmpInst::FCMP_OLT:
665 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
666 CondMovOpc = Mips::MOVT_I;
668 case CmpInst::FCMP_OLE:
669 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
670 CondMovOpc = Mips::MOVT_I;
672 case CmpInst::FCMP_OGT:
673 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
674 CondMovOpc = Mips::MOVF_I;
676 case CmpInst::FCMP_OGE:
677 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
678 CondMovOpc = Mips::MOVF_I;
681 llvm_unreachable("Only switching of a subset of CCs.");
683 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
684 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
685 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
686 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
687 emitInst(Opc).addReg(LeftReg).addReg(RightReg).addReg(
688 Mips::FCC0, RegState::ImplicitDefine);
689 MachineInstrBuilder MI = emitInst(CondMovOpc, ResultReg)
692 .addReg(RegWithZero, RegState::Implicit);
693 MI->tieOperands(0, 3);
699 bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
700 unsigned Alignment) {
702 // more cases will be handled here in following patches.
705 switch (VT.SimpleTy) {
707 ResultReg = createResultReg(&Mips::GPR32RegClass);
712 ResultReg = createResultReg(&Mips::GPR32RegClass);
717 ResultReg = createResultReg(&Mips::GPR32RegClass);
722 if (UnsupportedFPMode)
724 ResultReg = createResultReg(&Mips::FGR32RegClass);
729 if (UnsupportedFPMode)
731 ResultReg = createResultReg(&Mips::AFGR64RegClass);
738 if (Addr.isRegBase()) {
739 simplifyAddress(Addr);
740 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
743 if (Addr.isFIBase()) {
744 unsigned FI = Addr.getFI();
746 unsigned Offset = Addr.getOffset();
747 MachineFrameInfo &MFI = *MF->getFrameInfo();
748 MachineMemOperand *MMO = MF->getMachineMemOperand(
749 MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
750 MFI.getObjectSize(FI), Align);
751 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
760 bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
761 unsigned Alignment) {
763 // more cases will be handled here in following patches.
766 switch (VT.SimpleTy) {
777 if (UnsupportedFPMode)
782 if (UnsupportedFPMode)
789 if (Addr.isRegBase()) {
790 simplifyAddress(Addr);
791 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
794 if (Addr.isFIBase()) {
795 unsigned FI = Addr.getFI();
797 unsigned Offset = Addr.getOffset();
798 MachineFrameInfo &MFI = *MF->getFrameInfo();
799 MachineMemOperand *MMO = MF->getMachineMemOperand(
800 MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
801 MFI.getObjectSize(FI), Align);
802 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
812 bool MipsFastISel::selectLogicalOp(const Instruction *I) {
814 if (!isTypeSupported(I->getType(), VT))
818 switch (I->getOpcode()) {
820 llvm_unreachable("Unexpected instruction.");
821 case Instruction::And:
822 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
824 case Instruction::Or:
825 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
827 case Instruction::Xor:
828 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
835 updateValueMap(I, ResultReg);
839 bool MipsFastISel::selectLoad(const Instruction *I) {
840 // Atomic loads need special handling.
841 if (cast<LoadInst>(I)->isAtomic())
844 // Verify we have a legal type before going any further.
846 if (!isLoadTypeLegal(I->getType(), VT))
849 // See if we can handle this address.
851 if (!computeAddress(I->getOperand(0), Addr))
855 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
857 updateValueMap(I, ResultReg);
861 bool MipsFastISel::selectStore(const Instruction *I) {
862 Value *Op0 = I->getOperand(0);
865 // Atomic stores need special handling.
866 if (cast<StoreInst>(I)->isAtomic())
869 // Verify we have a legal type before going any further.
871 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
874 // Get the value to be stored into a register.
875 SrcReg = getRegForValue(Op0);
879 // See if we can handle this address.
881 if (!computeAddress(I->getOperand(1), Addr))
884 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
890 // This can cause a redundant sltiu to be generated.
891 // FIXME: try and eliminate this in a future patch.
893 bool MipsFastISel::selectBranch(const Instruction *I) {
894 const BranchInst *BI = cast<BranchInst>(I);
895 MachineBasicBlock *BrBB = FuncInfo.MBB;
897 // TBB is the basic block for the case where the comparison is true.
898 // FBB is the basic block for the case where the comparison is false.
899 // if (cond) goto TBB
903 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
904 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
906 // For now, just try the simplest case where it's fed by a compare.
907 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
908 unsigned CondReg = createResultReg(&Mips::GPR32RegClass);
909 if (!emitCmp(CondReg, CI))
911 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
914 fastEmitBranch(FBB, DbgLoc);
915 FuncInfo.MBB->addSuccessor(TBB);
921 bool MipsFastISel::selectCmp(const Instruction *I) {
922 const CmpInst *CI = cast<CmpInst>(I);
923 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
924 if (!emitCmp(ResultReg, CI))
926 updateValueMap(I, ResultReg);
930 // Attempt to fast-select a floating-point extend instruction.
931 bool MipsFastISel::selectFPExt(const Instruction *I) {
932 if (UnsupportedFPMode)
934 Value *Src = I->getOperand(0);
935 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
936 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
938 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
942 getRegForValue(Src); // his must be a 32 bit floating point register class
943 // maybe we should handle this differently
947 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
948 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
949 updateValueMap(I, DestReg);
953 bool MipsFastISel::selectSelect(const Instruction *I) {
954 assert(isa<SelectInst>(I) && "Expected a select instruction.");
957 if (!isTypeSupported(I->getType(), VT))
961 const TargetRegisterClass *RC;
963 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
964 CondMovOpc = Mips::MOVN_I_I;
965 RC = &Mips::GPR32RegClass;
966 } else if (VT == MVT::f32) {
967 CondMovOpc = Mips::MOVN_I_S;
968 RC = &Mips::FGR32RegClass;
969 } else if (VT == MVT::f64) {
970 CondMovOpc = Mips::MOVN_I_D32;
971 RC = &Mips::AFGR64RegClass;
975 const SelectInst *SI = cast<SelectInst>(I);
976 const Value *Cond = SI->getCondition();
977 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
978 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
979 unsigned CondReg = getRegForValue(Cond);
981 if (!Src1Reg || !Src2Reg || !CondReg)
984 unsigned ResultReg = createResultReg(RC);
985 unsigned TempReg = createResultReg(RC);
987 if (!ResultReg || !TempReg)
990 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
991 emitInst(CondMovOpc, ResultReg)
992 .addReg(Src1Reg).addReg(CondReg).addReg(TempReg);
993 updateValueMap(I, ResultReg);
997 // Attempt to fast-select a floating-point truncate instruction.
998 bool MipsFastISel::selectFPTrunc(const Instruction *I) {
999 if (UnsupportedFPMode)
1001 Value *Src = I->getOperand(0);
1002 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1003 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1005 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1008 unsigned SrcReg = getRegForValue(Src);
1012 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1016 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1017 updateValueMap(I, DestReg);
1021 // Attempt to fast-select a floating-point-to-integer conversion.
1022 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1023 if (UnsupportedFPMode)
1027 return false; // We don't handle this case yet. There is no native
1028 // instruction for this but it can be synthesized.
1029 Type *DstTy = I->getType();
1030 if (!isTypeLegal(DstTy, DstVT))
1033 if (DstVT != MVT::i32)
1036 Value *Src = I->getOperand(0);
1037 Type *SrcTy = Src->getType();
1038 if (!isTypeLegal(SrcTy, SrcVT))
1041 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1044 unsigned SrcReg = getRegForValue(Src);
1048 // Determine the opcode for the conversion, which takes place
1049 // entirely within FPRs.
1050 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1051 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
1054 if (SrcVT == MVT::f32)
1055 Opc = Mips::TRUNC_W_S;
1057 Opc = Mips::TRUNC_W_D32;
1059 // Generate the convert.
1060 emitInst(Opc, TempReg).addReg(SrcReg);
1062 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1064 updateValueMap(I, DestReg);
1068 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1069 SmallVectorImpl<MVT> &OutVTs,
1070 unsigned &NumBytes) {
1071 CallingConv::ID CC = CLI.CallConv;
1072 SmallVector<CCValAssign, 16> ArgLocs;
1073 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1074 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1075 // Get a count of how many bytes are to be pushed on the stack.
1076 NumBytes = CCInfo.getNextStackOffset();
1077 // This is the minimum argument area used for A0-A3.
1081 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16);
1082 // Process the args.
1084 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1085 CCValAssign &VA = ArgLocs[i];
1086 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1087 MVT ArgVT = OutVTs[VA.getValNo()];
1091 if (ArgVT == MVT::f32) {
1092 VA.convertToReg(Mips::F12);
1093 } else if (ArgVT == MVT::f64) {
1094 VA.convertToReg(Mips::D6);
1096 } else if (i == 1) {
1097 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1098 if (ArgVT == MVT::f32) {
1099 VA.convertToReg(Mips::F14);
1100 } else if (ArgVT == MVT::f64) {
1101 VA.convertToReg(Mips::D7);
1105 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1106 (ArgVT == MVT::i8)) &&
1108 switch (VA.getLocMemOffset()) {
1110 VA.convertToReg(Mips::A0);
1113 VA.convertToReg(Mips::A1);
1116 VA.convertToReg(Mips::A2);
1119 VA.convertToReg(Mips::A3);
1125 unsigned ArgReg = getRegForValue(ArgVal);
1129 // Handle arg promotion: SExt, ZExt, AExt.
1130 switch (VA.getLocInfo()) {
1131 case CCValAssign::Full:
1133 case CCValAssign::AExt:
1134 case CCValAssign::SExt: {
1135 MVT DestVT = VA.getLocVT();
1137 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1142 case CCValAssign::ZExt: {
1143 MVT DestVT = VA.getLocVT();
1145 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1151 llvm_unreachable("Unknown arg promotion!");
1154 // Now copy/store arg to correct locations.
1155 if (VA.isRegLoc() && !VA.needsCustom()) {
1156 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1157 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1158 CLI.OutRegs.push_back(VA.getLocReg());
1159 } else if (VA.needsCustom()) {
1160 llvm_unreachable("Mips does not use custom args.");
1164 // FIXME: This path will currently return false. It was copied
1165 // from the AArch64 port and should be essentially fine for Mips too.
1166 // The work to finish up this path will be done in a follow-on patch.
1168 assert(VA.isMemLoc() && "Assuming store on stack.");
1169 // Don't emit stores for undef values.
1170 if (isa<UndefValue>(ArgVal))
1173 // Need to store on the stack.
1174 // FIXME: This alignment is incorrect but this path is disabled
1175 // for now (will return false). We need to determine the right alignment
1176 // based on the normal alignment for the underlying machine type.
1178 unsigned ArgSize = RoundUpToAlignment(ArgVT.getSizeInBits(), 4);
1180 unsigned BEAlign = 0;
1181 if (ArgSize < 8 && !Subtarget->isLittle())
1182 BEAlign = 8 - ArgSize;
1185 Addr.setKind(Address::RegBase);
1186 Addr.setReg(Mips::SP);
1187 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1189 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1190 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1191 MachinePointerInfo::getStack(Addr.getOffset()),
1192 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1194 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1195 return false; // can't store on the stack yet.
1202 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1203 unsigned NumBytes) {
1204 CallingConv::ID CC = CLI.CallConv;
1205 emitInst(Mips::ADJCALLSTACKUP).addImm(16);
1206 if (RetVT != MVT::isVoid) {
1207 SmallVector<CCValAssign, 16> RVLocs;
1208 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1209 CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips);
1211 // Only handle a single return value.
1212 if (RVLocs.size() != 1)
1214 // Copy all of the result registers out of their specified physreg.
1215 MVT CopyVT = RVLocs[0].getValVT();
1216 // Special handling for extended integers.
1217 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1220 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1223 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1224 TII.get(TargetOpcode::COPY),
1225 ResultReg).addReg(RVLocs[0].getLocReg());
1226 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1228 CLI.ResultReg = ResultReg;
1229 CLI.NumResultRegs = 1;
1234 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1235 CallingConv::ID CC = CLI.CallConv;
1236 bool IsTailCall = CLI.IsTailCall;
1237 bool IsVarArg = CLI.IsVarArg;
1238 const Value *Callee = CLI.Callee;
1239 MCSymbol *Symbol = CLI.Symbol;
1241 // Allow SelectionDAG isel to handle tail calls.
1245 // Let SDISel handle vararg functions.
1249 // FIXME: Only handle *simple* calls for now.
1251 if (CLI.RetTy->isVoidTy())
1252 RetVT = MVT::isVoid;
1253 else if (!isTypeSupported(CLI.RetTy, RetVT))
1256 for (auto Flag : CLI.OutFlags)
1257 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1260 // Set up the argument vectors.
1261 SmallVector<MVT, 16> OutVTs;
1262 OutVTs.reserve(CLI.OutVals.size());
1264 for (auto *Val : CLI.OutVals) {
1266 if (!isTypeLegal(Val->getType(), VT) &&
1267 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1270 // We don't handle vector parameters yet.
1271 if (VT.isVector() || VT.getSizeInBits() > 64)
1274 OutVTs.push_back(VT);
1278 if (!computeCallAddress(Callee, Addr))
1281 // Handle the arguments now that we've gotten them.
1283 if (!processCallArgs(CLI, OutVTs, NumBytes))
1286 if (!Addr.getGlobalValue())
1290 unsigned DestAddress;
1292 DestAddress = materializeExternalCallSym(Symbol);
1294 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
1295 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1296 MachineInstrBuilder MIB =
1297 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1298 Mips::RA).addReg(Mips::T9);
1300 // Add implicit physical register uses to the call.
1301 for (auto Reg : CLI.OutRegs)
1302 MIB.addReg(Reg, RegState::Implicit);
1304 // Add a register mask with the call-preserved registers.
1305 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1306 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
1310 // Finish off the call including any return values.
1311 return finishCall(CLI, RetVT, NumBytes);
1314 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
1315 switch (II->getIntrinsicID()) {
1318 case Intrinsic::bswap: {
1319 Type *RetTy = II->getCalledFunction()->getReturnType();
1322 if (!isTypeSupported(RetTy, VT))
1325 unsigned SrcReg = getRegForValue(II->getOperand(0));
1328 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1331 if (VT == MVT::i16) {
1332 if (Subtarget->hasMips32r2()) {
1333 emitInst(Mips::WSBH, DestReg).addReg(SrcReg);
1334 updateValueMap(II, DestReg);
1337 unsigned TempReg[3];
1338 for (int i = 0; i < 3; i++) {
1339 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1340 if (TempReg[i] == 0)
1343 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8);
1344 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8);
1345 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]);
1346 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF);
1347 updateValueMap(II, DestReg);
1350 } else if (VT == MVT::i32) {
1351 if (Subtarget->hasMips32r2()) {
1352 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1353 emitInst(Mips::WSBH, TempReg).addReg(SrcReg);
1354 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16);
1355 updateValueMap(II, DestReg);
1358 unsigned TempReg[8];
1359 for (int i = 0; i < 8; i++) {
1360 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1361 if (TempReg[i] == 0)
1365 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8);
1366 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24);
1367 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00);
1368 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]);
1370 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00);
1371 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8);
1373 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24);
1374 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]);
1375 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]);
1376 updateValueMap(II, DestReg);
1382 case Intrinsic::memcpy:
1383 case Intrinsic::memmove: {
1384 const auto *MTI = cast<MemTransferInst>(II);
1385 // Don't handle volatile.
1386 if (MTI->isVolatile())
1388 if (!MTI->getLength()->getType()->isIntegerTy(32))
1390 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
1391 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
1393 case Intrinsic::memset: {
1394 const MemSetInst *MSI = cast<MemSetInst>(II);
1395 // Don't handle volatile.
1396 if (MSI->isVolatile())
1398 if (!MSI->getLength()->getType()->isIntegerTy(32))
1400 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
1406 bool MipsFastISel::selectRet(const Instruction *I) {
1407 const Function &F = *I->getParent()->getParent();
1408 const ReturnInst *Ret = cast<ReturnInst>(I);
1410 if (!FuncInfo.CanLowerReturn)
1413 // Build a list of return value registers.
1414 SmallVector<unsigned, 4> RetRegs;
1416 if (Ret->getNumOperands() > 0) {
1417 CallingConv::ID CC = F.getCallingConv();
1418 SmallVector<ISD::OutputArg, 4> Outs;
1419 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1421 // Analyze operands of the call, assigning locations to each operand.
1422 SmallVector<CCValAssign, 16> ValLocs;
1423 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1425 CCAssignFn *RetCC = RetCC_Mips;
1426 CCInfo.AnalyzeReturn(Outs, RetCC);
1428 // Only handle a single return value for now.
1429 if (ValLocs.size() != 1)
1432 CCValAssign &VA = ValLocs[0];
1433 const Value *RV = Ret->getOperand(0);
1435 // Don't bother handling odd stuff for now.
1436 if ((VA.getLocInfo() != CCValAssign::Full) &&
1437 (VA.getLocInfo() != CCValAssign::BCvt))
1440 // Only handle register returns for now.
1444 unsigned Reg = getRegForValue(RV);
1448 unsigned SrcReg = Reg + VA.getValNo();
1449 unsigned DestReg = VA.getLocReg();
1450 // Avoid a cross-class copy. This is very unlikely.
1451 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1454 EVT RVEVT = TLI.getValueType(DL, RV->getType());
1455 if (!RVEVT.isSimple())
1458 if (RVEVT.isVector())
1461 MVT RVVT = RVEVT.getSimpleVT();
1462 if (RVVT == MVT::f128)
1465 MVT DestVT = VA.getValVT();
1466 // Special handling for extended integers.
1467 if (RVVT != DestVT) {
1468 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1471 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1472 bool IsZExt = Outs[0].Flags.isZExt();
1473 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1481 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1483 // Add register to return instruction.
1484 RetRegs.push_back(VA.getLocReg());
1486 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1487 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1488 MIB.addReg(RetRegs[i], RegState::Implicit);
1492 bool MipsFastISel::selectTrunc(const Instruction *I) {
1493 // The high bits for a type smaller than the register size are assumed to be
1495 Value *Op = I->getOperand(0);
1498 SrcVT = TLI.getValueType(DL, Op->getType(), true);
1499 DestVT = TLI.getValueType(DL, I->getType(), true);
1501 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1503 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1506 unsigned SrcReg = getRegForValue(Op);
1510 // Because the high bits are undefined, a truncate doesn't generate
1512 updateValueMap(I, SrcReg);
1515 bool MipsFastISel::selectIntExt(const Instruction *I) {
1516 Type *DestTy = I->getType();
1517 Value *Src = I->getOperand(0);
1518 Type *SrcTy = Src->getType();
1520 bool isZExt = isa<ZExtInst>(I);
1521 unsigned SrcReg = getRegForValue(Src);
1525 EVT SrcEVT, DestEVT;
1526 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1527 DestEVT = TLI.getValueType(DL, DestTy, true);
1528 if (!SrcEVT.isSimple())
1530 if (!DestEVT.isSimple())
1533 MVT SrcVT = SrcEVT.getSimpleVT();
1534 MVT DestVT = DestEVT.getSimpleVT();
1535 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1537 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1539 updateValueMap(I, ResultReg);
1542 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1545 switch (SrcVT.SimpleTy) {
1555 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1556 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1557 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1561 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1563 switch (SrcVT.SimpleTy) {
1567 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1570 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1576 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1578 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1580 if (Subtarget->hasMips32r2())
1581 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1582 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1585 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1587 switch (SrcVT.SimpleTy) {
1591 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(1);
1594 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xff);
1597 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xffff);
1603 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1604 unsigned DestReg, bool IsZExt) {
1605 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1606 // DestVT are odd things, so test to make sure that they are both types we can
1607 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1608 // bail out to SelectionDAG.
1609 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1610 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1613 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1614 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1617 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1619 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1620 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1621 return Success ? DestReg : 0;
1624 bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
1625 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
1626 if (!DestEVT.isSimple())
1629 MVT DestVT = DestEVT.getSimpleVT();
1630 if (DestVT != MVT::i32)
1634 switch (ISDOpcode) {
1639 DivOpc = Mips::SDIV;
1643 DivOpc = Mips::UDIV;
1647 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1648 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1649 if (!Src0Reg || !Src1Reg)
1652 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1653 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1655 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1659 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1662 emitInst(MFOpc, ResultReg);
1664 updateValueMap(I, ResultReg);
1668 bool MipsFastISel::selectShift(const Instruction *I) {
1671 if (!isTypeSupported(I->getType(), RetVT))
1674 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1678 unsigned Opcode = I->getOpcode();
1679 const Value *Op0 = I->getOperand(0);
1680 unsigned Op0Reg = getRegForValue(Op0);
1684 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1685 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1686 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1690 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT();
1691 bool IsZExt = Opcode == Instruction::LShr;
1692 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1698 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1699 uint64_t ShiftVal = C->getZExtValue();
1703 llvm_unreachable("Unexpected instruction.");
1704 case Instruction::Shl:
1707 case Instruction::AShr:
1710 case Instruction::LShr:
1715 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1716 updateValueMap(I, ResultReg);
1720 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1726 llvm_unreachable("Unexpected instruction.");
1727 case Instruction::Shl:
1728 Opcode = Mips::SLLV;
1730 case Instruction::AShr:
1731 Opcode = Mips::SRAV;
1733 case Instruction::LShr:
1734 Opcode = Mips::SRLV;
1738 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1739 updateValueMap(I, ResultReg);
1743 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
1744 if (!TargetSupported)
1746 switch (I->getOpcode()) {
1749 case Instruction::Load:
1750 return selectLoad(I);
1751 case Instruction::Store:
1752 return selectStore(I);
1753 case Instruction::SDiv:
1754 if (!selectBinaryOp(I, ISD::SDIV))
1755 return selectDivRem(I, ISD::SDIV);
1757 case Instruction::UDiv:
1758 if (!selectBinaryOp(I, ISD::UDIV))
1759 return selectDivRem(I, ISD::UDIV);
1761 case Instruction::SRem:
1762 if (!selectBinaryOp(I, ISD::SREM))
1763 return selectDivRem(I, ISD::SREM);
1765 case Instruction::URem:
1766 if (!selectBinaryOp(I, ISD::UREM))
1767 return selectDivRem(I, ISD::UREM);
1769 case Instruction::Shl:
1770 case Instruction::LShr:
1771 case Instruction::AShr:
1772 return selectShift(I);
1773 case Instruction::And:
1774 case Instruction::Or:
1775 case Instruction::Xor:
1776 return selectLogicalOp(I);
1777 case Instruction::Br:
1778 return selectBranch(I);
1779 case Instruction::Ret:
1780 return selectRet(I);
1781 case Instruction::Trunc:
1782 return selectTrunc(I);
1783 case Instruction::ZExt:
1784 case Instruction::SExt:
1785 return selectIntExt(I);
1786 case Instruction::FPTrunc:
1787 return selectFPTrunc(I);
1788 case Instruction::FPExt:
1789 return selectFPExt(I);
1790 case Instruction::FPToSI:
1791 return selectFPToInt(I, /*isSigned*/ true);
1792 case Instruction::FPToUI:
1793 return selectFPToInt(I, /*isSigned*/ false);
1794 case Instruction::ICmp:
1795 case Instruction::FCmp:
1796 return selectCmp(I);
1797 case Instruction::Select:
1798 return selectSelect(I);
1803 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
1805 unsigned VReg = getRegForValue(V);
1808 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT();
1809 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
1810 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1811 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
1818 void MipsFastISel::simplifyAddress(Address &Addr) {
1819 if (!isInt<16>(Addr.getOffset())) {
1821 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
1822 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1823 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
1824 Addr.setReg(DestReg);
1829 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
1830 const TargetRegisterClass *RC,
1831 unsigned Op0, bool Op0IsKill,
1832 unsigned Op1, bool Op1IsKill) {
1833 // We treat the MUL instruction in a special way because it clobbers
1834 // the HI0 & LO0 registers. The TableGen definition of this instruction can
1835 // mark these registers only as implicitly defined. As a result, the
1836 // register allocator runs out of registers when this instruction is
1837 // followed by another instruction that defines the same registers too.
1838 // We can fix this by explicitly marking those registers as dead.
1839 if (MachineInstOpcode == Mips::MUL) {
1840 unsigned ResultReg = createResultReg(RC);
1841 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1842 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1843 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1845 .addReg(Op0, getKillRegState(Op0IsKill))
1846 .addReg(Op1, getKillRegState(Op1IsKill))
1847 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
1848 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
1852 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
1857 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
1858 const TargetLibraryInfo *libInfo) {
1859 return new MipsFastISel(funcInfo, libInfo);