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