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/CodeGenTypes/MachineValueType.h"
42 #include "llvm/IR/Attributes.h"
43 #include "llvm/IR/CallingConv.h"
44 #include "llvm/IR/Constant.h"
45 #include "llvm/IR/Constants.h"
46 #include "llvm/IR/DataLayout.h"
47 #include "llvm/IR/Function.h"
48 #include "llvm/IR/GetElementPtrTypeIterator.h"
49 #include "llvm/IR/GlobalValue.h"
50 #include "llvm/IR/GlobalVariable.h"
51 #include "llvm/IR/InstrTypes.h"
52 #include "llvm/IR/Instruction.h"
53 #include "llvm/IR/Instructions.h"
54 #include "llvm/IR/IntrinsicInst.h"
55 #include "llvm/IR/Operator.h"
56 #include "llvm/IR/Type.h"
57 #include "llvm/IR/User.h"
58 #include "llvm/IR/Value.h"
59 #include "llvm/MC/MCContext.h"
60 #include "llvm/MC/MCInstrDesc.h"
61 #include "llvm/MC/MCRegisterInfo.h"
62 #include "llvm/MC/MCSymbol.h"
63 #include "llvm/Support/Casting.h"
64 #include "llvm/Support/Compiler.h"
65 #include "llvm/Support/Debug.h"
66 #include "llvm/Support/ErrorHandling.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 bool emitStore(MVT VT
, unsigned SrcReg
, Address
&Addr
);
183 unsigned emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, bool isZExt
);
184 bool emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
,
187 bool emitIntZExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
);
189 bool emitIntSExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
);
190 bool emitIntSExt32r1(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
192 bool emitIntSExt32r2(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
195 unsigned getRegEnsuringSimpleIntegerWidening(const Value
*, bool IsUnsigned
);
197 unsigned emitLogicalOp(unsigned ISDOpc
, MVT RetVT
, const Value
*LHS
,
200 unsigned materializeFP(const ConstantFP
*CFP
, MVT VT
);
201 unsigned materializeGV(const GlobalValue
*GV
, MVT VT
);
202 unsigned materializeInt(const Constant
*C
, MVT VT
);
203 unsigned materialize32BitInt(int64_t Imm
, const TargetRegisterClass
*RC
);
204 unsigned materializeExternalCallSym(MCSymbol
*Syn
);
206 MachineInstrBuilder
emitInst(unsigned Opc
) {
207 return BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Opc
));
210 MachineInstrBuilder
emitInst(unsigned Opc
, unsigned DstReg
) {
211 return BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Opc
),
215 MachineInstrBuilder
emitInstStore(unsigned Opc
, unsigned SrcReg
,
216 unsigned MemReg
, int64_t MemOffset
) {
217 return emitInst(Opc
).addReg(SrcReg
).addReg(MemReg
).addImm(MemOffset
);
220 MachineInstrBuilder
emitInstLoad(unsigned Opc
, unsigned DstReg
,
221 unsigned MemReg
, int64_t MemOffset
) {
222 return emitInst(Opc
, DstReg
).addReg(MemReg
).addImm(MemOffset
);
225 unsigned fastEmitInst_rr(unsigned MachineInstOpcode
,
226 const TargetRegisterClass
*RC
,
227 unsigned Op0
, unsigned Op1
);
229 // for some reason, this default is not generated by tablegen
230 // so we explicitly generate it here.
231 unsigned fastEmitInst_riir(uint64_t inst
, const TargetRegisterClass
*RC
,
232 unsigned Op0
, uint64_t imm1
, uint64_t imm2
,
237 // Call handling routines.
239 CCAssignFn
*CCAssignFnForCall(CallingConv::ID CC
) const;
240 bool processCallArgs(CallLoweringInfo
&CLI
, SmallVectorImpl
<MVT
> &ArgVTs
,
242 bool finishCall(CallLoweringInfo
&CLI
, MVT RetVT
, unsigned NumBytes
);
244 const MipsABIInfo
&getABI() const {
245 return static_cast<const MipsTargetMachine
&>(TM
).getABI();
249 // Backend specific FastISel code.
250 explicit MipsFastISel(FunctionLoweringInfo
&funcInfo
,
251 const TargetLibraryInfo
*libInfo
)
252 : FastISel(funcInfo
, libInfo
), TM(funcInfo
.MF
->getTarget()),
253 Subtarget(&funcInfo
.MF
->getSubtarget
<MipsSubtarget
>()),
254 TII(*Subtarget
->getInstrInfo()), TLI(*Subtarget
->getTargetLowering()) {
255 MFI
= funcInfo
.MF
->getInfo
<MipsFunctionInfo
>();
256 Context
= &funcInfo
.Fn
->getContext();
257 UnsupportedFPMode
= Subtarget
->isFP64bit() || Subtarget
->useSoftFloat();
260 unsigned fastMaterializeAlloca(const AllocaInst
*AI
) override
;
261 unsigned fastMaterializeConstant(const Constant
*C
) override
;
262 bool fastSelectInstruction(const Instruction
*I
) override
;
264 #include "MipsGenFastISel.inc"
267 } // end anonymous namespace
269 static bool CC_Mips(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
270 CCValAssign::LocInfo LocInfo
, ISD::ArgFlagsTy ArgFlags
,
271 CCState
&State
) LLVM_ATTRIBUTE_UNUSED
;
273 static bool CC_MipsO32_FP32(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
274 CCValAssign::LocInfo LocInfo
,
275 ISD::ArgFlagsTy ArgFlags
, CCState
&State
) {
276 llvm_unreachable("should not be called");
279 static bool CC_MipsO32_FP64(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
280 CCValAssign::LocInfo LocInfo
,
281 ISD::ArgFlagsTy ArgFlags
, CCState
&State
) {
282 llvm_unreachable("should not be called");
285 #include "MipsGenCallingConv.inc"
287 CCAssignFn
*MipsFastISel::CCAssignFnForCall(CallingConv::ID CC
) const {
291 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc
, MVT RetVT
,
292 const Value
*LHS
, const Value
*RHS
) {
293 // Canonicalize immediates to the RHS first.
294 if (isa
<ConstantInt
>(LHS
) && !isa
<ConstantInt
>(RHS
))
309 llvm_unreachable("unexpected opcode");
312 Register LHSReg
= getRegForValue(LHS
);
317 if (const auto *C
= dyn_cast
<ConstantInt
>(RHS
))
318 RHSReg
= materializeInt(C
, MVT::i32
);
320 RHSReg
= getRegForValue(RHS
);
324 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
328 emitInst(Opc
, ResultReg
).addReg(LHSReg
).addReg(RHSReg
);
332 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst
*AI
) {
333 assert(TLI
.getValueType(DL
, AI
->getType(), true) == MVT::i32
&&
334 "Alloca should always return a pointer.");
336 DenseMap
<const AllocaInst
*, int>::iterator SI
=
337 FuncInfo
.StaticAllocaMap
.find(AI
);
339 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
340 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
341 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Mips::LEA_ADDiu
),
343 .addFrameIndex(SI
->second
)
351 unsigned MipsFastISel::materializeInt(const Constant
*C
, MVT VT
) {
352 if (VT
!= MVT::i32
&& VT
!= MVT::i16
&& VT
!= MVT::i8
&& VT
!= MVT::i1
)
354 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
355 const ConstantInt
*CI
= cast
<ConstantInt
>(C
);
356 return materialize32BitInt(CI
->getZExtValue(), RC
);
359 unsigned MipsFastISel::materialize32BitInt(int64_t Imm
,
360 const TargetRegisterClass
*RC
) {
361 Register ResultReg
= createResultReg(RC
);
363 if (isInt
<16>(Imm
)) {
364 unsigned Opc
= Mips::ADDiu
;
365 emitInst(Opc
, ResultReg
).addReg(Mips::ZERO
).addImm(Imm
);
367 } else if (isUInt
<16>(Imm
)) {
368 emitInst(Mips::ORi
, ResultReg
).addReg(Mips::ZERO
).addImm(Imm
);
371 unsigned Lo
= Imm
& 0xFFFF;
372 unsigned Hi
= (Imm
>> 16) & 0xFFFF;
374 // Both Lo and Hi have nonzero bits.
375 Register TmpReg
= createResultReg(RC
);
376 emitInst(Mips::LUi
, TmpReg
).addImm(Hi
);
377 emitInst(Mips::ORi
, ResultReg
).addReg(TmpReg
).addImm(Lo
);
379 emitInst(Mips::LUi
, ResultReg
).addImm(Hi
);
384 unsigned MipsFastISel::materializeFP(const ConstantFP
*CFP
, MVT VT
) {
385 if (UnsupportedFPMode
)
387 int64_t Imm
= CFP
->getValueAPF().bitcastToAPInt().getZExtValue();
388 if (VT
== MVT::f32
) {
389 const TargetRegisterClass
*RC
= &Mips::FGR32RegClass
;
390 Register DestReg
= createResultReg(RC
);
391 unsigned TempReg
= materialize32BitInt(Imm
, &Mips::GPR32RegClass
);
392 emitInst(Mips::MTC1
, DestReg
).addReg(TempReg
);
394 } else if (VT
== MVT::f64
) {
395 const TargetRegisterClass
*RC
= &Mips::AFGR64RegClass
;
396 Register DestReg
= createResultReg(RC
);
397 unsigned TempReg1
= materialize32BitInt(Imm
>> 32, &Mips::GPR32RegClass
);
399 materialize32BitInt(Imm
& 0xFFFFFFFF, &Mips::GPR32RegClass
);
400 emitInst(Mips::BuildPairF64
, DestReg
).addReg(TempReg2
).addReg(TempReg1
);
406 unsigned MipsFastISel::materializeGV(const GlobalValue
*GV
, MVT VT
) {
407 // For now 32-bit only.
410 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
411 Register DestReg
= createResultReg(RC
);
412 const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
);
413 bool IsThreadLocal
= GVar
&& GVar
->isThreadLocal();
414 // TLS not supported at this time.
417 emitInst(Mips::LW
, DestReg
)
418 .addReg(MFI
->getGlobalBaseReg(*MF
))
419 .addGlobalAddress(GV
, 0, MipsII::MO_GOT
);
420 if ((GV
->hasInternalLinkage() ||
421 (GV
->hasLocalLinkage() && !isa
<Function
>(GV
)))) {
422 Register TempReg
= createResultReg(RC
);
423 emitInst(Mips::ADDiu
, TempReg
)
425 .addGlobalAddress(GV
, 0, MipsII::MO_ABS_LO
);
431 unsigned MipsFastISel::materializeExternalCallSym(MCSymbol
*Sym
) {
432 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
433 Register DestReg
= createResultReg(RC
);
434 emitInst(Mips::LW
, DestReg
)
435 .addReg(MFI
->getGlobalBaseReg(*MF
))
436 .addSym(Sym
, MipsII::MO_GOT
);
440 // Materialize a constant into a register, and return the register
441 // number (or zero if we failed to handle it).
442 unsigned MipsFastISel::fastMaterializeConstant(const Constant
*C
) {
443 EVT CEVT
= TLI
.getValueType(DL
, C
->getType(), true);
445 // Only handle simple types.
446 if (!CEVT
.isSimple())
448 MVT VT
= CEVT
.getSimpleVT();
450 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(C
))
451 return (UnsupportedFPMode
) ? 0 : materializeFP(CFP
, VT
);
452 else if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(C
))
453 return materializeGV(GV
, VT
);
454 else if (isa
<ConstantInt
>(C
))
455 return materializeInt(C
, VT
);
460 bool MipsFastISel::computeAddress(const Value
*Obj
, Address
&Addr
) {
461 const User
*U
= nullptr;
462 unsigned Opcode
= Instruction::UserOp1
;
463 if (const Instruction
*I
= dyn_cast
<Instruction
>(Obj
)) {
464 // Don't walk into other basic blocks unless the object is an alloca from
465 // another block, otherwise it may not have a virtual register assigned.
466 if (FuncInfo
.StaticAllocaMap
.count(static_cast<const AllocaInst
*>(Obj
)) ||
467 FuncInfo
.MBBMap
[I
->getParent()] == FuncInfo
.MBB
) {
468 Opcode
= I
->getOpcode();
471 } else if (const ConstantExpr
*C
= dyn_cast
<ConstantExpr
>(Obj
)) {
472 Opcode
= C
->getOpcode();
478 case Instruction::BitCast
:
479 // Look through bitcasts.
480 return computeAddress(U
->getOperand(0), Addr
);
481 case Instruction::GetElementPtr
: {
482 Address SavedAddr
= Addr
;
483 int64_t TmpOffset
= Addr
.getOffset();
484 // Iterate through the GEP folding the constants into offsets where
486 gep_type_iterator GTI
= gep_type_begin(U
);
487 for (User::const_op_iterator i
= U
->op_begin() + 1, e
= U
->op_end(); i
!= e
;
489 const Value
*Op
= *i
;
490 if (StructType
*STy
= GTI
.getStructTypeOrNull()) {
491 const StructLayout
*SL
= DL
.getStructLayout(STy
);
492 unsigned Idx
= cast
<ConstantInt
>(Op
)->getZExtValue();
493 TmpOffset
+= SL
->getElementOffset(Idx
);
495 uint64_t S
= GTI
.getSequentialElementStride(DL
);
497 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Op
)) {
498 // Constant-offset addressing.
499 TmpOffset
+= CI
->getSExtValue() * S
;
502 if (canFoldAddIntoGEP(U
, Op
)) {
503 // A compatible add with a constant operand. Fold the constant.
505 cast
<ConstantInt
>(cast
<AddOperator
>(Op
)->getOperand(1));
506 TmpOffset
+= CI
->getSExtValue() * S
;
507 // Iterate on the other operand.
508 Op
= cast
<AddOperator
>(Op
)->getOperand(0);
512 goto unsupported_gep
;
516 // Try to grab the base operand now.
517 Addr
.setOffset(TmpOffset
);
518 if (computeAddress(U
->getOperand(0), Addr
))
520 // We failed, restore everything and try the other options.
525 case Instruction::Alloca
: {
526 const AllocaInst
*AI
= cast
<AllocaInst
>(Obj
);
527 DenseMap
<const AllocaInst
*, int>::iterator SI
=
528 FuncInfo
.StaticAllocaMap
.find(AI
);
529 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
530 Addr
.setKind(Address::FrameIndexBase
);
531 Addr
.setFI(SI
->second
);
537 Addr
.setReg(getRegForValue(Obj
));
538 return Addr
.getReg() != 0;
541 bool MipsFastISel::computeCallAddress(const Value
*V
, Address
&Addr
) {
542 const User
*U
= nullptr;
543 unsigned Opcode
= Instruction::UserOp1
;
545 if (const auto *I
= dyn_cast
<Instruction
>(V
)) {
546 // Check if the value is defined in the same basic block. This information
547 // is crucial to know whether or not folding an operand is valid.
548 if (I
->getParent() == FuncInfo
.MBB
->getBasicBlock()) {
549 Opcode
= I
->getOpcode();
552 } else if (const auto *C
= dyn_cast
<ConstantExpr
>(V
)) {
553 Opcode
= C
->getOpcode();
560 case Instruction::BitCast
:
561 // Look past bitcasts if its operand is in the same BB.
562 return computeCallAddress(U
->getOperand(0), Addr
);
564 case Instruction::IntToPtr
:
565 // Look past no-op inttoptrs if its operand is in the same BB.
566 if (TLI
.getValueType(DL
, U
->getOperand(0)->getType()) ==
567 TLI
.getPointerTy(DL
))
568 return computeCallAddress(U
->getOperand(0), Addr
);
570 case Instruction::PtrToInt
:
571 // Look past no-op ptrtoints if its operand is in the same BB.
572 if (TLI
.getValueType(DL
, U
->getType()) == TLI
.getPointerTy(DL
))
573 return computeCallAddress(U
->getOperand(0), Addr
);
577 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
578 Addr
.setGlobalValue(GV
);
582 // If all else fails, try to materialize the value in a register.
583 if (!Addr
.getGlobalValue()) {
584 Addr
.setReg(getRegForValue(V
));
585 return Addr
.getReg() != 0;
591 bool MipsFastISel::isTypeLegal(Type
*Ty
, MVT
&VT
) {
592 EVT evt
= TLI
.getValueType(DL
, Ty
, true);
593 // Only handle simple types.
594 if (evt
== MVT::Other
|| !evt
.isSimple())
596 VT
= evt
.getSimpleVT();
598 // Handle all legal types, i.e. a register that will directly hold this
600 return TLI
.isTypeLegal(VT
);
603 bool MipsFastISel::isTypeSupported(Type
*Ty
, MVT
&VT
) {
604 if (Ty
->isVectorTy())
607 if (isTypeLegal(Ty
, VT
))
610 // If this is a type than can be sign or zero-extended to a basic operation
611 // go ahead and accept it now.
612 if (VT
== MVT::i1
|| VT
== MVT::i8
|| VT
== MVT::i16
)
618 bool MipsFastISel::isLoadTypeLegal(Type
*Ty
, MVT
&VT
) {
619 if (isTypeLegal(Ty
, VT
))
621 // We will extend this in a later patch:
622 // If this is a type than can be sign or zero-extended to a basic operation
623 // go ahead and accept it now.
624 if (VT
== MVT::i8
|| VT
== MVT::i16
)
629 // Because of how EmitCmp is called with fast-isel, you can
630 // end up with redundant "andi" instructions after the sequences emitted below.
631 // We should try and solve this issue in the future.
633 bool MipsFastISel::emitCmp(unsigned ResultReg
, const CmpInst
*CI
) {
634 const Value
*Left
= CI
->getOperand(0), *Right
= CI
->getOperand(1);
635 bool IsUnsigned
= CI
->isUnsigned();
636 unsigned LeftReg
= getRegEnsuringSimpleIntegerWidening(Left
, IsUnsigned
);
639 unsigned RightReg
= getRegEnsuringSimpleIntegerWidening(Right
, IsUnsigned
);
642 CmpInst::Predicate P
= CI
->getPredicate();
647 case CmpInst::ICMP_EQ
: {
648 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
649 emitInst(Mips::XOR
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
650 emitInst(Mips::SLTiu
, ResultReg
).addReg(TempReg
).addImm(1);
653 case CmpInst::ICMP_NE
: {
654 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
655 emitInst(Mips::XOR
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
656 emitInst(Mips::SLTu
, ResultReg
).addReg(Mips::ZERO
).addReg(TempReg
);
659 case CmpInst::ICMP_UGT
:
660 emitInst(Mips::SLTu
, ResultReg
).addReg(RightReg
).addReg(LeftReg
);
662 case CmpInst::ICMP_ULT
:
663 emitInst(Mips::SLTu
, ResultReg
).addReg(LeftReg
).addReg(RightReg
);
665 case CmpInst::ICMP_UGE
: {
666 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
667 emitInst(Mips::SLTu
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
668 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
671 case CmpInst::ICMP_ULE
: {
672 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
673 emitInst(Mips::SLTu
, TempReg
).addReg(RightReg
).addReg(LeftReg
);
674 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
677 case CmpInst::ICMP_SGT
:
678 emitInst(Mips::SLT
, ResultReg
).addReg(RightReg
).addReg(LeftReg
);
680 case CmpInst::ICMP_SLT
:
681 emitInst(Mips::SLT
, ResultReg
).addReg(LeftReg
).addReg(RightReg
);
683 case CmpInst::ICMP_SGE
: {
684 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
685 emitInst(Mips::SLT
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
686 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
689 case CmpInst::ICMP_SLE
: {
690 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
691 emitInst(Mips::SLT
, TempReg
).addReg(RightReg
).addReg(LeftReg
);
692 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
695 case CmpInst::FCMP_OEQ
:
696 case CmpInst::FCMP_UNE
:
697 case CmpInst::FCMP_OLT
:
698 case CmpInst::FCMP_OLE
:
699 case CmpInst::FCMP_OGT
:
700 case CmpInst::FCMP_OGE
: {
701 if (UnsupportedFPMode
)
703 bool IsFloat
= Left
->getType()->isFloatTy();
704 bool IsDouble
= Left
->getType()->isDoubleTy();
705 if (!IsFloat
&& !IsDouble
)
707 unsigned Opc
, CondMovOpc
;
709 case CmpInst::FCMP_OEQ
:
710 Opc
= IsFloat
? Mips::C_EQ_S
: Mips::C_EQ_D32
;
711 CondMovOpc
= Mips::MOVT_I
;
713 case CmpInst::FCMP_UNE
:
714 Opc
= IsFloat
? Mips::C_EQ_S
: Mips::C_EQ_D32
;
715 CondMovOpc
= Mips::MOVF_I
;
717 case CmpInst::FCMP_OLT
:
718 Opc
= IsFloat
? Mips::C_OLT_S
: Mips::C_OLT_D32
;
719 CondMovOpc
= Mips::MOVT_I
;
721 case CmpInst::FCMP_OLE
:
722 Opc
= IsFloat
? Mips::C_OLE_S
: Mips::C_OLE_D32
;
723 CondMovOpc
= Mips::MOVT_I
;
725 case CmpInst::FCMP_OGT
:
726 Opc
= IsFloat
? Mips::C_ULE_S
: Mips::C_ULE_D32
;
727 CondMovOpc
= Mips::MOVF_I
;
729 case CmpInst::FCMP_OGE
:
730 Opc
= IsFloat
? Mips::C_ULT_S
: Mips::C_ULT_D32
;
731 CondMovOpc
= Mips::MOVF_I
;
734 llvm_unreachable("Only switching of a subset of CCs.");
736 Register RegWithZero
= createResultReg(&Mips::GPR32RegClass
);
737 Register RegWithOne
= createResultReg(&Mips::GPR32RegClass
);
738 emitInst(Mips::ADDiu
, RegWithZero
).addReg(Mips::ZERO
).addImm(0);
739 emitInst(Mips::ADDiu
, RegWithOne
).addReg(Mips::ZERO
).addImm(1);
740 emitInst(Opc
).addReg(Mips::FCC0
, RegState::Define
).addReg(LeftReg
)
742 emitInst(CondMovOpc
, ResultReg
)
745 .addReg(RegWithZero
);
752 bool MipsFastISel::emitLoad(MVT VT
, unsigned &ResultReg
, Address
&Addr
) {
754 // more cases will be handled here in following patches.
757 switch (VT
.SimpleTy
) {
759 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
763 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
767 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
771 if (UnsupportedFPMode
)
773 ResultReg
= createResultReg(&Mips::FGR32RegClass
);
777 if (UnsupportedFPMode
)
779 ResultReg
= createResultReg(&Mips::AFGR64RegClass
);
785 if (Addr
.isRegBase()) {
786 simplifyAddress(Addr
);
787 emitInstLoad(Opc
, ResultReg
, Addr
.getReg(), Addr
.getOffset());
790 if (Addr
.isFIBase()) {
791 unsigned FI
= Addr
.getFI();
792 int64_t Offset
= Addr
.getOffset();
793 MachineFrameInfo
&MFI
= MF
->getFrameInfo();
794 MachineMemOperand
*MMO
= MF
->getMachineMemOperand(
795 MachinePointerInfo::getFixedStack(*MF
, FI
), MachineMemOperand::MOLoad
,
796 MFI
.getObjectSize(FI
), Align(4));
797 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Opc
), ResultReg
)
806 bool MipsFastISel::emitStore(MVT VT
, unsigned SrcReg
, Address
&Addr
) {
808 // more cases will be handled here in following patches.
811 switch (VT
.SimpleTy
) {
822 if (UnsupportedFPMode
)
827 if (UnsupportedFPMode
)
834 if (Addr
.isRegBase()) {
835 simplifyAddress(Addr
);
836 emitInstStore(Opc
, SrcReg
, Addr
.getReg(), Addr
.getOffset());
839 if (Addr
.isFIBase()) {
840 unsigned FI
= Addr
.getFI();
841 int64_t Offset
= Addr
.getOffset();
842 MachineFrameInfo
&MFI
= MF
->getFrameInfo();
843 MachineMemOperand
*MMO
= MF
->getMachineMemOperand(
844 MachinePointerInfo::getFixedStack(*MF
, FI
), MachineMemOperand::MOStore
,
845 MFI
.getObjectSize(FI
), Align(4));
846 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Opc
))
856 bool MipsFastISel::selectLogicalOp(const Instruction
*I
) {
858 if (!isTypeSupported(I
->getType(), VT
))
862 switch (I
->getOpcode()) {
864 llvm_unreachable("Unexpected instruction.");
865 case Instruction::And
:
866 ResultReg
= emitLogicalOp(ISD::AND
, VT
, I
->getOperand(0), I
->getOperand(1));
868 case Instruction::Or
:
869 ResultReg
= emitLogicalOp(ISD::OR
, VT
, I
->getOperand(0), I
->getOperand(1));
871 case Instruction::Xor
:
872 ResultReg
= emitLogicalOp(ISD::XOR
, VT
, I
->getOperand(0), I
->getOperand(1));
879 updateValueMap(I
, ResultReg
);
883 bool MipsFastISel::selectLoad(const Instruction
*I
) {
884 // Atomic loads need special handling.
885 if (cast
<LoadInst
>(I
)->isAtomic())
888 // Verify we have a legal type before going any further.
890 if (!isLoadTypeLegal(I
->getType(), VT
))
893 // See if we can handle this address.
895 if (!computeAddress(I
->getOperand(0), Addr
))
899 if (!emitLoad(VT
, ResultReg
, Addr
))
901 updateValueMap(I
, ResultReg
);
905 bool MipsFastISel::selectStore(const Instruction
*I
) {
906 Value
*Op0
= I
->getOperand(0);
909 // Atomic stores need special handling.
910 if (cast
<StoreInst
>(I
)->isAtomic())
913 // Verify we have a legal type before going any further.
915 if (!isLoadTypeLegal(I
->getOperand(0)->getType(), VT
))
918 // Get the value to be stored into a register.
919 SrcReg
= getRegForValue(Op0
);
923 // See if we can handle this address.
925 if (!computeAddress(I
->getOperand(1), Addr
))
928 if (!emitStore(VT
, SrcReg
, Addr
))
933 // This can cause a redundant sltiu to be generated.
934 // FIXME: try and eliminate this in a future patch.
935 bool MipsFastISel::selectBranch(const Instruction
*I
) {
936 const BranchInst
*BI
= cast
<BranchInst
>(I
);
937 MachineBasicBlock
*BrBB
= FuncInfo
.MBB
;
939 // TBB is the basic block for the case where the comparison is true.
940 // FBB is the basic block for the case where the comparison is false.
941 // if (cond) goto TBB
945 MachineBasicBlock
*TBB
= FuncInfo
.MBBMap
[BI
->getSuccessor(0)];
946 MachineBasicBlock
*FBB
= FuncInfo
.MBBMap
[BI
->getSuccessor(1)];
948 // Fold the common case of a conditional branch with a comparison
949 // in the same block.
950 unsigned ZExtCondReg
= 0;
951 if (const CmpInst
*CI
= dyn_cast
<CmpInst
>(BI
->getCondition())) {
952 if (CI
->hasOneUse() && CI
->getParent() == I
->getParent()) {
953 ZExtCondReg
= createResultReg(&Mips::GPR32RegClass
);
954 if (!emitCmp(ZExtCondReg
, CI
))
959 // For the general case, we need to mask with 1.
960 if (ZExtCondReg
== 0) {
961 Register CondReg
= getRegForValue(BI
->getCondition());
965 ZExtCondReg
= emitIntExt(MVT::i1
, CondReg
, MVT::i32
, true);
966 if (ZExtCondReg
== 0)
970 BuildMI(*BrBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Mips::BGTZ
))
973 finishCondBranch(BI
->getParent(), TBB
, FBB
);
977 bool MipsFastISel::selectCmp(const Instruction
*I
) {
978 const CmpInst
*CI
= cast
<CmpInst
>(I
);
979 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
980 if (!emitCmp(ResultReg
, CI
))
982 updateValueMap(I
, ResultReg
);
986 // Attempt to fast-select a floating-point extend instruction.
987 bool MipsFastISel::selectFPExt(const Instruction
*I
) {
988 if (UnsupportedFPMode
)
990 Value
*Src
= I
->getOperand(0);
991 EVT SrcVT
= TLI
.getValueType(DL
, Src
->getType(), true);
992 EVT DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
994 if (SrcVT
!= MVT::f32
|| DestVT
!= MVT::f64
)
998 getRegForValue(Src
); // this must be a 32bit floating point register class
999 // maybe we should handle this differently
1003 Register DestReg
= createResultReg(&Mips::AFGR64RegClass
);
1004 emitInst(Mips::CVT_D32_S
, DestReg
).addReg(SrcReg
);
1005 updateValueMap(I
, DestReg
);
1009 bool MipsFastISel::selectSelect(const Instruction
*I
) {
1010 assert(isa
<SelectInst
>(I
) && "Expected a select instruction.");
1012 LLVM_DEBUG(dbgs() << "selectSelect\n");
1015 if (!isTypeSupported(I
->getType(), VT
) || UnsupportedFPMode
) {
1017 dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
1021 unsigned CondMovOpc
;
1022 const TargetRegisterClass
*RC
;
1024 if (VT
.isInteger() && !VT
.isVector() && VT
.getSizeInBits() <= 32) {
1025 CondMovOpc
= Mips::MOVN_I_I
;
1026 RC
= &Mips::GPR32RegClass
;
1027 } else if (VT
== MVT::f32
) {
1028 CondMovOpc
= Mips::MOVN_I_S
;
1029 RC
= &Mips::FGR32RegClass
;
1030 } else if (VT
== MVT::f64
) {
1031 CondMovOpc
= Mips::MOVN_I_D32
;
1032 RC
= &Mips::AFGR64RegClass
;
1036 const SelectInst
*SI
= cast
<SelectInst
>(I
);
1037 const Value
*Cond
= SI
->getCondition();
1038 Register Src1Reg
= getRegForValue(SI
->getTrueValue());
1039 Register Src2Reg
= getRegForValue(SI
->getFalseValue());
1040 Register CondReg
= getRegForValue(Cond
);
1042 if (!Src1Reg
|| !Src2Reg
|| !CondReg
)
1045 Register ZExtCondReg
= createResultReg(&Mips::GPR32RegClass
);
1049 if (!emitIntExt(MVT::i1
, CondReg
, MVT::i32
, ZExtCondReg
, true))
1052 Register ResultReg
= createResultReg(RC
);
1053 Register TempReg
= createResultReg(RC
);
1055 if (!ResultReg
|| !TempReg
)
1058 emitInst(TargetOpcode::COPY
, TempReg
).addReg(Src2Reg
);
1059 emitInst(CondMovOpc
, ResultReg
)
1060 .addReg(Src1Reg
).addReg(ZExtCondReg
).addReg(TempReg
);
1061 updateValueMap(I
, ResultReg
);
1065 // Attempt to fast-select a floating-point truncate instruction.
1066 bool MipsFastISel::selectFPTrunc(const Instruction
*I
) {
1067 if (UnsupportedFPMode
)
1069 Value
*Src
= I
->getOperand(0);
1070 EVT SrcVT
= TLI
.getValueType(DL
, Src
->getType(), true);
1071 EVT DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1073 if (SrcVT
!= MVT::f64
|| DestVT
!= MVT::f32
)
1076 Register SrcReg
= getRegForValue(Src
);
1080 Register DestReg
= createResultReg(&Mips::FGR32RegClass
);
1084 emitInst(Mips::CVT_S_D32
, DestReg
).addReg(SrcReg
);
1085 updateValueMap(I
, DestReg
);
1089 // Attempt to fast-select a floating-point-to-integer conversion.
1090 bool MipsFastISel::selectFPToInt(const Instruction
*I
, bool IsSigned
) {
1091 if (UnsupportedFPMode
)
1095 return false; // We don't handle this case yet. There is no native
1096 // instruction for this but it can be synthesized.
1097 Type
*DstTy
= I
->getType();
1098 if (!isTypeLegal(DstTy
, DstVT
))
1101 if (DstVT
!= MVT::i32
)
1104 Value
*Src
= I
->getOperand(0);
1105 Type
*SrcTy
= Src
->getType();
1106 if (!isTypeLegal(SrcTy
, SrcVT
))
1109 if (SrcVT
!= MVT::f32
&& SrcVT
!= MVT::f64
)
1112 Register SrcReg
= getRegForValue(Src
);
1116 // Determine the opcode for the conversion, which takes place
1117 // entirely within FPRs.
1118 Register DestReg
= createResultReg(&Mips::GPR32RegClass
);
1119 Register TempReg
= createResultReg(&Mips::FGR32RegClass
);
1120 unsigned Opc
= (SrcVT
== MVT::f32
) ? Mips::TRUNC_W_S
: Mips::TRUNC_W_D32
;
1122 // Generate the convert.
1123 emitInst(Opc
, TempReg
).addReg(SrcReg
);
1124 emitInst(Mips::MFC1
, DestReg
).addReg(TempReg
);
1126 updateValueMap(I
, DestReg
);
1130 bool MipsFastISel::processCallArgs(CallLoweringInfo
&CLI
,
1131 SmallVectorImpl
<MVT
> &OutVTs
,
1132 unsigned &NumBytes
) {
1133 CallingConv::ID CC
= CLI
.CallConv
;
1134 SmallVector
<CCValAssign
, 16> ArgLocs
;
1135 CCState
CCInfo(CC
, false, *FuncInfo
.MF
, ArgLocs
, *Context
);
1136 CCInfo
.AnalyzeCallOperands(OutVTs
, CLI
.OutFlags
, CCAssignFnForCall(CC
));
1137 // Get a count of how many bytes are to be pushed on the stack.
1138 NumBytes
= CCInfo
.getStackSize();
1139 // This is the minimum argument area used for A0-A3.
1143 emitInst(Mips::ADJCALLSTACKDOWN
).addImm(16).addImm(0);
1144 // Process the args.
1146 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
1147 CCValAssign
&VA
= ArgLocs
[i
];
1148 const Value
*ArgVal
= CLI
.OutVals
[VA
.getValNo()];
1149 MVT ArgVT
= OutVTs
[VA
.getValNo()];
1153 if (ArgVT
== MVT::f32
) {
1154 VA
.convertToReg(Mips::F12
);
1155 } else if (ArgVT
== MVT::f64
) {
1156 if (Subtarget
->isFP64bit())
1157 VA
.convertToReg(Mips::D6_64
);
1159 VA
.convertToReg(Mips::D6
);
1161 } else if (i
== 1) {
1162 if ((firstMVT
== MVT::f32
) || (firstMVT
== MVT::f64
)) {
1163 if (ArgVT
== MVT::f32
) {
1164 VA
.convertToReg(Mips::F14
);
1165 } else if (ArgVT
== MVT::f64
) {
1166 if (Subtarget
->isFP64bit())
1167 VA
.convertToReg(Mips::D7_64
);
1169 VA
.convertToReg(Mips::D7
);
1173 if (((ArgVT
== MVT::i32
) || (ArgVT
== MVT::f32
) || (ArgVT
== MVT::i16
) ||
1174 (ArgVT
== MVT::i8
)) &&
1176 switch (VA
.getLocMemOffset()) {
1178 VA
.convertToReg(Mips::A0
);
1181 VA
.convertToReg(Mips::A1
);
1184 VA
.convertToReg(Mips::A2
);
1187 VA
.convertToReg(Mips::A3
);
1193 Register ArgReg
= getRegForValue(ArgVal
);
1197 // Handle arg promotion: SExt, ZExt, AExt.
1198 switch (VA
.getLocInfo()) {
1199 case CCValAssign::Full
:
1201 case CCValAssign::AExt
:
1202 case CCValAssign::SExt
: {
1203 MVT DestVT
= VA
.getLocVT();
1205 ArgReg
= emitIntExt(SrcVT
, ArgReg
, DestVT
, /*isZExt=*/false);
1210 case CCValAssign::ZExt
: {
1211 MVT DestVT
= VA
.getLocVT();
1213 ArgReg
= emitIntExt(SrcVT
, ArgReg
, DestVT
, /*isZExt=*/true);
1219 llvm_unreachable("Unknown arg promotion!");
1222 // Now copy/store arg to correct locations.
1223 if (VA
.isRegLoc() && !VA
.needsCustom()) {
1224 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
,
1225 TII
.get(TargetOpcode::COPY
), VA
.getLocReg()).addReg(ArgReg
);
1226 CLI
.OutRegs
.push_back(VA
.getLocReg());
1227 } else if (VA
.needsCustom()) {
1228 llvm_unreachable("Mips does not use custom args.");
1232 // FIXME: This path will currently return false. It was copied
1233 // from the AArch64 port and should be essentially fine for Mips too.
1234 // The work to finish up this path will be done in a follow-on patch.
1236 assert(VA
.isMemLoc() && "Assuming store on stack.");
1237 // Don't emit stores for undef values.
1238 if (isa
<UndefValue
>(ArgVal
))
1241 // Need to store on the stack.
1242 // FIXME: This alignment is incorrect but this path is disabled
1243 // for now (will return false). We need to determine the right alignment
1244 // based on the normal alignment for the underlying machine type.
1246 unsigned ArgSize
= alignTo(ArgVT
.getSizeInBits(), 4);
1248 unsigned BEAlign
= 0;
1249 if (ArgSize
< 8 && !Subtarget
->isLittle())
1250 BEAlign
= 8 - ArgSize
;
1253 Addr
.setKind(Address::RegBase
);
1254 Addr
.setReg(Mips::SP
);
1255 Addr
.setOffset(VA
.getLocMemOffset() + BEAlign
);
1257 Align Alignment
= DL
.getABITypeAlign(ArgVal
->getType());
1258 MachineMemOperand
*MMO
= FuncInfo
.MF
->getMachineMemOperand(
1259 MachinePointerInfo::getStack(*FuncInfo
.MF
, Addr
.getOffset()),
1260 MachineMemOperand::MOStore
, ArgVT
.getStoreSize(), Alignment
);
1262 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1263 return false; // can't store on the stack yet.
1270 bool MipsFastISel::finishCall(CallLoweringInfo
&CLI
, MVT RetVT
,
1271 unsigned NumBytes
) {
1272 CallingConv::ID CC
= CLI
.CallConv
;
1273 emitInst(Mips::ADJCALLSTACKUP
).addImm(16).addImm(0);
1274 if (RetVT
!= MVT::isVoid
) {
1275 SmallVector
<CCValAssign
, 16> RVLocs
;
1276 MipsCCState
CCInfo(CC
, false, *FuncInfo
.MF
, RVLocs
, *Context
);
1278 CCInfo
.AnalyzeCallResult(CLI
.Ins
, RetCC_Mips
, CLI
.RetTy
,
1279 CLI
.Symbol
? CLI
.Symbol
->getName().data()
1282 // Only handle a single return value.
1283 if (RVLocs
.size() != 1)
1285 // Copy all of the result registers out of their specified physreg.
1286 MVT CopyVT
= RVLocs
[0].getValVT();
1287 // Special handling for extended integers.
1288 if (RetVT
== MVT::i1
|| RetVT
== MVT::i8
|| RetVT
== MVT::i16
)
1291 Register ResultReg
= createResultReg(TLI
.getRegClassFor(CopyVT
));
1294 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
,
1295 TII
.get(TargetOpcode::COPY
),
1296 ResultReg
).addReg(RVLocs
[0].getLocReg());
1297 CLI
.InRegs
.push_back(RVLocs
[0].getLocReg());
1299 CLI
.ResultReg
= ResultReg
;
1300 CLI
.NumResultRegs
= 1;
1305 bool MipsFastISel::fastLowerArguments() {
1306 LLVM_DEBUG(dbgs() << "fastLowerArguments\n");
1308 if (!FuncInfo
.CanLowerReturn
) {
1309 LLVM_DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
1313 const Function
*F
= FuncInfo
.Fn
;
1314 if (F
->isVarArg()) {
1315 LLVM_DEBUG(dbgs() << ".. gave up (varargs)\n");
1319 CallingConv::ID CC
= F
->getCallingConv();
1320 if (CC
!= CallingConv::C
) {
1321 LLVM_DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
1325 std::array
<MCPhysReg
, 4> GPR32ArgRegs
= {{Mips::A0
, Mips::A1
, Mips::A2
,
1327 std::array
<MCPhysReg
, 2> FGR32ArgRegs
= {{Mips::F12
, Mips::F14
}};
1328 std::array
<MCPhysReg
, 2> AFGR64ArgRegs
= {{Mips::D6
, Mips::D7
}};
1329 auto NextGPR32
= GPR32ArgRegs
.begin();
1330 auto NextFGR32
= FGR32ArgRegs
.begin();
1331 auto NextAFGR64
= AFGR64ArgRegs
.begin();
1333 struct AllocatedReg
{
1334 const TargetRegisterClass
*RC
;
1336 AllocatedReg(const TargetRegisterClass
*RC
, unsigned Reg
)
1337 : RC(RC
), Reg(Reg
) {}
1340 // Only handle simple cases. i.e. All arguments are directly mapped to
1341 // registers of the appropriate type.
1342 SmallVector
<AllocatedReg
, 4> Allocation
;
1343 for (const auto &FormalArg
: F
->args()) {
1344 if (FormalArg
.hasAttribute(Attribute::InReg
) ||
1345 FormalArg
.hasAttribute(Attribute::StructRet
) ||
1346 FormalArg
.hasAttribute(Attribute::ByVal
)) {
1347 LLVM_DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
1351 Type
*ArgTy
= FormalArg
.getType();
1352 if (ArgTy
->isStructTy() || ArgTy
->isArrayTy() || ArgTy
->isVectorTy()) {
1353 LLVM_DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
1357 EVT ArgVT
= TLI
.getValueType(DL
, ArgTy
);
1358 LLVM_DEBUG(dbgs() << ".. " << FormalArg
.getArgNo() << ": "
1360 if (!ArgVT
.isSimple()) {
1361 LLVM_DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
1365 switch (ArgVT
.getSimpleVT().SimpleTy
) {
1369 if (!FormalArg
.hasAttribute(Attribute::SExt
) &&
1370 !FormalArg
.hasAttribute(Attribute::ZExt
)) {
1371 // It must be any extend, this shouldn't happen for clang-generated IR
1372 // so just fall back on SelectionDAG.
1373 LLVM_DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
1377 if (NextGPR32
== GPR32ArgRegs
.end()) {
1378 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1382 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32
<< ")\n");
1383 Allocation
.emplace_back(&Mips::GPR32RegClass
, *NextGPR32
++);
1385 // Allocating any GPR32 prohibits further use of floating point arguments.
1386 NextFGR32
= FGR32ArgRegs
.end();
1387 NextAFGR64
= AFGR64ArgRegs
.end();
1391 if (FormalArg
.hasAttribute(Attribute::ZExt
)) {
1392 // The O32 ABI does not permit a zero-extended i32.
1393 LLVM_DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
1397 if (NextGPR32
== GPR32ArgRegs
.end()) {
1398 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1402 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32
<< ")\n");
1403 Allocation
.emplace_back(&Mips::GPR32RegClass
, *NextGPR32
++);
1405 // Allocating any GPR32 prohibits further use of floating point arguments.
1406 NextFGR32
= FGR32ArgRegs
.end();
1407 NextAFGR64
= AFGR64ArgRegs
.end();
1411 if (UnsupportedFPMode
) {
1412 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1415 if (NextFGR32
== FGR32ArgRegs
.end()) {
1416 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
1419 LLVM_DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32
<< ")\n");
1420 Allocation
.emplace_back(&Mips::FGR32RegClass
, *NextFGR32
++);
1421 // Allocating an FGR32 also allocates the super-register AFGR64, and
1422 // ABI rules require us to skip the corresponding GPR32.
1423 if (NextGPR32
!= GPR32ArgRegs
.end())
1425 if (NextAFGR64
!= AFGR64ArgRegs
.end())
1430 if (UnsupportedFPMode
) {
1431 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1434 if (NextAFGR64
== AFGR64ArgRegs
.end()) {
1435 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
1438 LLVM_DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64
<< ")\n");
1439 Allocation
.emplace_back(&Mips::AFGR64RegClass
, *NextAFGR64
++);
1440 // Allocating an FGR32 also allocates the super-register AFGR64, and
1441 // ABI rules require us to skip the corresponding GPR32 pair.
1442 if (NextGPR32
!= GPR32ArgRegs
.end())
1444 if (NextGPR32
!= GPR32ArgRegs
.end())
1446 if (NextFGR32
!= FGR32ArgRegs
.end())
1451 LLVM_DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
1456 for (const auto &FormalArg
: F
->args()) {
1457 unsigned ArgNo
= FormalArg
.getArgNo();
1458 unsigned SrcReg
= Allocation
[ArgNo
].Reg
;
1459 Register DstReg
= FuncInfo
.MF
->addLiveIn(SrcReg
, Allocation
[ArgNo
].RC
);
1460 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1461 // Without this, EmitLiveInCopies may eliminate the livein if its only
1462 // use is a bitcast (which isn't turned into an instruction).
1463 Register ResultReg
= createResultReg(Allocation
[ArgNo
].RC
);
1464 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
,
1465 TII
.get(TargetOpcode::COPY
), ResultReg
)
1466 .addReg(DstReg
, getKillRegState(true));
1467 updateValueMap(&FormalArg
, ResultReg
);
1470 // Calculate the size of the incoming arguments area.
1471 // We currently reject all the cases where this would be non-zero.
1472 unsigned IncomingArgSizeInBytes
= 0;
1474 // Account for the reserved argument area on ABI's that have one (O32).
1475 // It seems strange to do this on the caller side but it's necessary in
1476 // SelectionDAG's implementation.
1477 IncomingArgSizeInBytes
= std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC
),
1478 IncomingArgSizeInBytes
);
1480 MF
->getInfo
<MipsFunctionInfo
>()->setFormalArgInfo(IncomingArgSizeInBytes
,
1486 bool MipsFastISel::fastLowerCall(CallLoweringInfo
&CLI
) {
1487 CallingConv::ID CC
= CLI
.CallConv
;
1488 bool IsTailCall
= CLI
.IsTailCall
;
1489 bool IsVarArg
= CLI
.IsVarArg
;
1490 const Value
*Callee
= CLI
.Callee
;
1491 MCSymbol
*Symbol
= CLI
.Symbol
;
1493 // Do not handle FastCC.
1494 if (CC
== CallingConv::Fast
)
1497 // Allow SelectionDAG isel to handle tail calls.
1501 // Let SDISel handle vararg functions.
1505 // FIXME: Only handle *simple* calls for now.
1507 if (CLI
.RetTy
->isVoidTy())
1508 RetVT
= MVT::isVoid
;
1509 else if (!isTypeSupported(CLI
.RetTy
, RetVT
))
1512 for (auto Flag
: CLI
.OutFlags
)
1513 if (Flag
.isInReg() || Flag
.isSRet() || Flag
.isNest() || Flag
.isByVal())
1516 // Set up the argument vectors.
1517 SmallVector
<MVT
, 16> OutVTs
;
1518 OutVTs
.reserve(CLI
.OutVals
.size());
1520 for (auto *Val
: CLI
.OutVals
) {
1522 if (!isTypeLegal(Val
->getType(), VT
) &&
1523 !(VT
== MVT::i1
|| VT
== MVT::i8
|| VT
== MVT::i16
))
1526 // We don't handle vector parameters yet.
1527 if (VT
.isVector() || VT
.getSizeInBits() > 64)
1530 OutVTs
.push_back(VT
);
1534 if (!computeCallAddress(Callee
, Addr
))
1537 // Handle the arguments now that we've gotten them.
1539 if (!processCallArgs(CLI
, OutVTs
, NumBytes
))
1542 if (!Addr
.getGlobalValue())
1546 unsigned DestAddress
;
1548 DestAddress
= materializeExternalCallSym(Symbol
);
1550 DestAddress
= materializeGV(Addr
.getGlobalValue(), MVT::i32
);
1551 emitInst(TargetOpcode::COPY
, Mips::T9
).addReg(DestAddress
);
1552 MachineInstrBuilder MIB
=
1553 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Mips::JALR
),
1554 Mips::RA
).addReg(Mips::T9
);
1556 // Add implicit physical register uses to the call.
1557 for (auto Reg
: CLI
.OutRegs
)
1558 MIB
.addReg(Reg
, RegState::Implicit
);
1560 // Add a register mask with the call-preserved registers.
1561 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1562 MIB
.addRegMask(TRI
.getCallPreservedMask(*FuncInfo
.MF
, CC
));
1566 if (EmitJalrReloc
&& !Subtarget
->inMips16Mode()) {
1567 // Attach callee address to the instruction, let asm printer emit
1568 // .reloc R_MIPS_JALR.
1570 MIB
.addSym(Symbol
, MipsII::MO_JALR
);
1572 MIB
.addSym(FuncInfo
.MF
->getContext().getOrCreateSymbol(
1573 Addr
.getGlobalValue()->getName()), MipsII::MO_JALR
);
1576 // Finish off the call including any return values.
1577 return finishCall(CLI
, RetVT
, NumBytes
);
1580 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst
*II
) {
1581 switch (II
->getIntrinsicID()) {
1584 case Intrinsic::bswap
: {
1585 Type
*RetTy
= II
->getCalledFunction()->getReturnType();
1588 if (!isTypeSupported(RetTy
, VT
))
1591 Register SrcReg
= getRegForValue(II
->getOperand(0));
1594 Register DestReg
= createResultReg(&Mips::GPR32RegClass
);
1597 if (VT
== MVT::i16
) {
1598 if (Subtarget
->hasMips32r2()) {
1599 emitInst(Mips::WSBH
, DestReg
).addReg(SrcReg
);
1600 updateValueMap(II
, DestReg
);
1603 unsigned TempReg
[3];
1604 for (unsigned &R
: TempReg
) {
1605 R
= createResultReg(&Mips::GPR32RegClass
);
1609 emitInst(Mips::SLL
, TempReg
[0]).addReg(SrcReg
).addImm(8);
1610 emitInst(Mips::SRL
, TempReg
[1]).addReg(SrcReg
).addImm(8);
1611 emitInst(Mips::ANDi
, TempReg
[2]).addReg(TempReg
[1]).addImm(0xFF);
1612 emitInst(Mips::OR
, DestReg
).addReg(TempReg
[0]).addReg(TempReg
[2]);
1613 updateValueMap(II
, DestReg
);
1616 } else if (VT
== MVT::i32
) {
1617 if (Subtarget
->hasMips32r2()) {
1618 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
1619 emitInst(Mips::WSBH
, TempReg
).addReg(SrcReg
);
1620 emitInst(Mips::ROTR
, DestReg
).addReg(TempReg
).addImm(16);
1621 updateValueMap(II
, DestReg
);
1624 unsigned TempReg
[8];
1625 for (unsigned &R
: TempReg
) {
1626 R
= createResultReg(&Mips::GPR32RegClass
);
1631 emitInst(Mips::SRL
, TempReg
[0]).addReg(SrcReg
).addImm(8);
1632 emitInst(Mips::SRL
, TempReg
[1]).addReg(SrcReg
).addImm(24);
1633 emitInst(Mips::ANDi
, TempReg
[2]).addReg(TempReg
[0]).addImm(0xFF00);
1634 emitInst(Mips::OR
, TempReg
[3]).addReg(TempReg
[1]).addReg(TempReg
[2]);
1636 emitInst(Mips::ANDi
, TempReg
[4]).addReg(SrcReg
).addImm(0xFF00);
1637 emitInst(Mips::SLL
, TempReg
[5]).addReg(TempReg
[4]).addImm(8);
1639 emitInst(Mips::SLL
, TempReg
[6]).addReg(SrcReg
).addImm(24);
1640 emitInst(Mips::OR
, TempReg
[7]).addReg(TempReg
[3]).addReg(TempReg
[5]);
1641 emitInst(Mips::OR
, DestReg
).addReg(TempReg
[6]).addReg(TempReg
[7]);
1642 updateValueMap(II
, DestReg
);
1648 case Intrinsic::memcpy
:
1649 case Intrinsic::memmove
: {
1650 const auto *MTI
= cast
<MemTransferInst
>(II
);
1651 // Don't handle volatile.
1652 if (MTI
->isVolatile())
1654 if (!MTI
->getLength()->getType()->isIntegerTy(32))
1656 const char *IntrMemName
= isa
<MemCpyInst
>(II
) ? "memcpy" : "memmove";
1657 return lowerCallTo(II
, IntrMemName
, II
->arg_size() - 1);
1659 case Intrinsic::memset
: {
1660 const MemSetInst
*MSI
= cast
<MemSetInst
>(II
);
1661 // Don't handle volatile.
1662 if (MSI
->isVolatile())
1664 if (!MSI
->getLength()->getType()->isIntegerTy(32))
1666 return lowerCallTo(II
, "memset", II
->arg_size() - 1);
1672 bool MipsFastISel::selectRet(const Instruction
*I
) {
1673 const Function
&F
= *I
->getParent()->getParent();
1674 const ReturnInst
*Ret
= cast
<ReturnInst
>(I
);
1676 LLVM_DEBUG(dbgs() << "selectRet\n");
1678 if (!FuncInfo
.CanLowerReturn
)
1681 // Build a list of return value registers.
1682 SmallVector
<unsigned, 4> RetRegs
;
1684 if (Ret
->getNumOperands() > 0) {
1685 CallingConv::ID CC
= F
.getCallingConv();
1687 // Do not handle FastCC.
1688 if (CC
== CallingConv::Fast
)
1691 SmallVector
<ISD::OutputArg
, 4> Outs
;
1692 GetReturnInfo(CC
, F
.getReturnType(), F
.getAttributes(), Outs
, TLI
, DL
);
1694 // Analyze operands of the call, assigning locations to each operand.
1695 SmallVector
<CCValAssign
, 16> ValLocs
;
1696 MipsCCState
CCInfo(CC
, F
.isVarArg(), *FuncInfo
.MF
, ValLocs
,
1698 CCAssignFn
*RetCC
= RetCC_Mips
;
1699 CCInfo
.AnalyzeReturn(Outs
, RetCC
);
1701 // Only handle a single return value for now.
1702 if (ValLocs
.size() != 1)
1705 CCValAssign
&VA
= ValLocs
[0];
1706 const Value
*RV
= Ret
->getOperand(0);
1708 // Don't bother handling odd stuff for now.
1709 if ((VA
.getLocInfo() != CCValAssign::Full
) &&
1710 (VA
.getLocInfo() != CCValAssign::BCvt
))
1713 // Only handle register returns for now.
1717 Register Reg
= getRegForValue(RV
);
1721 unsigned SrcReg
= Reg
+ VA
.getValNo();
1722 Register DestReg
= VA
.getLocReg();
1723 // Avoid a cross-class copy. This is very unlikely.
1724 if (!MRI
.getRegClass(SrcReg
)->contains(DestReg
))
1727 EVT RVEVT
= TLI
.getValueType(DL
, RV
->getType());
1728 if (!RVEVT
.isSimple())
1731 if (RVEVT
.isVector())
1734 MVT RVVT
= RVEVT
.getSimpleVT();
1735 if (RVVT
== MVT::f128
)
1738 // Do not handle FGR64 returns for now.
1739 if (RVVT
== MVT::f64
&& UnsupportedFPMode
) {
1740 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1744 MVT DestVT
= VA
.getValVT();
1745 // Special handling for extended integers.
1746 if (RVVT
!= DestVT
) {
1747 if (RVVT
!= MVT::i1
&& RVVT
!= MVT::i8
&& RVVT
!= MVT::i16
)
1750 if (Outs
[0].Flags
.isZExt() || Outs
[0].Flags
.isSExt()) {
1751 bool IsZExt
= Outs
[0].Flags
.isZExt();
1752 SrcReg
= emitIntExt(RVVT
, SrcReg
, DestVT
, IsZExt
);
1759 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
,
1760 TII
.get(TargetOpcode::COPY
), DestReg
).addReg(SrcReg
);
1762 // Add register to return instruction.
1763 RetRegs
.push_back(VA
.getLocReg());
1765 MachineInstrBuilder MIB
= emitInst(Mips::RetRA
);
1766 for (unsigned Reg
: RetRegs
)
1767 MIB
.addReg(Reg
, RegState::Implicit
);
1771 bool MipsFastISel::selectTrunc(const Instruction
*I
) {
1772 // The high bits for a type smaller than the register size are assumed to be
1774 Value
*Op
= I
->getOperand(0);
1777 SrcVT
= TLI
.getValueType(DL
, Op
->getType(), true);
1778 DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1780 if (SrcVT
!= MVT::i32
&& SrcVT
!= MVT::i16
&& SrcVT
!= MVT::i8
)
1782 if (DestVT
!= MVT::i16
&& DestVT
!= MVT::i8
&& DestVT
!= MVT::i1
)
1785 Register SrcReg
= getRegForValue(Op
);
1789 // Because the high bits are undefined, a truncate doesn't generate
1791 updateValueMap(I
, SrcReg
);
1795 bool MipsFastISel::selectIntExt(const Instruction
*I
) {
1796 Type
*DestTy
= I
->getType();
1797 Value
*Src
= I
->getOperand(0);
1798 Type
*SrcTy
= Src
->getType();
1800 bool isZExt
= isa
<ZExtInst
>(I
);
1801 Register SrcReg
= getRegForValue(Src
);
1805 EVT SrcEVT
, DestEVT
;
1806 SrcEVT
= TLI
.getValueType(DL
, SrcTy
, true);
1807 DestEVT
= TLI
.getValueType(DL
, DestTy
, true);
1808 if (!SrcEVT
.isSimple())
1810 if (!DestEVT
.isSimple())
1813 MVT SrcVT
= SrcEVT
.getSimpleVT();
1814 MVT DestVT
= DestEVT
.getSimpleVT();
1815 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1817 if (!emitIntExt(SrcVT
, SrcReg
, DestVT
, ResultReg
, isZExt
))
1819 updateValueMap(I
, ResultReg
);
1823 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1826 switch (SrcVT
.SimpleTy
) {
1836 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
1837 emitInst(Mips::SLL
, TempReg
).addReg(SrcReg
).addImm(ShiftAmt
);
1838 emitInst(Mips::SRA
, DestReg
).addReg(TempReg
).addImm(ShiftAmt
);
1842 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1844 switch (SrcVT
.SimpleTy
) {
1848 emitInst(Mips::SEB
, DestReg
).addReg(SrcReg
);
1851 emitInst(Mips::SEH
, DestReg
).addReg(SrcReg
);
1857 bool MipsFastISel::emitIntSExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1859 if ((DestVT
!= MVT::i32
) && (DestVT
!= MVT::i16
))
1861 if (Subtarget
->hasMips32r2())
1862 return emitIntSExt32r2(SrcVT
, SrcReg
, DestVT
, DestReg
);
1863 return emitIntSExt32r1(SrcVT
, SrcReg
, DestVT
, DestReg
);
1866 bool MipsFastISel::emitIntZExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1870 switch (SrcVT
.SimpleTy
) {
1884 emitInst(Mips::ANDi
, DestReg
).addReg(SrcReg
).addImm(Imm
);
1888 bool MipsFastISel::emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1889 unsigned DestReg
, bool IsZExt
) {
1890 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1891 // DestVT are odd things, so test to make sure that they are both types we can
1892 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1893 // bail out to SelectionDAG.
1894 if (((DestVT
!= MVT::i8
) && (DestVT
!= MVT::i16
) && (DestVT
!= MVT::i32
)) ||
1895 ((SrcVT
!= MVT::i1
) && (SrcVT
!= MVT::i8
) && (SrcVT
!= MVT::i16
)))
1898 return emitIntZExt(SrcVT
, SrcReg
, DestVT
, DestReg
);
1899 return emitIntSExt(SrcVT
, SrcReg
, DestVT
, DestReg
);
1902 unsigned MipsFastISel::emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1904 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
1905 bool Success
= emitIntExt(SrcVT
, SrcReg
, DestVT
, DestReg
, isZExt
);
1906 return Success
? DestReg
: 0;
1909 bool MipsFastISel::selectDivRem(const Instruction
*I
, unsigned ISDOpcode
) {
1910 EVT DestEVT
= TLI
.getValueType(DL
, I
->getType(), true);
1911 if (!DestEVT
.isSimple())
1914 MVT DestVT
= DestEVT
.getSimpleVT();
1915 if (DestVT
!= MVT::i32
)
1919 switch (ISDOpcode
) {
1924 DivOpc
= Mips::SDIV
;
1928 DivOpc
= Mips::UDIV
;
1932 Register Src0Reg
= getRegForValue(I
->getOperand(0));
1933 Register Src1Reg
= getRegForValue(I
->getOperand(1));
1934 if (!Src0Reg
|| !Src1Reg
)
1937 emitInst(DivOpc
).addReg(Src0Reg
).addReg(Src1Reg
);
1938 emitInst(Mips::TEQ
).addReg(Src1Reg
).addReg(Mips::ZERO
).addImm(7);
1940 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1944 unsigned MFOpc
= (ISDOpcode
== ISD::SREM
|| ISDOpcode
== ISD::UREM
)
1947 emitInst(MFOpc
, ResultReg
);
1949 updateValueMap(I
, ResultReg
);
1953 bool MipsFastISel::selectShift(const Instruction
*I
) {
1956 if (!isTypeSupported(I
->getType(), RetVT
))
1959 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1963 unsigned Opcode
= I
->getOpcode();
1964 const Value
*Op0
= I
->getOperand(0);
1965 Register Op0Reg
= getRegForValue(Op0
);
1969 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1970 if (Opcode
== Instruction::AShr
|| Opcode
== Instruction::LShr
) {
1971 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
1975 MVT Op0MVT
= TLI
.getValueType(DL
, Op0
->getType(), true).getSimpleVT();
1976 bool IsZExt
= Opcode
== Instruction::LShr
;
1977 if (!emitIntExt(Op0MVT
, Op0Reg
, MVT::i32
, TempReg
, IsZExt
))
1983 if (const auto *C
= dyn_cast
<ConstantInt
>(I
->getOperand(1))) {
1984 uint64_t ShiftVal
= C
->getZExtValue();
1988 llvm_unreachable("Unexpected instruction.");
1989 case Instruction::Shl
:
1992 case Instruction::AShr
:
1995 case Instruction::LShr
:
2000 emitInst(Opcode
, ResultReg
).addReg(Op0Reg
).addImm(ShiftVal
);
2001 updateValueMap(I
, ResultReg
);
2005 Register Op1Reg
= getRegForValue(I
->getOperand(1));
2011 llvm_unreachable("Unexpected instruction.");
2012 case Instruction::Shl
:
2013 Opcode
= Mips::SLLV
;
2015 case Instruction::AShr
:
2016 Opcode
= Mips::SRAV
;
2018 case Instruction::LShr
:
2019 Opcode
= Mips::SRLV
;
2023 emitInst(Opcode
, ResultReg
).addReg(Op0Reg
).addReg(Op1Reg
);
2024 updateValueMap(I
, ResultReg
);
2028 bool MipsFastISel::fastSelectInstruction(const Instruction
*I
) {
2029 switch (I
->getOpcode()) {
2032 case Instruction::Load
:
2033 return selectLoad(I
);
2034 case Instruction::Store
:
2035 return selectStore(I
);
2036 case Instruction::SDiv
:
2037 if (!selectBinaryOp(I
, ISD::SDIV
))
2038 return selectDivRem(I
, ISD::SDIV
);
2040 case Instruction::UDiv
:
2041 if (!selectBinaryOp(I
, ISD::UDIV
))
2042 return selectDivRem(I
, ISD::UDIV
);
2044 case Instruction::SRem
:
2045 if (!selectBinaryOp(I
, ISD::SREM
))
2046 return selectDivRem(I
, ISD::SREM
);
2048 case Instruction::URem
:
2049 if (!selectBinaryOp(I
, ISD::UREM
))
2050 return selectDivRem(I
, ISD::UREM
);
2052 case Instruction::Shl
:
2053 case Instruction::LShr
:
2054 case Instruction::AShr
:
2055 return selectShift(I
);
2056 case Instruction::And
:
2057 case Instruction::Or
:
2058 case Instruction::Xor
:
2059 return selectLogicalOp(I
);
2060 case Instruction::Br
:
2061 return selectBranch(I
);
2062 case Instruction::Ret
:
2063 return selectRet(I
);
2064 case Instruction::Trunc
:
2065 return selectTrunc(I
);
2066 case Instruction::ZExt
:
2067 case Instruction::SExt
:
2068 return selectIntExt(I
);
2069 case Instruction::FPTrunc
:
2070 return selectFPTrunc(I
);
2071 case Instruction::FPExt
:
2072 return selectFPExt(I
);
2073 case Instruction::FPToSI
:
2074 return selectFPToInt(I
, /*isSigned*/ true);
2075 case Instruction::FPToUI
:
2076 return selectFPToInt(I
, /*isSigned*/ false);
2077 case Instruction::ICmp
:
2078 case Instruction::FCmp
:
2079 return selectCmp(I
);
2080 case Instruction::Select
:
2081 return selectSelect(I
);
2086 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value
*V
,
2088 Register VReg
= getRegForValue(V
);
2091 MVT VMVT
= TLI
.getValueType(DL
, V
->getType(), true).getSimpleVT();
2093 if (VMVT
== MVT::i1
)
2096 if ((VMVT
== MVT::i8
) || (VMVT
== MVT::i16
)) {
2097 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
2098 if (!emitIntExt(VMVT
, VReg
, MVT::i32
, TempReg
, IsUnsigned
))
2105 void MipsFastISel::simplifyAddress(Address
&Addr
) {
2106 if (!isInt
<16>(Addr
.getOffset())) {
2108 materialize32BitInt(Addr
.getOffset(), &Mips::GPR32RegClass
);
2109 Register DestReg
= createResultReg(&Mips::GPR32RegClass
);
2110 emitInst(Mips::ADDu
, DestReg
).addReg(TempReg
).addReg(Addr
.getReg());
2111 Addr
.setReg(DestReg
);
2116 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode
,
2117 const TargetRegisterClass
*RC
,
2118 unsigned Op0
, unsigned Op1
) {
2119 // We treat the MUL instruction in a special way because it clobbers
2120 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2121 // mark these registers only as implicitly defined. As a result, the
2122 // register allocator runs out of registers when this instruction is
2123 // followed by another instruction that defines the same registers too.
2124 // We can fix this by explicitly marking those registers as dead.
2125 if (MachineInstOpcode
== Mips::MUL
) {
2126 Register ResultReg
= createResultReg(RC
);
2127 const MCInstrDesc
&II
= TII
.get(MachineInstOpcode
);
2128 Op0
= constrainOperandRegClass(II
, Op0
, II
.getNumDefs());
2129 Op1
= constrainOperandRegClass(II
, Op1
, II
.getNumDefs() + 1);
2130 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, II
, ResultReg
)
2133 .addReg(Mips::HI0
, RegState::ImplicitDefine
| RegState::Dead
)
2134 .addReg(Mips::LO0
, RegState::ImplicitDefine
| RegState::Dead
);
2138 return FastISel::fastEmitInst_rr(MachineInstOpcode
, RC
, Op0
, Op1
);
2143 FastISel
*Mips::createFastISel(FunctionLoweringInfo
&funcInfo
,
2144 const TargetLibraryInfo
*libInfo
) {
2145 return new MipsFastISel(funcInfo
, libInfo
);
2148 } // end namespace llvm