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 ZExtCondReg = createResultReg(&Mips::GPR32RegClass);
988 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true))
991 unsigned ResultReg = createResultReg(RC);
992 unsigned TempReg = createResultReg(RC);
994 if (!ResultReg || !TempReg)
997 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
998 emitInst(CondMovOpc, ResultReg)
999 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg);
1000 updateValueMap(I, ResultReg);
1004 // Attempt to fast-select a floating-point truncate instruction.
1005 bool MipsFastISel::selectFPTrunc(const Instruction *I) {
1006 if (UnsupportedFPMode)
1008 Value *Src = I->getOperand(0);
1009 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1010 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1012 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1015 unsigned SrcReg = getRegForValue(Src);
1019 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1023 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1024 updateValueMap(I, DestReg);
1028 // Attempt to fast-select a floating-point-to-integer conversion.
1029 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1030 if (UnsupportedFPMode)
1034 return false; // We don't handle this case yet. There is no native
1035 // instruction for this but it can be synthesized.
1036 Type *DstTy = I->getType();
1037 if (!isTypeLegal(DstTy, DstVT))
1040 if (DstVT != MVT::i32)
1043 Value *Src = I->getOperand(0);
1044 Type *SrcTy = Src->getType();
1045 if (!isTypeLegal(SrcTy, SrcVT))
1048 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1051 unsigned SrcReg = getRegForValue(Src);
1055 // Determine the opcode for the conversion, which takes place
1056 // entirely within FPRs.
1057 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1058 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
1061 if (SrcVT == MVT::f32)
1062 Opc = Mips::TRUNC_W_S;
1064 Opc = Mips::TRUNC_W_D32;
1066 // Generate the convert.
1067 emitInst(Opc, TempReg).addReg(SrcReg);
1069 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1071 updateValueMap(I, DestReg);
1075 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1076 SmallVectorImpl<MVT> &OutVTs,
1077 unsigned &NumBytes) {
1078 CallingConv::ID CC = CLI.CallConv;
1079 SmallVector<CCValAssign, 16> ArgLocs;
1080 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1081 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1082 // Get a count of how many bytes are to be pushed on the stack.
1083 NumBytes = CCInfo.getNextStackOffset();
1084 // This is the minimum argument area used for A0-A3.
1088 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16);
1089 // Process the args.
1091 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1092 CCValAssign &VA = ArgLocs[i];
1093 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1094 MVT ArgVT = OutVTs[VA.getValNo()];
1098 if (ArgVT == MVT::f32) {
1099 VA.convertToReg(Mips::F12);
1100 } else if (ArgVT == MVT::f64) {
1101 VA.convertToReg(Mips::D6);
1103 } else if (i == 1) {
1104 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1105 if (ArgVT == MVT::f32) {
1106 VA.convertToReg(Mips::F14);
1107 } else if (ArgVT == MVT::f64) {
1108 VA.convertToReg(Mips::D7);
1112 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1113 (ArgVT == MVT::i8)) &&
1115 switch (VA.getLocMemOffset()) {
1117 VA.convertToReg(Mips::A0);
1120 VA.convertToReg(Mips::A1);
1123 VA.convertToReg(Mips::A2);
1126 VA.convertToReg(Mips::A3);
1132 unsigned ArgReg = getRegForValue(ArgVal);
1136 // Handle arg promotion: SExt, ZExt, AExt.
1137 switch (VA.getLocInfo()) {
1138 case CCValAssign::Full:
1140 case CCValAssign::AExt:
1141 case CCValAssign::SExt: {
1142 MVT DestVT = VA.getLocVT();
1144 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1149 case CCValAssign::ZExt: {
1150 MVT DestVT = VA.getLocVT();
1152 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1158 llvm_unreachable("Unknown arg promotion!");
1161 // Now copy/store arg to correct locations.
1162 if (VA.isRegLoc() && !VA.needsCustom()) {
1163 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1164 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1165 CLI.OutRegs.push_back(VA.getLocReg());
1166 } else if (VA.needsCustom()) {
1167 llvm_unreachable("Mips does not use custom args.");
1171 // FIXME: This path will currently return false. It was copied
1172 // from the AArch64 port and should be essentially fine for Mips too.
1173 // The work to finish up this path will be done in a follow-on patch.
1175 assert(VA.isMemLoc() && "Assuming store on stack.");
1176 // Don't emit stores for undef values.
1177 if (isa<UndefValue>(ArgVal))
1180 // Need to store on the stack.
1181 // FIXME: This alignment is incorrect but this path is disabled
1182 // for now (will return false). We need to determine the right alignment
1183 // based on the normal alignment for the underlying machine type.
1185 unsigned ArgSize = RoundUpToAlignment(ArgVT.getSizeInBits(), 4);
1187 unsigned BEAlign = 0;
1188 if (ArgSize < 8 && !Subtarget->isLittle())
1189 BEAlign = 8 - ArgSize;
1192 Addr.setKind(Address::RegBase);
1193 Addr.setReg(Mips::SP);
1194 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1196 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1197 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1198 MachinePointerInfo::getStack(Addr.getOffset()),
1199 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1201 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1202 return false; // can't store on the stack yet.
1209 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1210 unsigned NumBytes) {
1211 CallingConv::ID CC = CLI.CallConv;
1212 emitInst(Mips::ADJCALLSTACKUP).addImm(16);
1213 if (RetVT != MVT::isVoid) {
1214 SmallVector<CCValAssign, 16> RVLocs;
1215 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1216 CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips);
1218 // Only handle a single return value.
1219 if (RVLocs.size() != 1)
1221 // Copy all of the result registers out of their specified physreg.
1222 MVT CopyVT = RVLocs[0].getValVT();
1223 // Special handling for extended integers.
1224 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1227 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1230 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1231 TII.get(TargetOpcode::COPY),
1232 ResultReg).addReg(RVLocs[0].getLocReg());
1233 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1235 CLI.ResultReg = ResultReg;
1236 CLI.NumResultRegs = 1;
1241 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1242 CallingConv::ID CC = CLI.CallConv;
1243 bool IsTailCall = CLI.IsTailCall;
1244 bool IsVarArg = CLI.IsVarArg;
1245 const Value *Callee = CLI.Callee;
1246 MCSymbol *Symbol = CLI.Symbol;
1248 // Do not handle FastCC.
1249 if (CC == CallingConv::Fast)
1252 // Allow SelectionDAG isel to handle tail calls.
1256 // Let SDISel handle vararg functions.
1260 // FIXME: Only handle *simple* calls for now.
1262 if (CLI.RetTy->isVoidTy())
1263 RetVT = MVT::isVoid;
1264 else if (!isTypeSupported(CLI.RetTy, RetVT))
1267 for (auto Flag : CLI.OutFlags)
1268 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1271 // Set up the argument vectors.
1272 SmallVector<MVT, 16> OutVTs;
1273 OutVTs.reserve(CLI.OutVals.size());
1275 for (auto *Val : CLI.OutVals) {
1277 if (!isTypeLegal(Val->getType(), VT) &&
1278 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1281 // We don't handle vector parameters yet.
1282 if (VT.isVector() || VT.getSizeInBits() > 64)
1285 OutVTs.push_back(VT);
1289 if (!computeCallAddress(Callee, Addr))
1292 // Handle the arguments now that we've gotten them.
1294 if (!processCallArgs(CLI, OutVTs, NumBytes))
1297 if (!Addr.getGlobalValue())
1301 unsigned DestAddress;
1303 DestAddress = materializeExternalCallSym(Symbol);
1305 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
1306 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1307 MachineInstrBuilder MIB =
1308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1309 Mips::RA).addReg(Mips::T9);
1311 // Add implicit physical register uses to the call.
1312 for (auto Reg : CLI.OutRegs)
1313 MIB.addReg(Reg, RegState::Implicit);
1315 // Add a register mask with the call-preserved registers.
1316 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1317 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
1321 // Finish off the call including any return values.
1322 return finishCall(CLI, RetVT, NumBytes);
1325 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
1326 switch (II->getIntrinsicID()) {
1329 case Intrinsic::bswap: {
1330 Type *RetTy = II->getCalledFunction()->getReturnType();
1333 if (!isTypeSupported(RetTy, VT))
1336 unsigned SrcReg = getRegForValue(II->getOperand(0));
1339 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1342 if (VT == MVT::i16) {
1343 if (Subtarget->hasMips32r2()) {
1344 emitInst(Mips::WSBH, DestReg).addReg(SrcReg);
1345 updateValueMap(II, DestReg);
1348 unsigned TempReg[3];
1349 for (int i = 0; i < 3; i++) {
1350 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1351 if (TempReg[i] == 0)
1354 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8);
1355 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8);
1356 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]);
1357 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF);
1358 updateValueMap(II, DestReg);
1361 } else if (VT == MVT::i32) {
1362 if (Subtarget->hasMips32r2()) {
1363 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1364 emitInst(Mips::WSBH, TempReg).addReg(SrcReg);
1365 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16);
1366 updateValueMap(II, DestReg);
1369 unsigned TempReg[8];
1370 for (int i = 0; i < 8; i++) {
1371 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1372 if (TempReg[i] == 0)
1376 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8);
1377 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24);
1378 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00);
1379 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]);
1381 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00);
1382 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8);
1384 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24);
1385 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]);
1386 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]);
1387 updateValueMap(II, DestReg);
1393 case Intrinsic::memcpy:
1394 case Intrinsic::memmove: {
1395 const auto *MTI = cast<MemTransferInst>(II);
1396 // Don't handle volatile.
1397 if (MTI->isVolatile())
1399 if (!MTI->getLength()->getType()->isIntegerTy(32))
1401 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
1402 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
1404 case Intrinsic::memset: {
1405 const MemSetInst *MSI = cast<MemSetInst>(II);
1406 // Don't handle volatile.
1407 if (MSI->isVolatile())
1409 if (!MSI->getLength()->getType()->isIntegerTy(32))
1411 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
1417 bool MipsFastISel::selectRet(const Instruction *I) {
1418 const Function &F = *I->getParent()->getParent();
1419 const ReturnInst *Ret = cast<ReturnInst>(I);
1421 if (!FuncInfo.CanLowerReturn)
1424 // Build a list of return value registers.
1425 SmallVector<unsigned, 4> RetRegs;
1427 if (Ret->getNumOperands() > 0) {
1428 CallingConv::ID CC = F.getCallingConv();
1430 // Do not handle FastCC.
1431 if (CC == CallingConv::Fast)
1434 SmallVector<ISD::OutputArg, 4> Outs;
1435 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1437 // Analyze operands of the call, assigning locations to each operand.
1438 SmallVector<CCValAssign, 16> ValLocs;
1439 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1441 CCAssignFn *RetCC = RetCC_Mips;
1442 CCInfo.AnalyzeReturn(Outs, RetCC);
1444 // Only handle a single return value for now.
1445 if (ValLocs.size() != 1)
1448 CCValAssign &VA = ValLocs[0];
1449 const Value *RV = Ret->getOperand(0);
1451 // Don't bother handling odd stuff for now.
1452 if ((VA.getLocInfo() != CCValAssign::Full) &&
1453 (VA.getLocInfo() != CCValAssign::BCvt))
1456 // Only handle register returns for now.
1460 unsigned Reg = getRegForValue(RV);
1464 unsigned SrcReg = Reg + VA.getValNo();
1465 unsigned DestReg = VA.getLocReg();
1466 // Avoid a cross-class copy. This is very unlikely.
1467 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1470 EVT RVEVT = TLI.getValueType(DL, RV->getType());
1471 if (!RVEVT.isSimple())
1474 if (RVEVT.isVector())
1477 MVT RVVT = RVEVT.getSimpleVT();
1478 if (RVVT == MVT::f128)
1481 MVT DestVT = VA.getValVT();
1482 // Special handling for extended integers.
1483 if (RVVT != DestVT) {
1484 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1487 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1488 bool IsZExt = Outs[0].Flags.isZExt();
1489 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1496 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1497 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1499 // Add register to return instruction.
1500 RetRegs.push_back(VA.getLocReg());
1502 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1503 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1504 MIB.addReg(RetRegs[i], RegState::Implicit);
1508 bool MipsFastISel::selectTrunc(const Instruction *I) {
1509 // The high bits for a type smaller than the register size are assumed to be
1511 Value *Op = I->getOperand(0);
1514 SrcVT = TLI.getValueType(DL, Op->getType(), true);
1515 DestVT = TLI.getValueType(DL, I->getType(), true);
1517 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1519 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1522 unsigned SrcReg = getRegForValue(Op);
1526 // Because the high bits are undefined, a truncate doesn't generate
1528 updateValueMap(I, SrcReg);
1531 bool MipsFastISel::selectIntExt(const Instruction *I) {
1532 Type *DestTy = I->getType();
1533 Value *Src = I->getOperand(0);
1534 Type *SrcTy = Src->getType();
1536 bool isZExt = isa<ZExtInst>(I);
1537 unsigned SrcReg = getRegForValue(Src);
1541 EVT SrcEVT, DestEVT;
1542 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1543 DestEVT = TLI.getValueType(DL, DestTy, true);
1544 if (!SrcEVT.isSimple())
1546 if (!DestEVT.isSimple())
1549 MVT SrcVT = SrcEVT.getSimpleVT();
1550 MVT DestVT = DestEVT.getSimpleVT();
1551 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1553 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1555 updateValueMap(I, ResultReg);
1558 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1561 switch (SrcVT.SimpleTy) {
1571 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1572 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1573 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1577 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1579 switch (SrcVT.SimpleTy) {
1583 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1586 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1592 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1594 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1596 if (Subtarget->hasMips32r2())
1597 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1598 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1601 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1603 switch (SrcVT.SimpleTy) {
1607 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(1);
1610 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xff);
1613 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xffff);
1619 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1620 unsigned DestReg, bool IsZExt) {
1621 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1622 // DestVT are odd things, so test to make sure that they are both types we can
1623 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1624 // bail out to SelectionDAG.
1625 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1626 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1629 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1630 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1633 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1635 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1636 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1637 return Success ? DestReg : 0;
1640 bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
1641 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
1642 if (!DestEVT.isSimple())
1645 MVT DestVT = DestEVT.getSimpleVT();
1646 if (DestVT != MVT::i32)
1650 switch (ISDOpcode) {
1655 DivOpc = Mips::SDIV;
1659 DivOpc = Mips::UDIV;
1663 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1664 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1665 if (!Src0Reg || !Src1Reg)
1668 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1669 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1671 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1675 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1678 emitInst(MFOpc, ResultReg);
1680 updateValueMap(I, ResultReg);
1684 bool MipsFastISel::selectShift(const Instruction *I) {
1687 if (!isTypeSupported(I->getType(), RetVT))
1690 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1694 unsigned Opcode = I->getOpcode();
1695 const Value *Op0 = I->getOperand(0);
1696 unsigned Op0Reg = getRegForValue(Op0);
1700 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1701 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1702 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1706 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT();
1707 bool IsZExt = Opcode == Instruction::LShr;
1708 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1714 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1715 uint64_t ShiftVal = C->getZExtValue();
1719 llvm_unreachable("Unexpected instruction.");
1720 case Instruction::Shl:
1723 case Instruction::AShr:
1726 case Instruction::LShr:
1731 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1732 updateValueMap(I, ResultReg);
1736 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1742 llvm_unreachable("Unexpected instruction.");
1743 case Instruction::Shl:
1744 Opcode = Mips::SLLV;
1746 case Instruction::AShr:
1747 Opcode = Mips::SRAV;
1749 case Instruction::LShr:
1750 Opcode = Mips::SRLV;
1754 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1755 updateValueMap(I, ResultReg);
1759 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
1760 if (!TargetSupported)
1762 switch (I->getOpcode()) {
1765 case Instruction::Load:
1766 return selectLoad(I);
1767 case Instruction::Store:
1768 return selectStore(I);
1769 case Instruction::SDiv:
1770 if (!selectBinaryOp(I, ISD::SDIV))
1771 return selectDivRem(I, ISD::SDIV);
1773 case Instruction::UDiv:
1774 if (!selectBinaryOp(I, ISD::UDIV))
1775 return selectDivRem(I, ISD::UDIV);
1777 case Instruction::SRem:
1778 if (!selectBinaryOp(I, ISD::SREM))
1779 return selectDivRem(I, ISD::SREM);
1781 case Instruction::URem:
1782 if (!selectBinaryOp(I, ISD::UREM))
1783 return selectDivRem(I, ISD::UREM);
1785 case Instruction::Shl:
1786 case Instruction::LShr:
1787 case Instruction::AShr:
1788 return selectShift(I);
1789 case Instruction::And:
1790 case Instruction::Or:
1791 case Instruction::Xor:
1792 return selectLogicalOp(I);
1793 case Instruction::Br:
1794 return selectBranch(I);
1795 case Instruction::Ret:
1796 return selectRet(I);
1797 case Instruction::Trunc:
1798 return selectTrunc(I);
1799 case Instruction::ZExt:
1800 case Instruction::SExt:
1801 return selectIntExt(I);
1802 case Instruction::FPTrunc:
1803 return selectFPTrunc(I);
1804 case Instruction::FPExt:
1805 return selectFPExt(I);
1806 case Instruction::FPToSI:
1807 return selectFPToInt(I, /*isSigned*/ true);
1808 case Instruction::FPToUI:
1809 return selectFPToInt(I, /*isSigned*/ false);
1810 case Instruction::ICmp:
1811 case Instruction::FCmp:
1812 return selectCmp(I);
1813 case Instruction::Select:
1814 return selectSelect(I);
1819 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
1821 unsigned VReg = getRegForValue(V);
1824 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT();
1825 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
1826 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1827 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
1834 void MipsFastISel::simplifyAddress(Address &Addr) {
1835 if (!isInt<16>(Addr.getOffset())) {
1837 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
1838 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1839 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
1840 Addr.setReg(DestReg);
1845 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
1846 const TargetRegisterClass *RC,
1847 unsigned Op0, bool Op0IsKill,
1848 unsigned Op1, bool Op1IsKill) {
1849 // We treat the MUL instruction in a special way because it clobbers
1850 // the HI0 & LO0 registers. The TableGen definition of this instruction can
1851 // mark these registers only as implicitly defined. As a result, the
1852 // register allocator runs out of registers when this instruction is
1853 // followed by another instruction that defines the same registers too.
1854 // We can fix this by explicitly marking those registers as dead.
1855 if (MachineInstOpcode == Mips::MUL) {
1856 unsigned ResultReg = createResultReg(RC);
1857 const MCInstrDesc &II = TII.get(MachineInstOpcode);
1858 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1859 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1860 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1861 .addReg(Op0, getKillRegState(Op0IsKill))
1862 .addReg(Op1, getKillRegState(Op1IsKill))
1863 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
1864 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
1868 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
1873 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
1874 const TargetLibraryInfo *libInfo) {
1875 return new MipsFastISel(funcInfo, libInfo);