1 //===- MipsFastISel.cpp - Mips FastISel implementation --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file defines the MIPS-specific support for the FastISel class.
11 /// Some of the target-specific code is generated by tablegen in the file
12 /// MipsGenFastISel.inc, which is #included here.
14 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/MipsABIInfo.h"
17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "MipsCCState.h"
19 #include "MipsISelLowering.h"
20 #include "MipsInstrInfo.h"
21 #include "MipsMachineFunction.h"
22 #include "MipsSubtarget.h"
23 #include "MipsTargetMachine.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/Analysis/TargetLibraryInfo.h"
29 #include "llvm/CodeGen/CallingConvLower.h"
30 #include "llvm/CodeGen/FastISel.h"
31 #include "llvm/CodeGen/FunctionLoweringInfo.h"
32 #include "llvm/CodeGen/ISDOpcodes.h"
33 #include "llvm/CodeGen/MachineBasicBlock.h"
34 #include "llvm/CodeGen/MachineFrameInfo.h"
35 #include "llvm/CodeGen/MachineInstrBuilder.h"
36 #include "llvm/CodeGen/MachineMemOperand.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/TargetInstrInfo.h"
39 #include "llvm/CodeGen/TargetLowering.h"
40 #include "llvm/CodeGen/ValueTypes.h"
41 #include "llvm/IR/Attributes.h"
42 #include "llvm/IR/CallingConv.h"
43 #include "llvm/IR/Constant.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/Function.h"
47 #include "llvm/IR/GetElementPtrTypeIterator.h"
48 #include "llvm/IR/GlobalValue.h"
49 #include "llvm/IR/GlobalVariable.h"
50 #include "llvm/IR/InstrTypes.h"
51 #include "llvm/IR/Instruction.h"
52 #include "llvm/IR/Instructions.h"
53 #include "llvm/IR/IntrinsicInst.h"
54 #include "llvm/IR/Operator.h"
55 #include "llvm/IR/Type.h"
56 #include "llvm/IR/User.h"
57 #include "llvm/IR/Value.h"
58 #include "llvm/MC/MCContext.h"
59 #include "llvm/MC/MCInstrDesc.h"
60 #include "llvm/MC/MCRegisterInfo.h"
61 #include "llvm/MC/MCSymbol.h"
62 #include "llvm/Support/Casting.h"
63 #include "llvm/Support/Compiler.h"
64 #include "llvm/Support/Debug.h"
65 #include "llvm/Support/ErrorHandling.h"
66 #include "llvm/Support/MachineValueType.h"
67 #include "llvm/Support/MathExtras.h"
68 #include "llvm/Support/raw_ostream.h"
74 #define DEBUG_TYPE "mips-fastisel"
78 extern cl::opt
<bool> EmitJalrReloc
;
82 class MipsFastISel final
: public FastISel
{
84 // All possible address modes.
87 using BaseKind
= enum { RegBase
, FrameIndexBase
};
90 BaseKind Kind
= RegBase
;
98 const GlobalValue
*GV
= nullptr;
101 // Innocuous defaults for our address.
102 Address() { Base
.Reg
= 0; }
104 void setKind(BaseKind K
) { Kind
= K
; }
105 BaseKind
getKind() const { return Kind
; }
106 bool isRegBase() const { return Kind
== RegBase
; }
107 bool isFIBase() const { return Kind
== FrameIndexBase
; }
109 void setReg(unsigned Reg
) {
110 assert(isRegBase() && "Invalid base register access!");
114 unsigned getReg() const {
115 assert(isRegBase() && "Invalid base register access!");
119 void setFI(unsigned FI
) {
120 assert(isFIBase() && "Invalid base frame index access!");
124 unsigned getFI() const {
125 assert(isFIBase() && "Invalid base frame index access!");
129 void setOffset(int64_t Offset_
) { Offset
= Offset_
; }
130 int64_t getOffset() const { return Offset
; }
131 void setGlobalValue(const GlobalValue
*G
) { GV
= G
; }
132 const GlobalValue
*getGlobalValue() { return GV
; }
135 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
136 /// make the right decision when generating code for different targets.
137 const TargetMachine
&TM
;
138 const MipsSubtarget
*Subtarget
;
139 const TargetInstrInfo
&TII
;
140 const TargetLowering
&TLI
;
141 MipsFunctionInfo
*MFI
;
143 // Convenience variables to avoid some queries.
144 LLVMContext
*Context
;
146 bool fastLowerArguments() override
;
147 bool fastLowerCall(CallLoweringInfo
&CLI
) override
;
148 bool fastLowerIntrinsicCall(const IntrinsicInst
*II
) override
;
150 bool UnsupportedFPMode
; // To allow fast-isel to proceed and just not handle
151 // floating point but not reject doing fast-isel in other
155 // Selection routines.
156 bool selectLogicalOp(const Instruction
*I
);
157 bool selectLoad(const Instruction
*I
);
158 bool selectStore(const Instruction
*I
);
159 bool selectBranch(const Instruction
*I
);
160 bool selectSelect(const Instruction
*I
);
161 bool selectCmp(const Instruction
*I
);
162 bool selectFPExt(const Instruction
*I
);
163 bool selectFPTrunc(const Instruction
*I
);
164 bool selectFPToInt(const Instruction
*I
, bool IsSigned
);
165 bool selectRet(const Instruction
*I
);
166 bool selectTrunc(const Instruction
*I
);
167 bool selectIntExt(const Instruction
*I
);
168 bool selectShift(const Instruction
*I
);
169 bool selectDivRem(const Instruction
*I
, unsigned ISDOpcode
);
171 // Utility helper routines.
172 bool isTypeLegal(Type
*Ty
, MVT
&VT
);
173 bool isTypeSupported(Type
*Ty
, MVT
&VT
);
174 bool isLoadTypeLegal(Type
*Ty
, MVT
&VT
);
175 bool computeAddress(const Value
*Obj
, Address
&Addr
);
176 bool computeCallAddress(const Value
*V
, Address
&Addr
);
177 void simplifyAddress(Address
&Addr
);
179 // Emit helper routines.
180 bool emitCmp(unsigned DestReg
, const CmpInst
*CI
);
181 bool emitLoad(MVT VT
, unsigned &ResultReg
, Address
&Addr
,
182 unsigned Alignment
= 0);
183 bool emitStore(MVT VT
, unsigned SrcReg
, Address Addr
,
184 MachineMemOperand
*MMO
= nullptr);
185 bool emitStore(MVT VT
, unsigned SrcReg
, Address
&Addr
,
186 unsigned Alignment
= 0);
187 unsigned emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, bool isZExt
);
188 bool emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
,
191 bool emitIntZExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
);
193 bool emitIntSExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
);
194 bool emitIntSExt32r1(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
196 bool emitIntSExt32r2(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
199 unsigned getRegEnsuringSimpleIntegerWidening(const Value
*, bool IsUnsigned
);
201 unsigned emitLogicalOp(unsigned ISDOpc
, MVT RetVT
, const Value
*LHS
,
204 unsigned materializeFP(const ConstantFP
*CFP
, MVT VT
);
205 unsigned materializeGV(const GlobalValue
*GV
, MVT VT
);
206 unsigned materializeInt(const Constant
*C
, MVT VT
);
207 unsigned materialize32BitInt(int64_t Imm
, const TargetRegisterClass
*RC
);
208 unsigned materializeExternalCallSym(MCSymbol
*Syn
);
210 MachineInstrBuilder
emitInst(unsigned Opc
) {
211 return BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Opc
));
214 MachineInstrBuilder
emitInst(unsigned Opc
, unsigned DstReg
) {
215 return BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Opc
),
219 MachineInstrBuilder
emitInstStore(unsigned Opc
, unsigned SrcReg
,
220 unsigned MemReg
, int64_t MemOffset
) {
221 return emitInst(Opc
).addReg(SrcReg
).addReg(MemReg
).addImm(MemOffset
);
224 MachineInstrBuilder
emitInstLoad(unsigned Opc
, unsigned DstReg
,
225 unsigned MemReg
, int64_t MemOffset
) {
226 return emitInst(Opc
, DstReg
).addReg(MemReg
).addImm(MemOffset
);
229 unsigned fastEmitInst_rr(unsigned MachineInstOpcode
,
230 const TargetRegisterClass
*RC
,
231 unsigned Op0
, bool Op0IsKill
,
232 unsigned Op1
, bool Op1IsKill
);
234 // for some reason, this default is not generated by tablegen
235 // so we explicitly generate it here.
236 unsigned fastEmitInst_riir(uint64_t inst
, const TargetRegisterClass
*RC
,
237 unsigned Op0
, bool Op0IsKill
, uint64_t imm1
,
238 uint64_t imm2
, unsigned Op3
, bool Op3IsKill
) {
242 // Call handling routines.
244 CCAssignFn
*CCAssignFnForCall(CallingConv::ID CC
) const;
245 bool processCallArgs(CallLoweringInfo
&CLI
, SmallVectorImpl
<MVT
> &ArgVTs
,
247 bool finishCall(CallLoweringInfo
&CLI
, MVT RetVT
, unsigned NumBytes
);
249 const MipsABIInfo
&getABI() const {
250 return static_cast<const MipsTargetMachine
&>(TM
).getABI();
254 // Backend specific FastISel code.
255 explicit MipsFastISel(FunctionLoweringInfo
&funcInfo
,
256 const TargetLibraryInfo
*libInfo
)
257 : FastISel(funcInfo
, libInfo
), TM(funcInfo
.MF
->getTarget()),
258 Subtarget(&funcInfo
.MF
->getSubtarget
<MipsSubtarget
>()),
259 TII(*Subtarget
->getInstrInfo()), TLI(*Subtarget
->getTargetLowering()) {
260 MFI
= funcInfo
.MF
->getInfo
<MipsFunctionInfo
>();
261 Context
= &funcInfo
.Fn
->getContext();
262 UnsupportedFPMode
= Subtarget
->isFP64bit() || Subtarget
->useSoftFloat();
265 unsigned fastMaterializeAlloca(const AllocaInst
*AI
) override
;
266 unsigned fastMaterializeConstant(const Constant
*C
) override
;
267 bool fastSelectInstruction(const Instruction
*I
) override
;
269 #include "MipsGenFastISel.inc"
272 } // end anonymous namespace
274 static bool CC_Mips(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
275 CCValAssign::LocInfo LocInfo
, ISD::ArgFlagsTy ArgFlags
,
276 CCState
&State
) LLVM_ATTRIBUTE_UNUSED
;
278 static bool CC_MipsO32_FP32(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
279 CCValAssign::LocInfo LocInfo
,
280 ISD::ArgFlagsTy ArgFlags
, CCState
&State
) {
281 llvm_unreachable("should not be called");
284 static bool CC_MipsO32_FP64(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
285 CCValAssign::LocInfo LocInfo
,
286 ISD::ArgFlagsTy ArgFlags
, CCState
&State
) {
287 llvm_unreachable("should not be called");
290 #include "MipsGenCallingConv.inc"
292 CCAssignFn
*MipsFastISel::CCAssignFnForCall(CallingConv::ID CC
) const {
296 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc
, MVT RetVT
,
297 const Value
*LHS
, const Value
*RHS
) {
298 // Canonicalize immediates to the RHS first.
299 if (isa
<ConstantInt
>(LHS
) && !isa
<ConstantInt
>(RHS
))
314 llvm_unreachable("unexpected opcode");
317 unsigned LHSReg
= getRegForValue(LHS
);
322 if (const auto *C
= dyn_cast
<ConstantInt
>(RHS
))
323 RHSReg
= materializeInt(C
, MVT::i32
);
325 RHSReg
= getRegForValue(RHS
);
329 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
333 emitInst(Opc
, ResultReg
).addReg(LHSReg
).addReg(RHSReg
);
337 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst
*AI
) {
338 assert(TLI
.getValueType(DL
, AI
->getType(), true) == MVT::i32
&&
339 "Alloca should always return a pointer.");
341 DenseMap
<const AllocaInst
*, int>::iterator SI
=
342 FuncInfo
.StaticAllocaMap
.find(AI
);
344 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
345 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
346 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Mips::LEA_ADDiu
),
348 .addFrameIndex(SI
->second
)
356 unsigned MipsFastISel::materializeInt(const Constant
*C
, MVT VT
) {
357 if (VT
!= MVT::i32
&& VT
!= MVT::i16
&& VT
!= MVT::i8
&& VT
!= MVT::i1
)
359 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
360 const ConstantInt
*CI
= cast
<ConstantInt
>(C
);
361 return materialize32BitInt(CI
->getZExtValue(), RC
);
364 unsigned MipsFastISel::materialize32BitInt(int64_t Imm
,
365 const TargetRegisterClass
*RC
) {
366 unsigned ResultReg
= createResultReg(RC
);
368 if (isInt
<16>(Imm
)) {
369 unsigned Opc
= Mips::ADDiu
;
370 emitInst(Opc
, ResultReg
).addReg(Mips::ZERO
).addImm(Imm
);
372 } else if (isUInt
<16>(Imm
)) {
373 emitInst(Mips::ORi
, ResultReg
).addReg(Mips::ZERO
).addImm(Imm
);
376 unsigned Lo
= Imm
& 0xFFFF;
377 unsigned Hi
= (Imm
>> 16) & 0xFFFF;
379 // Both Lo and Hi have nonzero bits.
380 unsigned TmpReg
= createResultReg(RC
);
381 emitInst(Mips::LUi
, TmpReg
).addImm(Hi
);
382 emitInst(Mips::ORi
, ResultReg
).addReg(TmpReg
).addImm(Lo
);
384 emitInst(Mips::LUi
, ResultReg
).addImm(Hi
);
389 unsigned MipsFastISel::materializeFP(const ConstantFP
*CFP
, MVT VT
) {
390 if (UnsupportedFPMode
)
392 int64_t Imm
= CFP
->getValueAPF().bitcastToAPInt().getZExtValue();
393 if (VT
== MVT::f32
) {
394 const TargetRegisterClass
*RC
= &Mips::FGR32RegClass
;
395 unsigned DestReg
= createResultReg(RC
);
396 unsigned TempReg
= materialize32BitInt(Imm
, &Mips::GPR32RegClass
);
397 emitInst(Mips::MTC1
, DestReg
).addReg(TempReg
);
399 } else if (VT
== MVT::f64
) {
400 const TargetRegisterClass
*RC
= &Mips::AFGR64RegClass
;
401 unsigned DestReg
= createResultReg(RC
);
402 unsigned TempReg1
= materialize32BitInt(Imm
>> 32, &Mips::GPR32RegClass
);
404 materialize32BitInt(Imm
& 0xFFFFFFFF, &Mips::GPR32RegClass
);
405 emitInst(Mips::BuildPairF64
, DestReg
).addReg(TempReg2
).addReg(TempReg1
);
411 unsigned MipsFastISel::materializeGV(const GlobalValue
*GV
, MVT VT
) {
412 // For now 32-bit only.
415 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
416 unsigned DestReg
= createResultReg(RC
);
417 const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
);
418 bool IsThreadLocal
= GVar
&& GVar
->isThreadLocal();
419 // TLS not supported at this time.
422 emitInst(Mips::LW
, DestReg
)
423 .addReg(MFI
->getGlobalBaseReg())
424 .addGlobalAddress(GV
, 0, MipsII::MO_GOT
);
425 if ((GV
->hasInternalLinkage() ||
426 (GV
->hasLocalLinkage() && !isa
<Function
>(GV
)))) {
427 unsigned TempReg
= createResultReg(RC
);
428 emitInst(Mips::ADDiu
, TempReg
)
430 .addGlobalAddress(GV
, 0, MipsII::MO_ABS_LO
);
436 unsigned MipsFastISel::materializeExternalCallSym(MCSymbol
*Sym
) {
437 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
438 unsigned DestReg
= createResultReg(RC
);
439 emitInst(Mips::LW
, DestReg
)
440 .addReg(MFI
->getGlobalBaseReg())
441 .addSym(Sym
, MipsII::MO_GOT
);
445 // Materialize a constant into a register, and return the register
446 // number (or zero if we failed to handle it).
447 unsigned MipsFastISel::fastMaterializeConstant(const Constant
*C
) {
448 EVT CEVT
= TLI
.getValueType(DL
, C
->getType(), true);
450 // Only handle simple types.
451 if (!CEVT
.isSimple())
453 MVT VT
= CEVT
.getSimpleVT();
455 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(C
))
456 return (UnsupportedFPMode
) ? 0 : materializeFP(CFP
, VT
);
457 else if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(C
))
458 return materializeGV(GV
, VT
);
459 else if (isa
<ConstantInt
>(C
))
460 return materializeInt(C
, VT
);
465 bool MipsFastISel::computeAddress(const Value
*Obj
, Address
&Addr
) {
466 const User
*U
= nullptr;
467 unsigned Opcode
= Instruction::UserOp1
;
468 if (const Instruction
*I
= dyn_cast
<Instruction
>(Obj
)) {
469 // Don't walk into other basic blocks unless the object is an alloca from
470 // another block, otherwise it may not have a virtual register assigned.
471 if (FuncInfo
.StaticAllocaMap
.count(static_cast<const AllocaInst
*>(Obj
)) ||
472 FuncInfo
.MBBMap
[I
->getParent()] == FuncInfo
.MBB
) {
473 Opcode
= I
->getOpcode();
476 } else if (const ConstantExpr
*C
= dyn_cast
<ConstantExpr
>(Obj
)) {
477 Opcode
= C
->getOpcode();
483 case Instruction::BitCast
:
484 // Look through bitcasts.
485 return computeAddress(U
->getOperand(0), Addr
);
486 case Instruction::GetElementPtr
: {
487 Address SavedAddr
= Addr
;
488 int64_t TmpOffset
= Addr
.getOffset();
489 // Iterate through the GEP folding the constants into offsets where
491 gep_type_iterator GTI
= gep_type_begin(U
);
492 for (User::const_op_iterator i
= U
->op_begin() + 1, e
= U
->op_end(); i
!= e
;
494 const Value
*Op
= *i
;
495 if (StructType
*STy
= GTI
.getStructTypeOrNull()) {
496 const StructLayout
*SL
= DL
.getStructLayout(STy
);
497 unsigned Idx
= cast
<ConstantInt
>(Op
)->getZExtValue();
498 TmpOffset
+= SL
->getElementOffset(Idx
);
500 uint64_t S
= DL
.getTypeAllocSize(GTI
.getIndexedType());
502 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Op
)) {
503 // Constant-offset addressing.
504 TmpOffset
+= CI
->getSExtValue() * S
;
507 if (canFoldAddIntoGEP(U
, Op
)) {
508 // A compatible add with a constant operand. Fold the constant.
510 cast
<ConstantInt
>(cast
<AddOperator
>(Op
)->getOperand(1));
511 TmpOffset
+= CI
->getSExtValue() * S
;
512 // Iterate on the other operand.
513 Op
= cast
<AddOperator
>(Op
)->getOperand(0);
517 goto unsupported_gep
;
521 // Try to grab the base operand now.
522 Addr
.setOffset(TmpOffset
);
523 if (computeAddress(U
->getOperand(0), Addr
))
525 // We failed, restore everything and try the other options.
530 case Instruction::Alloca
: {
531 const AllocaInst
*AI
= cast
<AllocaInst
>(Obj
);
532 DenseMap
<const AllocaInst
*, int>::iterator SI
=
533 FuncInfo
.StaticAllocaMap
.find(AI
);
534 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
535 Addr
.setKind(Address::FrameIndexBase
);
536 Addr
.setFI(SI
->second
);
542 Addr
.setReg(getRegForValue(Obj
));
543 return Addr
.getReg() != 0;
546 bool MipsFastISel::computeCallAddress(const Value
*V
, Address
&Addr
) {
547 const User
*U
= nullptr;
548 unsigned Opcode
= Instruction::UserOp1
;
550 if (const auto *I
= dyn_cast
<Instruction
>(V
)) {
551 // Check if the value is defined in the same basic block. This information
552 // is crucial to know whether or not folding an operand is valid.
553 if (I
->getParent() == FuncInfo
.MBB
->getBasicBlock()) {
554 Opcode
= I
->getOpcode();
557 } else if (const auto *C
= dyn_cast
<ConstantExpr
>(V
)) {
558 Opcode
= C
->getOpcode();
565 case Instruction::BitCast
:
566 // Look past bitcasts if its operand is in the same BB.
567 return computeCallAddress(U
->getOperand(0), Addr
);
569 case Instruction::IntToPtr
:
570 // Look past no-op inttoptrs if its operand is in the same BB.
571 if (TLI
.getValueType(DL
, U
->getOperand(0)->getType()) ==
572 TLI
.getPointerTy(DL
))
573 return computeCallAddress(U
->getOperand(0), Addr
);
575 case Instruction::PtrToInt
:
576 // Look past no-op ptrtoints if its operand is in the same BB.
577 if (TLI
.getValueType(DL
, U
->getType()) == TLI
.getPointerTy(DL
))
578 return computeCallAddress(U
->getOperand(0), Addr
);
582 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
583 Addr
.setGlobalValue(GV
);
587 // If all else fails, try to materialize the value in a register.
588 if (!Addr
.getGlobalValue()) {
589 Addr
.setReg(getRegForValue(V
));
590 return Addr
.getReg() != 0;
596 bool MipsFastISel::isTypeLegal(Type
*Ty
, MVT
&VT
) {
597 EVT evt
= TLI
.getValueType(DL
, Ty
, true);
598 // Only handle simple types.
599 if (evt
== MVT::Other
|| !evt
.isSimple())
601 VT
= evt
.getSimpleVT();
603 // Handle all legal types, i.e. a register that will directly hold this
605 return TLI
.isTypeLegal(VT
);
608 bool MipsFastISel::isTypeSupported(Type
*Ty
, MVT
&VT
) {
609 if (Ty
->isVectorTy())
612 if (isTypeLegal(Ty
, VT
))
615 // If this is a type than can be sign or zero-extended to a basic operation
616 // go ahead and accept it now.
617 if (VT
== MVT::i1
|| VT
== MVT::i8
|| VT
== MVT::i16
)
623 bool MipsFastISel::isLoadTypeLegal(Type
*Ty
, MVT
&VT
) {
624 if (isTypeLegal(Ty
, VT
))
626 // We will extend this in a later patch:
627 // If this is a type than can be sign or zero-extended to a basic operation
628 // go ahead and accept it now.
629 if (VT
== MVT::i8
|| VT
== MVT::i16
)
634 // Because of how EmitCmp is called with fast-isel, you can
635 // end up with redundant "andi" instructions after the sequences emitted below.
636 // We should try and solve this issue in the future.
638 bool MipsFastISel::emitCmp(unsigned ResultReg
, const CmpInst
*CI
) {
639 const Value
*Left
= CI
->getOperand(0), *Right
= CI
->getOperand(1);
640 bool IsUnsigned
= CI
->isUnsigned();
641 unsigned LeftReg
= getRegEnsuringSimpleIntegerWidening(Left
, IsUnsigned
);
644 unsigned RightReg
= getRegEnsuringSimpleIntegerWidening(Right
, IsUnsigned
);
647 CmpInst::Predicate P
= CI
->getPredicate();
652 case CmpInst::ICMP_EQ
: {
653 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
654 emitInst(Mips::XOR
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
655 emitInst(Mips::SLTiu
, ResultReg
).addReg(TempReg
).addImm(1);
658 case CmpInst::ICMP_NE
: {
659 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
660 emitInst(Mips::XOR
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
661 emitInst(Mips::SLTu
, ResultReg
).addReg(Mips::ZERO
).addReg(TempReg
);
664 case CmpInst::ICMP_UGT
:
665 emitInst(Mips::SLTu
, ResultReg
).addReg(RightReg
).addReg(LeftReg
);
667 case CmpInst::ICMP_ULT
:
668 emitInst(Mips::SLTu
, ResultReg
).addReg(LeftReg
).addReg(RightReg
);
670 case CmpInst::ICMP_UGE
: {
671 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
672 emitInst(Mips::SLTu
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
673 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
676 case CmpInst::ICMP_ULE
: {
677 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
678 emitInst(Mips::SLTu
, TempReg
).addReg(RightReg
).addReg(LeftReg
);
679 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
682 case CmpInst::ICMP_SGT
:
683 emitInst(Mips::SLT
, ResultReg
).addReg(RightReg
).addReg(LeftReg
);
685 case CmpInst::ICMP_SLT
:
686 emitInst(Mips::SLT
, ResultReg
).addReg(LeftReg
).addReg(RightReg
);
688 case CmpInst::ICMP_SGE
: {
689 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
690 emitInst(Mips::SLT
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
691 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
694 case CmpInst::ICMP_SLE
: {
695 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
696 emitInst(Mips::SLT
, TempReg
).addReg(RightReg
).addReg(LeftReg
);
697 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
700 case CmpInst::FCMP_OEQ
:
701 case CmpInst::FCMP_UNE
:
702 case CmpInst::FCMP_OLT
:
703 case CmpInst::FCMP_OLE
:
704 case CmpInst::FCMP_OGT
:
705 case CmpInst::FCMP_OGE
: {
706 if (UnsupportedFPMode
)
708 bool IsFloat
= Left
->getType()->isFloatTy();
709 bool IsDouble
= Left
->getType()->isDoubleTy();
710 if (!IsFloat
&& !IsDouble
)
712 unsigned Opc
, CondMovOpc
;
714 case CmpInst::FCMP_OEQ
:
715 Opc
= IsFloat
? Mips::C_EQ_S
: Mips::C_EQ_D32
;
716 CondMovOpc
= Mips::MOVT_I
;
718 case CmpInst::FCMP_UNE
:
719 Opc
= IsFloat
? Mips::C_EQ_S
: Mips::C_EQ_D32
;
720 CondMovOpc
= Mips::MOVF_I
;
722 case CmpInst::FCMP_OLT
:
723 Opc
= IsFloat
? Mips::C_OLT_S
: Mips::C_OLT_D32
;
724 CondMovOpc
= Mips::MOVT_I
;
726 case CmpInst::FCMP_OLE
:
727 Opc
= IsFloat
? Mips::C_OLE_S
: Mips::C_OLE_D32
;
728 CondMovOpc
= Mips::MOVT_I
;
730 case CmpInst::FCMP_OGT
:
731 Opc
= IsFloat
? Mips::C_ULE_S
: Mips::C_ULE_D32
;
732 CondMovOpc
= Mips::MOVF_I
;
734 case CmpInst::FCMP_OGE
:
735 Opc
= IsFloat
? Mips::C_ULT_S
: Mips::C_ULT_D32
;
736 CondMovOpc
= Mips::MOVF_I
;
739 llvm_unreachable("Only switching of a subset of CCs.");
741 unsigned RegWithZero
= createResultReg(&Mips::GPR32RegClass
);
742 unsigned RegWithOne
= createResultReg(&Mips::GPR32RegClass
);
743 emitInst(Mips::ADDiu
, RegWithZero
).addReg(Mips::ZERO
).addImm(0);
744 emitInst(Mips::ADDiu
, RegWithOne
).addReg(Mips::ZERO
).addImm(1);
745 emitInst(Opc
).addReg(Mips::FCC0
, RegState::Define
).addReg(LeftReg
)
747 emitInst(CondMovOpc
, ResultReg
)
750 .addReg(RegWithZero
);
757 bool MipsFastISel::emitLoad(MVT VT
, unsigned &ResultReg
, Address
&Addr
,
758 unsigned Alignment
) {
760 // more cases will be handled here in following patches.
763 switch (VT
.SimpleTy
) {
765 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
769 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
773 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
777 if (UnsupportedFPMode
)
779 ResultReg
= createResultReg(&Mips::FGR32RegClass
);
783 if (UnsupportedFPMode
)
785 ResultReg
= createResultReg(&Mips::AFGR64RegClass
);
791 if (Addr
.isRegBase()) {
792 simplifyAddress(Addr
);
793 emitInstLoad(Opc
, ResultReg
, Addr
.getReg(), Addr
.getOffset());
796 if (Addr
.isFIBase()) {
797 unsigned FI
= Addr
.getFI();
799 int64_t Offset
= Addr
.getOffset();
800 MachineFrameInfo
&MFI
= MF
->getFrameInfo();
801 MachineMemOperand
*MMO
= MF
->getMachineMemOperand(
802 MachinePointerInfo::getFixedStack(*MF
, FI
), MachineMemOperand::MOLoad
,
803 MFI
.getObjectSize(FI
), Align
);
804 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Opc
), ResultReg
)
813 bool MipsFastISel::emitStore(MVT VT
, unsigned SrcReg
, Address
&Addr
,
814 unsigned Alignment
) {
816 // more cases will be handled here in following patches.
819 switch (VT
.SimpleTy
) {
830 if (UnsupportedFPMode
)
835 if (UnsupportedFPMode
)
842 if (Addr
.isRegBase()) {
843 simplifyAddress(Addr
);
844 emitInstStore(Opc
, SrcReg
, Addr
.getReg(), Addr
.getOffset());
847 if (Addr
.isFIBase()) {
848 unsigned FI
= Addr
.getFI();
850 int64_t Offset
= Addr
.getOffset();
851 MachineFrameInfo
&MFI
= MF
->getFrameInfo();
852 MachineMemOperand
*MMO
= MF
->getMachineMemOperand(
853 MachinePointerInfo::getFixedStack(*MF
, FI
), MachineMemOperand::MOStore
,
854 MFI
.getObjectSize(FI
), Align
);
855 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Opc
))
865 bool MipsFastISel::selectLogicalOp(const Instruction
*I
) {
867 if (!isTypeSupported(I
->getType(), VT
))
871 switch (I
->getOpcode()) {
873 llvm_unreachable("Unexpected instruction.");
874 case Instruction::And
:
875 ResultReg
= emitLogicalOp(ISD::AND
, VT
, I
->getOperand(0), I
->getOperand(1));
877 case Instruction::Or
:
878 ResultReg
= emitLogicalOp(ISD::OR
, VT
, I
->getOperand(0), I
->getOperand(1));
880 case Instruction::Xor
:
881 ResultReg
= emitLogicalOp(ISD::XOR
, VT
, I
->getOperand(0), I
->getOperand(1));
888 updateValueMap(I
, ResultReg
);
892 bool MipsFastISel::selectLoad(const Instruction
*I
) {
893 // Atomic loads need special handling.
894 if (cast
<LoadInst
>(I
)->isAtomic())
897 // Verify we have a legal type before going any further.
899 if (!isLoadTypeLegal(I
->getType(), VT
))
902 // See if we can handle this address.
904 if (!computeAddress(I
->getOperand(0), Addr
))
908 if (!emitLoad(VT
, ResultReg
, Addr
, cast
<LoadInst
>(I
)->getAlignment()))
910 updateValueMap(I
, ResultReg
);
914 bool MipsFastISel::selectStore(const Instruction
*I
) {
915 Value
*Op0
= I
->getOperand(0);
918 // Atomic stores need special handling.
919 if (cast
<StoreInst
>(I
)->isAtomic())
922 // Verify we have a legal type before going any further.
924 if (!isLoadTypeLegal(I
->getOperand(0)->getType(), VT
))
927 // Get the value to be stored into a register.
928 SrcReg
= getRegForValue(Op0
);
932 // See if we can handle this address.
934 if (!computeAddress(I
->getOperand(1), Addr
))
937 if (!emitStore(VT
, SrcReg
, Addr
, cast
<StoreInst
>(I
)->getAlignment()))
942 // This can cause a redundant sltiu to be generated.
943 // FIXME: try and eliminate this in a future patch.
944 bool MipsFastISel::selectBranch(const Instruction
*I
) {
945 const BranchInst
*BI
= cast
<BranchInst
>(I
);
946 MachineBasicBlock
*BrBB
= FuncInfo
.MBB
;
948 // TBB is the basic block for the case where the comparison is true.
949 // FBB is the basic block for the case where the comparison is false.
950 // if (cond) goto TBB
954 MachineBasicBlock
*TBB
= FuncInfo
.MBBMap
[BI
->getSuccessor(0)];
955 MachineBasicBlock
*FBB
= FuncInfo
.MBBMap
[BI
->getSuccessor(1)];
957 // Fold the common case of a conditional branch with a comparison
958 // in the same block.
959 unsigned ZExtCondReg
= 0;
960 if (const CmpInst
*CI
= dyn_cast
<CmpInst
>(BI
->getCondition())) {
961 if (CI
->hasOneUse() && CI
->getParent() == I
->getParent()) {
962 ZExtCondReg
= createResultReg(&Mips::GPR32RegClass
);
963 if (!emitCmp(ZExtCondReg
, CI
))
968 // For the general case, we need to mask with 1.
969 if (ZExtCondReg
== 0) {
970 unsigned CondReg
= getRegForValue(BI
->getCondition());
974 ZExtCondReg
= emitIntExt(MVT::i1
, CondReg
, MVT::i32
, true);
975 if (ZExtCondReg
== 0)
979 BuildMI(*BrBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Mips::BGTZ
))
982 finishCondBranch(BI
->getParent(), TBB
, FBB
);
986 bool MipsFastISel::selectCmp(const Instruction
*I
) {
987 const CmpInst
*CI
= cast
<CmpInst
>(I
);
988 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
989 if (!emitCmp(ResultReg
, CI
))
991 updateValueMap(I
, ResultReg
);
995 // Attempt to fast-select a floating-point extend instruction.
996 bool MipsFastISel::selectFPExt(const Instruction
*I
) {
997 if (UnsupportedFPMode
)
999 Value
*Src
= I
->getOperand(0);
1000 EVT SrcVT
= TLI
.getValueType(DL
, Src
->getType(), true);
1001 EVT DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1003 if (SrcVT
!= MVT::f32
|| DestVT
!= MVT::f64
)
1007 getRegForValue(Src
); // this must be a 32bit floating point register class
1008 // maybe we should handle this differently
1012 unsigned DestReg
= createResultReg(&Mips::AFGR64RegClass
);
1013 emitInst(Mips::CVT_D32_S
, DestReg
).addReg(SrcReg
);
1014 updateValueMap(I
, DestReg
);
1018 bool MipsFastISel::selectSelect(const Instruction
*I
) {
1019 assert(isa
<SelectInst
>(I
) && "Expected a select instruction.");
1021 LLVM_DEBUG(dbgs() << "selectSelect\n");
1024 if (!isTypeSupported(I
->getType(), VT
) || UnsupportedFPMode
) {
1026 dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
1030 unsigned CondMovOpc
;
1031 const TargetRegisterClass
*RC
;
1033 if (VT
.isInteger() && !VT
.isVector() && VT
.getSizeInBits() <= 32) {
1034 CondMovOpc
= Mips::MOVN_I_I
;
1035 RC
= &Mips::GPR32RegClass
;
1036 } else if (VT
== MVT::f32
) {
1037 CondMovOpc
= Mips::MOVN_I_S
;
1038 RC
= &Mips::FGR32RegClass
;
1039 } else if (VT
== MVT::f64
) {
1040 CondMovOpc
= Mips::MOVN_I_D32
;
1041 RC
= &Mips::AFGR64RegClass
;
1045 const SelectInst
*SI
= cast
<SelectInst
>(I
);
1046 const Value
*Cond
= SI
->getCondition();
1047 unsigned Src1Reg
= getRegForValue(SI
->getTrueValue());
1048 unsigned Src2Reg
= getRegForValue(SI
->getFalseValue());
1049 unsigned CondReg
= getRegForValue(Cond
);
1051 if (!Src1Reg
|| !Src2Reg
|| !CondReg
)
1054 unsigned ZExtCondReg
= createResultReg(&Mips::GPR32RegClass
);
1058 if (!emitIntExt(MVT::i1
, CondReg
, MVT::i32
, ZExtCondReg
, true))
1061 unsigned ResultReg
= createResultReg(RC
);
1062 unsigned TempReg
= createResultReg(RC
);
1064 if (!ResultReg
|| !TempReg
)
1067 emitInst(TargetOpcode::COPY
, TempReg
).addReg(Src2Reg
);
1068 emitInst(CondMovOpc
, ResultReg
)
1069 .addReg(Src1Reg
).addReg(ZExtCondReg
).addReg(TempReg
);
1070 updateValueMap(I
, ResultReg
);
1074 // Attempt to fast-select a floating-point truncate instruction.
1075 bool MipsFastISel::selectFPTrunc(const Instruction
*I
) {
1076 if (UnsupportedFPMode
)
1078 Value
*Src
= I
->getOperand(0);
1079 EVT SrcVT
= TLI
.getValueType(DL
, Src
->getType(), true);
1080 EVT DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1082 if (SrcVT
!= MVT::f64
|| DestVT
!= MVT::f32
)
1085 unsigned SrcReg
= getRegForValue(Src
);
1089 unsigned DestReg
= createResultReg(&Mips::FGR32RegClass
);
1093 emitInst(Mips::CVT_S_D32
, DestReg
).addReg(SrcReg
);
1094 updateValueMap(I
, DestReg
);
1098 // Attempt to fast-select a floating-point-to-integer conversion.
1099 bool MipsFastISel::selectFPToInt(const Instruction
*I
, bool IsSigned
) {
1100 if (UnsupportedFPMode
)
1104 return false; // We don't handle this case yet. There is no native
1105 // instruction for this but it can be synthesized.
1106 Type
*DstTy
= I
->getType();
1107 if (!isTypeLegal(DstTy
, DstVT
))
1110 if (DstVT
!= MVT::i32
)
1113 Value
*Src
= I
->getOperand(0);
1114 Type
*SrcTy
= Src
->getType();
1115 if (!isTypeLegal(SrcTy
, SrcVT
))
1118 if (SrcVT
!= MVT::f32
&& SrcVT
!= MVT::f64
)
1121 unsigned SrcReg
= getRegForValue(Src
);
1125 // Determine the opcode for the conversion, which takes place
1126 // entirely within FPRs.
1127 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
1128 unsigned TempReg
= createResultReg(&Mips::FGR32RegClass
);
1129 unsigned Opc
= (SrcVT
== MVT::f32
) ? Mips::TRUNC_W_S
: Mips::TRUNC_W_D32
;
1131 // Generate the convert.
1132 emitInst(Opc
, TempReg
).addReg(SrcReg
);
1133 emitInst(Mips::MFC1
, DestReg
).addReg(TempReg
);
1135 updateValueMap(I
, DestReg
);
1139 bool MipsFastISel::processCallArgs(CallLoweringInfo
&CLI
,
1140 SmallVectorImpl
<MVT
> &OutVTs
,
1141 unsigned &NumBytes
) {
1142 CallingConv::ID CC
= CLI
.CallConv
;
1143 SmallVector
<CCValAssign
, 16> ArgLocs
;
1144 CCState
CCInfo(CC
, false, *FuncInfo
.MF
, ArgLocs
, *Context
);
1145 CCInfo
.AnalyzeCallOperands(OutVTs
, CLI
.OutFlags
, CCAssignFnForCall(CC
));
1146 // Get a count of how many bytes are to be pushed on the stack.
1147 NumBytes
= CCInfo
.getNextStackOffset();
1148 // This is the minimum argument area used for A0-A3.
1152 emitInst(Mips::ADJCALLSTACKDOWN
).addImm(16).addImm(0);
1153 // Process the args.
1155 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
1156 CCValAssign
&VA
= ArgLocs
[i
];
1157 const Value
*ArgVal
= CLI
.OutVals
[VA
.getValNo()];
1158 MVT ArgVT
= OutVTs
[VA
.getValNo()];
1162 if (ArgVT
== MVT::f32
) {
1163 VA
.convertToReg(Mips::F12
);
1164 } else if (ArgVT
== MVT::f64
) {
1165 if (Subtarget
->isFP64bit())
1166 VA
.convertToReg(Mips::D6_64
);
1168 VA
.convertToReg(Mips::D6
);
1170 } else if (i
== 1) {
1171 if ((firstMVT
== MVT::f32
) || (firstMVT
== MVT::f64
)) {
1172 if (ArgVT
== MVT::f32
) {
1173 VA
.convertToReg(Mips::F14
);
1174 } else if (ArgVT
== MVT::f64
) {
1175 if (Subtarget
->isFP64bit())
1176 VA
.convertToReg(Mips::D7_64
);
1178 VA
.convertToReg(Mips::D7
);
1182 if (((ArgVT
== MVT::i32
) || (ArgVT
== MVT::f32
) || (ArgVT
== MVT::i16
) ||
1183 (ArgVT
== MVT::i8
)) &&
1185 switch (VA
.getLocMemOffset()) {
1187 VA
.convertToReg(Mips::A0
);
1190 VA
.convertToReg(Mips::A1
);
1193 VA
.convertToReg(Mips::A2
);
1196 VA
.convertToReg(Mips::A3
);
1202 unsigned ArgReg
= getRegForValue(ArgVal
);
1206 // Handle arg promotion: SExt, ZExt, AExt.
1207 switch (VA
.getLocInfo()) {
1208 case CCValAssign::Full
:
1210 case CCValAssign::AExt
:
1211 case CCValAssign::SExt
: {
1212 MVT DestVT
= VA
.getLocVT();
1214 ArgReg
= emitIntExt(SrcVT
, ArgReg
, DestVT
, /*isZExt=*/false);
1219 case CCValAssign::ZExt
: {
1220 MVT DestVT
= VA
.getLocVT();
1222 ArgReg
= emitIntExt(SrcVT
, ArgReg
, DestVT
, /*isZExt=*/true);
1228 llvm_unreachable("Unknown arg promotion!");
1231 // Now copy/store arg to correct locations.
1232 if (VA
.isRegLoc() && !VA
.needsCustom()) {
1233 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
,
1234 TII
.get(TargetOpcode::COPY
), VA
.getLocReg()).addReg(ArgReg
);
1235 CLI
.OutRegs
.push_back(VA
.getLocReg());
1236 } else if (VA
.needsCustom()) {
1237 llvm_unreachable("Mips does not use custom args.");
1241 // FIXME: This path will currently return false. It was copied
1242 // from the AArch64 port and should be essentially fine for Mips too.
1243 // The work to finish up this path will be done in a follow-on patch.
1245 assert(VA
.isMemLoc() && "Assuming store on stack.");
1246 // Don't emit stores for undef values.
1247 if (isa
<UndefValue
>(ArgVal
))
1250 // Need to store on the stack.
1251 // FIXME: This alignment is incorrect but this path is disabled
1252 // for now (will return false). We need to determine the right alignment
1253 // based on the normal alignment for the underlying machine type.
1255 unsigned ArgSize
= alignTo(ArgVT
.getSizeInBits(), 4);
1257 unsigned BEAlign
= 0;
1258 if (ArgSize
< 8 && !Subtarget
->isLittle())
1259 BEAlign
= 8 - ArgSize
;
1262 Addr
.setKind(Address::RegBase
);
1263 Addr
.setReg(Mips::SP
);
1264 Addr
.setOffset(VA
.getLocMemOffset() + BEAlign
);
1266 unsigned Alignment
= DL
.getABITypeAlignment(ArgVal
->getType());
1267 MachineMemOperand
*MMO
= FuncInfo
.MF
->getMachineMemOperand(
1268 MachinePointerInfo::getStack(*FuncInfo
.MF
, Addr
.getOffset()),
1269 MachineMemOperand::MOStore
, ArgVT
.getStoreSize(), Alignment
);
1271 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1272 return false; // can't store on the stack yet.
1279 bool MipsFastISel::finishCall(CallLoweringInfo
&CLI
, MVT RetVT
,
1280 unsigned NumBytes
) {
1281 CallingConv::ID CC
= CLI
.CallConv
;
1282 emitInst(Mips::ADJCALLSTACKUP
).addImm(16).addImm(0);
1283 if (RetVT
!= MVT::isVoid
) {
1284 SmallVector
<CCValAssign
, 16> RVLocs
;
1285 MipsCCState
CCInfo(CC
, false, *FuncInfo
.MF
, RVLocs
, *Context
);
1287 CCInfo
.AnalyzeCallResult(CLI
.Ins
, RetCC_Mips
, CLI
.RetTy
,
1288 CLI
.Symbol
? CLI
.Symbol
->getName().data()
1291 // Only handle a single return value.
1292 if (RVLocs
.size() != 1)
1294 // Copy all of the result registers out of their specified physreg.
1295 MVT CopyVT
= RVLocs
[0].getValVT();
1296 // Special handling for extended integers.
1297 if (RetVT
== MVT::i1
|| RetVT
== MVT::i8
|| RetVT
== MVT::i16
)
1300 unsigned ResultReg
= createResultReg(TLI
.getRegClassFor(CopyVT
));
1303 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
,
1304 TII
.get(TargetOpcode::COPY
),
1305 ResultReg
).addReg(RVLocs
[0].getLocReg());
1306 CLI
.InRegs
.push_back(RVLocs
[0].getLocReg());
1308 CLI
.ResultReg
= ResultReg
;
1309 CLI
.NumResultRegs
= 1;
1314 bool MipsFastISel::fastLowerArguments() {
1315 LLVM_DEBUG(dbgs() << "fastLowerArguments\n");
1317 if (!FuncInfo
.CanLowerReturn
) {
1318 LLVM_DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
1322 const Function
*F
= FuncInfo
.Fn
;
1323 if (F
->isVarArg()) {
1324 LLVM_DEBUG(dbgs() << ".. gave up (varargs)\n");
1328 CallingConv::ID CC
= F
->getCallingConv();
1329 if (CC
!= CallingConv::C
) {
1330 LLVM_DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
1334 std::array
<MCPhysReg
, 4> GPR32ArgRegs
= {{Mips::A0
, Mips::A1
, Mips::A2
,
1336 std::array
<MCPhysReg
, 2> FGR32ArgRegs
= {{Mips::F12
, Mips::F14
}};
1337 std::array
<MCPhysReg
, 2> AFGR64ArgRegs
= {{Mips::D6
, Mips::D7
}};
1338 auto NextGPR32
= GPR32ArgRegs
.begin();
1339 auto NextFGR32
= FGR32ArgRegs
.begin();
1340 auto NextAFGR64
= AFGR64ArgRegs
.begin();
1342 struct AllocatedReg
{
1343 const TargetRegisterClass
*RC
;
1345 AllocatedReg(const TargetRegisterClass
*RC
, unsigned Reg
)
1346 : RC(RC
), Reg(Reg
) {}
1349 // Only handle simple cases. i.e. All arguments are directly mapped to
1350 // registers of the appropriate type.
1351 SmallVector
<AllocatedReg
, 4> Allocation
;
1352 for (const auto &FormalArg
: F
->args()) {
1353 if (FormalArg
.hasAttribute(Attribute::InReg
) ||
1354 FormalArg
.hasAttribute(Attribute::StructRet
) ||
1355 FormalArg
.hasAttribute(Attribute::ByVal
)) {
1356 LLVM_DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
1360 Type
*ArgTy
= FormalArg
.getType();
1361 if (ArgTy
->isStructTy() || ArgTy
->isArrayTy() || ArgTy
->isVectorTy()) {
1362 LLVM_DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
1366 EVT ArgVT
= TLI
.getValueType(DL
, ArgTy
);
1367 LLVM_DEBUG(dbgs() << ".. " << FormalArg
.getArgNo() << ": "
1368 << ArgVT
.getEVTString() << "\n");
1369 if (!ArgVT
.isSimple()) {
1370 LLVM_DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
1374 switch (ArgVT
.getSimpleVT().SimpleTy
) {
1378 if (!FormalArg
.hasAttribute(Attribute::SExt
) &&
1379 !FormalArg
.hasAttribute(Attribute::ZExt
)) {
1380 // It must be any extend, this shouldn't happen for clang-generated IR
1381 // so just fall back on SelectionDAG.
1382 LLVM_DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
1386 if (NextGPR32
== GPR32ArgRegs
.end()) {
1387 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1391 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32
<< ")\n");
1392 Allocation
.emplace_back(&Mips::GPR32RegClass
, *NextGPR32
++);
1394 // Allocating any GPR32 prohibits further use of floating point arguments.
1395 NextFGR32
= FGR32ArgRegs
.end();
1396 NextAFGR64
= AFGR64ArgRegs
.end();
1400 if (FormalArg
.hasAttribute(Attribute::ZExt
)) {
1401 // The O32 ABI does not permit a zero-extended i32.
1402 LLVM_DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
1406 if (NextGPR32
== GPR32ArgRegs
.end()) {
1407 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1411 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32
<< ")\n");
1412 Allocation
.emplace_back(&Mips::GPR32RegClass
, *NextGPR32
++);
1414 // Allocating any GPR32 prohibits further use of floating point arguments.
1415 NextFGR32
= FGR32ArgRegs
.end();
1416 NextAFGR64
= AFGR64ArgRegs
.end();
1420 if (UnsupportedFPMode
) {
1421 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1424 if (NextFGR32
== FGR32ArgRegs
.end()) {
1425 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
1428 LLVM_DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32
<< ")\n");
1429 Allocation
.emplace_back(&Mips::FGR32RegClass
, *NextFGR32
++);
1430 // Allocating an FGR32 also allocates the super-register AFGR64, and
1431 // ABI rules require us to skip the corresponding GPR32.
1432 if (NextGPR32
!= GPR32ArgRegs
.end())
1434 if (NextAFGR64
!= AFGR64ArgRegs
.end())
1439 if (UnsupportedFPMode
) {
1440 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1443 if (NextAFGR64
== AFGR64ArgRegs
.end()) {
1444 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
1447 LLVM_DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64
<< ")\n");
1448 Allocation
.emplace_back(&Mips::AFGR64RegClass
, *NextAFGR64
++);
1449 // Allocating an FGR32 also allocates the super-register AFGR64, and
1450 // ABI rules require us to skip the corresponding GPR32 pair.
1451 if (NextGPR32
!= GPR32ArgRegs
.end())
1453 if (NextGPR32
!= GPR32ArgRegs
.end())
1455 if (NextFGR32
!= FGR32ArgRegs
.end())
1460 LLVM_DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
1465 for (const auto &FormalArg
: F
->args()) {
1466 unsigned ArgNo
= FormalArg
.getArgNo();
1467 unsigned SrcReg
= Allocation
[ArgNo
].Reg
;
1468 unsigned DstReg
= FuncInfo
.MF
->addLiveIn(SrcReg
, Allocation
[ArgNo
].RC
);
1469 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1470 // Without this, EmitLiveInCopies may eliminate the livein if its only
1471 // use is a bitcast (which isn't turned into an instruction).
1472 unsigned ResultReg
= createResultReg(Allocation
[ArgNo
].RC
);
1473 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
,
1474 TII
.get(TargetOpcode::COPY
), ResultReg
)
1475 .addReg(DstReg
, getKillRegState(true));
1476 updateValueMap(&FormalArg
, ResultReg
);
1479 // Calculate the size of the incoming arguments area.
1480 // We currently reject all the cases where this would be non-zero.
1481 unsigned IncomingArgSizeInBytes
= 0;
1483 // Account for the reserved argument area on ABI's that have one (O32).
1484 // It seems strange to do this on the caller side but it's necessary in
1485 // SelectionDAG's implementation.
1486 IncomingArgSizeInBytes
= std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC
),
1487 IncomingArgSizeInBytes
);
1489 MF
->getInfo
<MipsFunctionInfo
>()->setFormalArgInfo(IncomingArgSizeInBytes
,
1495 bool MipsFastISel::fastLowerCall(CallLoweringInfo
&CLI
) {
1496 CallingConv::ID CC
= CLI
.CallConv
;
1497 bool IsTailCall
= CLI
.IsTailCall
;
1498 bool IsVarArg
= CLI
.IsVarArg
;
1499 const Value
*Callee
= CLI
.Callee
;
1500 MCSymbol
*Symbol
= CLI
.Symbol
;
1502 // Do not handle FastCC.
1503 if (CC
== CallingConv::Fast
)
1506 // Allow SelectionDAG isel to handle tail calls.
1510 // Let SDISel handle vararg functions.
1514 // FIXME: Only handle *simple* calls for now.
1516 if (CLI
.RetTy
->isVoidTy())
1517 RetVT
= MVT::isVoid
;
1518 else if (!isTypeSupported(CLI
.RetTy
, RetVT
))
1521 for (auto Flag
: CLI
.OutFlags
)
1522 if (Flag
.isInReg() || Flag
.isSRet() || Flag
.isNest() || Flag
.isByVal())
1525 // Set up the argument vectors.
1526 SmallVector
<MVT
, 16> OutVTs
;
1527 OutVTs
.reserve(CLI
.OutVals
.size());
1529 for (auto *Val
: CLI
.OutVals
) {
1531 if (!isTypeLegal(Val
->getType(), VT
) &&
1532 !(VT
== MVT::i1
|| VT
== MVT::i8
|| VT
== MVT::i16
))
1535 // We don't handle vector parameters yet.
1536 if (VT
.isVector() || VT
.getSizeInBits() > 64)
1539 OutVTs
.push_back(VT
);
1543 if (!computeCallAddress(Callee
, Addr
))
1546 // Handle the arguments now that we've gotten them.
1548 if (!processCallArgs(CLI
, OutVTs
, NumBytes
))
1551 if (!Addr
.getGlobalValue())
1555 unsigned DestAddress
;
1557 DestAddress
= materializeExternalCallSym(Symbol
);
1559 DestAddress
= materializeGV(Addr
.getGlobalValue(), MVT::i32
);
1560 emitInst(TargetOpcode::COPY
, Mips::T9
).addReg(DestAddress
);
1561 MachineInstrBuilder MIB
=
1562 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Mips::JALR
),
1563 Mips::RA
).addReg(Mips::T9
);
1565 // Add implicit physical register uses to the call.
1566 for (auto Reg
: CLI
.OutRegs
)
1567 MIB
.addReg(Reg
, RegState::Implicit
);
1569 // Add a register mask with the call-preserved registers.
1570 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1571 MIB
.addRegMask(TRI
.getCallPreservedMask(*FuncInfo
.MF
, CC
));
1575 if (EmitJalrReloc
&& !Subtarget
->inMips16Mode()) {
1576 // Attach callee address to the instruction, let asm printer emit
1577 // .reloc R_MIPS_JALR.
1579 MIB
.addSym(Symbol
, MipsII::MO_JALR
);
1581 MIB
.addSym(FuncInfo
.MF
->getContext().getOrCreateSymbol(
1582 Addr
.getGlobalValue()->getName()), MipsII::MO_JALR
);
1585 // Finish off the call including any return values.
1586 return finishCall(CLI
, RetVT
, NumBytes
);
1589 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst
*II
) {
1590 switch (II
->getIntrinsicID()) {
1593 case Intrinsic::bswap
: {
1594 Type
*RetTy
= II
->getCalledFunction()->getReturnType();
1597 if (!isTypeSupported(RetTy
, VT
))
1600 unsigned SrcReg
= getRegForValue(II
->getOperand(0));
1603 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
1606 if (VT
== MVT::i16
) {
1607 if (Subtarget
->hasMips32r2()) {
1608 emitInst(Mips::WSBH
, DestReg
).addReg(SrcReg
);
1609 updateValueMap(II
, DestReg
);
1612 unsigned TempReg
[3];
1613 for (int i
= 0; i
< 3; i
++) {
1614 TempReg
[i
] = createResultReg(&Mips::GPR32RegClass
);
1615 if (TempReg
[i
] == 0)
1618 emitInst(Mips::SLL
, TempReg
[0]).addReg(SrcReg
).addImm(8);
1619 emitInst(Mips::SRL
, TempReg
[1]).addReg(SrcReg
).addImm(8);
1620 emitInst(Mips::OR
, TempReg
[2]).addReg(TempReg
[0]).addReg(TempReg
[1]);
1621 emitInst(Mips::ANDi
, DestReg
).addReg(TempReg
[2]).addImm(0xFFFF);
1622 updateValueMap(II
, DestReg
);
1625 } else if (VT
== MVT::i32
) {
1626 if (Subtarget
->hasMips32r2()) {
1627 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
1628 emitInst(Mips::WSBH
, TempReg
).addReg(SrcReg
);
1629 emitInst(Mips::ROTR
, DestReg
).addReg(TempReg
).addImm(16);
1630 updateValueMap(II
, DestReg
);
1633 unsigned TempReg
[8];
1634 for (int i
= 0; i
< 8; i
++) {
1635 TempReg
[i
] = createResultReg(&Mips::GPR32RegClass
);
1636 if (TempReg
[i
] == 0)
1640 emitInst(Mips::SRL
, TempReg
[0]).addReg(SrcReg
).addImm(8);
1641 emitInst(Mips::SRL
, TempReg
[1]).addReg(SrcReg
).addImm(24);
1642 emitInst(Mips::ANDi
, TempReg
[2]).addReg(TempReg
[0]).addImm(0xFF00);
1643 emitInst(Mips::OR
, TempReg
[3]).addReg(TempReg
[1]).addReg(TempReg
[2]);
1645 emitInst(Mips::ANDi
, TempReg
[4]).addReg(SrcReg
).addImm(0xFF00);
1646 emitInst(Mips::SLL
, TempReg
[5]).addReg(TempReg
[4]).addImm(8);
1648 emitInst(Mips::SLL
, TempReg
[6]).addReg(SrcReg
).addImm(24);
1649 emitInst(Mips::OR
, TempReg
[7]).addReg(TempReg
[3]).addReg(TempReg
[5]);
1650 emitInst(Mips::OR
, DestReg
).addReg(TempReg
[6]).addReg(TempReg
[7]);
1651 updateValueMap(II
, DestReg
);
1657 case Intrinsic::memcpy
:
1658 case Intrinsic::memmove
: {
1659 const auto *MTI
= cast
<MemTransferInst
>(II
);
1660 // Don't handle volatile.
1661 if (MTI
->isVolatile())
1663 if (!MTI
->getLength()->getType()->isIntegerTy(32))
1665 const char *IntrMemName
= isa
<MemCpyInst
>(II
) ? "memcpy" : "memmove";
1666 return lowerCallTo(II
, IntrMemName
, II
->getNumArgOperands() - 1);
1668 case Intrinsic::memset
: {
1669 const MemSetInst
*MSI
= cast
<MemSetInst
>(II
);
1670 // Don't handle volatile.
1671 if (MSI
->isVolatile())
1673 if (!MSI
->getLength()->getType()->isIntegerTy(32))
1675 return lowerCallTo(II
, "memset", II
->getNumArgOperands() - 1);
1681 bool MipsFastISel::selectRet(const Instruction
*I
) {
1682 const Function
&F
= *I
->getParent()->getParent();
1683 const ReturnInst
*Ret
= cast
<ReturnInst
>(I
);
1685 LLVM_DEBUG(dbgs() << "selectRet\n");
1687 if (!FuncInfo
.CanLowerReturn
)
1690 // Build a list of return value registers.
1691 SmallVector
<unsigned, 4> RetRegs
;
1693 if (Ret
->getNumOperands() > 0) {
1694 CallingConv::ID CC
= F
.getCallingConv();
1696 // Do not handle FastCC.
1697 if (CC
== CallingConv::Fast
)
1700 SmallVector
<ISD::OutputArg
, 4> Outs
;
1701 GetReturnInfo(CC
, F
.getReturnType(), F
.getAttributes(), Outs
, TLI
, DL
);
1703 // Analyze operands of the call, assigning locations to each operand.
1704 SmallVector
<CCValAssign
, 16> ValLocs
;
1705 MipsCCState
CCInfo(CC
, F
.isVarArg(), *FuncInfo
.MF
, ValLocs
,
1707 CCAssignFn
*RetCC
= RetCC_Mips
;
1708 CCInfo
.AnalyzeReturn(Outs
, RetCC
);
1710 // Only handle a single return value for now.
1711 if (ValLocs
.size() != 1)
1714 CCValAssign
&VA
= ValLocs
[0];
1715 const Value
*RV
= Ret
->getOperand(0);
1717 // Don't bother handling odd stuff for now.
1718 if ((VA
.getLocInfo() != CCValAssign::Full
) &&
1719 (VA
.getLocInfo() != CCValAssign::BCvt
))
1722 // Only handle register returns for now.
1726 unsigned Reg
= getRegForValue(RV
);
1730 unsigned SrcReg
= Reg
+ VA
.getValNo();
1731 Register DestReg
= VA
.getLocReg();
1732 // Avoid a cross-class copy. This is very unlikely.
1733 if (!MRI
.getRegClass(SrcReg
)->contains(DestReg
))
1736 EVT RVEVT
= TLI
.getValueType(DL
, RV
->getType());
1737 if (!RVEVT
.isSimple())
1740 if (RVEVT
.isVector())
1743 MVT RVVT
= RVEVT
.getSimpleVT();
1744 if (RVVT
== MVT::f128
)
1747 // Do not handle FGR64 returns for now.
1748 if (RVVT
== MVT::f64
&& UnsupportedFPMode
) {
1749 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1753 MVT DestVT
= VA
.getValVT();
1754 // Special handling for extended integers.
1755 if (RVVT
!= DestVT
) {
1756 if (RVVT
!= MVT::i1
&& RVVT
!= MVT::i8
&& RVVT
!= MVT::i16
)
1759 if (Outs
[0].Flags
.isZExt() || Outs
[0].Flags
.isSExt()) {
1760 bool IsZExt
= Outs
[0].Flags
.isZExt();
1761 SrcReg
= emitIntExt(RVVT
, SrcReg
, DestVT
, IsZExt
);
1768 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
,
1769 TII
.get(TargetOpcode::COPY
), DestReg
).addReg(SrcReg
);
1771 // Add register to return instruction.
1772 RetRegs
.push_back(VA
.getLocReg());
1774 MachineInstrBuilder MIB
= emitInst(Mips::RetRA
);
1775 for (unsigned i
= 0, e
= RetRegs
.size(); i
!= e
; ++i
)
1776 MIB
.addReg(RetRegs
[i
], RegState::Implicit
);
1780 bool MipsFastISel::selectTrunc(const Instruction
*I
) {
1781 // The high bits for a type smaller than the register size are assumed to be
1783 Value
*Op
= I
->getOperand(0);
1786 SrcVT
= TLI
.getValueType(DL
, Op
->getType(), true);
1787 DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1789 if (SrcVT
!= MVT::i32
&& SrcVT
!= MVT::i16
&& SrcVT
!= MVT::i8
)
1791 if (DestVT
!= MVT::i16
&& DestVT
!= MVT::i8
&& DestVT
!= MVT::i1
)
1794 unsigned SrcReg
= getRegForValue(Op
);
1798 // Because the high bits are undefined, a truncate doesn't generate
1800 updateValueMap(I
, SrcReg
);
1804 bool MipsFastISel::selectIntExt(const Instruction
*I
) {
1805 Type
*DestTy
= I
->getType();
1806 Value
*Src
= I
->getOperand(0);
1807 Type
*SrcTy
= Src
->getType();
1809 bool isZExt
= isa
<ZExtInst
>(I
);
1810 unsigned SrcReg
= getRegForValue(Src
);
1814 EVT SrcEVT
, DestEVT
;
1815 SrcEVT
= TLI
.getValueType(DL
, SrcTy
, true);
1816 DestEVT
= TLI
.getValueType(DL
, DestTy
, true);
1817 if (!SrcEVT
.isSimple())
1819 if (!DestEVT
.isSimple())
1822 MVT SrcVT
= SrcEVT
.getSimpleVT();
1823 MVT DestVT
= DestEVT
.getSimpleVT();
1824 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1826 if (!emitIntExt(SrcVT
, SrcReg
, DestVT
, ResultReg
, isZExt
))
1828 updateValueMap(I
, ResultReg
);
1832 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1835 switch (SrcVT
.SimpleTy
) {
1845 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
1846 emitInst(Mips::SLL
, TempReg
).addReg(SrcReg
).addImm(ShiftAmt
);
1847 emitInst(Mips::SRA
, DestReg
).addReg(TempReg
).addImm(ShiftAmt
);
1851 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1853 switch (SrcVT
.SimpleTy
) {
1857 emitInst(Mips::SEB
, DestReg
).addReg(SrcReg
);
1860 emitInst(Mips::SEH
, DestReg
).addReg(SrcReg
);
1866 bool MipsFastISel::emitIntSExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1868 if ((DestVT
!= MVT::i32
) && (DestVT
!= MVT::i16
))
1870 if (Subtarget
->hasMips32r2())
1871 return emitIntSExt32r2(SrcVT
, SrcReg
, DestVT
, DestReg
);
1872 return emitIntSExt32r1(SrcVT
, SrcReg
, DestVT
, DestReg
);
1875 bool MipsFastISel::emitIntZExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1879 switch (SrcVT
.SimpleTy
) {
1893 emitInst(Mips::ANDi
, DestReg
).addReg(SrcReg
).addImm(Imm
);
1897 bool MipsFastISel::emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1898 unsigned DestReg
, bool IsZExt
) {
1899 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1900 // DestVT are odd things, so test to make sure that they are both types we can
1901 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1902 // bail out to SelectionDAG.
1903 if (((DestVT
!= MVT::i8
) && (DestVT
!= MVT::i16
) && (DestVT
!= MVT::i32
)) ||
1904 ((SrcVT
!= MVT::i1
) && (SrcVT
!= MVT::i8
) && (SrcVT
!= MVT::i16
)))
1907 return emitIntZExt(SrcVT
, SrcReg
, DestVT
, DestReg
);
1908 return emitIntSExt(SrcVT
, SrcReg
, DestVT
, DestReg
);
1911 unsigned MipsFastISel::emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1913 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
1914 bool Success
= emitIntExt(SrcVT
, SrcReg
, DestVT
, DestReg
, isZExt
);
1915 return Success
? DestReg
: 0;
1918 bool MipsFastISel::selectDivRem(const Instruction
*I
, unsigned ISDOpcode
) {
1919 EVT DestEVT
= TLI
.getValueType(DL
, I
->getType(), true);
1920 if (!DestEVT
.isSimple())
1923 MVT DestVT
= DestEVT
.getSimpleVT();
1924 if (DestVT
!= MVT::i32
)
1928 switch (ISDOpcode
) {
1933 DivOpc
= Mips::SDIV
;
1937 DivOpc
= Mips::UDIV
;
1941 unsigned Src0Reg
= getRegForValue(I
->getOperand(0));
1942 unsigned Src1Reg
= getRegForValue(I
->getOperand(1));
1943 if (!Src0Reg
|| !Src1Reg
)
1946 emitInst(DivOpc
).addReg(Src0Reg
).addReg(Src1Reg
);
1947 emitInst(Mips::TEQ
).addReg(Src1Reg
).addReg(Mips::ZERO
).addImm(7);
1949 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1953 unsigned MFOpc
= (ISDOpcode
== ISD::SREM
|| ISDOpcode
== ISD::UREM
)
1956 emitInst(MFOpc
, ResultReg
);
1958 updateValueMap(I
, ResultReg
);
1962 bool MipsFastISel::selectShift(const Instruction
*I
) {
1965 if (!isTypeSupported(I
->getType(), RetVT
))
1968 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1972 unsigned Opcode
= I
->getOpcode();
1973 const Value
*Op0
= I
->getOperand(0);
1974 unsigned Op0Reg
= getRegForValue(Op0
);
1978 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1979 if (Opcode
== Instruction::AShr
|| Opcode
== Instruction::LShr
) {
1980 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
1984 MVT Op0MVT
= TLI
.getValueType(DL
, Op0
->getType(), true).getSimpleVT();
1985 bool IsZExt
= Opcode
== Instruction::LShr
;
1986 if (!emitIntExt(Op0MVT
, Op0Reg
, MVT::i32
, TempReg
, IsZExt
))
1992 if (const auto *C
= dyn_cast
<ConstantInt
>(I
->getOperand(1))) {
1993 uint64_t ShiftVal
= C
->getZExtValue();
1997 llvm_unreachable("Unexpected instruction.");
1998 case Instruction::Shl
:
2001 case Instruction::AShr
:
2004 case Instruction::LShr
:
2009 emitInst(Opcode
, ResultReg
).addReg(Op0Reg
).addImm(ShiftVal
);
2010 updateValueMap(I
, ResultReg
);
2014 unsigned Op1Reg
= getRegForValue(I
->getOperand(1));
2020 llvm_unreachable("Unexpected instruction.");
2021 case Instruction::Shl
:
2022 Opcode
= Mips::SLLV
;
2024 case Instruction::AShr
:
2025 Opcode
= Mips::SRAV
;
2027 case Instruction::LShr
:
2028 Opcode
= Mips::SRLV
;
2032 emitInst(Opcode
, ResultReg
).addReg(Op0Reg
).addReg(Op1Reg
);
2033 updateValueMap(I
, ResultReg
);
2037 bool MipsFastISel::fastSelectInstruction(const Instruction
*I
) {
2038 switch (I
->getOpcode()) {
2041 case Instruction::Load
:
2042 return selectLoad(I
);
2043 case Instruction::Store
:
2044 return selectStore(I
);
2045 case Instruction::SDiv
:
2046 if (!selectBinaryOp(I
, ISD::SDIV
))
2047 return selectDivRem(I
, ISD::SDIV
);
2049 case Instruction::UDiv
:
2050 if (!selectBinaryOp(I
, ISD::UDIV
))
2051 return selectDivRem(I
, ISD::UDIV
);
2053 case Instruction::SRem
:
2054 if (!selectBinaryOp(I
, ISD::SREM
))
2055 return selectDivRem(I
, ISD::SREM
);
2057 case Instruction::URem
:
2058 if (!selectBinaryOp(I
, ISD::UREM
))
2059 return selectDivRem(I
, ISD::UREM
);
2061 case Instruction::Shl
:
2062 case Instruction::LShr
:
2063 case Instruction::AShr
:
2064 return selectShift(I
);
2065 case Instruction::And
:
2066 case Instruction::Or
:
2067 case Instruction::Xor
:
2068 return selectLogicalOp(I
);
2069 case Instruction::Br
:
2070 return selectBranch(I
);
2071 case Instruction::Ret
:
2072 return selectRet(I
);
2073 case Instruction::Trunc
:
2074 return selectTrunc(I
);
2075 case Instruction::ZExt
:
2076 case Instruction::SExt
:
2077 return selectIntExt(I
);
2078 case Instruction::FPTrunc
:
2079 return selectFPTrunc(I
);
2080 case Instruction::FPExt
:
2081 return selectFPExt(I
);
2082 case Instruction::FPToSI
:
2083 return selectFPToInt(I
, /*isSigned*/ true);
2084 case Instruction::FPToUI
:
2085 return selectFPToInt(I
, /*isSigned*/ false);
2086 case Instruction::ICmp
:
2087 case Instruction::FCmp
:
2088 return selectCmp(I
);
2089 case Instruction::Select
:
2090 return selectSelect(I
);
2095 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value
*V
,
2097 unsigned VReg
= getRegForValue(V
);
2100 MVT VMVT
= TLI
.getValueType(DL
, V
->getType(), true).getSimpleVT();
2102 if (VMVT
== MVT::i1
)
2105 if ((VMVT
== MVT::i8
) || (VMVT
== MVT::i16
)) {
2106 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
2107 if (!emitIntExt(VMVT
, VReg
, MVT::i32
, TempReg
, IsUnsigned
))
2114 void MipsFastISel::simplifyAddress(Address
&Addr
) {
2115 if (!isInt
<16>(Addr
.getOffset())) {
2117 materialize32BitInt(Addr
.getOffset(), &Mips::GPR32RegClass
);
2118 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
2119 emitInst(Mips::ADDu
, DestReg
).addReg(TempReg
).addReg(Addr
.getReg());
2120 Addr
.setReg(DestReg
);
2125 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode
,
2126 const TargetRegisterClass
*RC
,
2127 unsigned Op0
, bool Op0IsKill
,
2128 unsigned Op1
, bool Op1IsKill
) {
2129 // We treat the MUL instruction in a special way because it clobbers
2130 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2131 // mark these registers only as implicitly defined. As a result, the
2132 // register allocator runs out of registers when this instruction is
2133 // followed by another instruction that defines the same registers too.
2134 // We can fix this by explicitly marking those registers as dead.
2135 if (MachineInstOpcode
== Mips::MUL
) {
2136 unsigned ResultReg
= createResultReg(RC
);
2137 const MCInstrDesc
&II
= TII
.get(MachineInstOpcode
);
2138 Op0
= constrainOperandRegClass(II
, Op0
, II
.getNumDefs());
2139 Op1
= constrainOperandRegClass(II
, Op1
, II
.getNumDefs() + 1);
2140 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, II
, ResultReg
)
2141 .addReg(Op0
, getKillRegState(Op0IsKill
))
2142 .addReg(Op1
, getKillRegState(Op1IsKill
))
2143 .addReg(Mips::HI0
, RegState::ImplicitDefine
| RegState::Dead
)
2144 .addReg(Mips::LO0
, RegState::ImplicitDefine
| RegState::Dead
);
2148 return FastISel::fastEmitInst_rr(MachineInstOpcode
, RC
, Op0
, Op0IsKill
, Op1
,
2154 FastISel
*Mips::createFastISel(FunctionLoweringInfo
&funcInfo
,
2155 const TargetLibraryInfo
*libInfo
) {
2156 return new MipsFastISel(funcInfo
, libInfo
);
2159 } // end namespace llvm