Implement floating point to integer conversion in mips fast-isel
[oota-llvm.git] / lib / Target / Mips / MipsFastISel.cpp
1 //===-- MipsastISel.cpp - Mips FastISel implementation
2 //---------------------===//
3
4 #include "llvm/CodeGen/FunctionLoweringInfo.h"
5 #include "llvm/CodeGen/FastISel.h"
6 #include "llvm/CodeGen/MachineInstrBuilder.h"
7 #include "llvm/IR/GlobalAlias.h"
8 #include "llvm/IR/GlobalVariable.h"
9 #include "llvm/Target/TargetInstrInfo.h"
10 #include "llvm/Target/TargetLibraryInfo.h"
11 #include "MipsRegisterInfo.h"
12 #include "MipsISelLowering.h"
13 #include "MipsMachineFunction.h"
14 #include "MipsSubtarget.h"
15 #include "MipsTargetMachine.h"
16
17 using namespace llvm;
18
19 namespace {
20
21 // All possible address modes.
22 typedef struct Address {
23   enum { RegBase, FrameIndexBase } BaseType;
24
25   union {
26     unsigned Reg;
27     int FI;
28   } Base;
29
30   int64_t Offset;
31
32   // Innocuous defaults for our address.
33   Address() : BaseType(RegBase), Offset(0) { Base.Reg = 0; }
34 } Address;
35
36 class MipsFastISel final : public FastISel {
37
38   /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
39   /// make the right decision when generating code for different targets.
40   Module &M;
41   const TargetMachine &TM;
42   const TargetInstrInfo &TII;
43   const TargetLowering &TLI;
44   const MipsSubtarget *Subtarget;
45   MipsFunctionInfo *MFI;
46
47   // Convenience variables to avoid some queries.
48   LLVMContext *Context;
49
50   bool TargetSupported;
51   bool UnsupportedFPMode;
52
53 public:
54   explicit MipsFastISel(FunctionLoweringInfo &funcInfo,
55                         const TargetLibraryInfo *libInfo)
56       : FastISel(funcInfo, libInfo),
57         M(const_cast<Module &>(*funcInfo.Fn->getParent())),
58         TM(funcInfo.MF->getTarget()),
59         TII(*TM.getSubtargetImpl()->getInstrInfo()),
60         TLI(*TM.getSubtargetImpl()->getTargetLowering()),
61         Subtarget(&TM.getSubtarget<MipsSubtarget>()) {
62     MFI = funcInfo.MF->getInfo<MipsFunctionInfo>();
63     Context = &funcInfo.Fn->getContext();
64     TargetSupported = ((Subtarget->getRelocationModel() == Reloc::PIC_) &&
65                        ((Subtarget->hasMips32r2() || Subtarget->hasMips32()) &&
66                         (Subtarget->isABI_O32())));
67     UnsupportedFPMode = Subtarget->isFP64bit();
68   }
69
70   bool fastSelectInstruction(const Instruction *I) override;
71   unsigned fastMaterializeConstant(const Constant *C) override;
72
73   bool ComputeAddress(const Value *Obj, Address &Addr);
74
75 private:
76   bool EmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
77                 unsigned Alignment = 0);
78   bool EmitStore(MVT VT, unsigned SrcReg, Address &Addr,
79                  unsigned Alignment = 0);
80   bool SelectLoad(const Instruction *I);
81   bool SelectRet(const Instruction *I);
82   bool SelectStore(const Instruction *I);
83   bool SelectIntExt(const Instruction *I);
84   bool SelectTrunc(const Instruction *I);
85   bool SelectFPExt(const Instruction *I);
86   bool SelectFPTrunc(const Instruction *I);
87   bool SelectFPToI(const Instruction *I, bool IsSigned);
88
89   bool isTypeLegal(Type *Ty, MVT &VT);
90   bool isLoadTypeLegal(Type *Ty, MVT &VT);
91
92   unsigned MaterializeFP(const ConstantFP *CFP, MVT VT);
93   unsigned MaterializeGV(const GlobalValue *GV, MVT VT);
94   unsigned MaterializeInt(const Constant *C, MVT VT);
95   unsigned Materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
96
97   bool EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
98                   bool IsZExt);
99
100   bool EmitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
101
102   bool EmitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
103   bool EmitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
104                        unsigned DestReg);
105   bool EmitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
106                        unsigned DestReg);
107   // for some reason, this default is not generated by tablegen
108   // so we explicitly generate it here.
109   //
110   unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
111                              unsigned Op0, bool Op0IsKill, uint64_t imm1,
112                              uint64_t imm2, unsigned Op3, bool Op3IsKill) {
113     return 0;
114   }
115
116   MachineInstrBuilder EmitInst(unsigned Opc) {
117     return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
118   }
119
120   MachineInstrBuilder EmitInst(unsigned Opc, unsigned DstReg) {
121     return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
122                    DstReg);
123   }
124
125   MachineInstrBuilder EmitInstStore(unsigned Opc, unsigned SrcReg,
126                                     unsigned MemReg, int64_t MemOffset) {
127     return EmitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
128   }
129
130   MachineInstrBuilder EmitInstLoad(unsigned Opc, unsigned DstReg,
131                                    unsigned MemReg, int64_t MemOffset) {
132     return EmitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
133   }
134
135 #include "MipsGenFastISel.inc"
136 };
137
138 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
139   EVT evt = TLI.getValueType(Ty, true);
140   // Only handle simple types.
141   if (evt == MVT::Other || !evt.isSimple())
142     return false;
143   VT = evt.getSimpleVT();
144
145   // Handle all legal types, i.e. a register that will directly hold this
146   // value.
147   return TLI.isTypeLegal(VT);
148 }
149
150 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
151   if (isTypeLegal(Ty, VT))
152     return true;
153   // We will extend this in a later patch:
154   //   If this is a type than can be sign or zero-extended to a basic operation
155   //   go ahead and accept it now.
156   if (VT == MVT::i8 || VT == MVT::i16)
157     return true;
158   return false;
159 }
160
161 bool MipsFastISel::ComputeAddress(const Value *Obj, Address &Addr) {
162   // This construct looks a big awkward but it is how other ports handle this
163   // and as this function is more fully completed, these cases which
164   // return false will have additional code in them.
165   //
166   if (isa<Instruction>(Obj))
167     return false;
168   else if (isa<ConstantExpr>(Obj))
169     return false;
170   Addr.Base.Reg = getRegForValue(Obj);
171   return Addr.Base.Reg != 0;
172 }
173
174 bool MipsFastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
175                             unsigned Alignment) {
176   //
177   // more cases will be handled here in following patches.
178   //
179   unsigned Opc;
180   switch (VT.SimpleTy) {
181   case MVT::i32: {
182     ResultReg = createResultReg(&Mips::GPR32RegClass);
183     Opc = Mips::LW;
184     break;
185   }
186   case MVT::i16: {
187     ResultReg = createResultReg(&Mips::GPR32RegClass);
188     Opc = Mips::LHu;
189     break;
190   }
191   case MVT::i8: {
192     ResultReg = createResultReg(&Mips::GPR32RegClass);
193     Opc = Mips::LBu;
194     break;
195   }
196   case MVT::f32: {
197     if (UnsupportedFPMode)
198       return false;
199     ResultReg = createResultReg(&Mips::FGR32RegClass);
200     Opc = Mips::LWC1;
201     break;
202   }
203   case MVT::f64: {
204     if (UnsupportedFPMode)
205       return false;
206     ResultReg = createResultReg(&Mips::AFGR64RegClass);
207     Opc = Mips::LDC1;
208     break;
209   }
210   default:
211     return false;
212   }
213   EmitInstLoad(Opc, ResultReg, Addr.Base.Reg, Addr.Offset);
214   return true;
215 }
216
217 // Materialize a constant into a register, and return the register
218 // number (or zero if we failed to handle it).
219 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
220   EVT CEVT = TLI.getValueType(C->getType(), true);
221
222   // Only handle simple types.
223   if (!CEVT.isSimple())
224     return 0;
225   MVT VT = CEVT.getSimpleVT();
226
227   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
228     return (UnsupportedFPMode) ? 0 : MaterializeFP(CFP, VT);
229   else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
230     return MaterializeGV(GV, VT);
231   else if (isa<ConstantInt>(C))
232     return MaterializeInt(C, VT);
233
234   return 0;
235 }
236
237 bool MipsFastISel::EmitStore(MVT VT, unsigned SrcReg, Address &Addr,
238                              unsigned Alignment) {
239   //
240   // more cases will be handled here in following patches.
241   //
242   unsigned Opc;
243   switch (VT.SimpleTy) {
244   case MVT::i8:
245     Opc = Mips::SB;
246     break;
247   case MVT::i16:
248     Opc = Mips::SH;
249     break;
250   case MVT::i32:
251     Opc = Mips::SW;
252     break;
253   case MVT::f32:
254     if (UnsupportedFPMode)
255       return false;
256     Opc = Mips::SWC1;
257     break;
258   case MVT::f64:
259     if (UnsupportedFPMode)
260       return false;
261     Opc = Mips::SDC1;
262     break;
263   default:
264     return false;
265   }
266   EmitInstStore(Opc, SrcReg, Addr.Base.Reg, Addr.Offset);
267   return true;
268 }
269
270 bool MipsFastISel::EmitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
271                                    unsigned DestReg) {
272   unsigned ShiftAmt;
273   switch (SrcVT.SimpleTy) {
274   default:
275     return false;
276   case MVT::i8:
277     ShiftAmt = 24;
278     break;
279   case MVT::i16:
280     ShiftAmt = 16;
281     break;
282   }
283   unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
284   EmitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
285   EmitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
286   return true;
287 }
288
289 bool MipsFastISel::EmitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
290                                    unsigned DestReg) {
291   switch (SrcVT.SimpleTy) {
292   default:
293     return false;
294   case MVT::i8:
295     EmitInst(Mips::SEB, DestReg).addReg(SrcReg);
296     break;
297   case MVT::i16:
298     EmitInst(Mips::SEH, DestReg).addReg(SrcReg);
299     break;
300   }
301   return true;
302 }
303
304 bool MipsFastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
305                               unsigned DestReg, bool IsZExt) {
306   if (IsZExt)
307     return EmitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
308   return EmitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
309 }
310
311 bool MipsFastISel::EmitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
312                                unsigned DestReg) {
313   if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
314     return false;
315   if (Subtarget->hasMips32r2())
316     return EmitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
317   return EmitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
318 }
319
320 bool MipsFastISel::EmitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
321                                unsigned DestReg) {
322   switch (SrcVT.SimpleTy) {
323   default:
324     return false;
325   case MVT::i1:
326     EmitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(1);
327     break;
328   case MVT::i8:
329     EmitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xff);
330     break;
331   case MVT::i16:
332     EmitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xffff);
333     break;
334   }
335   return true;
336 }
337
338 bool MipsFastISel::SelectLoad(const Instruction *I) {
339   // Atomic loads need special handling.
340   if (cast<LoadInst>(I)->isAtomic())
341     return false;
342
343   // Verify we have a legal type before going any further.
344   MVT VT;
345   if (!isLoadTypeLegal(I->getType(), VT))
346     return false;
347
348   // See if we can handle this address.
349   Address Addr;
350   if (!ComputeAddress(I->getOperand(0), Addr))
351     return false;
352
353   unsigned ResultReg;
354   if (!EmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
355     return false;
356   updateValueMap(I, ResultReg);
357   return true;
358 }
359
360 bool MipsFastISel::SelectStore(const Instruction *I) {
361   Value *Op0 = I->getOperand(0);
362   unsigned SrcReg = 0;
363
364   // Atomic stores need special handling.
365   if (cast<StoreInst>(I)->isAtomic())
366     return false;
367
368   // Verify we have a legal type before going any further.
369   MVT VT;
370   if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
371     return false;
372
373   // Get the value to be stored into a register.
374   SrcReg = getRegForValue(Op0);
375   if (SrcReg == 0)
376     return false;
377
378   // See if we can handle this address.
379   Address Addr;
380   if (!ComputeAddress(I->getOperand(1), Addr))
381     return false;
382
383   if (!EmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
384     return false;
385   return true;
386 }
387
388 bool MipsFastISel::SelectRet(const Instruction *I) {
389   const ReturnInst *Ret = cast<ReturnInst>(I);
390
391   if (!FuncInfo.CanLowerReturn)
392     return false;
393   if (Ret->getNumOperands() > 0) {
394     return false;
395   }
396   EmitInst(Mips::RetRA);
397   return true;
398 }
399
400 // Attempt to fast-select a floating-point extend instruction.
401 bool MipsFastISel::SelectFPExt(const Instruction *I) {
402   if (UnsupportedFPMode)
403     return false;
404   Value *Src = I->getOperand(0);
405   EVT SrcVT = TLI.getValueType(Src->getType(), true);
406   EVT DestVT = TLI.getValueType(I->getType(), true);
407
408   if (SrcVT != MVT::f32 || DestVT != MVT::f64)
409     return false;
410
411   unsigned SrcReg =
412       getRegForValue(Src); // his must be a 32 bit floating point register class
413                            // maybe we should handle this differently
414   if (!SrcReg)
415     return false;
416
417   unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
418   EmitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
419   updateValueMap(I, DestReg);
420   return true;
421 }
422
423 // Attempt to fast-select a floating-point truncate instruction.
424 bool MipsFastISel::SelectFPTrunc(const Instruction *I) {
425   if (UnsupportedFPMode)
426     return false;
427   Value *Src = I->getOperand(0);
428   EVT SrcVT = TLI.getValueType(Src->getType(), true);
429   EVT DestVT = TLI.getValueType(I->getType(), true);
430
431   if (SrcVT != MVT::f64 || DestVT != MVT::f32)
432     return false;
433
434   unsigned SrcReg = getRegForValue(Src);
435   if (!SrcReg)
436     return false;
437
438   unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
439   if (!DestReg)
440     return false;
441
442   EmitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
443   updateValueMap(I, DestReg);
444   return true;
445 }
446
447 bool MipsFastISel::SelectIntExt(const Instruction *I) {
448   Type *DestTy = I->getType();
449   Value *Src = I->getOperand(0);
450   Type *SrcTy = Src->getType();
451
452   bool isZExt = isa<ZExtInst>(I);
453   unsigned SrcReg = getRegForValue(Src);
454   if (!SrcReg)
455     return false;
456
457   EVT SrcEVT, DestEVT;
458   SrcEVT = TLI.getValueType(SrcTy, true);
459   DestEVT = TLI.getValueType(DestTy, true);
460   if (!SrcEVT.isSimple())
461     return false;
462   if (!DestEVT.isSimple())
463     return false;
464
465   MVT SrcVT = SrcEVT.getSimpleVT();
466   MVT DestVT = DestEVT.getSimpleVT();
467   unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
468
469   if (!EmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
470     return false;
471   updateValueMap(I, ResultReg);
472   return true;
473 }
474
475 bool MipsFastISel::SelectTrunc(const Instruction *I) {
476   // The high bits for a type smaller than the register size are assumed to be
477   // undefined.
478   Value *Op = I->getOperand(0);
479
480   EVT SrcVT, DestVT;
481   SrcVT = TLI.getValueType(Op->getType(), true);
482   DestVT = TLI.getValueType(I->getType(), true);
483
484   if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
485     return false;
486   if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
487     return false;
488
489   unsigned SrcReg = getRegForValue(Op);
490   if (!SrcReg)
491     return false;
492
493   // Because the high bits are undefined, a truncate doesn't generate
494   // any code.
495   updateValueMap(I, SrcReg);
496   return true;
497 }
498
499 // Attempt to fast-select a floating-point-to-integer conversion.
500 bool MipsFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
501   if (UnsupportedFPMode)
502     return false;
503   MVT DstVT, SrcVT;
504   if (!IsSigned)
505     return false; // We don't handle this case yet. There is no native
506                   // instruction for this but it can be synthesized.
507   Type *DstTy = I->getType();
508   if (!isTypeLegal(DstTy, DstVT))
509     return false;
510
511   if (DstVT != MVT::i32)
512     return false;
513
514   Value *Src = I->getOperand(0);
515   Type *SrcTy = Src->getType();
516   if (!isTypeLegal(SrcTy, SrcVT))
517     return false;
518
519   if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
520     return false;
521
522   unsigned SrcReg = getRegForValue(Src);
523   if (SrcReg == 0)
524     return false;
525
526   // Determine the opcode for the conversion, which takes place
527   // entirely within FPRs.
528   unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
529   unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
530   unsigned Opc;
531
532   if (SrcVT == MVT::f32)
533     Opc = Mips::TRUNC_W_S;
534   else
535     Opc = Mips::TRUNC_W_D32;
536
537   // Generate the convert.
538   EmitInst(Opc, TempReg).addReg(SrcReg);
539
540   EmitInst(Mips::MFC1, DestReg).addReg(TempReg);
541
542   updateValueMap(I, DestReg);
543   return true;
544 }
545
546 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
547   if (!TargetSupported)
548     return false;
549   switch (I->getOpcode()) {
550   default:
551     break;
552   case Instruction::Load:
553     return SelectLoad(I);
554   case Instruction::Store:
555     return SelectStore(I);
556   case Instruction::Ret:
557     return SelectRet(I);
558   case Instruction::Trunc:
559     return SelectTrunc(I);
560   case Instruction::ZExt:
561   case Instruction::SExt:
562     return SelectIntExt(I);
563   case Instruction::FPTrunc:
564     return SelectFPTrunc(I);
565   case Instruction::FPExt:
566     return SelectFPExt(I);
567   case Instruction::FPToSI:
568     return SelectFPToI(I, /*isSigned*/ true);
569   case Instruction::FPToUI:
570     return SelectFPToI(I, /*isSigned*/ false);
571   }
572   return false;
573 }
574
575 unsigned MipsFastISel::MaterializeFP(const ConstantFP *CFP, MVT VT) {
576   if (UnsupportedFPMode)
577     return 0;
578   int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
579   if (VT == MVT::f32) {
580     const TargetRegisterClass *RC = &Mips::FGR32RegClass;
581     unsigned DestReg = createResultReg(RC);
582     unsigned TempReg = Materialize32BitInt(Imm, &Mips::GPR32RegClass);
583     EmitInst(Mips::MTC1, DestReg).addReg(TempReg);
584     return DestReg;
585   } else if (VT == MVT::f64) {
586     const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
587     unsigned DestReg = createResultReg(RC);
588     unsigned TempReg1 = Materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
589     unsigned TempReg2 =
590         Materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
591     EmitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
592     return DestReg;
593   }
594   return 0;
595 }
596
597 unsigned MipsFastISel::MaterializeGV(const GlobalValue *GV, MVT VT) {
598   // For now 32-bit only.
599   if (VT != MVT::i32)
600     return 0;
601   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
602   unsigned DestReg = createResultReg(RC);
603   const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
604   bool IsThreadLocal = GVar && GVar->isThreadLocal();
605   // TLS not supported at this time.
606   if (IsThreadLocal)
607     return 0;
608   EmitInst(Mips::LW, DestReg)
609       .addReg(MFI->getGlobalBaseReg())
610       .addGlobalAddress(GV, 0, MipsII::MO_GOT);
611   if ((GV->hasInternalLinkage() ||
612        (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
613     unsigned TempReg = createResultReg(RC);
614     EmitInst(Mips::ADDiu, TempReg)
615         .addReg(DestReg)
616         .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
617     DestReg = TempReg;
618   }
619   return DestReg;
620 }
621
622 unsigned MipsFastISel::MaterializeInt(const Constant *C, MVT VT) {
623   if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
624     return 0;
625   const TargetRegisterClass *RC = &Mips::GPR32RegClass;
626   const ConstantInt *CI = cast<ConstantInt>(C);
627   int64_t Imm;
628   if ((VT != MVT::i1) && CI->isNegative())
629     Imm = CI->getSExtValue();
630   else
631     Imm = CI->getZExtValue();
632   return Materialize32BitInt(Imm, RC);
633 }
634
635 unsigned MipsFastISel::Materialize32BitInt(int64_t Imm,
636                                            const TargetRegisterClass *RC) {
637   unsigned ResultReg = createResultReg(RC);
638
639   if (isInt<16>(Imm)) {
640     unsigned Opc = Mips::ADDiu;
641     EmitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
642     return ResultReg;
643   } else if (isUInt<16>(Imm)) {
644     EmitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
645     return ResultReg;
646   }
647   unsigned Lo = Imm & 0xFFFF;
648   unsigned Hi = (Imm >> 16) & 0xFFFF;
649   if (Lo) {
650     // Both Lo and Hi have nonzero bits.
651     unsigned TmpReg = createResultReg(RC);
652     EmitInst(Mips::LUi, TmpReg).addImm(Hi);
653     EmitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
654   } else {
655     EmitInst(Mips::LUi, ResultReg).addImm(Hi);
656   }
657   return ResultReg;
658 }
659 }
660
661 namespace llvm {
662 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
663                                const TargetLibraryInfo *libInfo) {
664   return new MipsFastISel(funcInfo, libInfo);
665 }
666 }