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/GlobalAlias.h"
17 #include "llvm/IR/GlobalVariable.h"
18 #include "llvm/Target/TargetInstrInfo.h"
24 class MipsFastISel final : public FastISel {
26 // All possible address modes.
29 typedef enum { RegBase, FrameIndexBase } BaseKind;
40 const GlobalValue *GV;
43 // Innocuous defaults for our address.
44 Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; }
45 void setKind(BaseKind K) { Kind = K; }
46 BaseKind getKind() const { return Kind; }
47 bool isRegBase() const { return Kind == RegBase; }
48 bool isFIBase() const { return Kind == FrameIndexBase; }
49 void setReg(unsigned Reg) {
50 assert(isRegBase() && "Invalid base register access!");
53 unsigned getReg() const {
54 assert(isRegBase() && "Invalid base register access!");
57 void setFI(unsigned FI) {
58 assert(isFIBase() && "Invalid base frame index access!");
61 unsigned getFI() const {
62 assert(isFIBase() && "Invalid base frame index access!");
66 void setOffset(int64_t Offset_) { Offset = Offset_; }
67 int64_t getOffset() const { return Offset; }
68 void setGlobalValue(const GlobalValue *G) { GV = G; }
69 const GlobalValue *getGlobalValue() { return GV; }
72 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
73 /// make the right decision when generating code for different targets.
74 const TargetMachine &TM;
75 const MipsSubtarget *Subtarget;
76 const TargetInstrInfo &TII;
77 const TargetLowering &TLI;
78 MipsFunctionInfo *MFI;
80 // Convenience variables to avoid some queries.
83 bool fastLowerCall(CallLoweringInfo &CLI) override;
86 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
87 // floating point but not reject doing fast-isel in other
91 // Selection routines.
92 bool selectLoad(const Instruction *I);
93 bool selectStore(const Instruction *I);
94 bool selectBranch(const Instruction *I);
95 bool selectCmp(const Instruction *I);
96 bool selectFPExt(const Instruction *I);
97 bool selectFPTrunc(const Instruction *I);
98 bool selectFPToInt(const Instruction *I, bool IsSigned);
99 bool selectRet(const Instruction *I);
100 bool selectTrunc(const Instruction *I);
101 bool selectIntExt(const Instruction *I);
103 // Utility helper routines.
104 bool isTypeLegal(Type *Ty, MVT &VT);
105 bool isLoadTypeLegal(Type *Ty, MVT &VT);
106 bool computeAddress(const Value *Obj, Address &Addr);
107 bool computeCallAddress(const Value *V, Address &Addr);
108 void simplifyAddress(Address &Addr);
110 // Emit helper routines.
111 bool emitCmp(unsigned DestReg, const CmpInst *CI);
112 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
113 unsigned Alignment = 0);
114 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
115 MachineMemOperand *MMO = nullptr);
116 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
117 unsigned Alignment = 0);
118 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
119 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
122 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
124 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
125 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
127 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
130 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
132 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
133 unsigned materializeGV(const GlobalValue *GV, MVT VT);
134 unsigned materializeInt(const Constant *C, MVT VT);
135 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
137 MachineInstrBuilder emitInst(unsigned Opc) {
138 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
140 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
141 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
144 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
145 unsigned MemReg, int64_t MemOffset) {
146 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
148 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
149 unsigned MemReg, int64_t MemOffset) {
150 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
152 // for some reason, this default is not generated by tablegen
153 // so we explicitly generate it here.
155 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
156 unsigned Op0, bool Op0IsKill, uint64_t imm1,
157 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
161 // Call handling routines.
163 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
164 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
166 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
169 // Backend specific FastISel code.
170 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
171 const TargetLibraryInfo *libInfo)
172 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
173 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
174 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
175 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
176 Context = &funcInfo.Fn->getContext();
178 ((TM.getRelocationModel() == Reloc::PIC_) &&
179 ((Subtarget->hasMips32r2() || Subtarget->hasMips32()) &&
180 (static_cast<const MipsTargetMachine &>(TM).getABI().IsO32())));
181 UnsupportedFPMode = Subtarget->isFP64bit();
184 unsigned fastMaterializeConstant(const Constant *C) override;
185 bool fastSelectInstruction(const Instruction *I) override;
187 #include "MipsGenFastISel.inc"
189 } // end anonymous namespace.
191 static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
192 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
193 CCState &State) LLVM_ATTRIBUTE_UNUSED;
195 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
196 CCValAssign::LocInfo LocInfo,
197 ISD::ArgFlagsTy ArgFlags, CCState &State) {
198 llvm_unreachable("should not be called");
201 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
202 CCValAssign::LocInfo LocInfo,
203 ISD::ArgFlagsTy ArgFlags, CCState &State) {
204 llvm_unreachable("should not be called");
207 #include "MipsGenCallingConv.inc"
209 CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
213 unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
214 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
216 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
217 const ConstantInt *CI = cast<ConstantInt>(C);
219 if ((VT != MVT::i1) && CI->isNegative())
220 Imm = CI->getSExtValue();
222 Imm = CI->getZExtValue();
223 return materialize32BitInt(Imm, RC);
226 unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
227 const TargetRegisterClass *RC) {
228 unsigned ResultReg = createResultReg(RC);
230 if (isInt<16>(Imm)) {
231 unsigned Opc = Mips::ADDiu;
232 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
234 } else if (isUInt<16>(Imm)) {
235 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
238 unsigned Lo = Imm & 0xFFFF;
239 unsigned Hi = (Imm >> 16) & 0xFFFF;
241 // Both Lo and Hi have nonzero bits.
242 unsigned TmpReg = createResultReg(RC);
243 emitInst(Mips::LUi, TmpReg).addImm(Hi);
244 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
246 emitInst(Mips::LUi, ResultReg).addImm(Hi);
251 unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
252 if (UnsupportedFPMode)
254 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
255 if (VT == MVT::f32) {
256 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
257 unsigned DestReg = createResultReg(RC);
258 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
259 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
261 } else if (VT == MVT::f64) {
262 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
263 unsigned DestReg = createResultReg(RC);
264 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
266 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
267 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
273 unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
274 // For now 32-bit only.
277 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
278 unsigned DestReg = createResultReg(RC);
279 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
280 bool IsThreadLocal = GVar && GVar->isThreadLocal();
281 // TLS not supported at this time.
284 emitInst(Mips::LW, DestReg)
285 .addReg(MFI->getGlobalBaseReg())
286 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
287 if ((GV->hasInternalLinkage() ||
288 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
289 unsigned TempReg = createResultReg(RC);
290 emitInst(Mips::ADDiu, TempReg)
292 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
298 // Materialize a constant into a register, and return the register
299 // number (or zero if we failed to handle it).
300 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
301 EVT CEVT = TLI.getValueType(C->getType(), true);
303 // Only handle simple types.
304 if (!CEVT.isSimple())
306 MVT VT = CEVT.getSimpleVT();
308 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
309 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
310 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
311 return materializeGV(GV, VT);
312 else if (isa<ConstantInt>(C))
313 return materializeInt(C, VT);
318 bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
320 const User *U = nullptr;
321 unsigned Opcode = Instruction::UserOp1;
322 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
323 // Don't walk into other basic blocks unless the object is an alloca from
324 // another block, otherwise it may not have a virtual register assigned.
325 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
326 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
327 Opcode = I->getOpcode();
330 } else if (isa<ConstantExpr>(Obj))
335 case Instruction::BitCast: {
336 // Look through bitcasts.
337 return computeAddress(U->getOperand(0), Addr);
339 case Instruction::GetElementPtr: {
340 Address SavedAddr = Addr;
341 uint64_t TmpOffset = Addr.getOffset();
342 // Iterate through the GEP folding the constants into offsets where
344 gep_type_iterator GTI = gep_type_begin(U);
345 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
347 const Value *Op = *i;
348 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
349 const StructLayout *SL = DL.getStructLayout(STy);
350 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
351 TmpOffset += SL->getElementOffset(Idx);
353 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
355 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
356 // Constant-offset addressing.
357 TmpOffset += CI->getSExtValue() * S;
360 if (canFoldAddIntoGEP(U, Op)) {
361 // A compatible add with a constant operand. Fold the constant.
363 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
364 TmpOffset += CI->getSExtValue() * S;
365 // Iterate on the other operand.
366 Op = cast<AddOperator>(Op)->getOperand(0);
370 goto unsupported_gep;
374 // Try to grab the base operand now.
375 Addr.setOffset(TmpOffset);
376 if (computeAddress(U->getOperand(0), Addr))
378 // We failed, restore everything and try the other options.
383 case Instruction::Alloca: {
384 const AllocaInst *AI = cast<AllocaInst>(Obj);
385 DenseMap<const AllocaInst *, int>::iterator SI =
386 FuncInfo.StaticAllocaMap.find(AI);
387 if (SI != FuncInfo.StaticAllocaMap.end()) {
388 Addr.setKind(Address::FrameIndexBase);
389 Addr.setFI(SI->second);
395 Addr.setReg(getRegForValue(Obj));
396 return Addr.getReg() != 0;
399 bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
400 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
401 if (GV && isa<Function>(GV) && dyn_cast<Function>(GV)->isIntrinsic())
405 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
406 Addr.setGlobalValue(GV);
412 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
413 EVT evt = TLI.getValueType(Ty, true);
414 // Only handle simple types.
415 if (evt == MVT::Other || !evt.isSimple())
417 VT = evt.getSimpleVT();
419 // Handle all legal types, i.e. a register that will directly hold this
421 return TLI.isTypeLegal(VT);
424 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
425 if (isTypeLegal(Ty, VT))
427 // We will extend this in a later patch:
428 // If this is a type than can be sign or zero-extended to a basic operation
429 // go ahead and accept it now.
430 if (VT == MVT::i8 || VT == MVT::i16)
434 // Because of how EmitCmp is called with fast-isel, you can
435 // end up with redundant "andi" instructions after the sequences emitted below.
436 // We should try and solve this issue in the future.
438 bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
439 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
440 bool IsUnsigned = CI->isUnsigned();
441 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
444 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
447 CmpInst::Predicate P = CI->getPredicate();
452 case CmpInst::ICMP_EQ: {
453 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
454 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
455 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
458 case CmpInst::ICMP_NE: {
459 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
460 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
461 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
464 case CmpInst::ICMP_UGT: {
465 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
468 case CmpInst::ICMP_ULT: {
469 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
472 case CmpInst::ICMP_UGE: {
473 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
474 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
475 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
478 case CmpInst::ICMP_ULE: {
479 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
480 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
481 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
484 case CmpInst::ICMP_SGT: {
485 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
488 case CmpInst::ICMP_SLT: {
489 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
492 case CmpInst::ICMP_SGE: {
493 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
494 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
495 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
498 case CmpInst::ICMP_SLE: {
499 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
500 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
501 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
504 case CmpInst::FCMP_OEQ:
505 case CmpInst::FCMP_UNE:
506 case CmpInst::FCMP_OLT:
507 case CmpInst::FCMP_OLE:
508 case CmpInst::FCMP_OGT:
509 case CmpInst::FCMP_OGE: {
510 if (UnsupportedFPMode)
512 bool IsFloat = Left->getType()->isFloatTy();
513 bool IsDouble = Left->getType()->isDoubleTy();
514 if (!IsFloat && !IsDouble)
516 unsigned Opc, CondMovOpc;
518 case CmpInst::FCMP_OEQ:
519 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
520 CondMovOpc = Mips::MOVT_I;
522 case CmpInst::FCMP_UNE:
523 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
524 CondMovOpc = Mips::MOVF_I;
526 case CmpInst::FCMP_OLT:
527 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
528 CondMovOpc = Mips::MOVT_I;
530 case CmpInst::FCMP_OLE:
531 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
532 CondMovOpc = Mips::MOVT_I;
534 case CmpInst::FCMP_OGT:
535 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
536 CondMovOpc = Mips::MOVF_I;
538 case CmpInst::FCMP_OGE:
539 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
540 CondMovOpc = Mips::MOVF_I;
543 llvm_unreachable("Only switching of a subset of CCs.");
545 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
546 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
547 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
548 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
549 emitInst(Opc).addReg(LeftReg).addReg(RightReg).addReg(
550 Mips::FCC0, RegState::ImplicitDefine);
551 MachineInstrBuilder MI = emitInst(CondMovOpc, ResultReg)
554 .addReg(RegWithZero, RegState::Implicit);
555 MI->tieOperands(0, 3);
561 bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
562 unsigned Alignment) {
564 // more cases will be handled here in following patches.
567 switch (VT.SimpleTy) {
569 ResultReg = createResultReg(&Mips::GPR32RegClass);
574 ResultReg = createResultReg(&Mips::GPR32RegClass);
579 ResultReg = createResultReg(&Mips::GPR32RegClass);
584 if (UnsupportedFPMode)
586 ResultReg = createResultReg(&Mips::FGR32RegClass);
591 if (UnsupportedFPMode)
593 ResultReg = createResultReg(&Mips::AFGR64RegClass);
600 if (Addr.isRegBase()) {
601 simplifyAddress(Addr);
602 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
605 if (Addr.isFIBase()) {
606 unsigned FI = Addr.getFI();
608 unsigned Offset = Addr.getOffset();
609 MachineFrameInfo &MFI = *MF->getFrameInfo();
610 MachineMemOperand *MMO = MF->getMachineMemOperand(
611 MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
612 MFI.getObjectSize(FI), Align);
613 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
622 bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
623 unsigned Alignment) {
625 // more cases will be handled here in following patches.
628 switch (VT.SimpleTy) {
639 if (UnsupportedFPMode)
644 if (UnsupportedFPMode)
651 if (Addr.isRegBase()) {
652 simplifyAddress(Addr);
653 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
656 if (Addr.isFIBase()) {
657 unsigned FI = Addr.getFI();
659 unsigned Offset = Addr.getOffset();
660 MachineFrameInfo &MFI = *MF->getFrameInfo();
661 MachineMemOperand *MMO = MF->getMachineMemOperand(
662 MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
663 MFI.getObjectSize(FI), Align);
664 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
674 bool MipsFastISel::selectLoad(const Instruction *I) {
675 // Atomic loads need special handling.
676 if (cast<LoadInst>(I)->isAtomic())
679 // Verify we have a legal type before going any further.
681 if (!isLoadTypeLegal(I->getType(), VT))
684 // See if we can handle this address.
686 if (!computeAddress(I->getOperand(0), Addr))
690 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
692 updateValueMap(I, ResultReg);
696 bool MipsFastISel::selectStore(const Instruction *I) {
697 Value *Op0 = I->getOperand(0);
700 // Atomic stores need special handling.
701 if (cast<StoreInst>(I)->isAtomic())
704 // Verify we have a legal type before going any further.
706 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
709 // Get the value to be stored into a register.
710 SrcReg = getRegForValue(Op0);
714 // See if we can handle this address.
716 if (!computeAddress(I->getOperand(1), Addr))
719 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
725 // This can cause a redundant sltiu to be generated.
726 // FIXME: try and eliminate this in a future patch.
728 bool MipsFastISel::selectBranch(const Instruction *I) {
729 const BranchInst *BI = cast<BranchInst>(I);
730 MachineBasicBlock *BrBB = FuncInfo.MBB;
732 // TBB is the basic block for the case where the comparison is true.
733 // FBB is the basic block for the case where the comparison is false.
734 // if (cond) goto TBB
738 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
739 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
741 // For now, just try the simplest case where it's fed by a compare.
742 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
743 unsigned CondReg = createResultReg(&Mips::GPR32RegClass);
744 if (!emitCmp(CondReg, CI))
746 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
749 fastEmitBranch(FBB, DbgLoc);
750 FuncInfo.MBB->addSuccessor(TBB);
756 bool MipsFastISel::selectCmp(const Instruction *I) {
757 const CmpInst *CI = cast<CmpInst>(I);
758 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
759 if (!emitCmp(ResultReg, CI))
761 updateValueMap(I, ResultReg);
765 // Attempt to fast-select a floating-point extend instruction.
766 bool MipsFastISel::selectFPExt(const Instruction *I) {
767 if (UnsupportedFPMode)
769 Value *Src = I->getOperand(0);
770 EVT SrcVT = TLI.getValueType(Src->getType(), true);
771 EVT DestVT = TLI.getValueType(I->getType(), true);
773 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
777 getRegForValue(Src); // his must be a 32 bit floating point register class
778 // maybe we should handle this differently
782 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
783 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
784 updateValueMap(I, DestReg);
788 // Attempt to fast-select a floating-point truncate instruction.
789 bool MipsFastISel::selectFPTrunc(const Instruction *I) {
790 if (UnsupportedFPMode)
792 Value *Src = I->getOperand(0);
793 EVT SrcVT = TLI.getValueType(Src->getType(), true);
794 EVT DestVT = TLI.getValueType(I->getType(), true);
796 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
799 unsigned SrcReg = getRegForValue(Src);
803 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
807 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
808 updateValueMap(I, DestReg);
812 // Attempt to fast-select a floating-point-to-integer conversion.
813 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
814 if (UnsupportedFPMode)
818 return false; // We don't handle this case yet. There is no native
819 // instruction for this but it can be synthesized.
820 Type *DstTy = I->getType();
821 if (!isTypeLegal(DstTy, DstVT))
824 if (DstVT != MVT::i32)
827 Value *Src = I->getOperand(0);
828 Type *SrcTy = Src->getType();
829 if (!isTypeLegal(SrcTy, SrcVT))
832 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
835 unsigned SrcReg = getRegForValue(Src);
839 // Determine the opcode for the conversion, which takes place
840 // entirely within FPRs.
841 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
842 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
845 if (SrcVT == MVT::f32)
846 Opc = Mips::TRUNC_W_S;
848 Opc = Mips::TRUNC_W_D32;
850 // Generate the convert.
851 emitInst(Opc, TempReg).addReg(SrcReg);
853 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
855 updateValueMap(I, DestReg);
859 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
860 SmallVectorImpl<MVT> &OutVTs,
861 unsigned &NumBytes) {
862 CallingConv::ID CC = CLI.CallConv;
863 SmallVector<CCValAssign, 16> ArgLocs;
864 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
865 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
866 // Get a count of how many bytes are to be pushed on the stack.
867 NumBytes = CCInfo.getNextStackOffset();
868 // This is the minimum argument area used for A0-A3.
872 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16);
875 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
876 CCValAssign &VA = ArgLocs[i];
877 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
878 MVT ArgVT = OutVTs[VA.getValNo()];
882 if (ArgVT == MVT::f32) {
883 VA.convertToReg(Mips::F12);
884 } else if (ArgVT == MVT::f64) {
885 VA.convertToReg(Mips::D6);
888 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
889 if (ArgVT == MVT::f32) {
890 VA.convertToReg(Mips::F14);
891 } else if (ArgVT == MVT::f64) {
892 VA.convertToReg(Mips::D7);
896 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32)) && VA.isMemLoc()) {
897 switch (VA.getLocMemOffset()) {
899 VA.convertToReg(Mips::A0);
902 VA.convertToReg(Mips::A1);
905 VA.convertToReg(Mips::A2);
908 VA.convertToReg(Mips::A3);
914 unsigned ArgReg = getRegForValue(ArgVal);
918 // Handle arg promotion: SExt, ZExt, AExt.
919 switch (VA.getLocInfo()) {
920 case CCValAssign::Full:
922 case CCValAssign::AExt:
923 case CCValAssign::SExt: {
924 MVT DestVT = VA.getLocVT();
926 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
931 case CCValAssign::ZExt: {
932 MVT DestVT = VA.getLocVT();
934 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
940 llvm_unreachable("Unknown arg promotion!");
943 // Now copy/store arg to correct locations.
944 if (VA.isRegLoc() && !VA.needsCustom()) {
945 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
946 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
947 CLI.OutRegs.push_back(VA.getLocReg());
948 } else if (VA.needsCustom()) {
949 llvm_unreachable("Mips does not use custom args.");
953 // FIXME: This path will currently return false. It was copied
954 // from the AArch64 port and should be essentially fine for Mips too.
955 // The work to finish up this path will be done in a follow-on patch.
957 assert(VA.isMemLoc() && "Assuming store on stack.");
958 // Don't emit stores for undef values.
959 if (isa<UndefValue>(ArgVal))
962 // Need to store on the stack.
963 // FIXME: This alignment is incorrect but this path is disabled
964 // for now (will return false). We need to determine the right alignment
965 // based on the normal alignment for the underlying machine type.
967 unsigned ArgSize = RoundUpToAlignment(ArgVT.getSizeInBits(), 4);
969 unsigned BEAlign = 0;
970 if (ArgSize < 8 && !Subtarget->isLittle())
971 BEAlign = 8 - ArgSize;
974 Addr.setKind(Address::RegBase);
975 Addr.setReg(Mips::SP);
976 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
978 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
979 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
980 MachinePointerInfo::getStack(Addr.getOffset()),
981 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
983 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
984 return false; // can't store on the stack yet.
991 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
993 CallingConv::ID CC = CLI.CallConv;
994 emitInst(Mips::ADJCALLSTACKUP).addImm(16);
995 if (RetVT != MVT::isVoid) {
996 SmallVector<CCValAssign, 16> RVLocs;
997 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
998 CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips);
1000 // Only handle a single return value.
1001 if (RVLocs.size() != 1)
1003 // Copy all of the result registers out of their specified physreg.
1004 MVT CopyVT = RVLocs[0].getValVT();
1005 // Special handling for extended integers.
1006 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1009 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1010 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1011 TII.get(TargetOpcode::COPY),
1012 ResultReg).addReg(RVLocs[0].getLocReg());
1013 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1015 CLI.ResultReg = ResultReg;
1016 CLI.NumResultRegs = 1;
1021 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1022 CallingConv::ID CC = CLI.CallConv;
1023 bool IsTailCall = CLI.IsTailCall;
1024 bool IsVarArg = CLI.IsVarArg;
1025 const Value *Callee = CLI.Callee;
1026 // const char *SymName = CLI.SymName;
1028 // Allow SelectionDAG isel to handle tail calls.
1032 // Let SDISel handle vararg functions.
1036 // FIXME: Only handle *simple* calls for now.
1038 if (CLI.RetTy->isVoidTy())
1039 RetVT = MVT::isVoid;
1040 else if (!isTypeLegal(CLI.RetTy, RetVT))
1043 for (auto Flag : CLI.OutFlags)
1044 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1047 // Set up the argument vectors.
1048 SmallVector<MVT, 16> OutVTs;
1049 OutVTs.reserve(CLI.OutVals.size());
1051 for (auto *Val : CLI.OutVals) {
1053 if (!isTypeLegal(Val->getType(), VT) &&
1054 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1057 // We don't handle vector parameters yet.
1058 if (VT.isVector() || VT.getSizeInBits() > 64)
1061 OutVTs.push_back(VT);
1065 if (!computeCallAddress(Callee, Addr))
1068 // Handle the arguments now that we've gotten them.
1070 if (!processCallArgs(CLI, OutVTs, NumBytes))
1074 unsigned DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
1075 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1076 MachineInstrBuilder MIB =
1077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1078 Mips::RA).addReg(Mips::T9);
1080 // Add implicit physical register uses to the call.
1081 for (auto Reg : CLI.OutRegs)
1082 MIB.addReg(Reg, RegState::Implicit);
1084 // Add a register mask with the call-preserved registers.
1085 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1086 MIB.addRegMask(TRI.getCallPreservedMask(CC));
1090 // Finish off the call including any return values.
1091 return finishCall(CLI, RetVT, NumBytes);
1094 bool MipsFastISel::selectRet(const Instruction *I) {
1095 const Function &F = *I->getParent()->getParent();
1096 const ReturnInst *Ret = cast<ReturnInst>(I);
1098 if (!FuncInfo.CanLowerReturn)
1101 // Build a list of return value registers.
1102 SmallVector<unsigned, 4> RetRegs;
1104 if (Ret->getNumOperands() > 0) {
1105 CallingConv::ID CC = F.getCallingConv();
1106 SmallVector<ISD::OutputArg, 4> Outs;
1107 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1108 // Analyze operands of the call, assigning locations to each operand.
1109 SmallVector<CCValAssign, 16> ValLocs;
1110 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1112 CCAssignFn *RetCC = RetCC_Mips;
1113 CCInfo.AnalyzeReturn(Outs, RetCC);
1115 // Only handle a single return value for now.
1116 if (ValLocs.size() != 1)
1119 CCValAssign &VA = ValLocs[0];
1120 const Value *RV = Ret->getOperand(0);
1122 // Don't bother handling odd stuff for now.
1123 if ((VA.getLocInfo() != CCValAssign::Full) &&
1124 (VA.getLocInfo() != CCValAssign::BCvt))
1127 // Only handle register returns for now.
1131 unsigned Reg = getRegForValue(RV);
1135 unsigned SrcReg = Reg + VA.getValNo();
1136 unsigned DestReg = VA.getLocReg();
1137 // Avoid a cross-class copy. This is very unlikely.
1138 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1141 EVT RVEVT = TLI.getValueType(RV->getType());
1142 if (!RVEVT.isSimple())
1145 if (RVEVT.isVector())
1148 MVT RVVT = RVEVT.getSimpleVT();
1149 if (RVVT == MVT::f128)
1152 MVT DestVT = VA.getValVT();
1153 // Special handling for extended integers.
1154 if (RVVT != DestVT) {
1155 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1158 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1161 bool IsZExt = Outs[0].Flags.isZExt();
1162 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1168 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1169 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1171 // Add register to return instruction.
1172 RetRegs.push_back(VA.getLocReg());
1174 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1175 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1176 MIB.addReg(RetRegs[i], RegState::Implicit);
1180 bool MipsFastISel::selectTrunc(const Instruction *I) {
1181 // The high bits for a type smaller than the register size are assumed to be
1183 Value *Op = I->getOperand(0);
1186 SrcVT = TLI.getValueType(Op->getType(), true);
1187 DestVT = TLI.getValueType(I->getType(), true);
1189 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1191 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1194 unsigned SrcReg = getRegForValue(Op);
1198 // Because the high bits are undefined, a truncate doesn't generate
1200 updateValueMap(I, SrcReg);
1203 bool MipsFastISel::selectIntExt(const Instruction *I) {
1204 Type *DestTy = I->getType();
1205 Value *Src = I->getOperand(0);
1206 Type *SrcTy = Src->getType();
1208 bool isZExt = isa<ZExtInst>(I);
1209 unsigned SrcReg = getRegForValue(Src);
1213 EVT SrcEVT, DestEVT;
1214 SrcEVT = TLI.getValueType(SrcTy, true);
1215 DestEVT = TLI.getValueType(DestTy, true);
1216 if (!SrcEVT.isSimple())
1218 if (!DestEVT.isSimple())
1221 MVT SrcVT = SrcEVT.getSimpleVT();
1222 MVT DestVT = DestEVT.getSimpleVT();
1223 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1225 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1227 updateValueMap(I, ResultReg);
1230 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1233 switch (SrcVT.SimpleTy) {
1243 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1244 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1245 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1249 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1251 switch (SrcVT.SimpleTy) {
1255 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1258 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1264 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1266 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1268 if (Subtarget->hasMips32r2())
1269 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1270 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1273 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1275 switch (SrcVT.SimpleTy) {
1279 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(1);
1282 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xff);
1285 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xffff);
1291 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1292 unsigned DestReg, bool IsZExt) {
1294 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1295 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1298 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1300 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1301 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1302 return Success ? DestReg : 0;
1305 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
1306 if (!TargetSupported)
1308 switch (I->getOpcode()) {
1311 case Instruction::Load:
1312 return selectLoad(I);
1313 case Instruction::Store:
1314 return selectStore(I);
1315 case Instruction::Br:
1316 return selectBranch(I);
1317 case Instruction::Ret:
1318 return selectRet(I);
1319 case Instruction::Trunc:
1320 return selectTrunc(I);
1321 case Instruction::ZExt:
1322 case Instruction::SExt:
1323 return selectIntExt(I);
1324 case Instruction::FPTrunc:
1325 return selectFPTrunc(I);
1326 case Instruction::FPExt:
1327 return selectFPExt(I);
1328 case Instruction::FPToSI:
1329 return selectFPToInt(I, /*isSigned*/ true);
1330 case Instruction::FPToUI:
1331 return selectFPToInt(I, /*isSigned*/ false);
1332 case Instruction::ICmp:
1333 case Instruction::FCmp:
1334 return selectCmp(I);
1339 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
1341 unsigned VReg = getRegForValue(V);
1344 MVT VMVT = TLI.getValueType(V->getType(), true).getSimpleVT();
1345 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
1346 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1347 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
1354 void MipsFastISel::simplifyAddress(Address &Addr) {
1355 if (!isInt<16>(Addr.getOffset())) {
1357 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
1358 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1359 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
1360 Addr.setReg(DestReg);
1366 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
1367 const TargetLibraryInfo *libInfo) {
1368 return new MipsFastISel(funcInfo, libInfo);