[mips][FastISel] Implement intrinsics memset, memcopy & memmove.
[oota-llvm.git] / lib / Target / Mips / MipsFastISel.cpp
1 //===-- MipsastISel.cpp - Mips FastISel implementation
2 //---------------------===//
3
4 #include "MipsCCState.h"
5 #include "MipsInstrInfo.h"
6 #include "MipsISelLowering.h"
7 #include "MipsMachineFunction.h"
8 #include "MipsRegisterInfo.h"
9 #include "MipsSubtarget.h"
10 #include "MipsTargetMachine.h"
11 #include "llvm/Analysis/TargetLibraryInfo.h"
12 #include "llvm/CodeGen/FastISel.h"
13 #include "llvm/CodeGen/FunctionLoweringInfo.h"
14 #include "llvm/CodeGen/MachineInstrBuilder.h"
15 #include "llvm/CodeGen/MachineRegisterInfo.h"
16 #include "llvm/IR/GetElementPtrTypeIterator.h"
17 #include "llvm/IR/GlobalAlias.h"
18 #include "llvm/IR/GlobalVariable.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 class MipsFastISel final : public FastISel {
26
27   // All possible address modes.
28   class Address {
29   public:
30     typedef enum { RegBase, FrameIndexBase } BaseKind;
31
32   private:
33     BaseKind Kind;
34     union {
35       unsigned Reg;
36       int FI;
37     } Base;
38
39     int64_t Offset;
40
41     const GlobalValue *GV;
42
43   public:
44     // Innocuous defaults for our address.
45     Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; }
46     void setKind(BaseKind K) { Kind = K; }
47     BaseKind getKind() const { return Kind; }
48     bool isRegBase() const { return Kind == RegBase; }
49     bool isFIBase() const { return Kind == FrameIndexBase; }
50     void setReg(unsigned Reg) {
51       assert(isRegBase() && "Invalid base register access!");
52       Base.Reg = Reg;
53     }
54     unsigned getReg() const {
55       assert(isRegBase() && "Invalid base register access!");
56       return Base.Reg;
57     }
58     void setFI(unsigned FI) {
59       assert(isFIBase() && "Invalid base frame index access!");
60       Base.FI = FI;
61     }
62     unsigned getFI() const {
63       assert(isFIBase() && "Invalid base frame index access!");
64       return Base.FI;
65     }
66
67     void setOffset(int64_t Offset_) { Offset = Offset_; }
68     int64_t getOffset() const { return Offset; }
69     void setGlobalValue(const GlobalValue *G) { GV = G; }
70     const GlobalValue *getGlobalValue() { return GV; }
71   };
72
73   /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
74   /// make the right decision when generating code for different targets.
75   const TargetMachine &TM;
76   const MipsSubtarget *Subtarget;
77   const TargetInstrInfo &TII;
78   const TargetLowering &TLI;
79   MipsFunctionInfo *MFI;
80
81   // Convenience variables to avoid some queries.
82   LLVMContext *Context;
83
84   bool fastLowerCall(CallLoweringInfo &CLI) override;
85   bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
86
87   bool TargetSupported;
88   bool UnsupportedFPMode; // To allow fast-isel to proceed and just not handle
89   // floating point but not reject doing fast-isel in other
90   // situations
91
92 private:
93   // Selection routines.
94   bool selectLogicalOp(const Instruction *I);
95   bool selectLoad(const Instruction *I);
96   bool selectStore(const Instruction *I);
97   bool selectBranch(const Instruction *I);
98   bool selectSelect(const Instruction *I);
99   bool selectCmp(const Instruction *I);
100   bool selectFPExt(const Instruction *I);
101   bool selectFPTrunc(const Instruction *I);
102   bool selectFPToInt(const Instruction *I, bool IsSigned);
103   bool selectRet(const Instruction *I);
104   bool selectTrunc(const Instruction *I);
105   bool selectIntExt(const Instruction *I);
106   bool selectShift(const Instruction *I);
107   bool selectDivRem(const Instruction *I, unsigned ISDOpcode);
108
109   // Utility helper routines.
110   bool isTypeLegal(Type *Ty, MVT &VT);
111   bool isTypeSupported(Type *Ty, MVT &VT);
112   bool isLoadTypeLegal(Type *Ty, MVT &VT);
113   bool computeAddress(const Value *Obj, Address &Addr);
114   bool computeCallAddress(const Value *V, Address &Addr);
115   void simplifyAddress(Address &Addr);
116
117   // Emit helper routines.
118   bool emitCmp(unsigned DestReg, const CmpInst *CI);
119   bool emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
120                 unsigned Alignment = 0);
121   bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
122                  MachineMemOperand *MMO = nullptr);
123   bool emitStore(MVT VT, unsigned SrcReg, Address &Addr,
124                  unsigned Alignment = 0);
125   unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
126   bool emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
127
128                   bool IsZExt);
129   bool emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
130
131   bool emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
132   bool emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
133                        unsigned DestReg);
134   bool emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
135                        unsigned DestReg);
136
137   unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
138
139   unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
140                          const Value *RHS);
141
142   unsigned materializeFP(const ConstantFP *CFP, MVT VT);
143   unsigned materializeGV(const GlobalValue *GV, MVT VT);
144   unsigned materializeInt(const Constant *C, MVT VT);
145   unsigned materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
146   unsigned materializeExternalCallSym(const char *SynName);
147
148   MachineInstrBuilder emitInst(unsigned Opc) {
149     return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
150   }
151   MachineInstrBuilder emitInst(unsigned Opc, unsigned DstReg) {
152     return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
153                    DstReg);
154   }
155   MachineInstrBuilder emitInstStore(unsigned Opc, unsigned SrcReg,
156                                     unsigned MemReg, int64_t MemOffset) {
157     return emitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
158   }
159   MachineInstrBuilder emitInstLoad(unsigned Opc, unsigned DstReg,
160                                    unsigned MemReg, int64_t MemOffset) {
161     return emitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
162   }
163
164   unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
165                            const TargetRegisterClass *RC,
166                            unsigned Op0, bool Op0IsKill,
167                            unsigned Op1, bool Op1IsKill);
168
169   // for some reason, this default is not generated by tablegen
170   // so we explicitly generate it here.
171   //
172   unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
173                              unsigned Op0, bool Op0IsKill, uint64_t imm1,
174                              uint64_t imm2, unsigned Op3, bool Op3IsKill) {
175     return 0;
176   }
177
178   // Call handling routines.
179 private:
180   CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
181   bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
182                        unsigned &NumBytes);
183   bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
184
185 public:
186   // Backend specific FastISel code.
187   explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
188                         const TargetLibraryInfo *libInfo)
189       : FastISel(funcInfo, libInfo), TM(funcInfo.MF->getTarget()),
190         Subtarget(&funcInfo.MF->getSubtarget<MipsSubtarget>()),
191         TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()) {
192     MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
193     Context = &funcInfo.Fn->getContext();
194     TargetSupported =
195         ((TM.getRelocationModel() == Reloc::PIC_) &&
196          ((Subtarget->hasMips32r2() || Subtarget->hasMips32()) &&
197           (static_cast<const MipsTargetMachine &>(TM).getABI().IsO32())));
198     UnsupportedFPMode = Subtarget->isFP64bit();
199   }
200
201   unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
202   unsigned fastMaterializeConstant(const Constant *C) override;
203   bool fastSelectInstruction(const Instruction *I) override;
204
205 #include "MipsGenFastISel.inc"
206 };
207 } // end anonymous namespace.
208
209 static bool CC_Mips(unsigned ValNo, MVT ValVT, MVT LocVT,
210                     CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
211                     CCState &State) LLVM_ATTRIBUTE_UNUSED;
212
213 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT, MVT LocVT,
214                             CCValAssign::LocInfo LocInfo,
215                             ISD::ArgFlagsTy ArgFlags, CCState &State) {
216   llvm_unreachable("should not be called");
217 }
218
219 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT, MVT LocVT,
220                             CCValAssign::LocInfo LocInfo,
221                             ISD::ArgFlagsTy ArgFlags, CCState &State) {
222   llvm_unreachable("should not be called");
223 }
224
225 #include "MipsGenCallingConv.inc"
226
227 CCAssignFn *MipsFastISel::CCAssignFnForCall(CallingConv::ID CC) const {
228   return CC_MipsO32;
229 }
230
231 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
232                                      const Value *LHS, const Value *RHS) {
233   // Canonicalize immediates to the RHS first.
234   if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
235     std::swap(LHS, RHS);
236
237   unsigned Opc;
238   if (ISDOpc == ISD::AND) {
239     Opc = Mips::AND;
240   } else if (ISDOpc == ISD::OR) {
241     Opc = Mips::OR;
242   } else if (ISDOpc == ISD::XOR) {
243     Opc = Mips::XOR;
244   } else
245     llvm_unreachable("unexpected opcode");
246
247   unsigned LHSReg = getRegForValue(LHS);
248   unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
249   if (!ResultReg)
250     return 0;
251
252   unsigned RHSReg;
253   if (!LHSReg)
254     return 0;
255
256   if (const auto *C = dyn_cast<ConstantInt>(RHS))
257     RHSReg = materializeInt(C, MVT::i32);
258   else
259     RHSReg = getRegForValue(RHS);
260
261   if (!RHSReg)
262     return 0;
263
264   emitInst(Opc, ResultReg).addReg(LHSReg).addReg(RHSReg);
265   return ResultReg;
266 }
267
268 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
269   assert(TLI.getValueType(AI->getType(), true) == MVT::i32 &&
270          "Alloca should always return a pointer.");
271
272   DenseMap<const AllocaInst *, int>::iterator SI =
273       FuncInfo.StaticAllocaMap.find(AI);
274
275   if (SI != FuncInfo.StaticAllocaMap.end()) {
276     unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
277     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LEA_ADDiu),
278             ResultReg)
279         .addFrameIndex(SI->second)
280         .addImm(0);
281     return ResultReg;
282   }
283
284   return 0;
285 }
286
287 unsigned MipsFastISel::materializeInt(const Constant *C, MVT VT) {
288   if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
289     return 0;
290   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
291   const ConstantInt *CI = cast<ConstantInt>(C);
292   int64_t Imm;
293   if ((VT != MVT::i1) && CI->isNegative())
294     Imm = CI->getSExtValue();
295   else
296     Imm = CI->getZExtValue();
297   return materialize32BitInt(Imm, RC);
298 }
299
300 unsigned MipsFastISel::materialize32BitInt(int64_t Imm,
301                                            const TargetRegisterClass *RC) {
302   unsigned ResultReg = createResultReg(RC);
303
304   if (isInt<16>(Imm)) {
305     unsigned Opc = Mips::ADDiu;
306     emitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
307     return ResultReg;
308   } else if (isUInt<16>(Imm)) {
309     emitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
310     return ResultReg;
311   }
312   unsigned Lo = Imm & 0xFFFF;
313   unsigned Hi = (Imm >> 16) & 0xFFFF;
314   if (Lo) {
315     // Both Lo and Hi have nonzero bits.
316     unsigned TmpReg = createResultReg(RC);
317     emitInst(Mips::LUi, TmpReg).addImm(Hi);
318     emitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
319   } else {
320     emitInst(Mips::LUi, ResultReg).addImm(Hi);
321   }
322   return ResultReg;
323 }
324
325 unsigned MipsFastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
326   if (UnsupportedFPMode)
327     return 0;
328   int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
329   if (VT == MVT::f32) {
330     const TargetRegisterClass *RC = &Mips::FGR32RegClass;
331     unsigned DestReg = createResultReg(RC);
332     unsigned TempReg = materialize32BitInt(Imm, &Mips::GPR32RegClass);
333     emitInst(Mips::MTC1, DestReg).addReg(TempReg);
334     return DestReg;
335   } else if (VT == MVT::f64) {
336     const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
337     unsigned DestReg = createResultReg(RC);
338     unsigned TempReg1 = materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
339     unsigned TempReg2 =
340         materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
341     emitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
342     return DestReg;
343   }
344   return 0;
345 }
346
347 unsigned MipsFastISel::materializeGV(const GlobalValue *GV, MVT VT) {
348   // For now 32-bit only.
349   if (VT != MVT::i32)
350     return 0;
351   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
352   unsigned DestReg = createResultReg(RC);
353   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
354   bool IsThreadLocal = GVar && GVar->isThreadLocal();
355   // TLS not supported at this time.
356   if (IsThreadLocal)
357     return 0;
358   emitInst(Mips::LW, DestReg)
359       .addReg(MFI->getGlobalBaseReg())
360       .addGlobalAddress(GV, 0, MipsII::MO_GOT);
361   if ((GV->hasInternalLinkage() ||
362        (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
363     unsigned TempReg = createResultReg(RC);
364     emitInst(Mips::ADDiu, TempReg)
365         .addReg(DestReg)
366         .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
367     DestReg = TempReg;
368   }
369   return DestReg;
370 }
371
372 unsigned MipsFastISel::materializeExternalCallSym(const char *SymName) {
373   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
374   unsigned DestReg = createResultReg(RC);
375   emitInst(Mips::LW, DestReg)
376       .addReg(MFI->getGlobalBaseReg())
377       .addExternalSymbol(SymName, MipsII::MO_GOT);
378   return DestReg;
379 }
380
381 // Materialize a constant into a register, and return the register
382 // number (or zero if we failed to handle it).
383 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
384   EVT CEVT = TLI.getValueType(C->getType(), true);
385
386   // Only handle simple types.
387   if (!CEVT.isSimple())
388     return 0;
389   MVT VT = CEVT.getSimpleVT();
390
391   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
392     return (UnsupportedFPMode) ? 0 : materializeFP(CFP, VT);
393   else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
394     return materializeGV(GV, VT);
395   else if (isa<ConstantInt>(C))
396     return materializeInt(C, VT);
397
398   return 0;
399 }
400
401 bool MipsFastISel::computeAddress(const Value *Obj, Address &Addr) {
402
403   const User *U = nullptr;
404   unsigned Opcode = Instruction::UserOp1;
405   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
406     // Don't walk into other basic blocks unless the object is an alloca from
407     // another block, otherwise it may not have a virtual register assigned.
408     if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
409         FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
410       Opcode = I->getOpcode();
411       U = I;
412     }
413   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
414     Opcode = C->getOpcode();
415     U = C;
416   }
417   switch (Opcode) {
418   default:
419     break;
420   case Instruction::BitCast: {
421     // Look through bitcasts.
422     return computeAddress(U->getOperand(0), Addr);
423   }
424   case Instruction::GetElementPtr: {
425     Address SavedAddr = Addr;
426     uint64_t TmpOffset = Addr.getOffset();
427     // Iterate through the GEP folding the constants into offsets where
428     // we can.
429     gep_type_iterator GTI = gep_type_begin(U);
430     for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
431          ++i, ++GTI) {
432       const Value *Op = *i;
433       if (StructType *STy = dyn_cast<StructType>(*GTI)) {
434         const StructLayout *SL = DL.getStructLayout(STy);
435         unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
436         TmpOffset += SL->getElementOffset(Idx);
437       } else {
438         uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
439         for (;;) {
440           if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
441             // Constant-offset addressing.
442             TmpOffset += CI->getSExtValue() * S;
443             break;
444           }
445           if (canFoldAddIntoGEP(U, Op)) {
446             // A compatible add with a constant operand. Fold the constant.
447             ConstantInt *CI =
448                 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
449             TmpOffset += CI->getSExtValue() * S;
450             // Iterate on the other operand.
451             Op = cast<AddOperator>(Op)->getOperand(0);
452             continue;
453           }
454           // Unsupported
455           goto unsupported_gep;
456         }
457       }
458     }
459     // Try to grab the base operand now.
460     Addr.setOffset(TmpOffset);
461     if (computeAddress(U->getOperand(0), Addr))
462       return true;
463     // We failed, restore everything and try the other options.
464     Addr = SavedAddr;
465   unsupported_gep:
466     break;
467   }
468   case Instruction::Alloca: {
469     const AllocaInst *AI = cast<AllocaInst>(Obj);
470     DenseMap<const AllocaInst *, int>::iterator SI =
471         FuncInfo.StaticAllocaMap.find(AI);
472     if (SI != FuncInfo.StaticAllocaMap.end()) {
473       Addr.setKind(Address::FrameIndexBase);
474       Addr.setFI(SI->second);
475       return true;
476     }
477     break;
478   }
479   }
480   Addr.setReg(getRegForValue(Obj));
481   return Addr.getReg() != 0;
482 }
483
484 bool MipsFastISel::computeCallAddress(const Value *V, Address &Addr) {
485   const User *U = nullptr;
486   unsigned Opcode = Instruction::UserOp1;
487
488   if (const auto *I = dyn_cast<Instruction>(V)) {
489     // Check if the value is defined in the same basic block. This information
490     // is crucial to know whether or not folding an operand is valid.
491     if (I->getParent() == FuncInfo.MBB->getBasicBlock()) {
492       Opcode = I->getOpcode();
493       U = I;
494     }
495   } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
496     Opcode = C->getOpcode();
497     U = C;
498   }
499
500   switch (Opcode) {
501   default:
502     break;
503   case Instruction::BitCast:
504     // Look past bitcasts if its operand is in the same BB.
505       return computeCallAddress(U->getOperand(0), Addr);
506     break;
507   case Instruction::IntToPtr:
508     // Look past no-op inttoptrs if its operand is in the same BB.
509     if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
510       return computeCallAddress(U->getOperand(0), Addr);
511     break;
512   case Instruction::PtrToInt:
513     // Look past no-op ptrtoints if its operand is in the same BB.
514     if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
515       return computeCallAddress(U->getOperand(0), Addr);
516     break;
517   }
518
519   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
520     Addr.setGlobalValue(GV);
521     return true;
522   }
523
524   // If all else fails, try to materialize the value in a register.
525   if (!Addr.getGlobalValue()) {
526     Addr.setReg(getRegForValue(V));
527     return Addr.getReg() != 0;
528   }
529
530   return false;
531 }
532
533 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
534   EVT evt = TLI.getValueType(Ty, true);
535   // Only handle simple types.
536   if (evt == MVT::Other || !evt.isSimple())
537     return false;
538   VT = evt.getSimpleVT();
539
540   // Handle all legal types, i.e. a register that will directly hold this
541   // value.
542   return TLI.isTypeLegal(VT);
543 }
544
545 bool MipsFastISel::isTypeSupported(Type *Ty, MVT &VT) {
546   if (Ty->isVectorTy())
547     return false;
548
549   if (isTypeLegal(Ty, VT))
550     return true;
551
552   // If this is a type than can be sign or zero-extended to a basic operation
553   // go ahead and accept it now.
554   if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
555     return true;
556
557   return false;
558 }
559
560 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
561   if (isTypeLegal(Ty, VT))
562     return true;
563   // We will extend this in a later patch:
564   //   If this is a type than can be sign or zero-extended to a basic operation
565   //   go ahead and accept it now.
566   if (VT == MVT::i8 || VT == MVT::i16)
567     return true;
568   return false;
569 }
570 // Because of how EmitCmp is called with fast-isel, you can
571 // end up with redundant "andi" instructions after the sequences emitted below.
572 // We should try and solve this issue in the future.
573 //
574 bool MipsFastISel::emitCmp(unsigned ResultReg, const CmpInst *CI) {
575   const Value *Left = CI->getOperand(0), *Right = CI->getOperand(1);
576   bool IsUnsigned = CI->isUnsigned();
577   unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
578   if (LeftReg == 0)
579     return false;
580   unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
581   if (RightReg == 0)
582     return false;
583   CmpInst::Predicate P = CI->getPredicate();
584
585   switch (P) {
586   default:
587     return false;
588   case CmpInst::ICMP_EQ: {
589     unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
590     emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
591     emitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
592     break;
593   }
594   case CmpInst::ICMP_NE: {
595     unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
596     emitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
597     emitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
598     break;
599   }
600   case CmpInst::ICMP_UGT: {
601     emitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
602     break;
603   }
604   case CmpInst::ICMP_ULT: {
605     emitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
606     break;
607   }
608   case CmpInst::ICMP_UGE: {
609     unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
610     emitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
611     emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
612     break;
613   }
614   case CmpInst::ICMP_ULE: {
615     unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
616     emitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
617     emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
618     break;
619   }
620   case CmpInst::ICMP_SGT: {
621     emitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
622     break;
623   }
624   case CmpInst::ICMP_SLT: {
625     emitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
626     break;
627   }
628   case CmpInst::ICMP_SGE: {
629     unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
630     emitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
631     emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
632     break;
633   }
634   case CmpInst::ICMP_SLE: {
635     unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
636     emitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
637     emitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
638     break;
639   }
640   case CmpInst::FCMP_OEQ:
641   case CmpInst::FCMP_UNE:
642   case CmpInst::FCMP_OLT:
643   case CmpInst::FCMP_OLE:
644   case CmpInst::FCMP_OGT:
645   case CmpInst::FCMP_OGE: {
646     if (UnsupportedFPMode)
647       return false;
648     bool IsFloat = Left->getType()->isFloatTy();
649     bool IsDouble = Left->getType()->isDoubleTy();
650     if (!IsFloat && !IsDouble)
651       return false;
652     unsigned Opc, CondMovOpc;
653     switch (P) {
654     case CmpInst::FCMP_OEQ:
655       Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
656       CondMovOpc = Mips::MOVT_I;
657       break;
658     case CmpInst::FCMP_UNE:
659       Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
660       CondMovOpc = Mips::MOVF_I;
661       break;
662     case CmpInst::FCMP_OLT:
663       Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
664       CondMovOpc = Mips::MOVT_I;
665       break;
666     case CmpInst::FCMP_OLE:
667       Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
668       CondMovOpc = Mips::MOVT_I;
669       break;
670     case CmpInst::FCMP_OGT:
671       Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
672       CondMovOpc = Mips::MOVF_I;
673       break;
674     case CmpInst::FCMP_OGE:
675       Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
676       CondMovOpc = Mips::MOVF_I;
677       break;
678     default:
679       llvm_unreachable("Only switching of a subset of CCs.");
680     }
681     unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
682     unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
683     emitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
684     emitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
685     emitInst(Opc).addReg(LeftReg).addReg(RightReg).addReg(
686         Mips::FCC0, RegState::ImplicitDefine);
687     MachineInstrBuilder MI = emitInst(CondMovOpc, ResultReg)
688                                  .addReg(RegWithOne)
689                                  .addReg(Mips::FCC0)
690                                  .addReg(RegWithZero, RegState::Implicit);
691     MI->tieOperands(0, 3);
692     break;
693   }
694   }
695   return true;
696 }
697 bool MipsFastISel::emitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
698                             unsigned Alignment) {
699   //
700   // more cases will be handled here in following patches.
701   //
702   unsigned Opc;
703   switch (VT.SimpleTy) {
704   case MVT::i32: {
705     ResultReg = createResultReg(&Mips::GPR32RegClass);
706     Opc = Mips::LW;
707     break;
708   }
709   case MVT::i16: {
710     ResultReg = createResultReg(&Mips::GPR32RegClass);
711     Opc = Mips::LHu;
712     break;
713   }
714   case MVT::i8: {
715     ResultReg = createResultReg(&Mips::GPR32RegClass);
716     Opc = Mips::LBu;
717     break;
718   }
719   case MVT::f32: {
720     if (UnsupportedFPMode)
721       return false;
722     ResultReg = createResultReg(&Mips::FGR32RegClass);
723     Opc = Mips::LWC1;
724     break;
725   }
726   case MVT::f64: {
727     if (UnsupportedFPMode)
728       return false;
729     ResultReg = createResultReg(&Mips::AFGR64RegClass);
730     Opc = Mips::LDC1;
731     break;
732   }
733   default:
734     return false;
735   }
736   if (Addr.isRegBase()) {
737     simplifyAddress(Addr);
738     emitInstLoad(Opc, ResultReg, Addr.getReg(), Addr.getOffset());
739     return true;
740   }
741   if (Addr.isFIBase()) {
742     unsigned FI = Addr.getFI();
743     unsigned Align = 4;
744     unsigned Offset = Addr.getOffset();
745     MachineFrameInfo &MFI = *MF->getFrameInfo();
746     MachineMemOperand *MMO = MF->getMachineMemOperand(
747         MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
748         MFI.getObjectSize(FI), Align);
749     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
750         .addFrameIndex(FI)
751         .addImm(Offset)
752         .addMemOperand(MMO);
753     return true;
754   }
755   return false;
756 }
757
758 bool MipsFastISel::emitStore(MVT VT, unsigned SrcReg, Address &Addr,
759                              unsigned Alignment) {
760   //
761   // more cases will be handled here in following patches.
762   //
763   unsigned Opc;
764   switch (VT.SimpleTy) {
765   case MVT::i8:
766     Opc = Mips::SB;
767     break;
768   case MVT::i16:
769     Opc = Mips::SH;
770     break;
771   case MVT::i32:
772     Opc = Mips::SW;
773     break;
774   case MVT::f32:
775     if (UnsupportedFPMode)
776       return false;
777     Opc = Mips::SWC1;
778     break;
779   case MVT::f64:
780     if (UnsupportedFPMode)
781       return false;
782     Opc = Mips::SDC1;
783     break;
784   default:
785     return false;
786   }
787   if (Addr.isRegBase()) {
788     simplifyAddress(Addr);
789     emitInstStore(Opc, SrcReg, Addr.getReg(), Addr.getOffset());
790     return true;
791   }
792   if (Addr.isFIBase()) {
793     unsigned FI = Addr.getFI();
794     unsigned Align = 4;
795     unsigned Offset = Addr.getOffset();
796     MachineFrameInfo &MFI = *MF->getFrameInfo();
797     MachineMemOperand *MMO = MF->getMachineMemOperand(
798         MachinePointerInfo::getFixedStack(FI), MachineMemOperand::MOLoad,
799         MFI.getObjectSize(FI), Align);
800     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
801         .addReg(SrcReg)
802         .addFrameIndex(FI)
803         .addImm(Offset)
804         .addMemOperand(MMO);
805     return true;
806   }
807   return false;
808 }
809
810 bool MipsFastISel::selectLogicalOp(const Instruction *I) {
811   MVT VT;
812   if (!isTypeSupported(I->getType(), VT))
813     return false;
814
815   unsigned ResultReg;
816   switch (I->getOpcode()) {
817   default:
818     llvm_unreachable("Unexpected instruction.");
819   case Instruction::And:
820     ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
821     break;
822   case Instruction::Or:
823     ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
824     break;
825   case Instruction::Xor:
826     ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
827     break;
828   }
829
830   if (!ResultReg)
831     return false;
832
833   updateValueMap(I, ResultReg);
834   return true;
835 }
836
837 bool MipsFastISel::selectLoad(const Instruction *I) {
838   // Atomic loads need special handling.
839   if (cast<LoadInst>(I)->isAtomic())
840     return false;
841
842   // Verify we have a legal type before going any further.
843   MVT VT;
844   if (!isLoadTypeLegal(I->getType(), VT))
845     return false;
846
847   // See if we can handle this address.
848   Address Addr;
849   if (!computeAddress(I->getOperand(0), Addr))
850     return false;
851
852   unsigned ResultReg;
853   if (!emitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
854     return false;
855   updateValueMap(I, ResultReg);
856   return true;
857 }
858
859 bool MipsFastISel::selectStore(const Instruction *I) {
860   Value *Op0 = I->getOperand(0);
861   unsigned SrcReg = 0;
862
863   // Atomic stores need special handling.
864   if (cast<StoreInst>(I)->isAtomic())
865     return false;
866
867   // Verify we have a legal type before going any further.
868   MVT VT;
869   if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
870     return false;
871
872   // Get the value to be stored into a register.
873   SrcReg = getRegForValue(Op0);
874   if (SrcReg == 0)
875     return false;
876
877   // See if we can handle this address.
878   Address Addr;
879   if (!computeAddress(I->getOperand(1), Addr))
880     return false;
881
882   if (!emitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
883     return false;
884   return true;
885 }
886
887 //
888 // This can cause a redundant sltiu to be generated.
889 // FIXME: try and eliminate this in a future patch.
890 //
891 bool MipsFastISel::selectBranch(const Instruction *I) {
892   const BranchInst *BI = cast<BranchInst>(I);
893   MachineBasicBlock *BrBB = FuncInfo.MBB;
894   //
895   // TBB is the basic block for the case where the comparison is true.
896   // FBB is the basic block for the case where the comparison is false.
897   // if (cond) goto TBB
898   // goto FBB
899   // TBB:
900   //
901   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
902   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
903   BI->getCondition();
904   // For now, just try the simplest case where it's fed by a compare.
905   if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
906     unsigned CondReg = createResultReg(&Mips::GPR32RegClass);
907     if (!emitCmp(CondReg, CI))
908       return false;
909     BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
910         .addReg(CondReg)
911         .addMBB(TBB);
912     fastEmitBranch(FBB, DbgLoc);
913     FuncInfo.MBB->addSuccessor(TBB);
914     return true;
915   }
916   return false;
917 }
918
919 bool MipsFastISel::selectCmp(const Instruction *I) {
920   const CmpInst *CI = cast<CmpInst>(I);
921   unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
922   if (!emitCmp(ResultReg, CI))
923     return false;
924   updateValueMap(I, ResultReg);
925   return true;
926 }
927
928 // Attempt to fast-select a floating-point extend instruction.
929 bool MipsFastISel::selectFPExt(const Instruction *I) {
930   if (UnsupportedFPMode)
931     return false;
932   Value *Src = I->getOperand(0);
933   EVT SrcVT = TLI.getValueType(Src->getType(), true);
934   EVT DestVT = TLI.getValueType(I->getType(), true);
935
936   if (SrcVT != MVT::f32 || DestVT != MVT::f64)
937     return false;
938
939   unsigned SrcReg =
940       getRegForValue(Src); // his must be a 32 bit floating point register class
941                            // maybe we should handle this differently
942   if (!SrcReg)
943     return false;
944
945   unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
946   emitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
947   updateValueMap(I, DestReg);
948   return true;
949 }
950
951 bool MipsFastISel::selectSelect(const Instruction *I) {
952   assert(isa<SelectInst>(I) && "Expected a select instruction.");
953
954   MVT VT;
955   if (!isTypeSupported(I->getType(), VT))
956     return false;
957
958   unsigned CondMovOpc;
959   const TargetRegisterClass *RC;
960
961   if (VT.isInteger() && !VT.isVector() && VT.getSizeInBits() <= 32) {
962     CondMovOpc = Mips::MOVN_I_I;
963     RC = &Mips::GPR32RegClass;
964   } else if (VT == MVT::f32) {
965     CondMovOpc = Mips::MOVN_I_S;
966     RC = &Mips::FGR32RegClass;
967   } else if (VT == MVT::f64) {
968     CondMovOpc = Mips::MOVN_I_D32;
969     RC = &Mips::AFGR64RegClass;
970   } else
971     return false;
972
973   const SelectInst *SI = cast<SelectInst>(I);
974   const Value *Cond = SI->getCondition();
975   unsigned Src1Reg = getRegForValue(SI->getTrueValue());
976   unsigned Src2Reg = getRegForValue(SI->getFalseValue());
977   unsigned CondReg = getRegForValue(Cond);
978
979   if (!Src1Reg || !Src2Reg || !CondReg)
980     return false;
981
982   unsigned ResultReg = createResultReg(RC);
983   unsigned TempReg = createResultReg(RC);
984
985   if (!ResultReg || !TempReg)
986     return false;
987
988   emitInst(TargetOpcode::COPY, TempReg).addReg(Src2Reg);
989   emitInst(CondMovOpc, ResultReg)
990     .addReg(Src1Reg).addReg(CondReg).addReg(TempReg);
991   updateValueMap(I, ResultReg);
992   return true;
993 }
994
995 // Attempt to fast-select a floating-point truncate instruction.
996 bool MipsFastISel::selectFPTrunc(const Instruction *I) {
997   if (UnsupportedFPMode)
998     return false;
999   Value *Src = I->getOperand(0);
1000   EVT SrcVT = TLI.getValueType(Src->getType(), true);
1001   EVT DestVT = TLI.getValueType(I->getType(), true);
1002
1003   if (SrcVT != MVT::f64 || DestVT != MVT::f32)
1004     return false;
1005
1006   unsigned SrcReg = getRegForValue(Src);
1007   if (!SrcReg)
1008     return false;
1009
1010   unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
1011   if (!DestReg)
1012     return false;
1013
1014   emitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
1015   updateValueMap(I, DestReg);
1016   return true;
1017 }
1018
1019 // Attempt to fast-select a floating-point-to-integer conversion.
1020 bool MipsFastISel::selectFPToInt(const Instruction *I, bool IsSigned) {
1021   if (UnsupportedFPMode)
1022     return false;
1023   MVT DstVT, SrcVT;
1024   if (!IsSigned)
1025     return false; // We don't handle this case yet. There is no native
1026                   // instruction for this but it can be synthesized.
1027   Type *DstTy = I->getType();
1028   if (!isTypeLegal(DstTy, DstVT))
1029     return false;
1030
1031   if (DstVT != MVT::i32)
1032     return false;
1033
1034   Value *Src = I->getOperand(0);
1035   Type *SrcTy = Src->getType();
1036   if (!isTypeLegal(SrcTy, SrcVT))
1037     return false;
1038
1039   if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
1040     return false;
1041
1042   unsigned SrcReg = getRegForValue(Src);
1043   if (SrcReg == 0)
1044     return false;
1045
1046   // Determine the opcode for the conversion, which takes place
1047   // entirely within FPRs.
1048   unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1049   unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
1050   unsigned Opc;
1051
1052   if (SrcVT == MVT::f32)
1053     Opc = Mips::TRUNC_W_S;
1054   else
1055     Opc = Mips::TRUNC_W_D32;
1056
1057   // Generate the convert.
1058   emitInst(Opc, TempReg).addReg(SrcReg);
1059
1060   emitInst(Mips::MFC1, DestReg).addReg(TempReg);
1061
1062   updateValueMap(I, DestReg);
1063   return true;
1064 }
1065 //
1066 bool MipsFastISel::processCallArgs(CallLoweringInfo &CLI,
1067                                    SmallVectorImpl<MVT> &OutVTs,
1068                                    unsigned &NumBytes) {
1069   CallingConv::ID CC = CLI.CallConv;
1070   SmallVector<CCValAssign, 16> ArgLocs;
1071   CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1072   CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1073   // Get a count of how many bytes are to be pushed on the stack.
1074   NumBytes = CCInfo.getNextStackOffset();
1075   // This is the minimum argument area used for A0-A3.
1076   if (NumBytes < 16)
1077     NumBytes = 16;
1078
1079   emitInst(Mips::ADJCALLSTACKDOWN).addImm(16);
1080   // Process the args.
1081   MVT firstMVT;
1082   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1083     CCValAssign &VA = ArgLocs[i];
1084     const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1085     MVT ArgVT = OutVTs[VA.getValNo()];
1086
1087     if (i == 0) {
1088       firstMVT = ArgVT;
1089       if (ArgVT == MVT::f32) {
1090         VA.convertToReg(Mips::F12);
1091       } else if (ArgVT == MVT::f64) {
1092         VA.convertToReg(Mips::D6);
1093       }
1094     } else if (i == 1) {
1095       if ((firstMVT == MVT::f32) || (firstMVT == MVT::f64)) {
1096         if (ArgVT == MVT::f32) {
1097           VA.convertToReg(Mips::F14);
1098         } else if (ArgVT == MVT::f64) {
1099           VA.convertToReg(Mips::D7);
1100         }
1101       }
1102     }
1103     if (((ArgVT == MVT::i32) || (ArgVT == MVT::f32) || (ArgVT == MVT::i16) ||
1104          (ArgVT == MVT::i8)) &&
1105         VA.isMemLoc()) {
1106       switch (VA.getLocMemOffset()) {
1107       case 0:
1108         VA.convertToReg(Mips::A0);
1109         break;
1110       case 4:
1111         VA.convertToReg(Mips::A1);
1112         break;
1113       case 8:
1114         VA.convertToReg(Mips::A2);
1115         break;
1116       case 12:
1117         VA.convertToReg(Mips::A3);
1118         break;
1119       default:
1120         break;
1121       }
1122     }
1123     unsigned ArgReg = getRegForValue(ArgVal);
1124     if (!ArgReg)
1125       return false;
1126
1127     // Handle arg promotion: SExt, ZExt, AExt.
1128     switch (VA.getLocInfo()) {
1129     case CCValAssign::Full:
1130       break;
1131     case CCValAssign::AExt:
1132     case CCValAssign::SExt: {
1133       MVT DestVT = VA.getLocVT();
1134       MVT SrcVT = ArgVT;
1135       ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1136       if (!ArgReg)
1137         return false;
1138       break;
1139     }
1140     case CCValAssign::ZExt: {
1141       MVT DestVT = VA.getLocVT();
1142       MVT SrcVT = ArgVT;
1143       ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1144       if (!ArgReg)
1145         return false;
1146       break;
1147     }
1148     default:
1149       llvm_unreachable("Unknown arg promotion!");
1150     }
1151
1152     // Now copy/store arg to correct locations.
1153     if (VA.isRegLoc() && !VA.needsCustom()) {
1154       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1155               TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1156       CLI.OutRegs.push_back(VA.getLocReg());
1157     } else if (VA.needsCustom()) {
1158       llvm_unreachable("Mips does not use custom args.");
1159       return false;
1160     } else {
1161       //
1162       // FIXME: This path will currently return false. It was copied
1163       // from the AArch64 port and should be essentially fine for Mips too.
1164       // The work to finish up this path will be done in a follow-on patch.
1165       //
1166       assert(VA.isMemLoc() && "Assuming store on stack.");
1167       // Don't emit stores for undef values.
1168       if (isa<UndefValue>(ArgVal))
1169         continue;
1170
1171       // Need to store on the stack.
1172       // FIXME: This alignment is incorrect but this path is disabled
1173       // for now (will return false). We need to determine the right alignment
1174       // based on the normal alignment for the underlying machine type.
1175       //
1176       unsigned ArgSize = RoundUpToAlignment(ArgVT.getSizeInBits(), 4);
1177
1178       unsigned BEAlign = 0;
1179       if (ArgSize < 8 && !Subtarget->isLittle())
1180         BEAlign = 8 - ArgSize;
1181
1182       Address Addr;
1183       Addr.setKind(Address::RegBase);
1184       Addr.setReg(Mips::SP);
1185       Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1186
1187       unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1188       MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1189           MachinePointerInfo::getStack(Addr.getOffset()),
1190           MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1191       (void)(MMO);
1192       // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1193       return false; // can't store on the stack yet.
1194     }
1195   }
1196
1197   return true;
1198 }
1199
1200 bool MipsFastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
1201                               unsigned NumBytes) {
1202   CallingConv::ID CC = CLI.CallConv;
1203   emitInst(Mips::ADJCALLSTACKUP).addImm(16);
1204   if (RetVT != MVT::isVoid) {
1205     SmallVector<CCValAssign, 16> RVLocs;
1206     CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1207     CCInfo.AnalyzeCallResult(RetVT, RetCC_Mips);
1208
1209     // Only handle a single return value.
1210     if (RVLocs.size() != 1)
1211       return false;
1212     // Copy all of the result registers out of their specified physreg.
1213     MVT CopyVT = RVLocs[0].getValVT();
1214     // Special handling for extended integers.
1215     if (RetVT == MVT::i1 || RetVT == MVT::i8 || RetVT == MVT::i16)
1216       CopyVT = MVT::i32;
1217
1218     unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1219     if (!ResultReg)
1220       return false;
1221     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1222             TII.get(TargetOpcode::COPY),
1223             ResultReg).addReg(RVLocs[0].getLocReg());
1224     CLI.InRegs.push_back(RVLocs[0].getLocReg());
1225
1226     CLI.ResultReg = ResultReg;
1227     CLI.NumResultRegs = 1;
1228   }
1229   return true;
1230 }
1231
1232 bool MipsFastISel::fastLowerCall(CallLoweringInfo &CLI) {
1233   CallingConv::ID CC = CLI.CallConv;
1234   bool IsTailCall = CLI.IsTailCall;
1235   bool IsVarArg = CLI.IsVarArg;
1236   const Value *Callee = CLI.Callee;
1237   const char *SymName = CLI.SymName;
1238
1239   // Allow SelectionDAG isel to handle tail calls.
1240   if (IsTailCall)
1241     return false;
1242
1243   // Let SDISel handle vararg functions.
1244   if (IsVarArg)
1245     return false;
1246
1247   // FIXME: Only handle *simple* calls for now.
1248   MVT RetVT;
1249   if (CLI.RetTy->isVoidTy())
1250     RetVT = MVT::isVoid;
1251   else if (!isTypeSupported(CLI.RetTy, RetVT))
1252     return false;
1253
1254   for (auto Flag : CLI.OutFlags)
1255     if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1256       return false;
1257
1258   // Set up the argument vectors.
1259   SmallVector<MVT, 16> OutVTs;
1260   OutVTs.reserve(CLI.OutVals.size());
1261
1262   for (auto *Val : CLI.OutVals) {
1263     MVT VT;
1264     if (!isTypeLegal(Val->getType(), VT) &&
1265         !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1266       return false;
1267
1268     // We don't handle vector parameters yet.
1269     if (VT.isVector() || VT.getSizeInBits() > 64)
1270       return false;
1271
1272     OutVTs.push_back(VT);
1273   }
1274
1275   Address Addr;
1276   if (!computeCallAddress(Callee, Addr))
1277     return false;
1278
1279   // Handle the arguments now that we've gotten them.
1280   unsigned NumBytes;
1281   if (!processCallArgs(CLI, OutVTs, NumBytes))
1282     return false;
1283
1284   if (!Addr.getGlobalValue())
1285     return false;
1286
1287   // Issue the call.
1288   unsigned DestAddress;
1289   if (SymName)
1290     DestAddress = materializeExternalCallSym(SymName);
1291   else
1292     DestAddress = materializeGV(Addr.getGlobalValue(), MVT::i32);
1293   emitInst(TargetOpcode::COPY, Mips::T9).addReg(DestAddress);
1294   MachineInstrBuilder MIB =
1295       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::JALR),
1296               Mips::RA).addReg(Mips::T9);
1297
1298   // Add implicit physical register uses to the call.
1299   for (auto Reg : CLI.OutRegs)
1300     MIB.addReg(Reg, RegState::Implicit);
1301
1302   // Add a register mask with the call-preserved registers.
1303   // Proper defs for return values will be added by setPhysRegsDeadExcept().
1304   MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
1305
1306   CLI.Call = MIB;
1307
1308   // Finish off the call including any return values.
1309   return finishCall(CLI, RetVT, NumBytes);
1310 }
1311
1312 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
1313   switch (II->getIntrinsicID()) {
1314   default:
1315     return false;
1316   case Intrinsic::memcpy:
1317   case Intrinsic::memmove: {
1318     const auto *MTI = cast<MemTransferInst>(II);
1319     // Don't handle volatile.
1320     if (MTI->isVolatile())
1321       return false;
1322     if (!MTI->getLength()->getType()->isIntegerTy(32))
1323       return false;
1324     const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
1325     return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
1326   }
1327   case Intrinsic::memset: {
1328     const MemSetInst *MSI = cast<MemSetInst>(II);
1329     // Don't handle volatile.
1330     if (MSI->isVolatile())
1331       return false;
1332     if (!MSI->getLength()->getType()->isIntegerTy(32))
1333       return false;
1334     return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
1335   }
1336   }
1337   return false;
1338 }
1339
1340 bool MipsFastISel::selectRet(const Instruction *I) {
1341   const Function &F = *I->getParent()->getParent();
1342   const ReturnInst *Ret = cast<ReturnInst>(I);
1343
1344   if (!FuncInfo.CanLowerReturn)
1345     return false;
1346
1347   // Build a list of return value registers.
1348   SmallVector<unsigned, 4> RetRegs;
1349
1350   if (Ret->getNumOperands() > 0) {
1351     CallingConv::ID CC = F.getCallingConv();
1352     SmallVector<ISD::OutputArg, 4> Outs;
1353     GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1354     // Analyze operands of the call, assigning locations to each operand.
1355     SmallVector<CCValAssign, 16> ValLocs;
1356     MipsCCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs,
1357                        I->getContext());
1358     CCAssignFn *RetCC = RetCC_Mips;
1359     CCInfo.AnalyzeReturn(Outs, RetCC);
1360
1361     // Only handle a single return value for now.
1362     if (ValLocs.size() != 1)
1363       return false;
1364
1365     CCValAssign &VA = ValLocs[0];
1366     const Value *RV = Ret->getOperand(0);
1367
1368     // Don't bother handling odd stuff for now.
1369     if ((VA.getLocInfo() != CCValAssign::Full) &&
1370         (VA.getLocInfo() != CCValAssign::BCvt))
1371       return false;
1372
1373     // Only handle register returns for now.
1374     if (!VA.isRegLoc())
1375       return false;
1376
1377     unsigned Reg = getRegForValue(RV);
1378     if (Reg == 0)
1379       return false;
1380
1381     unsigned SrcReg = Reg + VA.getValNo();
1382     unsigned DestReg = VA.getLocReg();
1383     // Avoid a cross-class copy. This is very unlikely.
1384     if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1385       return false;
1386
1387     EVT RVEVT = TLI.getValueType(RV->getType());
1388     if (!RVEVT.isSimple())
1389       return false;
1390
1391     if (RVEVT.isVector())
1392       return false;
1393
1394     MVT RVVT = RVEVT.getSimpleVT();
1395     if (RVVT == MVT::f128)
1396       return false;
1397
1398     MVT DestVT = VA.getValVT();
1399     // Special handling for extended integers.
1400     if (RVVT != DestVT) {
1401       if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1402         return false;
1403
1404       if (Outs[0].Flags.isZExt() || Outs[0].Flags.isSExt()) {
1405         bool IsZExt = Outs[0].Flags.isZExt();
1406         SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
1407         if (SrcReg == 0)
1408           return false;
1409       }
1410     }
1411
1412     // Make the copy.
1413     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1414             TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1415
1416     // Add register to return instruction.
1417     RetRegs.push_back(VA.getLocReg());
1418   }
1419   MachineInstrBuilder MIB = emitInst(Mips::RetRA);
1420   for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1421     MIB.addReg(RetRegs[i], RegState::Implicit);
1422   return true;
1423 }
1424
1425 bool MipsFastISel::selectTrunc(const Instruction *I) {
1426   // The high bits for a type smaller than the register size are assumed to be
1427   // undefined.
1428   Value *Op = I->getOperand(0);
1429
1430   EVT SrcVT, DestVT;
1431   SrcVT = TLI.getValueType(Op->getType(), true);
1432   DestVT = TLI.getValueType(I->getType(), true);
1433
1434   if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
1435     return false;
1436   if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
1437     return false;
1438
1439   unsigned SrcReg = getRegForValue(Op);
1440   if (!SrcReg)
1441     return false;
1442
1443   // Because the high bits are undefined, a truncate doesn't generate
1444   // any code.
1445   updateValueMap(I, SrcReg);
1446   return true;
1447 }
1448 bool MipsFastISel::selectIntExt(const Instruction *I) {
1449   Type *DestTy = I->getType();
1450   Value *Src = I->getOperand(0);
1451   Type *SrcTy = Src->getType();
1452
1453   bool isZExt = isa<ZExtInst>(I);
1454   unsigned SrcReg = getRegForValue(Src);
1455   if (!SrcReg)
1456     return false;
1457
1458   EVT SrcEVT, DestEVT;
1459   SrcEVT = TLI.getValueType(SrcTy, true);
1460   DestEVT = TLI.getValueType(DestTy, true);
1461   if (!SrcEVT.isSimple())
1462     return false;
1463   if (!DestEVT.isSimple())
1464     return false;
1465
1466   MVT SrcVT = SrcEVT.getSimpleVT();
1467   MVT DestVT = DestEVT.getSimpleVT();
1468   unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1469
1470   if (!emitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
1471     return false;
1472   updateValueMap(I, ResultReg);
1473   return true;
1474 }
1475 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1476                                    unsigned DestReg) {
1477   unsigned ShiftAmt;
1478   switch (SrcVT.SimpleTy) {
1479   default:
1480     return false;
1481   case MVT::i8:
1482     ShiftAmt = 24;
1483     break;
1484   case MVT::i16:
1485     ShiftAmt = 16;
1486     break;
1487   }
1488   unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1489   emitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
1490   emitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
1491   return true;
1492 }
1493
1494 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1495                                    unsigned DestReg) {
1496   switch (SrcVT.SimpleTy) {
1497   default:
1498     return false;
1499   case MVT::i8:
1500     emitInst(Mips::SEB, DestReg).addReg(SrcReg);
1501     break;
1502   case MVT::i16:
1503     emitInst(Mips::SEH, DestReg).addReg(SrcReg);
1504     break;
1505   }
1506   return true;
1507 }
1508
1509 bool MipsFastISel::emitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1510                                unsigned DestReg) {
1511   if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
1512     return false;
1513   if (Subtarget->hasMips32r2())
1514     return emitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
1515   return emitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
1516 }
1517
1518 bool MipsFastISel::emitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1519                                unsigned DestReg) {
1520   switch (SrcVT.SimpleTy) {
1521   default:
1522     return false;
1523   case MVT::i1:
1524     emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(1);
1525     break;
1526   case MVT::i8:
1527     emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xff);
1528     break;
1529   case MVT::i16:
1530     emitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xffff);
1531     break;
1532   }
1533   return true;
1534 }
1535
1536 bool MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1537                               unsigned DestReg, bool IsZExt) {
1538   // FastISel does not have plumbing to deal with extensions where the SrcVT or
1539   // DestVT are odd things, so test to make sure that they are both types we can
1540   // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1541   // bail out to SelectionDAG.
1542   if (((DestVT != MVT::i8) && (DestVT != MVT::i16) && (DestVT != MVT::i32)) ||
1543       ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) && (SrcVT != MVT::i16)))
1544     return false;
1545   if (IsZExt)
1546     return emitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
1547   return emitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
1548 }
1549
1550 unsigned MipsFastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1551                                   bool isZExt) {
1552   unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1553   bool Success = emitIntExt(SrcVT, SrcReg, DestVT, DestReg, isZExt);
1554   return Success ? DestReg : 0;
1555 }
1556
1557 bool MipsFastISel::selectDivRem(const Instruction *I, unsigned ISDOpcode) {
1558   EVT DestEVT = TLI.getValueType(I->getType(), true);
1559   if (!DestEVT.isSimple())
1560     return false;
1561
1562   MVT DestVT = DestEVT.getSimpleVT();
1563   if (DestVT != MVT::i32)
1564     return false;
1565
1566   unsigned DivOpc;
1567   switch (ISDOpcode) {
1568   default:
1569     return false;
1570   case ISD::SDIV:
1571   case ISD::SREM:
1572     DivOpc = Mips::SDIV;
1573     break;
1574   case ISD::UDIV:
1575   case ISD::UREM:
1576     DivOpc = Mips::UDIV;
1577     break;
1578   }
1579
1580   unsigned Src0Reg = getRegForValue(I->getOperand(0));
1581   unsigned Src1Reg = getRegForValue(I->getOperand(1));
1582   if (!Src0Reg || !Src1Reg)
1583     return false;
1584
1585   emitInst(DivOpc).addReg(Src0Reg).addReg(Src1Reg);
1586   emitInst(Mips::TEQ).addReg(Src1Reg).addReg(Mips::ZERO).addImm(7);
1587
1588   unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1589   if (!ResultReg)
1590     return false;
1591
1592   unsigned MFOpc = (ISDOpcode == ISD::SREM || ISDOpcode == ISD::UREM)
1593                        ? Mips::MFHI
1594                        : Mips::MFLO;
1595   emitInst(MFOpc, ResultReg);
1596
1597   updateValueMap(I, ResultReg);
1598   return true;
1599 }
1600
1601 bool MipsFastISel::selectShift(const Instruction *I) {
1602   MVT RetVT;
1603
1604   if (!isTypeSupported(I->getType(), RetVT))
1605     return false;
1606
1607   unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
1608   if (!ResultReg)
1609     return false;
1610
1611   unsigned Opcode = I->getOpcode();
1612   const Value *Op0 = I->getOperand(0);
1613   unsigned Op0Reg = getRegForValue(Op0);
1614   if (!Op0Reg)
1615     return false;
1616
1617   // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1618   if (Opcode == Instruction::AShr || Opcode == Instruction::LShr) {
1619     unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1620     if (!TempReg)
1621       return false;
1622
1623     MVT Op0MVT = TLI.getValueType(Op0->getType(), true).getSimpleVT();
1624     bool IsZExt = Opcode == Instruction::LShr;
1625     if (!emitIntExt(Op0MVT, Op0Reg, MVT::i32, TempReg, IsZExt))
1626       return false;
1627
1628     Op0Reg = TempReg;
1629   }
1630
1631   if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
1632     uint64_t ShiftVal = C->getZExtValue();
1633
1634     switch (Opcode) {
1635     default:
1636       llvm_unreachable("Unexpected instruction.");
1637     case Instruction::Shl:
1638       Opcode = Mips::SLL;
1639       break;
1640     case Instruction::AShr:
1641       Opcode = Mips::SRA;
1642       break;
1643     case Instruction::LShr:
1644       Opcode = Mips::SRL;
1645       break;
1646     }
1647
1648     emitInst(Opcode, ResultReg).addReg(Op0Reg).addImm(ShiftVal);
1649     updateValueMap(I, ResultReg);
1650     return true;
1651   }
1652
1653   unsigned Op1Reg = getRegForValue(I->getOperand(1));
1654   if (!Op1Reg)
1655     return false;
1656
1657   switch (Opcode) {
1658   default:
1659     llvm_unreachable("Unexpected instruction.");
1660   case Instruction::Shl:
1661     Opcode = Mips::SLLV;
1662     break;
1663   case Instruction::AShr:
1664     Opcode = Mips::SRAV;
1665     break;
1666   case Instruction::LShr:
1667     Opcode = Mips::SRLV;
1668     break;
1669   }
1670
1671   emitInst(Opcode, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1672   updateValueMap(I, ResultReg);
1673   return true;
1674 }
1675
1676 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
1677   if (!TargetSupported)
1678     return false;
1679   switch (I->getOpcode()) {
1680   default:
1681     break;
1682   case Instruction::Load:
1683     return selectLoad(I);
1684   case Instruction::Store:
1685     return selectStore(I);
1686   case Instruction::SDiv:
1687     if (!selectBinaryOp(I, ISD::SDIV))
1688       return selectDivRem(I, ISD::SDIV);
1689     return true;
1690   case Instruction::UDiv:
1691     if (!selectBinaryOp(I, ISD::UDIV))
1692       return selectDivRem(I, ISD::UDIV);
1693     return true;
1694   case Instruction::SRem:
1695     if (!selectBinaryOp(I, ISD::SREM))
1696       return selectDivRem(I, ISD::SREM);
1697     return true;
1698   case Instruction::URem:
1699     if (!selectBinaryOp(I, ISD::UREM))
1700       return selectDivRem(I, ISD::UREM);
1701     return true;
1702   case Instruction::Shl:
1703   case Instruction::LShr:
1704   case Instruction::AShr:
1705     return selectShift(I);
1706   case Instruction::And:
1707   case Instruction::Or:
1708   case Instruction::Xor:
1709     return selectLogicalOp(I);
1710   case Instruction::Br:
1711     return selectBranch(I);
1712   case Instruction::Ret:
1713     return selectRet(I);
1714   case Instruction::Trunc:
1715     return selectTrunc(I);
1716   case Instruction::ZExt:
1717   case Instruction::SExt:
1718     return selectIntExt(I);
1719   case Instruction::FPTrunc:
1720     return selectFPTrunc(I);
1721   case Instruction::FPExt:
1722     return selectFPExt(I);
1723   case Instruction::FPToSI:
1724     return selectFPToInt(I, /*isSigned*/ true);
1725   case Instruction::FPToUI:
1726     return selectFPToInt(I, /*isSigned*/ false);
1727   case Instruction::ICmp:
1728   case Instruction::FCmp:
1729     return selectCmp(I);
1730   case Instruction::Select:
1731     return selectSelect(I);
1732   }
1733   return false;
1734 }
1735
1736 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
1737                                                            bool IsUnsigned) {
1738   unsigned VReg = getRegForValue(V);
1739   if (VReg == 0)
1740     return 0;
1741   MVT VMVT = TLI.getValueType(V->getType(), true).getSimpleVT();
1742   if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
1743     unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
1744     if (!emitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
1745       return 0;
1746     VReg = TempReg;
1747   }
1748   return VReg;
1749 }
1750
1751 void MipsFastISel::simplifyAddress(Address &Addr) {
1752   if (!isInt<16>(Addr.getOffset())) {
1753     unsigned TempReg =
1754         materialize32BitInt(Addr.getOffset(), &Mips::GPR32RegClass);
1755     unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
1756     emitInst(Mips::ADDu, DestReg).addReg(TempReg).addReg(Addr.getReg());
1757     Addr.setReg(DestReg);
1758     Addr.setOffset(0);
1759   }
1760 }
1761
1762 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
1763                                        const TargetRegisterClass *RC,
1764                                        unsigned Op0, bool Op0IsKill,
1765                                        unsigned Op1, bool Op1IsKill) {
1766   // We treat the MUL instruction in a special way because it clobbers
1767   // the HI0 & LO0 registers. The TableGen definition of this instruction can
1768   // mark these registers only as implicitly defined. As a result, the
1769   // register allocator runs out of registers when this instruction is
1770   // followed by another instruction that defines the same registers too.
1771   // We can fix this by explicitly marking those registers as dead.
1772   if (MachineInstOpcode == Mips::MUL) {
1773     unsigned ResultReg = createResultReg(RC);
1774     const MCInstrDesc &II = TII.get(MachineInstOpcode);
1775     Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
1776     Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
1777     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1778       .addReg(Op0, getKillRegState(Op0IsKill))
1779       .addReg(Op1, getKillRegState(Op1IsKill))
1780       .addReg(Mips::HI0, RegState::ImplicitDefine | RegState::Dead)
1781       .addReg(Mips::LO0, RegState::ImplicitDefine | RegState::Dead);
1782     return ResultReg;
1783   }
1784
1785   return FastISel::fastEmitInst_rr(MachineInstOpcode, RC, Op0, Op0IsKill, Op1,
1786                                    Op1IsKill);
1787 }
1788
1789 namespace llvm {
1790 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
1791                                const TargetLibraryInfo *libInfo) {
1792   return new MipsFastISel(funcInfo, libInfo);
1793 }
1794 }