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