1 //===-- MipsastISel.cpp - Mips FastISel implementation
2 //---------------------===//
4 #include "MipsCCState.h"
5 #include "MipsISelLowering.h"
6 #include "MipsMachineFunction.h"
7 #include "MipsRegisterInfo.h"
8 #include "MipsSubtarget.h"
9 #include "MipsTargetMachine.h"
10 #include "llvm/Analysis/TargetLibraryInfo.h"
11 #include "llvm/CodeGen/FastISel.h"
12 #include "llvm/CodeGen/FunctionLoweringInfo.h"
13 #include "llvm/CodeGen/MachineInstrBuilder.h"
14 #include "llvm/CodeGen/MachineRegisterInfo.h"
15 #include "llvm/IR/GlobalAlias.h"
16 #include "llvm/IR/GlobalVariable.h"
17 #include "llvm/Target/TargetInstrInfo.h"
23 class MipsFastISel final : public FastISel {
25 // All possible address modes.
28 typedef enum { RegBase, FrameIndexBase } BaseKind;
39 const GlobalValue *GV;
42 // Innocuous defaults for our address.
43 Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; }
44 void setKind(BaseKind K) { Kind = K; }
45 BaseKind getKind() const { return Kind; }
46 bool isRegBase() const { return Kind == RegBase; }
47 void setReg(unsigned Reg) {
48 assert(isRegBase() && "Invalid base register access!");
51 unsigned getReg() const {
52 assert(isRegBase() && "Invalid base register access!");
55 void setOffset(int64_t Offset_) { Offset = Offset_; }
56 int64_t getOffset() const { return Offset; }
57 void setGlobalValue(const GlobalValue *G) { GV = G; }
58 const GlobalValue *getGlobalValue() { return GV; }
61 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
62 /// make the right decision when generating code for different targets.
63 const TargetMachine &TM;
64 const MipsSubtarget *Subtarget;
65 const TargetInstrInfo &TII;
66 const TargetLowering &TLI;
67 MipsFunctionInfo *MFI;
69 // Convenience variables to avoid some queries.
72 bool fastLowerCall(CallLoweringInfo &CLI) override;
75 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
76 // floating point but not reject doing fast-isel in other
80 // Selection routines.
81 bool selectLoad(const Instruction *I);
82 bool selectStore(const Instruction *I);
83 bool selectBranch(const Instruction *I);
84 bool selectCmp(const Instruction *I);
85 bool selectFPExt(const Instruction *I);
86 bool selectFPTrunc(const Instruction *I);
87 bool selectFPToInt(const Instruction *I, bool IsSigned);
88 bool selectRet(const Instruction *I);
89 bool selectTrunc(const Instruction *I);
90 bool selectIntExt(const Instruction *I);
92 // Utility helper routines.
93 bool isTypeLegal(Type *Ty, MVT &VT);
94 bool isLoadTypeLegal(Type *Ty, MVT &VT);
95 bool computeAddress(const Value *Obj, Address &Addr);
96 bool computeCallAddress(const Value *V, Address &Addr);
98 // Emit helper routines.
99 bool emitCmp(unsigned DestReg, const CmpInst *CI);
100 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
101 unsigned Alignment = 0);
102 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
103 MachineMemOperand *MMO = nullptr);
104 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
105 unsigned Alignment = 0);
106 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
107 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
110 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
112 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
113 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
115 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
118 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
120 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
121 unsigned materializeGV(const GlobalValue *GV, MVT VT);
122 unsigned materializeInt(const Constant *C, MVT VT);
123 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
125 MachineInstrBuilder emitInst(unsigned Opc) {
126 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
128 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
129 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
132 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
133 unsigned MemReg, int64_t MemOffset) {
134 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
136 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
137 unsigned MemReg, int64_t MemOffset) {
138 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
140 // for some reason, this default is not generated by tablegen
141 // so we explicitly generate it here.
143 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
144 unsigned Op0, bool Op0IsKill, uint64_t imm1,
145 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
149 // Call handling routines.
151 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
152 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
154 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
157 // Backend specific FastISel code.
158 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
159 const TargetLibraryInfo *libInfo)
160 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
162 &static_cast<const MipsSubtarget &>(funcInfo.MF->getSubtarget())),
163 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
164 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
165 Context = &funcInfo.Fn->getContext();
167 ((TM.getRelocationModel() == Reloc::PIC_) &&
168 ((Subtarget->hasMips32r2() || Subtarget->hasMips32()) &&
169 (static_cast<const MipsTargetMachine &>(TM).getABI().IsO32())));
170 UnsupportedFPMode = Subtarget->isFP64bit();
173 unsigned fastMaterializeConstant(const Constant *C) override;
174 bool fastSelectInstruction(const Instruction *I) override;
176 #include "MipsGenFastISel.inc"
178 } // end anonymous namespace.
180 static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
181 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
182 CCState &State) LLVM_ATTRIBUTE_UNUSED;
184 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
185 CCValAssign::LocInfo LocInfo,
186 ISD::ArgFlagsTy ArgFlags, CCState &State) {
187 llvm_unreachable("should not be called");
190 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
191 CCValAssign::LocInfo LocInfo,
192 ISD::ArgFlagsTy ArgFlags, CCState &State) {
193 llvm_unreachable("should not be called");
196 #include "MipsGenCallingConv.inc"
198 CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
202 unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
203 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
205 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
206 const ConstantInt *CI = cast<ConstantInt>(C);
208 if ((VT != MVT::i1) && CI->isNegative())
209 Imm = CI->getSExtValue();
211 Imm = CI->getZExtValue();
212 return materialize32BitInt(Imm, RC);
215 unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
216 const TargetRegisterClass *RC) {
217 unsigned ResultReg = createResultReg(RC);
219 if (isInt<16>(Imm)) {
220 unsigned Opc = Mips::ADDiu;
221 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
223 } else if (isUInt<16>(Imm)) {
224 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
227 unsigned Lo = Imm & 0xFFFF;
228 unsigned Hi = (Imm >> 16) & 0xFFFF;
230 // Both Lo and Hi have nonzero bits.
231 unsigned TmpReg = createResultReg(RC);
232 emitInst(Mips::LUi, TmpReg).addImm(Hi);
233 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
235 emitInst(Mips::LUi, ResultReg).addImm(Hi);
240 unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
241 if (UnsupportedFPMode)
243 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
244 if (VT == MVT::f32) {
245 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
246 unsigned DestReg = createResultReg(RC);
247 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
248 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
250 } else if (VT == MVT::f64) {
251 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
252 unsigned DestReg = createResultReg(RC);
253 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
255 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
256 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
262 unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
263 // For now 32-bit only.
266 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
267 unsigned DestReg = createResultReg(RC);
268 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
269 bool IsThreadLocal = GVar && GVar->isThreadLocal();
270 // TLS not supported at this time.
273 emitInst(Mips::LW, DestReg)
274 .addReg(MFI->getGlobalBaseReg())
275 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
276 if ((GV->hasInternalLinkage() ||
277 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
278 unsigned TempReg = createResultReg(RC);
279 emitInst(Mips::ADDiu, TempReg)
281 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
287 // Materialize a constant into a register, and return the register
288 // number (or zero if we failed to handle it).
289 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
290 EVT CEVT = TLI.getValueType(C->getType(), true);
292 // Only handle simple types.
293 if (!CEVT.isSimple())
295 MVT VT = CEVT.getSimpleVT();
297 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
298 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
299 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
300 return materializeGV(GV, VT);
301 else if (isa<ConstantInt>(C))
302 return materializeInt(C, VT);
307 bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
308 // This construct looks a big awkward but it is how other ports handle this
309 // and as this function is more fully completed, these cases which
310 // return false will have additional code in them.
312 if (isa<Instruction>(Obj))
314 else if (isa<ConstantExpr>(Obj))
316 Addr.setReg(getRegForValue(Obj));
317 return Addr.getReg() != 0;
320 bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
321 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
322 if (GV && isa<Function>(GV) && dyn_cast<Function>(GV)->isIntrinsic())
326 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
327 Addr.setGlobalValue(GV);
333 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
334 EVT evt = TLI.getValueType(Ty, true);
335 // Only handle simple types.
336 if (evt == MVT::Other || !evt.isSimple())
338 VT = evt.getSimpleVT();
340 // Handle all legal types, i.e. a register that will directly hold this
342 return TLI.isTypeLegal(VT);
345 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
346 if (isTypeLegal(Ty, VT))
348 // We will extend this in a later patch:
349 // If this is a type than can be sign or zero-extended to a basic operation
350 // go ahead and accept it now.
351 if (VT == MVT::i8 || VT == MVT::i16)
355 // Because of how EmitCmp is called with fast-isel, you can
356 // end up with redundant "andi" instructions after the sequences emitted below.
357 // We should try and solve this issue in the future.
359 bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
360 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
361 bool IsUnsigned = CI->isUnsigned();
362 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
365 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
368 CmpInst::Predicate P = CI->getPredicate();
373 case CmpInst::ICMP_EQ: {
374 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
375 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
376 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
379 case CmpInst::ICMP_NE: {
380 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
381 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
382 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
385 case CmpInst::ICMP_UGT: {
386 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
389 case CmpInst::ICMP_ULT: {
390 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
393 case CmpInst::ICMP_UGE: {
394 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
395 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
396 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
399 case CmpInst::ICMP_ULE: {
400 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
401 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
402 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
405 case CmpInst::ICMP_SGT: {
406 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
409 case CmpInst::ICMP_SLT: {
410 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
413 case CmpInst::ICMP_SGE: {
414 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
415 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
416 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
419 case CmpInst::ICMP_SLE: {
420 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
421 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
422 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
425 case CmpInst::FCMP_OEQ:
426 case CmpInst::FCMP_UNE:
427 case CmpInst::FCMP_OLT:
428 case CmpInst::FCMP_OLE:
429 case CmpInst::FCMP_OGT:
430 case CmpInst::FCMP_OGE: {
431 if (UnsupportedFPMode)
433 bool IsFloat = Left->getType()->isFloatTy();
434 bool IsDouble = Left->getType()->isDoubleTy();
435 if (!IsFloat && !IsDouble)
437 unsigned Opc, CondMovOpc;
439 case CmpInst::FCMP_OEQ:
440 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
441 CondMovOpc = Mips::MOVT_I;
443 case CmpInst::FCMP_UNE:
444 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
445 CondMovOpc = Mips::MOVF_I;
447 case CmpInst::FCMP_OLT:
448 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
449 CondMovOpc = Mips::MOVT_I;
451 case CmpInst::FCMP_OLE:
452 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
453 CondMovOpc = Mips::MOVT_I;
455 case CmpInst::FCMP_OGT:
456 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
457 CondMovOpc = Mips::MOVF_I;
459 case CmpInst::FCMP_OGE:
460 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
461 CondMovOpc = Mips::MOVF_I;
464 llvm_unreachable("Only switching of a subset of CCs.");
466 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
467 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
468 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
469 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
470 emitInst(Opc).addReg(LeftReg).addReg(RightReg).addReg(
471 Mips::FCC0, RegState::ImplicitDefine);
472 MachineInstrBuilder MI = emitInst(CondMovOpc, ResultReg)
475 .addReg(RegWithZero, RegState::Implicit);
476 MI->tieOperands(0, 3);
482 bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
483 unsigned Alignment) {
485 // more cases will be handled here in following patches.
488 switch (VT.SimpleTy) {
490 ResultReg = createResultReg(&Mips::GPR32RegClass);
495 ResultReg = createResultReg(&Mips::GPR32RegClass);
500 ResultReg = createResultReg(&Mips::GPR32RegClass);
505 if (UnsupportedFPMode)
507 ResultReg = createResultReg(&Mips::FGR32RegClass);
512 if (UnsupportedFPMode)
514 ResultReg = createResultReg(&Mips::AFGR64RegClass);
521 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
525 bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
526 unsigned Alignment) {
528 // more cases will be handled here in following patches.
531 switch (VT.SimpleTy) {
542 if (UnsupportedFPMode)
547 if (UnsupportedFPMode)
554 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
558 bool MipsFastISel::selectLoad(const Instruction *I) {
559 // Atomic loads need special handling.
560 if (cast<LoadInst>(I)->isAtomic())
563 // Verify we have a legal type before going any further.
565 if (!isLoadTypeLegal(I->getType(), VT))
568 // See if we can handle this address.
570 if (!computeAddress(I->getOperand(0), Addr))
574 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
576 updateValueMap(I, ResultReg);
580 bool MipsFastISel::selectStore(const Instruction *I) {
581 Value *Op0 = I->getOperand(0);
584 // Atomic stores need special handling.
585 if (cast<StoreInst>(I)->isAtomic())
588 // Verify we have a legal type before going any further.
590 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
593 // Get the value to be stored into a register.
594 SrcReg = getRegForValue(Op0);
598 // See if we can handle this address.
600 if (!computeAddress(I->getOperand(1), Addr))
603 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
609 // This can cause a redundant sltiu to be generated.
610 // FIXME: try and eliminate this in a future patch.
612 bool MipsFastISel::selectBranch(const Instruction *I) {
613 const BranchInst *BI = cast<BranchInst>(I);
614 MachineBasicBlock *BrBB = FuncInfo.MBB;
616 // TBB is the basic block for the case where the comparison is true.
617 // FBB is the basic block for the case where the comparison is false.
618 // if (cond) goto TBB
622 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
623 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
625 // For now, just try the simplest case where it's fed by a compare.
626 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
627 unsigned CondReg = createResultReg(&Mips::GPR32RegClass);
628 if (!emitCmp(CondReg, CI))
630 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
633 fastEmitBranch(FBB, DbgLoc);
634 FuncInfo.MBB->addSuccessor(TBB);
640 bool MipsFastISel::selectCmp(const Instruction *I) {
641 const CmpInst *CI = cast<CmpInst>(I);
642 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
643 if (!emitCmp(ResultReg, CI))
645 updateValueMap(I, ResultReg);
649 // Attempt to fast-select a floating-point extend instruction.
650 bool MipsFastISel::selectFPExt(const Instruction *I) {
651 if (UnsupportedFPMode)
653 Value *Src = I->getOperand(0);
654 EVT SrcVT = TLI.getValueType(Src->getType(), true);
655 EVT DestVT = TLI.getValueType(I->getType(), true);
657 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
661 getRegForValue(Src); // his must be a 32 bit floating point register class
662 // maybe we should handle this differently
666 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
667 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
668 updateValueMap(I, DestReg);
672 // Attempt to fast-select a floating-point truncate instruction.
673 bool MipsFastISel::selectFPTrunc(const Instruction *I) {
674 if (UnsupportedFPMode)
676 Value *Src = I->getOperand(0);
677 EVT SrcVT = TLI.getValueType(Src->getType(), true);
678 EVT DestVT = TLI.getValueType(I->getType(), true);
680 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
683 unsigned SrcReg = getRegForValue(Src);
687 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
691 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
692 updateValueMap(I, DestReg);
696 // Attempt to fast-select a floating-point-to-integer conversion.
697 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
698 if (UnsupportedFPMode)
702 return false; // We don't handle this case yet. There is no native
703 // instruction for this but it can be synthesized.
704 Type *DstTy = I->getType();
705 if (!isTypeLegal(DstTy, DstVT))
708 if (DstVT != MVT::i32)
711 Value *Src = I->getOperand(0);
712 Type *SrcTy = Src->getType();
713 if (!isTypeLegal(SrcTy, SrcVT))
716 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
719 unsigned SrcReg = getRegForValue(Src);
723 // Determine the opcode for the conversion, which takes place
724 // entirely within FPRs.
725 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
726 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
729 if (SrcVT == MVT::f32)
730 Opc = Mips::TRUNC_W_S;
732 Opc = Mips::TRUNC_W_D32;
734 // Generate the convert.
735 emitInst(Opc, TempReg).addReg(SrcReg);
737 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
739 updateValueMap(I, DestReg);
743 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
744 SmallVectorImpl<MVT> &OutVTs,
745 unsigned &NumBytes) {
746 CallingConv::ID CC = CLI.CallConv;
747 SmallVector<CCValAssign, 16> ArgLocs;
748 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
749 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
750 // Get a count of how many bytes are to be pushed on the stack.
751 NumBytes = CCInfo.getNextStackOffset();
752 // This is the minimum argument area used for A0-A3.
756 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16);
759 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
760 CCValAssign &VA = ArgLocs[i];
761 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
762 MVT ArgVT = OutVTs[VA.getValNo()];
766 if (ArgVT == MVT::f32) {
767 VA.convertToReg(Mips::F12);
768 } else if (ArgVT == MVT::f64) {
769 VA.convertToReg(Mips::D6);
772 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
773 if (ArgVT == MVT::f32) {
774 VA.convertToReg(Mips::F14);
775 } else if (ArgVT == MVT::f64) {
776 VA.convertToReg(Mips::D7);
780 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32)) && VA.isMemLoc()) {
781 switch (VA.getLocMemOffset()) {
783 VA.convertToReg(Mips::A0);
786 VA.convertToReg(Mips::A1);
789 VA.convertToReg(Mips::A2);
792 VA.convertToReg(Mips::A3);
798 unsigned ArgReg = getRegForValue(ArgVal);
802 // Handle arg promotion: SExt, ZExt, AExt.
803 switch (VA.getLocInfo()) {
804 case CCValAssign::Full:
806 case CCValAssign::AExt:
807 case CCValAssign::SExt: {
808 MVT DestVT = VA.getLocVT();
810 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
815 case CCValAssign::ZExt: {
816 MVT DestVT = VA.getLocVT();
818 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
824 llvm_unreachable("Unknown arg promotion!");
827 // Now copy/store arg to correct locations.
828 if (VA.isRegLoc() && !VA.needsCustom()) {
829 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
830 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
831 CLI.OutRegs.push_back(VA.getLocReg());
832 } else if (VA.needsCustom()) {
833 llvm_unreachable("Mips does not use custom args.");
837 // FIXME: This path will currently return false. It was copied
838 // from the AArch64 port and should be essentially fine for Mips too.
839 // The work to finish up this path will be done in a follow-on patch.
841 assert(VA.isMemLoc() && "Assuming store on stack.");
842 // Don't emit stores for undef values.
843 if (isa<UndefValue>(ArgVal))
846 // Need to store on the stack.
847 // FIXME: This alignment is incorrect but this path is disabled
848 // for now (will return false). We need to determine the right alignment
849 // based on the normal alignment for the underlying machine type.
851 unsigned ArgSize = RoundUpToAlignment(ArgVT.getSizeInBits(), 4);
853 unsigned BEAlign = 0;
854 if (ArgSize < 8 && !Subtarget->isLittle())
855 BEAlign = 8 - ArgSize;
858 Addr.setKind(Address::RegBase);
859 Addr.setReg(Mips::SP);
860 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
862 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
863 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
864 MachinePointerInfo::getStack(Addr.getOffset()),
865 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
867 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
868 return false; // can't store on the stack yet.
875 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
877 CallingConv::ID CC = CLI.CallConv;
878 emitInst(Mips::ADJCALLSTACKUP).addImm(16);
879 if (RetVT != MVT::isVoid) {
880 SmallVector<CCValAssign, 16> RVLocs;
881 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
882 CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips);
884 // Only handle a single return value.
885 if (RVLocs.size() != 1)
887 // Copy all of the result registers out of their specified physreg.
888 MVT CopyVT = RVLocs[0].getValVT();
889 // Special handling for extended integers.
890 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
893 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
894 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
895 TII.get(TargetOpcode::COPY),
896 ResultReg).addReg(RVLocs[0].getLocReg());
897 CLI.InRegs.push_back(RVLocs[0].getLocReg());
899 CLI.ResultReg = ResultReg;
900 CLI.NumResultRegs = 1;
905 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
906 CallingConv::ID CC = CLI.CallConv;
907 bool IsTailCall = CLI.IsTailCall;
908 bool IsVarArg = CLI.IsVarArg;
909 const Value *Callee = CLI.Callee;
910 // const char *SymName = CLI.SymName;
912 // Allow SelectionDAG isel to handle tail calls.
916 // Let SDISel handle vararg functions.
920 // FIXME: Only handle *simple* calls for now.
922 if (CLI.RetTy->isVoidTy())
924 else if (!isTypeLegal(CLI.RetTy, RetVT))
927 for (auto Flag : CLI.OutFlags)
928 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
931 // Set up the argument vectors.
932 SmallVector<MVT, 16> OutVTs;
933 OutVTs.reserve(CLI.OutVals.size());
935 for (auto *Val : CLI.OutVals) {
937 if (!isTypeLegal(Val->getType(), VT) &&
938 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
941 // We don't handle vector parameters yet.
942 if (VT.isVector() || VT.getSizeInBits() > 64)
945 OutVTs.push_back(VT);
949 if (!computeCallAddress(Callee, Addr))
952 // Handle the arguments now that we've gotten them.
954 if (!processCallArgs(CLI, OutVTs, NumBytes))
958 unsigned DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
959 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
960 MachineInstrBuilder MIB =
961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
962 Mips::RA).addReg(Mips::T9);
964 // Add implicit physical register uses to the call.
965 for (auto Reg : CLI.OutRegs)
966 MIB.addReg(Reg, RegState::Implicit);
968 // Add a register mask with the call-preserved registers.
969 // Proper defs for return values will be added by setPhysRegsDeadExcept().
970 MIB.addRegMask(TRI.getCallPreservedMask(CC));
974 // Add implicit physical register uses to the call.
975 for (auto Reg : CLI.OutRegs)
976 MIB.addReg(Reg, RegState::Implicit);
978 // Add a register mask with the call-preserved registers. Proper
979 // defs for return values will be added by setPhysRegsDeadExcept().
980 MIB.addRegMask(TRI.getCallPreservedMask(CC));
983 // Finish off the call including any return values.
984 return finishCall(CLI, RetVT, NumBytes);
987 bool MipsFastISel::selectRet(const Instruction *I) {
988 const Function &F = *I->getParent()->getParent();
989 const ReturnInst *Ret = cast<ReturnInst>(I);
991 if (!FuncInfo.CanLowerReturn)
994 // Build a list of return value registers.
995 SmallVector<unsigned, 4> RetRegs;
997 if (Ret->getNumOperands() > 0) {
998 CallingConv::ID CC = F.getCallingConv();
999 SmallVector<ISD::OutputArg, 4> Outs;
1000 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1001 // Analyze operands of the call, assigning locations to each operand.
1002 SmallVector<CCValAssign, 16> ValLocs;
1003 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1005 CCAssignFn *RetCC = RetCC_Mips;
1006 CCInfo.AnalyzeReturn(Outs, RetCC);
1008 // Only handle a single return value for now.
1009 if (ValLocs.size() != 1)
1012 CCValAssign &VA = ValLocs[0];
1013 const Value *RV = Ret->getOperand(0);
1015 // Don't bother handling odd stuff for now.
1016 if ((VA.getLocInfo() != CCValAssign::Full) &&
1017 (VA.getLocInfo() != CCValAssign::BCvt))
1020 // Only handle register returns for now.
1024 unsigned Reg = getRegForValue(RV);
1028 unsigned SrcReg = Reg + VA.getValNo();
1029 unsigned DestReg = VA.getLocReg();
1030 // Avoid a cross-class copy. This is very unlikely.
1031 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1034 EVT RVEVT = TLI.getValueType(RV->getType());
1035 if (!RVEVT.isSimple())
1038 if (RVEVT.isVector())
1041 MVT RVVT = RVEVT.getSimpleVT();
1042 if (RVVT == MVT::f128)
1045 MVT DestVT = VA.getValVT();
1046 // Special handling for extended integers.
1047 if (RVVT != DestVT) {
1048 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1051 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1054 bool IsZExt = Outs[0].Flags.isZExt();
1055 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1061 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1062 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1064 // Add register to return instruction.
1065 RetRegs.push_back(VA.getLocReg());
1067 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1068 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1069 MIB.addReg(RetRegs[i], RegState::Implicit);
1073 bool MipsFastISel::selectTrunc(const Instruction *I) {
1074 // The high bits for a type smaller than the register size are assumed to be
1076 Value *Op = I->getOperand(0);
1079 SrcVT = TLI.getValueType(Op->getType(), true);
1080 DestVT = TLI.getValueType(I->getType(), true);
1082 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1084 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1087 unsigned SrcReg = getRegForValue(Op);
1091 // Because the high bits are undefined, a truncate doesn't generate
1093 updateValueMap(I, SrcReg);
1096 bool MipsFastISel::selectIntExt(const Instruction *I) {
1097 Type *DestTy = I->getType();
1098 Value *Src = I->getOperand(0);
1099 Type *SrcTy = Src->getType();
1101 bool isZExt = isa<ZExtInst>(I);
1102 unsigned SrcReg = getRegForValue(Src);
1106 EVT SrcEVT, DestEVT;
1107 SrcEVT = TLI.getValueType(SrcTy, true);
1108 DestEVT = TLI.getValueType(DestTy, true);
1109 if (!SrcEVT.isSimple())
1111 if (!DestEVT.isSimple())
1114 MVT SrcVT = SrcEVT.getSimpleVT();
1115 MVT DestVT = DestEVT.getSimpleVT();
1116 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1118 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1120 updateValueMap(I, ResultReg);
1123 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1126 switch (SrcVT.SimpleTy) {
1136 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1137 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1138 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1142 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1144 switch (SrcVT.SimpleTy) {
1148 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1151 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1157 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1159 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1161 if (Subtarget->hasMips32r2())
1162 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1163 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1166 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1168 switch (SrcVT.SimpleTy) {
1172 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(1);
1175 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xff);
1178 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xffff);
1184 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1185 unsigned DestReg, bool IsZExt) {
1187 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1188 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1191 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1193 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1194 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1195 return Success ? DestReg : 0;
1198 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
1199 if (!TargetSupported)
1201 switch (I->getOpcode()) {
1204 case Instruction::Load:
1205 return selectLoad(I);
1206 case Instruction::Store:
1207 return selectStore(I);
1208 case Instruction::Br:
1209 return selectBranch(I);
1210 case Instruction::Ret:
1211 return selectRet(I);
1212 case Instruction::Trunc:
1213 return selectTrunc(I);
1214 case Instruction::ZExt:
1215 case Instruction::SExt:
1216 return selectIntExt(I);
1217 case Instruction::FPTrunc:
1218 return selectFPTrunc(I);
1219 case Instruction::FPExt:
1220 return selectFPExt(I);
1221 case Instruction::FPToSI:
1222 return selectFPToInt(I, /*isSigned*/ true);
1223 case Instruction::FPToUI:
1224 return selectFPToInt(I, /*isSigned*/ false);
1225 case Instruction::ICmp:
1226 case Instruction::FCmp:
1227 return selectCmp(I);
1232 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
1234 unsigned VReg = getRegForValue(V);
1237 MVT VMVT = TLI.getValueType(V->getType(), true).getSimpleVT();
1238 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
1239 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1240 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
1248 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
1249 const TargetLibraryInfo *libInfo) {
1250 return new MipsFastISel(funcInfo, libInfo);