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