1 //===-- MipsastISel.cpp - Mips FastISel implementation
2 //---------------------===//
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"
21 // All possible address modes.
22 typedef struct Address {
23 enum { RegBase, FrameIndexBase } BaseType;
32 // Innocuous defaults for our address.
33 Address() : BaseType(RegBase), Offset(0) { Base.Reg = 0; }
36 class MipsFastISel final : public FastISel {
38 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
39 /// make the right decision when generating code for different targets.
41 const TargetMachine &TM;
42 const TargetInstrInfo &TII;
43 const TargetLowering &TLI;
44 const MipsSubtarget *Subtarget;
45 MipsFunctionInfo *MFI;
47 // Convenience variables to avoid some queries.
51 bool UnsupportedFPMode;
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();
70 bool fastSelectInstruction(const Instruction *I) override;
71 unsigned fastMaterializeConstant(const Constant *C) override;
73 bool ComputeAddress(const Value *Obj, Address &Addr);
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 bool SelectCmp(const Instruction *I);
90 bool isTypeLegal(Type *Ty, MVT &VT);
91 bool isLoadTypeLegal(Type *Ty, MVT &VT);
93 unsigned getRegEnsuringSimpleIntegerWidening(const Value *, bool IsUnsigned);
95 unsigned MaterializeFP(const ConstantFP *CFP, MVT VT);
96 unsigned MaterializeGV(const GlobalValue *GV, MVT VT);
97 unsigned MaterializeInt(const Constant *C, MVT VT);
98 unsigned Materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC);
100 bool EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg,
103 bool EmitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
105 bool EmitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, unsigned DestReg);
106 bool EmitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
108 bool EmitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
110 // for some reason, this default is not generated by tablegen
111 // so we explicitly generate it here.
113 unsigned fastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC,
114 unsigned Op0, bool Op0IsKill, uint64_t imm1,
115 uint64_t imm2, unsigned Op3, bool Op3IsKill) {
119 MachineInstrBuilder EmitInst(unsigned Opc) {
120 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
123 MachineInstrBuilder EmitInst(unsigned Opc, unsigned DstReg) {
124 return BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
128 MachineInstrBuilder EmitInstStore(unsigned Opc, unsigned SrcReg,
129 unsigned MemReg, int64_t MemOffset) {
130 return EmitInst(Opc).addReg(SrcReg).addReg(MemReg).addImm(MemOffset);
133 MachineInstrBuilder EmitInstLoad(unsigned Opc, unsigned DstReg,
134 unsigned MemReg, int64_t MemOffset) {
135 return EmitInst(Opc, DstReg).addReg(MemReg).addImm(MemOffset);
138 #include "MipsGenFastISel.inc"
141 bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) {
142 EVT evt = TLI.getValueType(Ty, true);
143 // Only handle simple types.
144 if (evt == MVT::Other || !evt.isSimple())
146 VT = evt.getSimpleVT();
148 // Handle all legal types, i.e. a register that will directly hold this
150 return TLI.isTypeLegal(VT);
153 bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
154 if (isTypeLegal(Ty, VT))
156 // We will extend this in a later patch:
157 // If this is a type than can be sign or zero-extended to a basic operation
158 // go ahead and accept it now.
159 if (VT == MVT::i8 || VT == MVT::i16)
164 bool MipsFastISel::ComputeAddress(const Value *Obj, Address &Addr) {
165 // This construct looks a big awkward but it is how other ports handle this
166 // and as this function is more fully completed, these cases which
167 // return false will have additional code in them.
169 if (isa<Instruction>(Obj))
171 else if (isa<ConstantExpr>(Obj))
173 Addr.Base.Reg = getRegForValue(Obj);
174 return Addr.Base.Reg != 0;
177 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value *V,
179 unsigned VReg = getRegForValue(V);
182 MVT VMVT = TLI.getValueType(V->getType(), true).getSimpleVT();
183 if ((VMVT == MVT::i8) || (VMVT == MVT::i16)) {
184 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
185 if (!EmitIntExt(VMVT, VReg, MVT::i32, TempReg, IsUnsigned))
192 bool MipsFastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address &Addr,
193 unsigned Alignment) {
195 // more cases will be handled here in following patches.
198 switch (VT.SimpleTy) {
200 ResultReg = createResultReg(&Mips::GPR32RegClass);
205 ResultReg = createResultReg(&Mips::GPR32RegClass);
210 ResultReg = createResultReg(&Mips::GPR32RegClass);
215 if (UnsupportedFPMode)
217 ResultReg = createResultReg(&Mips::FGR32RegClass);
222 if (UnsupportedFPMode)
224 ResultReg = createResultReg(&Mips::AFGR64RegClass);
231 EmitInstLoad(Opc, ResultReg, Addr.Base.Reg, Addr.Offset);
235 // Materialize a constant into a register, and return the register
236 // number (or zero if we failed to handle it).
237 unsigned MipsFastISel::fastMaterializeConstant(const Constant *C) {
238 EVT CEVT = TLI.getValueType(C->getType(), true);
240 // Only handle simple types.
241 if (!CEVT.isSimple())
243 MVT VT = CEVT.getSimpleVT();
245 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
246 return (UnsupportedFPMode) ? 0 : MaterializeFP(CFP, VT);
247 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
248 return MaterializeGV(GV, VT);
249 else if (isa<ConstantInt>(C))
250 return MaterializeInt(C, VT);
255 bool MipsFastISel::EmitStore(MVT VT, unsigned SrcReg, Address &Addr,
256 unsigned Alignment) {
258 // more cases will be handled here in following patches.
261 switch (VT.SimpleTy) {
272 if (UnsupportedFPMode)
277 if (UnsupportedFPMode)
284 EmitInstStore(Opc, SrcReg, Addr.Base.Reg, Addr.Offset);
288 bool MipsFastISel::EmitIntSExt32r1(MVT SrcVT, unsigned SrcReg, MVT DestVT,
291 switch (SrcVT.SimpleTy) {
301 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
302 EmitInst(Mips::SLL, TempReg).addReg(SrcReg).addImm(ShiftAmt);
303 EmitInst(Mips::SRA, DestReg).addReg(TempReg).addImm(ShiftAmt);
307 bool MipsFastISel::EmitIntSExt32r2(MVT SrcVT, unsigned SrcReg, MVT DestVT,
309 switch (SrcVT.SimpleTy) {
313 EmitInst(Mips::SEB, DestReg).addReg(SrcReg);
316 EmitInst(Mips::SEH, DestReg).addReg(SrcReg);
322 bool MipsFastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
323 unsigned DestReg, bool IsZExt) {
325 return EmitIntZExt(SrcVT, SrcReg, DestVT, DestReg);
326 return EmitIntSExt(SrcVT, SrcReg, DestVT, DestReg);
329 bool MipsFastISel::EmitIntSExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
331 if ((DestVT != MVT::i32) && (DestVT != MVT::i16))
333 if (Subtarget->hasMips32r2())
334 return EmitIntSExt32r2(SrcVT, SrcReg, DestVT, DestReg);
335 return EmitIntSExt32r1(SrcVT, SrcReg, DestVT, DestReg);
338 bool MipsFastISel::EmitIntZExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
340 switch (SrcVT.SimpleTy) {
344 EmitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(1);
347 EmitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xff);
350 EmitInst(Mips::ANDi, DestReg).addReg(SrcReg).addImm(0xffff);
356 bool MipsFastISel::SelectLoad(const Instruction *I) {
357 // Atomic loads need special handling.
358 if (cast<LoadInst>(I)->isAtomic())
361 // Verify we have a legal type before going any further.
363 if (!isLoadTypeLegal(I->getType(), VT))
366 // See if we can handle this address.
368 if (!ComputeAddress(I->getOperand(0), Addr))
372 if (!EmitLoad(VT, ResultReg, Addr, cast<LoadInst>(I)->getAlignment()))
374 updateValueMap(I, ResultReg);
378 bool MipsFastISel::SelectStore(const Instruction *I) {
379 Value *Op0 = I->getOperand(0);
382 // Atomic stores need special handling.
383 if (cast<StoreInst>(I)->isAtomic())
386 // Verify we have a legal type before going any further.
388 if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT))
391 // Get the value to be stored into a register.
392 SrcReg = getRegForValue(Op0);
396 // See if we can handle this address.
398 if (!ComputeAddress(I->getOperand(1), Addr))
401 if (!EmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment()))
406 bool MipsFastISel::SelectRet(const Instruction *I) {
407 const ReturnInst *Ret = cast<ReturnInst>(I);
409 if (!FuncInfo.CanLowerReturn)
411 if (Ret->getNumOperands() > 0) {
414 EmitInst(Mips::RetRA);
418 // Attempt to fast-select a floating-point extend instruction.
419 bool MipsFastISel::SelectFPExt(const Instruction *I) {
420 if (UnsupportedFPMode)
422 Value *Src = I->getOperand(0);
423 EVT SrcVT = TLI.getValueType(Src->getType(), true);
424 EVT DestVT = TLI.getValueType(I->getType(), true);
426 if (SrcVT != MVT::f32 || DestVT != MVT::f64)
430 getRegForValue(Src); // his must be a 32 bit floating point register class
431 // maybe we should handle this differently
435 unsigned DestReg = createResultReg(&Mips::AFGR64RegClass);
436 EmitInst(Mips::CVT_D32_S, DestReg).addReg(SrcReg);
437 updateValueMap(I, DestReg);
441 // Attempt to fast-select a floating-point truncate instruction.
442 bool MipsFastISel::SelectFPTrunc(const Instruction *I) {
443 if (UnsupportedFPMode)
445 Value *Src = I->getOperand(0);
446 EVT SrcVT = TLI.getValueType(Src->getType(), true);
447 EVT DestVT = TLI.getValueType(I->getType(), true);
449 if (SrcVT != MVT::f64 || DestVT != MVT::f32)
452 unsigned SrcReg = getRegForValue(Src);
456 unsigned DestReg = createResultReg(&Mips::FGR32RegClass);
460 EmitInst(Mips::CVT_S_D32, DestReg).addReg(SrcReg);
461 updateValueMap(I, DestReg);
465 bool MipsFastISel::SelectIntExt(const Instruction *I) {
466 Type *DestTy = I->getType();
467 Value *Src = I->getOperand(0);
468 Type *SrcTy = Src->getType();
470 bool isZExt = isa<ZExtInst>(I);
471 unsigned SrcReg = getRegForValue(Src);
476 SrcEVT = TLI.getValueType(SrcTy, true);
477 DestEVT = TLI.getValueType(DestTy, true);
478 if (!SrcEVT.isSimple())
480 if (!DestEVT.isSimple())
483 MVT SrcVT = SrcEVT.getSimpleVT();
484 MVT DestVT = DestEVT.getSimpleVT();
485 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
487 if (!EmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, isZExt))
489 updateValueMap(I, ResultReg);
493 bool MipsFastISel::SelectTrunc(const Instruction *I) {
494 // The high bits for a type smaller than the register size are assumed to be
496 Value *Op = I->getOperand(0);
499 SrcVT = TLI.getValueType(Op->getType(), true);
500 DestVT = TLI.getValueType(I->getType(), true);
502 if (SrcVT != MVT::i32 && SrcVT != MVT::i16 && SrcVT != MVT::i8)
504 if (DestVT != MVT::i16 && DestVT != MVT::i8 && DestVT != MVT::i1)
507 unsigned SrcReg = getRegForValue(Op);
511 // Because the high bits are undefined, a truncate doesn't generate
513 updateValueMap(I, SrcReg);
517 // Attempt to fast-select a floating-point-to-integer conversion.
518 bool MipsFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
519 if (UnsupportedFPMode)
523 return false; // We don't handle this case yet. There is no native
524 // instruction for this but it can be synthesized.
525 Type *DstTy = I->getType();
526 if (!isTypeLegal(DstTy, DstVT))
529 if (DstVT != MVT::i32)
532 Value *Src = I->getOperand(0);
533 Type *SrcTy = Src->getType();
534 if (!isTypeLegal(SrcTy, SrcVT))
537 if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
540 unsigned SrcReg = getRegForValue(Src);
544 // Determine the opcode for the conversion, which takes place
545 // entirely within FPRs.
546 unsigned DestReg = createResultReg(&Mips::GPR32RegClass);
547 unsigned TempReg = createResultReg(&Mips::FGR32RegClass);
550 if (SrcVT == MVT::f32)
551 Opc = Mips::TRUNC_W_S;
553 Opc = Mips::TRUNC_W_D32;
555 // Generate the convert.
556 EmitInst(Opc, TempReg).addReg(SrcReg);
558 EmitInst(Mips::MFC1, DestReg).addReg(TempReg);
560 updateValueMap(I, DestReg);
565 // Because of how SelectCmp is called with fast-isel, you can
566 // end up with redundant "andi" instructions after the sequences emitted below.
567 // We should try and solve this issue in the future.
569 bool MipsFastISel::SelectCmp(const Instruction *I) {
570 const CmpInst *CI = cast<CmpInst>(I);
571 bool IsUnsigned = CI->isUnsigned();
572 const Value *Left = I->getOperand(0), *Right = I->getOperand(1);
574 unsigned LeftReg = getRegEnsuringSimpleIntegerWidening(Left, IsUnsigned);
577 unsigned RightReg = getRegEnsuringSimpleIntegerWidening(Right, IsUnsigned);
580 unsigned ResultReg = createResultReg(&Mips::GPR32RegClass);
581 CmpInst::Predicate P = CI->getPredicate();
585 case CmpInst::ICMP_EQ: {
586 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
587 EmitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
588 EmitInst(Mips::SLTiu, ResultReg).addReg(TempReg).addImm(1);
591 case CmpInst::ICMP_NE: {
592 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
593 EmitInst(Mips::XOR, TempReg).addReg(LeftReg).addReg(RightReg);
594 EmitInst(Mips::SLTu, ResultReg).addReg(Mips::ZERO).addReg(TempReg);
597 case CmpInst::ICMP_UGT: {
598 EmitInst(Mips::SLTu, ResultReg).addReg(RightReg).addReg(LeftReg);
601 case CmpInst::ICMP_ULT: {
602 EmitInst(Mips::SLTu, ResultReg).addReg(LeftReg).addReg(RightReg);
605 case CmpInst::ICMP_UGE: {
606 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
607 EmitInst(Mips::SLTu, TempReg).addReg(LeftReg).addReg(RightReg);
608 EmitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
611 case CmpInst::ICMP_ULE: {
612 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
613 EmitInst(Mips::SLTu, TempReg).addReg(RightReg).addReg(LeftReg);
614 EmitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
617 case CmpInst::ICMP_SGT: {
618 EmitInst(Mips::SLT, ResultReg).addReg(RightReg).addReg(LeftReg);
621 case CmpInst::ICMP_SLT: {
622 EmitInst(Mips::SLT, ResultReg).addReg(LeftReg).addReg(RightReg);
625 case CmpInst::ICMP_SGE: {
626 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
627 EmitInst(Mips::SLT, TempReg).addReg(LeftReg).addReg(RightReg);
628 EmitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
631 case CmpInst::ICMP_SLE: {
632 unsigned TempReg = createResultReg(&Mips::GPR32RegClass);
633 EmitInst(Mips::SLT, TempReg).addReg(RightReg).addReg(LeftReg);
634 EmitInst(Mips::XORi, ResultReg).addReg(TempReg).addImm(1);
637 case CmpInst::FCMP_OEQ:
638 case CmpInst::FCMP_UNE:
639 case CmpInst::FCMP_OLT:
640 case CmpInst::FCMP_OLE:
641 case CmpInst::FCMP_OGT:
642 case CmpInst::FCMP_OGE: {
643 if (UnsupportedFPMode)
645 bool IsFloat = Left->getType()->isFloatTy();
646 bool IsDouble = Left->getType()->isDoubleTy();
647 if (!IsFloat && !IsDouble)
649 unsigned Opc, CondMovOpc;
651 case CmpInst::FCMP_OEQ:
652 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
653 CondMovOpc = Mips::MOVT_I;
655 case CmpInst::FCMP_UNE:
656 Opc = IsFloat ? Mips::C_EQ_S : Mips::C_EQ_D32;
657 CondMovOpc = Mips::MOVF_I;
659 case CmpInst::FCMP_OLT:
660 Opc = IsFloat ? Mips::C_OLT_S : Mips::C_OLT_D32;
661 CondMovOpc = Mips::MOVT_I;
663 case CmpInst::FCMP_OLE:
664 Opc = IsFloat ? Mips::C_OLE_S : Mips::C_OLE_D32;
665 CondMovOpc = Mips::MOVT_I;
667 case CmpInst::FCMP_OGT:
668 Opc = IsFloat ? Mips::C_ULE_S : Mips::C_ULE_D32;
669 CondMovOpc = Mips::MOVF_I;
671 case CmpInst::FCMP_OGE:
672 Opc = IsFloat ? Mips::C_ULT_S : Mips::C_ULT_D32;
673 CondMovOpc = Mips::MOVF_I;
678 unsigned RegWithZero = createResultReg(&Mips::GPR32RegClass);
679 unsigned RegWithOne = createResultReg(&Mips::GPR32RegClass);
680 EmitInst(Mips::ADDiu, RegWithZero).addReg(Mips::ZERO).addImm(0);
681 EmitInst(Mips::ADDiu, RegWithOne).addReg(Mips::ZERO).addImm(1);
682 EmitInst(Opc).addReg(LeftReg).addReg(RightReg).addReg(
683 Mips::FCC0, RegState::ImplicitDefine);
684 MachineInstrBuilder MI = EmitInst(CondMovOpc, ResultReg)
687 .addReg(RegWithZero, RegState::Implicit);
688 MI->tieOperands(0, 3);
692 updateValueMap(I, ResultReg);
696 bool MipsFastISel::fastSelectInstruction(const Instruction *I) {
697 if (!TargetSupported)
699 switch (I->getOpcode()) {
702 case Instruction::Load:
703 return SelectLoad(I);
704 case Instruction::Store:
705 return SelectStore(I);
706 case Instruction::Ret:
708 case Instruction::Trunc:
709 return SelectTrunc(I);
710 case Instruction::ZExt:
711 case Instruction::SExt:
712 return SelectIntExt(I);
713 case Instruction::FPTrunc:
714 return SelectFPTrunc(I);
715 case Instruction::FPExt:
716 return SelectFPExt(I);
717 case Instruction::FPToSI:
718 return SelectFPToI(I, /*isSigned*/ true);
719 case Instruction::FPToUI:
720 return SelectFPToI(I, /*isSigned*/ false);
721 case Instruction::ICmp:
722 case Instruction::FCmp:
728 unsigned MipsFastISel::MaterializeFP(const ConstantFP *CFP, MVT VT) {
729 if (UnsupportedFPMode)
731 int64_t Imm = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
732 if (VT == MVT::f32) {
733 const TargetRegisterClass *RC = &Mips::FGR32RegClass;
734 unsigned DestReg = createResultReg(RC);
735 unsigned TempReg = Materialize32BitInt(Imm, &Mips::GPR32RegClass);
736 EmitInst(Mips::MTC1, DestReg).addReg(TempReg);
738 } else if (VT == MVT::f64) {
739 const TargetRegisterClass *RC = &Mips::AFGR64RegClass;
740 unsigned DestReg = createResultReg(RC);
741 unsigned TempReg1 = Materialize32BitInt(Imm >> 32, &Mips::GPR32RegClass);
743 Materialize32BitInt(Imm & 0xFFFFFFFF, &Mips::GPR32RegClass);
744 EmitInst(Mips::BuildPairF64, DestReg).addReg(TempReg2).addReg(TempReg1);
750 unsigned MipsFastISel::MaterializeGV(const GlobalValue *GV, MVT VT) {
751 // For now 32-bit only.
754 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
755 unsigned DestReg = createResultReg(RC);
756 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
757 bool IsThreadLocal = GVar && GVar->isThreadLocal();
758 // TLS not supported at this time.
761 EmitInst(Mips::LW, DestReg)
762 .addReg(MFI->getGlobalBaseReg())
763 .addGlobalAddress(GV, 0, MipsII::MO_GOT);
764 if ((GV->hasInternalLinkage() ||
765 (GV->hasLocalLinkage() && !isa<Function>(GV)))) {
766 unsigned TempReg = createResultReg(RC);
767 EmitInst(Mips::ADDiu, TempReg)
769 .addGlobalAddress(GV, 0, MipsII::MO_ABS_LO);
775 unsigned MipsFastISel::MaterializeInt(const Constant *C, MVT VT) {
776 if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1)
778 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
779 const ConstantInt *CI = cast<ConstantInt>(C);
781 if ((VT != MVT::i1) && CI->isNegative())
782 Imm = CI->getSExtValue();
784 Imm = CI->getZExtValue();
785 return Materialize32BitInt(Imm, RC);
788 unsigned MipsFastISel::Materialize32BitInt(int64_t Imm,
789 const TargetRegisterClass *RC) {
790 unsigned ResultReg = createResultReg(RC);
792 if (isInt<16>(Imm)) {
793 unsigned Opc = Mips::ADDiu;
794 EmitInst(Opc, ResultReg).addReg(Mips::ZERO).addImm(Imm);
796 } else if (isUInt<16>(Imm)) {
797 EmitInst(Mips::ORi, ResultReg).addReg(Mips::ZERO).addImm(Imm);
800 unsigned Lo = Imm & 0xFFFF;
801 unsigned Hi = (Imm >> 16) & 0xFFFF;
803 // Both Lo and Hi have nonzero bits.
804 unsigned TmpReg = createResultReg(RC);
805 EmitInst(Mips::LUi, TmpReg).addImm(Hi);
806 EmitInst(Mips::ORi, ResultReg).addReg(TmpReg).addImm(Lo);
808 EmitInst(Mips::LUi, ResultReg).addImm(Hi);
815 FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo,
816 const TargetLibraryInfo *libInfo) {
817 return new MipsFastISel(funcInfo, libInfo);