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