1 //===- MipsFastISel.cpp - Mips FastISel implementation --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file defines the MIPS-specific support for the FastISel class.
12 /// Some of the target-specific code is generated by tablegen in the file
13 /// MipsGenFastISel.inc, which is #included here.
15 //===----------------------------------------------------------------------===//
17 #include "MCTargetDesc/MipsABIInfo.h"
18 #include "MCTargetDesc/MipsBaseInfo.h"
19 #include "MipsCCState.h"
20 #include "MipsISelLowering.h"
21 #include "MipsInstrInfo.h"
22 #include "MipsMachineFunction.h"
23 #include "MipsSubtarget.h"
24 #include "MipsTargetMachine.h"
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h"
30 #include "llvm/CodeGen/CallingConvLower.h"
31 #include "llvm/CodeGen/FastISel.h"
32 #include "llvm/CodeGen/FunctionLoweringInfo.h"
33 #include "llvm/CodeGen/ISDOpcodes.h"
34 #include "llvm/CodeGen/MachineBasicBlock.h"
35 #include "llvm/CodeGen/MachineFrameInfo.h"
36 #include "llvm/CodeGen/MachineInstrBuilder.h"
37 #include "llvm/CodeGen/MachineMemOperand.h"
38 #include "llvm/CodeGen/MachineRegisterInfo.h"
39 #include "llvm/CodeGen/TargetInstrInfo.h"
40 #include "llvm/CodeGen/TargetLowering.h"
41 #include "llvm/CodeGen/ValueTypes.h"
42 #include "llvm/IR/Attributes.h"
43 #include "llvm/IR/CallingConv.h"
44 #include "llvm/IR/Constant.h"
45 #include "llvm/IR/Constants.h"
46 #include "llvm/IR/DataLayout.h"
47 #include "llvm/IR/Function.h"
48 #include "llvm/IR/GetElementPtrTypeIterator.h"
49 #include "llvm/IR/GlobalValue.h"
50 #include "llvm/IR/GlobalVariable.h"
51 #include "llvm/IR/InstrTypes.h"
52 #include "llvm/IR/Instruction.h"
53 #include "llvm/IR/Instructions.h"
54 #include "llvm/IR/IntrinsicInst.h"
55 #include "llvm/IR/Operator.h"
56 #include "llvm/IR/Type.h"
57 #include "llvm/IR/User.h"
58 #include "llvm/IR/Value.h"
59 #include "llvm/MC/MCContext.h"
60 #include "llvm/MC/MCInstrDesc.h"
61 #include "llvm/MC/MCRegisterInfo.h"
62 #include "llvm/MC/MCSymbol.h"
63 #include "llvm/Support/Casting.h"
64 #include "llvm/Support/Compiler.h"
65 #include "llvm/Support/Debug.h"
66 #include "llvm/Support/ErrorHandling.h"
67 #include "llvm/Support/MachineValueType.h"
68 #include "llvm/Support/MathExtras.h"
69 #include "llvm/Support/raw_ostream.h"
75 #define DEBUG_TYPE "mips-fastisel"
79 extern cl::opt<bool> EmitJalrReloc;
83 class MipsFastISel final : public FastISel {
85 // All possible address modes.
88 using BaseKind = enum { RegBase, FrameIndexBase };
91 BaseKind Kind = RegBase;
99 const GlobalValue *GV = nullptr;
102 // Innocuous defaults for our address.
103 Address() { Base.Reg = 0; }
105 void setKind(BaseKind K) { Kind = K; }
106 BaseKind getKind() const { return Kind; }
107 bool isRegBase() const { return Kind == RegBase; }
108 bool isFIBase() const { return Kind == FrameIndexBase; }
110 void setReg(unsigned Reg) {
111 assert(isRegBase() && "Invalid base register access!");
115 unsigned getReg() const {
116 assert(isRegBase() && "Invalid base register access!");
120 void setFI(unsigned FI) {
121 assert(isFIBase() && "Invalid base frame index access!");
125 unsigned getFI() const {
126 assert(isFIBase() && "Invalid base frame index access!");
130 void setOffset(int64_t Offset_) { Offset = Offset_; }
131 int64_t getOffset() const { return Offset; }
132 void setGlobalValue(const GlobalValue *G) { GV = G; }
133 const GlobalValue *getGlobalValue() { return GV; }
136 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
137 /// make the right decision when generating code for different targets.
138 const TargetMachine &TM;
139 const MipsSubtarget *Subtarget;
140 const TargetInstrInfo &TII;
141 const TargetLowering &TLI;
142 MipsFunctionInfo *MFI;
144 // Convenience variables to avoid some queries.
145 LLVMContext *Context;
147 bool fastLowerArguments() override;
148 bool fastLowerCall(CallLoweringInfo &CLI) override;
149 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
151 bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
152 // floating point but not reject doing fast-isel in other
156 // Selection routines.
157 bool selectLogicalOp(const Instruction *I);
158 bool selectLoad(const Instruction *I);
159 bool selectStore(const Instruction *I);
160 bool selectBranch(const Instruction *I);
161 bool selectSelect(const Instruction *I);
162 bool selectCmp(const Instruction *I);
163 bool selectFPExt(const Instruction *I);
164 bool selectFPTrunc(const Instruction *I);
165 bool selectFPToInt(const Instruction *I, bool IsSigned);
166 bool selectRet(const Instruction *I);
167 bool selectTrunc(const Instruction *I);
168 bool selectIntExt(const Instruction *I);
169 bool selectShift(const Instruction *I);
170 bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
172 // Utility helper routines.
173 bool isTypeLegal(Type *Ty, MVT &VT);
174 bool isTypeSupported(Type *Ty, MVT &VT);
175 bool isLoadTypeLegal(Type *Ty, MVT &VT);
176 bool computeAddress(const Value *Obj, Address &Addr);
177 bool computeCallAddress(const Value *V, Address &Addr);
178 void simplifyAddress(Address &Addr);
180 // Emit helper routines.
181 bool emitCmp(unsigned DestReg, const CmpInst *CI);
182 bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
183 unsigned Alignment = 0);
184 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
185 MachineMemOperand *MMO = nullptr);
186 bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
187 unsigned Alignment = 0);
188 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
189 bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
192 bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
194 bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
195 bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
197 bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
200 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
202 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
205 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
206 unsigned materializeGV(const GlobalValue *GV, MVT VT);
207 unsigned materializeInt(const Constant *C, MVT VT);
208 unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
209 unsigned materializeExternalCallSym(MCSymbol *Syn);
211 MachineInstrBuilder emitInst(unsigned Opc) {
212 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
215 MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
216 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
220 MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
221 unsigned MemReg, int64_t MemOffset) {
222 return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
225 MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
226 unsigned MemReg, int64_t MemOffset) {
227 return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
230 unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
231 const TargetRegisterClass *RC,
232 unsigned Op0, bool Op0IsKill,
233 unsigned Op1, bool Op1IsKill);
235 // for some reason, this default is not generated by tablegen
236 // so we explicitly generate it here.
237 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
238 unsigned Op0, bool Op0IsKill, uint64_t imm1,
239 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
243 // Call handling routines.
245 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
246 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
248 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
250 const MipsABIInfo &getABI() const {
251 return static_cast<const MipsTargetMachine &>(TM).getABI();
255 // Backend specific FastISel code.
256 explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
257 const TargetLibraryInfo *libInfo)
258 : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
259 Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
260 TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
261 MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
262 Context = &funcInfo.Fn->getContext();
263 UnsupportedFPMode = Subtarget->isFP64bit() || Subtarget->useSoftFloat();
266 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
267 unsigned fastMaterializeConstant(const Constant *C) override;
268 bool fastSelectInstruction(const Instruction *I) override;
270 #include "MipsGenFastISel.inc"
273 } // end anonymous namespace
275 static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
276 CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
277 CCState &State) LLVM_ATTRIBUTE_UNUSED;
279 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
280 CCValAssign::LocInfo LocInfo,
281 ISD::ArgFlagsTy ArgFlags, CCState &State) {
282 llvm_unreachable("should not be called");
285 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
286 CCValAssign::LocInfo LocInfo,
287 ISD::ArgFlagsTy ArgFlags, CCState &State) {
288 llvm_unreachable("should not be called");
291 #include "MipsGenCallingConv.inc"
293 CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
297 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
298 const Value *LHS, const Value *RHS) {
299 // Canonicalize immediates to the RHS first.
300 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
315 llvm_unreachable("unexpected opcode");
318 unsigned LHSReg = getRegForValue(LHS);
323 if (const auto *C = dyn_cast<ConstantInt>(RHS))
324 RHSReg = materializeInt(C, MVT::i32);
326 RHSReg = getRegForValue(RHS);
330 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
334 emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
338 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
339 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i32 &&
340 "Alloca should always return a pointer.");
342 DenseMap<const AllocaInst *, int>::iterator SI =
343 FuncInfo.StaticAllocaMap.find(AI);
345 if (SI != FuncInfo.StaticAllocaMap.end()) {
346 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
349 .addFrameIndex(SI->second)
357 unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
358 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
360 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
361 const ConstantInt *CI = cast<ConstantInt>(C);
362 return materialize32BitInt(CI->getZExtValue(), RC);
365 unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
366 const TargetRegisterClass *RC) {
367 unsigned ResultReg = createResultReg(RC);
369 if (isInt<16>(Imm)) {
370 unsigned Opc = Mips::ADDiu;
371 emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
373 } else if (isUInt<16>(Imm)) {
374 emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
377 unsigned Lo = Imm & 0xFFFF;
378 unsigned Hi = (Imm >> 16) & 0xFFFF;
380 // Both Lo and Hi have nonzero bits.
381 unsigned TmpReg = createResultReg(RC);
382 emitInst(Mips::LUi, TmpReg).addImm(Hi);
383 emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
385 emitInst(Mips::LUi, ResultReg).addImm(Hi);
390 unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
391 if (UnsupportedFPMode)
393 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
394 if (VT == MVT::f32) {
395 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
396 unsigned DestReg = createResultReg(RC);
397 unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
398 emitInst(Mips::MTC1, DestReg).addReg(TempReg);
400 } else if (VT == MVT::f64) {
401 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
402 unsigned DestReg = createResultReg(RC);
403 unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
405 materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
406 emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
412 unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
413 // For now 32-bit only.
416 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
417 unsigned DestReg = createResultReg(RC);
418 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
419 bool IsThreadLocal = GVar && GVar->isThreadLocal();
420 // TLS not supported at this time.
423 emitInst(Mips::LW, DestReg)
424 .addReg(MFI->getGlobalBaseReg())
425 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
426 if ((GV->hasInternalLinkage() ||
427 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
428 unsigned TempReg = createResultReg(RC);
429 emitInst(Mips::ADDiu, TempReg)
431 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
437 unsigned MipsFastISel::materializeExternalCallSym(MCSymbol *Sym) {
438 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
439 unsigned DestReg = createResultReg(RC);
440 emitInst(Mips::LW, DestReg)
441 .addReg(MFI->getGlobalBaseReg())
442 .addSym(Sym, MipsII::MO_GOT);
446 // Materialize a constant into a register, and return the register
447 // number (or zero if we failed to handle it).
448 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
449 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
451 // Only handle simple types.
452 if (!CEVT.isSimple())
454 MVT VT = CEVT.getSimpleVT();
456 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
457 return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
458 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
459 return materializeGV(GV, VT);
460 else if (isa<ConstantInt>(C))
461 return materializeInt(C, VT);
466 bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
467 const User *U = nullptr;
468 unsigned Opcode = Instruction::UserOp1;
469 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
470 // Don't walk into other basic blocks unless the object is an alloca from
471 // another block, otherwise it may not have a virtual register assigned.
472 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
473 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
474 Opcode = I->getOpcode();
477 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
478 Opcode = C->getOpcode();
484 case Instruction::BitCast:
485 // Look through bitcasts.
486 return computeAddress(U->getOperand(0), Addr);
487 case Instruction::GetElementPtr: {
488 Address SavedAddr = Addr;
489 int64_t TmpOffset = Addr.getOffset();
490 // Iterate through the GEP folding the constants into offsets where
492 gep_type_iterator GTI = gep_type_begin(U);
493 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
495 const Value *Op = *i;
496 if (StructType *STy = GTI.getStructTypeOrNull()) {
497 const StructLayout *SL = DL.getStructLayout(STy);
498 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
499 TmpOffset += SL->getElementOffset(Idx);
501 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
503 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
504 // Constant-offset addressing.
505 TmpOffset += CI->getSExtValue() * S;
508 if (canFoldAddIntoGEP(U, Op)) {
509 // A compatible add with a constant operand. Fold the constant.
511 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
512 TmpOffset += CI->getSExtValue() * S;
513 // Iterate on the other operand.
514 Op = cast<AddOperator>(Op)->getOperand(0);
518 goto unsupported_gep;
522 // Try to grab the base operand now.
523 Addr.setOffset(TmpOffset);
524 if (computeAddress(U->getOperand(0), Addr))
526 // We failed, restore everything and try the other options.
531 case Instruction::Alloca: {
532 const AllocaInst *AI = cast<AllocaInst>(Obj);
533 DenseMap<const AllocaInst *, int>::iterator SI =
534 FuncInfo.StaticAllocaMap.find(AI);
535 if (SI != FuncInfo.StaticAllocaMap.end()) {
536 Addr.setKind(Address::FrameIndexBase);
537 Addr.setFI(SI->second);
543 Addr.setReg(getRegForValue(Obj));
544 return Addr.getReg() != 0;
547 bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
548 const User *U = nullptr;
549 unsigned Opcode = Instruction::UserOp1;
551 if (const auto *I = dyn_cast<Instruction>(V)) {
552 // Check if the value is defined in the same basic block. This information
553 // is crucial to know whether or not folding an operand is valid.
554 if (I->getParent() == FuncInfo.MBB->getBasicBlock()) {
555 Opcode = I->getOpcode();
558 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
559 Opcode = C->getOpcode();
566 case Instruction::BitCast:
567 // Look past bitcasts if its operand is in the same BB.
568 return computeCallAddress(U->getOperand(0), Addr);
570 case Instruction::IntToPtr:
571 // Look past no-op inttoptrs if its operand is in the same BB.
572 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
573 TLI.getPointerTy(DL))
574 return computeCallAddress(U->getOperand(0), Addr);
576 case Instruction::PtrToInt:
577 // Look past no-op ptrtoints if its operand is in the same BB.
578 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
579 return computeCallAddress(U->getOperand(0), Addr);
583 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
584 Addr.setGlobalValue(GV);
588 // If all else fails, try to materialize the value in a register.
589 if (!Addr.getGlobalValue()) {
590 Addr.setReg(getRegForValue(V));
591 return Addr.getReg() != 0;
597 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
598 EVT evt = TLI.getValueType(DL, Ty, true);
599 // Only handle simple types.
600 if (evt == MVT::Other || !evt.isSimple())
602 VT = evt.getSimpleVT();
604 // Handle all legal types, i.e. a register that will directly hold this
606 return TLI.isTypeLegal(VT);
609 bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
610 if (Ty->isVectorTy())
613 if (isTypeLegal(Ty, VT))
616 // If this is a type than can be sign or zero-extended to a basic operation
617 // go ahead and accept it now.
618 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
624 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
625 if (isTypeLegal(Ty, VT))
627 // We will extend this in a later patch:
628 // If this is a type than can be sign or zero-extended to a basic operation
629 // go ahead and accept it now.
630 if (VT == MVT::i8 || VT == MVT::i16)
635 // Because of how EmitCmp is called with fast-isel, you can
636 // end up with redundant "andi" instructions after the sequences emitted below.
637 // We should try and solve this issue in the future.
639 bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
640 const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
641 bool IsUnsigned = CI->isUnsigned();
642 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
645 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
648 CmpInst::Predicate P = CI->getPredicate();
653 case CmpInst::ICMP_EQ: {
654 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
655 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
656 emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
659 case CmpInst::ICMP_NE: {
660 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
661 emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
662 emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
665 case CmpInst::ICMP_UGT:
666 emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
668 case CmpInst::ICMP_ULT:
669 emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
671 case CmpInst::ICMP_UGE: {
672 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
673 emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
674 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
677 case CmpInst::ICMP_ULE: {
678 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
679 emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
680 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
683 case CmpInst::ICMP_SGT:
684 emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
686 case CmpInst::ICMP_SLT:
687 emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
689 case CmpInst::ICMP_SGE: {
690 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
691 emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
692 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
695 case CmpInst::ICMP_SLE: {
696 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
697 emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
698 emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
701 case CmpInst::FCMP_OEQ:
702 case CmpInst::FCMP_UNE:
703 case CmpInst::FCMP_OLT:
704 case CmpInst::FCMP_OLE:
705 case CmpInst::FCMP_OGT:
706 case CmpInst::FCMP_OGE: {
707 if (UnsupportedFPMode)
709 bool IsFloat = Left->getType()->isFloatTy();
710 bool IsDouble = Left->getType()->isDoubleTy();
711 if (!IsFloat && !IsDouble)
713 unsigned Opc, CondMovOpc;
715 case CmpInst::FCMP_OEQ:
716 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
717 CondMovOpc = Mips::MOVT_I;
719 case CmpInst::FCMP_UNE:
720 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
721 CondMovOpc = Mips::MOVF_I;
723 case CmpInst::FCMP_OLT:
724 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
725 CondMovOpc = Mips::MOVT_I;
727 case CmpInst::FCMP_OLE:
728 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
729 CondMovOpc = Mips::MOVT_I;
731 case CmpInst::FCMP_OGT:
732 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
733 CondMovOpc = Mips::MOVF_I;
735 case CmpInst::FCMP_OGE:
736 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
737 CondMovOpc = Mips::MOVF_I;
740 llvm_unreachable("Only switching of a subset of CCs.");
742 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
743 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
744 emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
745 emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
746 emitInst(Opc).addReg(Mips::FCC0, RegState::Define).addReg(LeftReg)
748 emitInst(CondMovOpc, ResultReg)
751 .addReg(RegWithZero);
758 bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
759 unsigned Alignment) {
761 // more cases will be handled here in following patches.
764 switch (VT.SimpleTy) {
766 ResultReg = createResultReg(&Mips::GPR32RegClass);
770 ResultReg = createResultReg(&Mips::GPR32RegClass);
774 ResultReg = createResultReg(&Mips::GPR32RegClass);
778 if (UnsupportedFPMode)
780 ResultReg = createResultReg(&Mips::FGR32RegClass);
784 if (UnsupportedFPMode)
786 ResultReg = createResultReg(&Mips::AFGR64RegClass);
792 if (Addr.isRegBase()) {
793 simplifyAddress(Addr);
794 emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
797 if (Addr.isFIBase()) {
798 unsigned FI = Addr.getFI();
800 int64_t Offset = Addr.getOffset();
801 MachineFrameInfo &MFI = MF->getFrameInfo();
802 MachineMemOperand *MMO = MF->getMachineMemOperand(
803 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
804 MFI.getObjectSize(FI), Align);
805 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
814 bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
815 unsigned Alignment) {
817 // more cases will be handled here in following patches.
820 switch (VT.SimpleTy) {
831 if (UnsupportedFPMode)
836 if (UnsupportedFPMode)
843 if (Addr.isRegBase()) {
844 simplifyAddress(Addr);
845 emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
848 if (Addr.isFIBase()) {
849 unsigned FI = Addr.getFI();
851 int64_t Offset = Addr.getOffset();
852 MachineFrameInfo &MFI = MF->getFrameInfo();
853 MachineMemOperand *MMO = MF->getMachineMemOperand(
854 MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
855 MFI.getObjectSize(FI), Align);
856 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
866 bool MipsFastISel::selectLogicalOp(const Instruction *I) {
868 if (!isTypeSupported(I->getType(), VT))
872 switch (I->getOpcode()) {
874 llvm_unreachable("Unexpected instruction.");
875 case Instruction::And:
876 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
878 case Instruction::Or:
879 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
881 case Instruction::Xor:
882 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
889 updateValueMap(I, ResultReg);
893 bool MipsFastISel::selectLoad(const Instruction *I) {
894 // Atomic loads need special handling.
895 if (cast<LoadInst>(I)->isAtomic())
898 // Verify we have a legal type before going any further.
900 if (!isLoadTypeLegal(I->getType(), VT))
903 // See if we can handle this address.
905 if (!computeAddress(I->getOperand(0), Addr))
909 if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
911 updateValueMap(I, ResultReg);
915 bool MipsFastISel::selectStore(const Instruction *I) {
916 Value *Op0 = I->getOperand(0);
919 // Atomic stores need special handling.
920 if (cast<StoreInst>(I)->isAtomic())
923 // Verify we have a legal type before going any further.
925 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
928 // Get the value to be stored into a register.
929 SrcReg = getRegForValue(Op0);
933 // See if we can handle this address.
935 if (!computeAddress(I->getOperand(1), Addr))
938 if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
943 // This can cause a redundant sltiu to be generated.
944 // FIXME: try and eliminate this in a future patch.
945 bool MipsFastISel::selectBranch(const Instruction *I) {
946 const BranchInst *BI = cast<BranchInst>(I);
947 MachineBasicBlock *BrBB = FuncInfo.MBB;
949 // TBB is the basic block for the case where the comparison is true.
950 // FBB is the basic block for the case where the comparison is false.
951 // if (cond) goto TBB
955 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
956 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
957 // For now, just try the simplest case where it's fed by a compare.
958 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
960 TLI.getValueType(DL, CI->getOperand(0)->getType(), true).getSimpleVT();
961 if (CIMVT == MVT::i1)
964 unsigned CondReg = getRegForValue(CI);
965 BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
968 finishCondBranch(BI->getParent(), TBB, FBB);
974 bool MipsFastISel::selectCmp(const Instruction *I) {
975 const CmpInst *CI = cast<CmpInst>(I);
976 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
977 if (!emitCmp(ResultReg, CI))
979 updateValueMap(I, ResultReg);
983 // Attempt to fast-select a floating-point extend instruction.
984 bool MipsFastISel::selectFPExt(const Instruction *I) {
985 if (UnsupportedFPMode)
987 Value *Src = I->getOperand(0);
988 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
989 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
991 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
995 getRegForValue(Src); // this must be a 32bit floating point register class
996 // maybe we should handle this differently
1000 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
1001 emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
1002 updateValueMap(I, DestReg);
1006 bool MipsFastISel::selectSelect(const Instruction *I) {
1007 assert(isa<SelectInst>(I) && "Expected a select instruction.");
1009 LLVM_DEBUG(dbgs() << "selectSelect\n");
1012 if (!isTypeSupported(I->getType(), VT) || UnsupportedFPMode) {
1014 dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
1018 unsigned CondMovOpc;
1019 const TargetRegisterClass *RC;
1021 if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
1022 CondMovOpc = Mips::MOVN_I_I;
1023 RC = &Mips::GPR32RegClass;
1024 } else if (VT == MVT::f32) {
1025 CondMovOpc = Mips::MOVN_I_S;
1026 RC = &Mips::FGR32RegClass;
1027 } else if (VT == MVT::f64) {
1028 CondMovOpc = Mips::MOVN_I_D32;
1029 RC = &Mips::AFGR64RegClass;
1033 const SelectInst *SI = cast<SelectInst>(I);
1034 const Value *Cond = SI->getCondition();
1035 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
1036 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
1037 unsigned CondReg = getRegForValue(Cond);
1039 if (!Src1Reg || !Src2Reg || !CondReg)
1042 unsigned ZExtCondReg = createResultReg(&Mips::GPR32RegClass);
1046 if (!emitIntExt(MVT::i1, CondReg, MVT::i32, ZExtCondReg, true))
1049 unsigned ResultReg = createResultReg(RC);
1050 unsigned TempReg = createResultReg(RC);
1052 if (!ResultReg || !TempReg)
1055 emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
1056 emitInst(CondMovOpc, ResultReg)
1057 .addReg(Src1Reg).addReg(ZExtCondReg).addReg(TempReg);
1058 updateValueMap(I, ResultReg);
1062 // Attempt to fast-select a floating-point truncate instruction.
1063 bool MipsFastISel::selectFPTrunc(const Instruction *I) {
1064 if (UnsupportedFPMode)
1066 Value *Src = I->getOperand(0);
1067 EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
1068 EVT DestVT = TLI.getValueType(DL, I->getType(), true);
1070 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1073 unsigned SrcReg = getRegForValue(Src);
1077 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1081 emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1082 updateValueMap(I, DestReg);
1086 // Attempt to fast-select a floating-point-to-integer conversion.
1087 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1088 if (UnsupportedFPMode)
1092 return false; // We don't handle this case yet. There is no native
1093 // instruction for this but it can be synthesized.
1094 Type *DstTy = I->getType();
1095 if (!isTypeLegal(DstTy, DstVT))
1098 if (DstVT != MVT::i32)
1101 Value *Src = I->getOperand(0);
1102 Type *SrcTy = Src->getType();
1103 if (!isTypeLegal(SrcTy, SrcVT))
1106 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1109 unsigned SrcReg = getRegForValue(Src);
1113 // Determine the opcode for the conversion, which takes place
1114 // entirely within FPRs.
1115 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1116 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
1117 unsigned Opc = (SrcVT == MVT::f32) ? Mips::TRUNC_W_S : Mips::TRUNC_W_D32;
1119 // Generate the convert.
1120 emitInst(Opc, TempReg).addReg(SrcReg);
1121 emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1123 updateValueMap(I, DestReg);
1127 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1128 SmallVectorImpl<MVT> &OutVTs,
1129 unsigned &NumBytes) {
1130 CallingConv::ID CC = CLI.CallConv;
1131 SmallVector<CCValAssign, 16> ArgLocs;
1132 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1133 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1134 // Get a count of how many bytes are to be pushed on the stack.
1135 NumBytes = CCInfo.getNextStackOffset();
1136 // This is the minimum argument area used for A0-A3.
1140 emitInst(Mips::ADJCALLSTACKDOWN).addImm(16).addImm(0);
1141 // Process the args.
1143 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1144 CCValAssign &VA = ArgLocs[i];
1145 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1146 MVT ArgVT = OutVTs[VA.getValNo()];
1150 if (ArgVT == MVT::f32) {
1151 VA.convertToReg(Mips::F12);
1152 } else if (ArgVT == MVT::f64) {
1153 VA.convertToReg(Mips::D6);
1155 } else if (i == 1) {
1156 if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1157 if (ArgVT == MVT::f32) {
1158 VA.convertToReg(Mips::F14);
1159 } else if (ArgVT == MVT::f64) {
1160 VA.convertToReg(Mips::D7);
1164 if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1165 (ArgVT == MVT::i8)) &&
1167 switch (VA.getLocMemOffset()) {
1169 VA.convertToReg(Mips::A0);
1172 VA.convertToReg(Mips::A1);
1175 VA.convertToReg(Mips::A2);
1178 VA.convertToReg(Mips::A3);
1184 unsigned ArgReg = getRegForValue(ArgVal);
1188 // Handle arg promotion: SExt, ZExt, AExt.
1189 switch (VA.getLocInfo()) {
1190 case CCValAssign::Full:
1192 case CCValAssign::AExt:
1193 case CCValAssign::SExt: {
1194 MVT DestVT = VA.getLocVT();
1196 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1201 case CCValAssign::ZExt: {
1202 MVT DestVT = VA.getLocVT();
1204 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1210 llvm_unreachable("Unknown arg promotion!");
1213 // Now copy/store arg to correct locations.
1214 if (VA.isRegLoc() && !VA.needsCustom()) {
1215 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1216 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1217 CLI.OutRegs.push_back(VA.getLocReg());
1218 } else if (VA.needsCustom()) {
1219 llvm_unreachable("Mips does not use custom args.");
1223 // FIXME: This path will currently return false. It was copied
1224 // from the AArch64 port and should be essentially fine for Mips too.
1225 // The work to finish up this path will be done in a follow-on patch.
1227 assert(VA.isMemLoc() && "Assuming store on stack.");
1228 // Don't emit stores for undef values.
1229 if (isa<UndefValue>(ArgVal))
1232 // Need to store on the stack.
1233 // FIXME: This alignment is incorrect but this path is disabled
1234 // for now (will return false). We need to determine the right alignment
1235 // based on the normal alignment for the underlying machine type.
1237 unsigned ArgSize = alignTo(ArgVT.getSizeInBits(), 4);
1239 unsigned BEAlign = 0;
1240 if (ArgSize < 8 && !Subtarget->isLittle())
1241 BEAlign = 8 - ArgSize;
1244 Addr.setKind(Address::RegBase);
1245 Addr.setReg(Mips::SP);
1246 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1248 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1249 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1250 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
1251 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1253 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1254 return false; // can't store on the stack yet.
1261 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1262 unsigned NumBytes) {
1263 CallingConv::ID CC = CLI.CallConv;
1264 emitInst(Mips::ADJCALLSTACKUP).addImm(16).addImm(0);
1265 if (RetVT != MVT::isVoid) {
1266 SmallVector<CCValAssign, 16> RVLocs;
1267 MipsCCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1269 CCInfo.AnalyzeCallResult(CLI.Ins, RetCC_Mips, CLI.RetTy,
1270 CLI.Symbol ? CLI.Symbol->getName().data()
1273 // Only handle a single return value.
1274 if (RVLocs.size() != 1)
1276 // Copy all of the result registers out of their specified physreg.
1277 MVT CopyVT = RVLocs[0].getValVT();
1278 // Special handling for extended integers.
1279 if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1282 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1285 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1286 TII.get(TargetOpcode::COPY),
1287 ResultReg).addReg(RVLocs[0].getLocReg());
1288 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1290 CLI.ResultReg = ResultReg;
1291 CLI.NumResultRegs = 1;
1296 bool MipsFastISel::fastLowerArguments() {
1297 LLVM_DEBUG(dbgs() << "fastLowerArguments\n");
1299 if (!FuncInfo.CanLowerReturn) {
1300 LLVM_DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
1304 const Function *F = FuncInfo.Fn;
1305 if (F->isVarArg()) {
1306 LLVM_DEBUG(dbgs() << ".. gave up (varargs)\n");
1310 CallingConv::ID CC = F->getCallingConv();
1311 if (CC != CallingConv::C) {
1312 LLVM_DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
1316 std::array<MCPhysReg, 4> GPR32ArgRegs = {{Mips::A0, Mips::A1, Mips::A2,
1318 std::array<MCPhysReg, 2> FGR32ArgRegs = {{Mips::F12, Mips::F14}};
1319 std::array<MCPhysReg, 2> AFGR64ArgRegs = {{Mips::D6, Mips::D7}};
1320 auto NextGPR32 = GPR32ArgRegs.begin();
1321 auto NextFGR32 = FGR32ArgRegs.begin();
1322 auto NextAFGR64 = AFGR64ArgRegs.begin();
1324 struct AllocatedReg {
1325 const TargetRegisterClass *RC;
1327 AllocatedReg(const TargetRegisterClass *RC, unsigned Reg)
1328 : RC(RC), Reg(Reg) {}
1331 // Only handle simple cases. i.e. All arguments are directly mapped to
1332 // registers of the appropriate type.
1333 SmallVector<AllocatedReg, 4> Allocation;
1334 for (const auto &FormalArg : F->args()) {
1335 if (FormalArg.hasAttribute(Attribute::InReg) ||
1336 FormalArg.hasAttribute(Attribute::StructRet) ||
1337 FormalArg.hasAttribute(Attribute::ByVal)) {
1338 LLVM_DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
1342 Type *ArgTy = FormalArg.getType();
1343 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy()) {
1344 LLVM_DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
1348 EVT ArgVT = TLI.getValueType(DL, ArgTy);
1349 LLVM_DEBUG(dbgs() << ".. " << FormalArg.getArgNo() << ": "
1350 << ArgVT.getEVTString() << "\n");
1351 if (!ArgVT.isSimple()) {
1352 LLVM_DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
1356 switch (ArgVT.getSimpleVT().SimpleTy) {
1360 if (!FormalArg.hasAttribute(Attribute::SExt) &&
1361 !FormalArg.hasAttribute(Attribute::ZExt)) {
1362 // It must be any extend, this shouldn't happen for clang-generated IR
1363 // so just fall back on SelectionDAG.
1364 LLVM_DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
1368 if (NextGPR32 == GPR32ArgRegs.end()) {
1369 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1373 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
1374 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1376 // Allocating any GPR32 prohibits further use of floating point arguments.
1377 NextFGR32 = FGR32ArgRegs.end();
1378 NextAFGR64 = AFGR64ArgRegs.end();
1382 if (FormalArg.hasAttribute(Attribute::ZExt)) {
1383 // The O32 ABI does not permit a zero-extended i32.
1384 LLVM_DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
1388 if (NextGPR32 == GPR32ArgRegs.end()) {
1389 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1393 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32 << ")\n");
1394 Allocation.emplace_back(&Mips::GPR32RegClass, *NextGPR32++);
1396 // Allocating any GPR32 prohibits further use of floating point arguments.
1397 NextFGR32 = FGR32ArgRegs.end();
1398 NextAFGR64 = AFGR64ArgRegs.end();
1402 if (UnsupportedFPMode) {
1403 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1406 if (NextFGR32 == FGR32ArgRegs.end()) {
1407 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
1410 LLVM_DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32 << ")\n");
1411 Allocation.emplace_back(&Mips::FGR32RegClass, *NextFGR32++);
1412 // Allocating an FGR32 also allocates the super-register AFGR64, and
1413 // ABI rules require us to skip the corresponding GPR32.
1414 if (NextGPR32 != GPR32ArgRegs.end())
1416 if (NextAFGR64 != AFGR64ArgRegs.end())
1421 if (UnsupportedFPMode) {
1422 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1425 if (NextAFGR64 == AFGR64ArgRegs.end()) {
1426 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
1429 LLVM_DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64 << ")\n");
1430 Allocation.emplace_back(&Mips::AFGR64RegClass, *NextAFGR64++);
1431 // Allocating an FGR32 also allocates the super-register AFGR64, and
1432 // ABI rules require us to skip the corresponding GPR32 pair.
1433 if (NextGPR32 != GPR32ArgRegs.end())
1435 if (NextGPR32 != GPR32ArgRegs.end())
1437 if (NextFGR32 != FGR32ArgRegs.end())
1442 LLVM_DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
1447 for (const auto &FormalArg : F->args()) {
1448 unsigned ArgNo = FormalArg.getArgNo();
1449 unsigned SrcReg = Allocation[ArgNo].Reg;
1450 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, Allocation[ArgNo].RC);
1451 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1452 // Without this, EmitLiveInCopies may eliminate the livein if its only
1453 // use is a bitcast (which isn't turned into an instruction).
1454 unsigned ResultReg = createResultReg(Allocation[ArgNo].RC);
1455 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1456 TII.get(TargetOpcode::COPY), ResultReg)
1457 .addReg(DstReg, getKillRegState(true));
1458 updateValueMap(&FormalArg, ResultReg);
1461 // Calculate the size of the incoming arguments area.
1462 // We currently reject all the cases where this would be non-zero.
1463 unsigned IncomingArgSizeInBytes = 0;
1465 // Account for the reserved argument area on ABI's that have one (O32).
1466 // It seems strange to do this on the caller side but it's necessary in
1467 // SelectionDAG's implementation.
1468 IncomingArgSizeInBytes = std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC),
1469 IncomingArgSizeInBytes);
1471 MF->getInfo<MipsFunctionInfo>()->setFormalArgInfo(IncomingArgSizeInBytes,
1477 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1478 CallingConv::ID CC = CLI.CallConv;
1479 bool IsTailCall = CLI.IsTailCall;
1480 bool IsVarArg = CLI.IsVarArg;
1481 const Value *Callee = CLI.Callee;
1482 MCSymbol *Symbol = CLI.Symbol;
1484 // Do not handle FastCC.
1485 if (CC == CallingConv::Fast)
1488 // Allow SelectionDAG isel to handle tail calls.
1492 // Let SDISel handle vararg functions.
1496 // FIXME: Only handle *simple* calls for now.
1498 if (CLI.RetTy->isVoidTy())
1499 RetVT = MVT::isVoid;
1500 else if (!isTypeSupported(CLI.RetTy, RetVT))
1503 for (auto Flag : CLI.OutFlags)
1504 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1507 // Set up the argument vectors.
1508 SmallVector<MVT, 16> OutVTs;
1509 OutVTs.reserve(CLI.OutVals.size());
1511 for (auto *Val : CLI.OutVals) {
1513 if (!isTypeLegal(Val->getType(), VT) &&
1514 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1517 // We don't handle vector parameters yet.
1518 if (VT.isVector() || VT.getSizeInBits() > 64)
1521 OutVTs.push_back(VT);
1525 if (!computeCallAddress(Callee, Addr))
1528 // Handle the arguments now that we've gotten them.
1530 if (!processCallArgs(CLI, OutVTs, NumBytes))
1533 if (!Addr.getGlobalValue())
1537 unsigned DestAddress;
1539 DestAddress = materializeExternalCallSym(Symbol);
1541 DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
1542 emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1543 MachineInstrBuilder MIB =
1544 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1545 Mips::RA).addReg(Mips::T9);
1547 // Add implicit physical register uses to the call.
1548 for (auto Reg : CLI.OutRegs)
1549 MIB.addReg(Reg, RegState::Implicit);
1551 // Add a register mask with the call-preserved registers.
1552 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1553 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
1557 if (EmitJalrReloc && !Subtarget->inMips16Mode()) {
1558 // Attach callee address to the instruction, let asm printer emit
1559 // .reloc R_MIPS_JALR.
1561 MIB.addSym(Symbol, MipsII::MO_JALR);
1563 MIB.addSym(FuncInfo.MF->getContext().getOrCreateSymbol(
1564 Addr.getGlobalValue()->getName()), MipsII::MO_JALR);
1567 // Finish off the call including any return values.
1568 return finishCall(CLI, RetVT, NumBytes);
1571 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
1572 switch (II->getIntrinsicID()) {
1575 case Intrinsic::bswap: {
1576 Type *RetTy = II->getCalledFunction()->getReturnType();
1579 if (!isTypeSupported(RetTy, VT))
1582 unsigned SrcReg = getRegForValue(II->getOperand(0));
1585 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1588 if (VT == MVT::i16) {
1589 if (Subtarget->hasMips32r2()) {
1590 emitInst(Mips::WSBH, DestReg).addReg(SrcReg);
1591 updateValueMap(II, DestReg);
1594 unsigned TempReg[3];
1595 for (int i = 0; i < 3; i++) {
1596 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1597 if (TempReg[i] == 0)
1600 emitInst(Mips::SLL, TempReg[0]).addReg(SrcReg).addImm(8);
1601 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(8);
1602 emitInst(Mips::OR, TempReg[2]).addReg(TempReg[0]).addReg(TempReg[1]);
1603 emitInst(Mips::ANDi, DestReg).addReg(TempReg[2]).addImm(0xFFFF);
1604 updateValueMap(II, DestReg);
1607 } else if (VT == MVT::i32) {
1608 if (Subtarget->hasMips32r2()) {
1609 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1610 emitInst(Mips::WSBH, TempReg).addReg(SrcReg);
1611 emitInst(Mips::ROTR, DestReg).addReg(TempReg).addImm(16);
1612 updateValueMap(II, DestReg);
1615 unsigned TempReg[8];
1616 for (int i = 0; i < 8; i++) {
1617 TempReg[i] = createResultReg(&Mips::GPR32RegClass);
1618 if (TempReg[i] == 0)
1622 emitInst(Mips::SRL, TempReg[0]).addReg(SrcReg).addImm(8);
1623 emitInst(Mips::SRL, TempReg[1]).addReg(SrcReg).addImm(24);
1624 emitInst(Mips::ANDi, TempReg[2]).addReg(TempReg[0]).addImm(0xFF00);
1625 emitInst(Mips::OR, TempReg[3]).addReg(TempReg[1]).addReg(TempReg[2]);
1627 emitInst(Mips::ANDi, TempReg[4]).addReg(SrcReg).addImm(0xFF00);
1628 emitInst(Mips::SLL, TempReg[5]).addReg(TempReg[4]).addImm(8);
1630 emitInst(Mips::SLL, TempReg[6]).addReg(SrcReg).addImm(24);
1631 emitInst(Mips::OR, TempReg[7]).addReg(TempReg[3]).addReg(TempReg[5]);
1632 emitInst(Mips::OR, DestReg).addReg(TempReg[6]).addReg(TempReg[7]);
1633 updateValueMap(II, DestReg);
1639 case Intrinsic::memcpy:
1640 case Intrinsic::memmove: {
1641 const auto *MTI = cast<MemTransferInst>(II);
1642 // Don't handle volatile.
1643 if (MTI->isVolatile())
1645 if (!MTI->getLength()->getType()->isIntegerTy(32))
1647 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
1648 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 1);
1650 case Intrinsic::memset: {
1651 const MemSetInst *MSI = cast<MemSetInst>(II);
1652 // Don't handle volatile.
1653 if (MSI->isVolatile())
1655 if (!MSI->getLength()->getType()->isIntegerTy(32))
1657 return lowerCallTo(II, "memset", II->getNumArgOperands() - 1);
1663 bool MipsFastISel::selectRet(const Instruction *I) {
1664 const Function &F = *I->getParent()->getParent();
1665 const ReturnInst *Ret = cast<ReturnInst>(I);
1667 LLVM_DEBUG(dbgs() << "selectRet\n");
1669 if (!FuncInfo.CanLowerReturn)
1672 // Build a list of return value registers.
1673 SmallVector<unsigned, 4> RetRegs;
1675 if (Ret->getNumOperands() > 0) {
1676 CallingConv::ID CC = F.getCallingConv();
1678 // Do not handle FastCC.
1679 if (CC == CallingConv::Fast)
1682 SmallVector<ISD::OutputArg, 4> Outs;
1683 GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
1685 // Analyze operands of the call, assigning locations to each operand.
1686 SmallVector<CCValAssign, 16> ValLocs;
1687 MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1689 CCAssignFn *RetCC = RetCC_Mips;
1690 CCInfo.AnalyzeReturn(Outs, RetCC);
1692 // Only handle a single return value for now.
1693 if (ValLocs.size() != 1)
1696 CCValAssign &VA = ValLocs[0];
1697 const Value *RV = Ret->getOperand(0);
1699 // Don't bother handling odd stuff for now.
1700 if ((VA.getLocInfo() != CCValAssign::Full) &&
1701 (VA.getLocInfo() != CCValAssign::BCvt))
1704 // Only handle register returns for now.
1708 unsigned Reg = getRegForValue(RV);
1712 unsigned SrcReg = Reg + VA.getValNo();
1713 unsigned DestReg = VA.getLocReg();
1714 // Avoid a cross-class copy. This is very unlikely.
1715 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1718 EVT RVEVT = TLI.getValueType(DL, RV->getType());
1719 if (!RVEVT.isSimple())
1722 if (RVEVT.isVector())
1725 MVT RVVT = RVEVT.getSimpleVT();
1726 if (RVVT == MVT::f128)
1729 // Do not handle FGR64 returns for now.
1730 if (RVVT == MVT::f64 && UnsupportedFPMode) {
1731 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1735 MVT DestVT = VA.getValVT();
1736 // Special handling for extended integers.
1737 if (RVVT != DestVT) {
1738 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1741 if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1742 bool IsZExt = Outs[0].Flags.isZExt();
1743 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1750 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1751 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1753 // Add register to return instruction.
1754 RetRegs.push_back(VA.getLocReg());
1756 MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1757 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1758 MIB.addReg(RetRegs[i], RegState::Implicit);
1762 bool MipsFastISel::selectTrunc(const Instruction *I) {
1763 // The high bits for a type smaller than the register size are assumed to be
1765 Value *Op = I->getOperand(0);
1768 SrcVT = TLI.getValueType(DL, Op->getType(), true);
1769 DestVT = TLI.getValueType(DL, I->getType(), true);
1771 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1773 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1776 unsigned SrcReg = getRegForValue(Op);
1780 // Because the high bits are undefined, a truncate doesn't generate
1782 updateValueMap(I, SrcReg);
1786 bool MipsFastISel::selectIntExt(const Instruction *I) {
1787 Type *DestTy = I->getType();
1788 Value *Src = I->getOperand(0);
1789 Type *SrcTy = Src->getType();
1791 bool isZExt = isa<ZExtInst>(I);
1792 unsigned SrcReg = getRegForValue(Src);
1796 EVT SrcEVT, DestEVT;
1797 SrcEVT = TLI.getValueType(DL, SrcTy, true);
1798 DestEVT = TLI.getValueType(DL, DestTy, true);
1799 if (!SrcEVT.isSimple())
1801 if (!DestEVT.isSimple())
1804 MVT SrcVT = SrcEVT.getSimpleVT();
1805 MVT DestVT = DestEVT.getSimpleVT();
1806 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1808 if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1810 updateValueMap(I, ResultReg);
1814 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1817 switch (SrcVT.SimpleTy) {
1827 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1828 emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1829 emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1833 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1835 switch (SrcVT.SimpleTy) {
1839 emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1842 emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1848 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1850 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1852 if (Subtarget->hasMips32r2())
1853 return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1854 return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1857 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1861 switch (SrcVT.SimpleTy) {
1875 emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(Imm);
1879 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1880 unsigned DestReg, bool IsZExt) {
1881 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1882 // DestVT are odd things, so test to make sure that they are both types we can
1883 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1884 // bail out to SelectionDAG.
1885 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1886 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1889 return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1890 return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1893 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1895 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1896 bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1897 return Success ? DestReg : 0;
1900 bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
1901 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
1902 if (!DestEVT.isSimple())
1905 MVT DestVT = DestEVT.getSimpleVT();
1906 if (DestVT != MVT::i32)
1910 switch (ISDOpcode) {
1915 DivOpc = Mips::SDIV;
1919 DivOpc = Mips::UDIV;
1923 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1924 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1925 if (!Src0Reg || !Src1Reg)
1928 emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1929 emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1931 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1935 unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1938 emitInst(MFOpc, ResultReg);
1940 updateValueMap(I, ResultReg);
1944 bool MipsFastISel::selectShift(const Instruction *I) {
1947 if (!isTypeSupported(I->getType(), RetVT))
1950 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1954 unsigned Opcode = I->getOpcode();
1955 const Value *Op0 = I->getOperand(0);
1956 unsigned Op0Reg = getRegForValue(Op0);
1960 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1961 if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1962 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1966 MVT Op0MVT = TLI.getValueType(DL, Op0->getType(), true).getSimpleVT();
1967 bool IsZExt = Opcode == Instruction::LShr;
1968 if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1974 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1975 uint64_t ShiftVal = C->getZExtValue();
1979 llvm_unreachable("Unexpected instruction.");
1980 case Instruction::Shl:
1983 case Instruction::AShr:
1986 case Instruction::LShr:
1991 emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1992 updateValueMap(I, ResultReg);
1996 unsigned Op1Reg = getRegForValue(I->getOperand(1));
2002 llvm_unreachable("Unexpected instruction.");
2003 case Instruction::Shl:
2004 Opcode = Mips::SLLV;
2006 case Instruction::AShr:
2007 Opcode = Mips::SRAV;
2009 case Instruction::LShr:
2010 Opcode = Mips::SRLV;
2014 emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
2015 updateValueMap(I, ResultReg);
2019 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
2020 switch (I->getOpcode()) {
2023 case Instruction::Load:
2024 return selectLoad(I);
2025 case Instruction::Store:
2026 return selectStore(I);
2027 case Instruction::SDiv:
2028 if (!selectBinaryOp(I, ISD::SDIV))
2029 return selectDivRem(I, ISD::SDIV);
2031 case Instruction::UDiv:
2032 if (!selectBinaryOp(I, ISD::UDIV))
2033 return selectDivRem(I, ISD::UDIV);
2035 case Instruction::SRem:
2036 if (!selectBinaryOp(I, ISD::SREM))
2037 return selectDivRem(I, ISD::SREM);
2039 case Instruction::URem:
2040 if (!selectBinaryOp(I, ISD::UREM))
2041 return selectDivRem(I, ISD::UREM);
2043 case Instruction::Shl:
2044 case Instruction::LShr:
2045 case Instruction::AShr:
2046 return selectShift(I);
2047 case Instruction::And:
2048 case Instruction::Or:
2049 case Instruction::Xor:
2050 return selectLogicalOp(I);
2051 case Instruction::Br:
2052 return selectBranch(I);
2053 case Instruction::Ret:
2054 return selectRet(I);
2055 case Instruction::Trunc:
2056 return selectTrunc(I);
2057 case Instruction::ZExt:
2058 case Instruction::SExt:
2059 return selectIntExt(I);
2060 case Instruction::FPTrunc:
2061 return selectFPTrunc(I);
2062 case Instruction::FPExt:
2063 return selectFPExt(I);
2064 case Instruction::FPToSI:
2065 return selectFPToInt(I, /*isSigned*/ true);
2066 case Instruction::FPToUI:
2067 return selectFPToInt(I, /*isSigned*/ false);
2068 case Instruction::ICmp:
2069 case Instruction::FCmp:
2070 return selectCmp(I);
2071 case Instruction::Select:
2072 return selectSelect(I);
2077 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
2079 unsigned VReg = getRegForValue(V);
2082 MVT VMVT = TLI.getValueType(DL, V->getType(), true).getSimpleVT();
2084 if (VMVT == MVT::i1)
2087 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
2088 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
2089 if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
2096 void MipsFastISel::simplifyAddress(Address &Addr) {
2097 if (!isInt<16>(Addr.getOffset())) {
2099 materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
2100 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
2101 emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
2102 Addr.setReg(DestReg);
2107 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
2108 const TargetRegisterClass *RC,
2109 unsigned Op0, bool Op0IsKill,
2110 unsigned Op1, bool Op1IsKill) {
2111 // We treat the MUL instruction in a special way because it clobbers
2112 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2113 // mark these registers only as implicitly defined. As a result, the
2114 // register allocator runs out of registers when this instruction is
2115 // followed by another instruction that defines the same registers too.
2116 // We can fix this by explicitly marking those registers as dead.
2117 if (MachineInstOpcode == Mips::MUL) {
2118 unsigned ResultReg = createResultReg(RC);
2119 const MCInstrDesc &II = TII.get(MachineInstOpcode);
2120 Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
2121 Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
2122 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
2123 .addReg(Op0, getKillRegState(Op0IsKill))
2124 .addReg(Op1, getKillRegState(Op1IsKill))
2125 .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
2126 .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
2130 return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
2136 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
2137 const TargetLibraryInfo *libInfo) {
2138 return new MipsFastISel(funcInfo, libInfo);
2141 } // end namespace llvm