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/DenseMap.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/Analysis/TargetLibraryInfo.h"
28 #include "llvm/CodeGen/CallingConvLower.h"
29 #include "llvm/CodeGen/FastISel.h"
30 #include "llvm/CodeGen/FunctionLoweringInfo.h"
31 #include "llvm/CodeGen/ISDOpcodes.h"
32 #include "llvm/CodeGen/MachineBasicBlock.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineInstrBuilder.h"
35 #include "llvm/CodeGen/MachineMemOperand.h"
36 #include "llvm/CodeGen/MachineRegisterInfo.h"
37 #include "llvm/CodeGen/TargetInstrInfo.h"
38 #include "llvm/CodeGen/TargetLowering.h"
39 #include "llvm/CodeGen/ValueTypes.h"
40 #include "llvm/CodeGenTypes/MachineValueType.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/MCSymbol.h"
61 #include "llvm/Support/Casting.h"
62 #include "llvm/Support/Compiler.h"
63 #include "llvm/Support/Debug.h"
64 #include "llvm/Support/ErrorHandling.h"
65 #include "llvm/Support/MathExtras.h"
66 #include "llvm/Support/raw_ostream.h"
72 #define DEBUG_TYPE "mips-fastisel"
76 extern cl::opt
<bool> EmitJalrReloc
;
80 class MipsFastISel final
: public FastISel
{
82 // All possible address modes.
85 using BaseKind
= enum { RegBase
, FrameIndexBase
};
88 BaseKind Kind
= RegBase
;
96 const GlobalValue
*GV
= nullptr;
99 // Innocuous defaults for our address.
100 Address() { Base
.Reg
= 0; }
102 void setKind(BaseKind K
) { Kind
= K
; }
103 BaseKind
getKind() const { return Kind
; }
104 bool isRegBase() const { return Kind
== RegBase
; }
105 bool isFIBase() const { return Kind
== FrameIndexBase
; }
107 void setReg(unsigned Reg
) {
108 assert(isRegBase() && "Invalid base register access!");
112 unsigned getReg() const {
113 assert(isRegBase() && "Invalid base register access!");
117 void setFI(unsigned FI
) {
118 assert(isFIBase() && "Invalid base frame index access!");
122 unsigned getFI() const {
123 assert(isFIBase() && "Invalid base frame index access!");
127 void setOffset(int64_t Offset_
) { Offset
= Offset_
; }
128 int64_t getOffset() const { return Offset
; }
129 void setGlobalValue(const GlobalValue
*G
) { GV
= G
; }
130 const GlobalValue
*getGlobalValue() { return GV
; }
133 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
134 /// make the right decision when generating code for different targets.
135 const TargetMachine
&TM
;
136 const MipsSubtarget
*Subtarget
;
137 const TargetInstrInfo
&TII
;
138 const TargetLowering
&TLI
;
139 MipsFunctionInfo
*MFI
;
141 // Convenience variables to avoid some queries.
142 LLVMContext
*Context
;
144 bool fastLowerArguments() override
;
145 bool fastLowerCall(CallLoweringInfo
&CLI
) override
;
146 bool fastLowerIntrinsicCall(const IntrinsicInst
*II
) override
;
148 bool UnsupportedFPMode
; // To allow fast-isel to proceed and just not handle
149 // floating point but not reject doing fast-isel in other
153 // Selection routines.
154 bool selectLogicalOp(const Instruction
*I
);
155 bool selectLoad(const Instruction
*I
);
156 bool selectStore(const Instruction
*I
);
157 bool selectBranch(const Instruction
*I
);
158 bool selectSelect(const Instruction
*I
);
159 bool selectCmp(const Instruction
*I
);
160 bool selectFPExt(const Instruction
*I
);
161 bool selectFPTrunc(const Instruction
*I
);
162 bool selectFPToInt(const Instruction
*I
, bool IsSigned
);
163 bool selectRet(const Instruction
*I
);
164 bool selectTrunc(const Instruction
*I
);
165 bool selectIntExt(const Instruction
*I
);
166 bool selectShift(const Instruction
*I
);
167 bool selectDivRem(const Instruction
*I
, unsigned ISDOpcode
);
169 // Utility helper routines.
170 bool isTypeLegal(Type
*Ty
, MVT
&VT
);
171 bool isTypeSupported(Type
*Ty
, MVT
&VT
);
172 bool isLoadTypeLegal(Type
*Ty
, MVT
&VT
);
173 bool computeAddress(const Value
*Obj
, Address
&Addr
);
174 bool computeCallAddress(const Value
*V
, Address
&Addr
);
175 void simplifyAddress(Address
&Addr
);
177 // Emit helper routines.
178 bool emitCmp(unsigned DestReg
, const CmpInst
*CI
);
179 bool emitLoad(MVT VT
, unsigned &ResultReg
, Address
&Addr
);
180 bool emitStore(MVT VT
, unsigned SrcReg
, Address
&Addr
);
181 unsigned emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, bool isZExt
);
182 bool emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
,
185 bool emitIntZExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
);
187 bool emitIntSExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
);
188 bool emitIntSExt32r1(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
190 bool emitIntSExt32r2(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
193 unsigned getRegEnsuringSimpleIntegerWidening(const Value
*, bool IsUnsigned
);
195 unsigned emitLogicalOp(unsigned ISDOpc
, MVT RetVT
, const Value
*LHS
,
198 unsigned materializeFP(const ConstantFP
*CFP
, MVT VT
);
199 unsigned materializeGV(const GlobalValue
*GV
, MVT VT
);
200 unsigned materializeInt(const Constant
*C
, MVT VT
);
201 unsigned materialize32BitInt(int64_t Imm
, const TargetRegisterClass
*RC
);
202 unsigned materializeExternalCallSym(MCSymbol
*Syn
);
204 MachineInstrBuilder
emitInst(unsigned Opc
) {
205 return BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Opc
));
208 MachineInstrBuilder
emitInst(unsigned Opc
, unsigned DstReg
) {
209 return BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Opc
),
213 MachineInstrBuilder
emitInstStore(unsigned Opc
, unsigned SrcReg
,
214 unsigned MemReg
, int64_t MemOffset
) {
215 return emitInst(Opc
).addReg(SrcReg
).addReg(MemReg
).addImm(MemOffset
);
218 MachineInstrBuilder
emitInstLoad(unsigned Opc
, unsigned DstReg
,
219 unsigned MemReg
, int64_t MemOffset
) {
220 return emitInst(Opc
, DstReg
).addReg(MemReg
).addImm(MemOffset
);
223 unsigned fastEmitInst_rr(unsigned MachineInstOpcode
,
224 const TargetRegisterClass
*RC
,
225 unsigned Op0
, unsigned Op1
);
227 // for some reason, this default is not generated by tablegen
228 // so we explicitly generate it here.
229 unsigned fastEmitInst_riir(uint64_t inst
, const TargetRegisterClass
*RC
,
230 unsigned Op0
, uint64_t imm1
, uint64_t imm2
,
235 // Call handling routines.
237 CCAssignFn
*CCAssignFnForCall(CallingConv::ID CC
) const;
238 bool processCallArgs(CallLoweringInfo
&CLI
, SmallVectorImpl
<MVT
> &ArgVTs
,
240 bool finishCall(CallLoweringInfo
&CLI
, MVT RetVT
, unsigned NumBytes
);
242 const MipsABIInfo
&getABI() const {
243 return static_cast<const MipsTargetMachine
&>(TM
).getABI();
247 // Backend specific FastISel code.
248 explicit MipsFastISel(FunctionLoweringInfo
&funcInfo
,
249 const TargetLibraryInfo
*libInfo
)
250 : FastISel(funcInfo
, libInfo
), TM(funcInfo
.MF
->getTarget()),
251 Subtarget(&funcInfo
.MF
->getSubtarget
<MipsSubtarget
>()),
252 TII(*Subtarget
->getInstrInfo()), TLI(*Subtarget
->getTargetLowering()) {
253 MFI
= funcInfo
.MF
->getInfo
<MipsFunctionInfo
>();
254 Context
= &funcInfo
.Fn
->getContext();
255 UnsupportedFPMode
= Subtarget
->isFP64bit() || Subtarget
->useSoftFloat();
258 unsigned fastMaterializeAlloca(const AllocaInst
*AI
) override
;
259 unsigned fastMaterializeConstant(const Constant
*C
) override
;
260 bool fastSelectInstruction(const Instruction
*I
) override
;
262 #include "MipsGenFastISel.inc"
265 } // end anonymous namespace
267 static bool CC_Mips(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
268 CCValAssign::LocInfo LocInfo
, ISD::ArgFlagsTy ArgFlags
,
269 CCState
&State
) LLVM_ATTRIBUTE_UNUSED
;
271 static bool CC_MipsO32_FP32(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
272 CCValAssign::LocInfo LocInfo
,
273 ISD::ArgFlagsTy ArgFlags
, CCState
&State
) {
274 llvm_unreachable("should not be called");
277 static bool CC_MipsO32_FP64(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
278 CCValAssign::LocInfo LocInfo
,
279 ISD::ArgFlagsTy ArgFlags
, CCState
&State
) {
280 llvm_unreachable("should not be called");
283 #include "MipsGenCallingConv.inc"
285 CCAssignFn
*MipsFastISel::CCAssignFnForCall(CallingConv::ID CC
) const {
289 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc
, MVT RetVT
,
290 const Value
*LHS
, const Value
*RHS
) {
291 // Canonicalize immediates to the RHS first.
292 if (isa
<ConstantInt
>(LHS
) && !isa
<ConstantInt
>(RHS
))
307 llvm_unreachable("unexpected opcode");
310 Register LHSReg
= getRegForValue(LHS
);
315 if (const auto *C
= dyn_cast
<ConstantInt
>(RHS
))
316 RHSReg
= materializeInt(C
, MVT::i32
);
318 RHSReg
= getRegForValue(RHS
);
322 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
326 emitInst(Opc
, ResultReg
).addReg(LHSReg
).addReg(RHSReg
);
330 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst
*AI
) {
331 assert(TLI
.getValueType(DL
, AI
->getType(), true) == MVT::i32
&&
332 "Alloca should always return a pointer.");
334 DenseMap
<const AllocaInst
*, int>::iterator SI
=
335 FuncInfo
.StaticAllocaMap
.find(AI
);
337 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
338 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
339 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Mips::LEA_ADDiu
),
341 .addFrameIndex(SI
->second
)
349 unsigned MipsFastISel::materializeInt(const Constant
*C
, MVT VT
) {
350 if (VT
!= MVT::i32
&& VT
!= MVT::i16
&& VT
!= MVT::i8
&& VT
!= MVT::i1
)
352 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
353 const ConstantInt
*CI
= cast
<ConstantInt
>(C
);
354 return materialize32BitInt(CI
->getZExtValue(), RC
);
357 unsigned MipsFastISel::materialize32BitInt(int64_t Imm
,
358 const TargetRegisterClass
*RC
) {
359 Register ResultReg
= createResultReg(RC
);
361 if (isInt
<16>(Imm
)) {
362 unsigned Opc
= Mips::ADDiu
;
363 emitInst(Opc
, ResultReg
).addReg(Mips::ZERO
).addImm(Imm
);
365 } else if (isUInt
<16>(Imm
)) {
366 emitInst(Mips::ORi
, ResultReg
).addReg(Mips::ZERO
).addImm(Imm
);
369 unsigned Lo
= Imm
& 0xFFFF;
370 unsigned Hi
= (Imm
>> 16) & 0xFFFF;
372 // Both Lo and Hi have nonzero bits.
373 Register TmpReg
= createResultReg(RC
);
374 emitInst(Mips::LUi
, TmpReg
).addImm(Hi
);
375 emitInst(Mips::ORi
, ResultReg
).addReg(TmpReg
).addImm(Lo
);
377 emitInst(Mips::LUi
, ResultReg
).addImm(Hi
);
382 unsigned MipsFastISel::materializeFP(const ConstantFP
*CFP
, MVT VT
) {
383 if (UnsupportedFPMode
)
385 int64_t Imm
= CFP
->getValueAPF().bitcastToAPInt().getZExtValue();
386 if (VT
== MVT::f32
) {
387 const TargetRegisterClass
*RC
= &Mips::FGR32RegClass
;
388 Register DestReg
= createResultReg(RC
);
389 unsigned TempReg
= materialize32BitInt(Imm
, &Mips::GPR32RegClass
);
390 emitInst(Mips::MTC1
, DestReg
).addReg(TempReg
);
392 } else if (VT
== MVT::f64
) {
393 const TargetRegisterClass
*RC
= &Mips::AFGR64RegClass
;
394 Register DestReg
= createResultReg(RC
);
395 unsigned TempReg1
= materialize32BitInt(Imm
>> 32, &Mips::GPR32RegClass
);
397 materialize32BitInt(Imm
& 0xFFFFFFFF, &Mips::GPR32RegClass
);
398 emitInst(Mips::BuildPairF64
, DestReg
).addReg(TempReg2
).addReg(TempReg1
);
404 unsigned MipsFastISel::materializeGV(const GlobalValue
*GV
, MVT VT
) {
405 // For now 32-bit only.
408 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
409 Register DestReg
= createResultReg(RC
);
410 const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
);
411 bool IsThreadLocal
= GVar
&& GVar
->isThreadLocal();
412 // TLS not supported at this time.
415 emitInst(Mips::LW
, DestReg
)
416 .addReg(MFI
->getGlobalBaseReg(*MF
))
417 .addGlobalAddress(GV
, 0, MipsII::MO_GOT
);
418 if ((GV
->hasInternalLinkage() ||
419 (GV
->hasLocalLinkage() && !isa
<Function
>(GV
)))) {
420 Register TempReg
= createResultReg(RC
);
421 emitInst(Mips::ADDiu
, TempReg
)
423 .addGlobalAddress(GV
, 0, MipsII::MO_ABS_LO
);
429 unsigned MipsFastISel::materializeExternalCallSym(MCSymbol
*Sym
) {
430 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
431 Register DestReg
= createResultReg(RC
);
432 emitInst(Mips::LW
, DestReg
)
433 .addReg(MFI
->getGlobalBaseReg(*MF
))
434 .addSym(Sym
, MipsII::MO_GOT
);
438 // Materialize a constant into a register, and return the register
439 // number (or zero if we failed to handle it).
440 unsigned MipsFastISel::fastMaterializeConstant(const Constant
*C
) {
441 EVT CEVT
= TLI
.getValueType(DL
, C
->getType(), true);
443 // Only handle simple types.
444 if (!CEVT
.isSimple())
446 MVT VT
= CEVT
.getSimpleVT();
448 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(C
))
449 return (UnsupportedFPMode
) ? 0 : materializeFP(CFP
, VT
);
450 else if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(C
))
451 return materializeGV(GV
, VT
);
452 else if (isa
<ConstantInt
>(C
))
453 return materializeInt(C
, VT
);
458 bool MipsFastISel::computeAddress(const Value
*Obj
, Address
&Addr
) {
459 const User
*U
= nullptr;
460 unsigned Opcode
= Instruction::UserOp1
;
461 if (const Instruction
*I
= dyn_cast
<Instruction
>(Obj
)) {
462 // Don't walk into other basic blocks unless the object is an alloca from
463 // another block, otherwise it may not have a virtual register assigned.
464 if (FuncInfo
.StaticAllocaMap
.count(static_cast<const AllocaInst
*>(Obj
)) ||
465 FuncInfo
.getMBB(I
->getParent()) == FuncInfo
.MBB
) {
466 Opcode
= I
->getOpcode();
469 } else if (const ConstantExpr
*C
= dyn_cast
<ConstantExpr
>(Obj
)) {
470 Opcode
= C
->getOpcode();
476 case Instruction::BitCast
:
477 // Look through bitcasts.
478 return computeAddress(U
->getOperand(0), Addr
);
479 case Instruction::GetElementPtr
: {
480 Address SavedAddr
= Addr
;
481 int64_t TmpOffset
= Addr
.getOffset();
482 // Iterate through the GEP folding the constants into offsets where
484 gep_type_iterator GTI
= gep_type_begin(U
);
485 for (User::const_op_iterator i
= U
->op_begin() + 1, e
= U
->op_end(); i
!= e
;
487 const Value
*Op
= *i
;
488 if (StructType
*STy
= GTI
.getStructTypeOrNull()) {
489 const StructLayout
*SL
= DL
.getStructLayout(STy
);
490 unsigned Idx
= cast
<ConstantInt
>(Op
)->getZExtValue();
491 TmpOffset
+= SL
->getElementOffset(Idx
);
493 uint64_t S
= GTI
.getSequentialElementStride(DL
);
495 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Op
)) {
496 // Constant-offset addressing.
497 TmpOffset
+= CI
->getSExtValue() * S
;
500 if (canFoldAddIntoGEP(U
, Op
)) {
501 // A compatible add with a constant operand. Fold the constant.
503 cast
<ConstantInt
>(cast
<AddOperator
>(Op
)->getOperand(1));
504 TmpOffset
+= CI
->getSExtValue() * S
;
505 // Iterate on the other operand.
506 Op
= cast
<AddOperator
>(Op
)->getOperand(0);
510 goto unsupported_gep
;
514 // Try to grab the base operand now.
515 Addr
.setOffset(TmpOffset
);
516 if (computeAddress(U
->getOperand(0), Addr
))
518 // We failed, restore everything and try the other options.
523 case Instruction::Alloca
: {
524 const AllocaInst
*AI
= cast
<AllocaInst
>(Obj
);
525 DenseMap
<const AllocaInst
*, int>::iterator SI
=
526 FuncInfo
.StaticAllocaMap
.find(AI
);
527 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
528 Addr
.setKind(Address::FrameIndexBase
);
529 Addr
.setFI(SI
->second
);
535 Addr
.setReg(getRegForValue(Obj
));
536 return Addr
.getReg() != 0;
539 bool MipsFastISel::computeCallAddress(const Value
*V
, Address
&Addr
) {
540 const User
*U
= nullptr;
541 unsigned Opcode
= Instruction::UserOp1
;
543 if (const auto *I
= dyn_cast
<Instruction
>(V
)) {
544 // Check if the value is defined in the same basic block. This information
545 // is crucial to know whether or not folding an operand is valid.
546 if (I
->getParent() == FuncInfo
.MBB
->getBasicBlock()) {
547 Opcode
= I
->getOpcode();
550 } else if (const auto *C
= dyn_cast
<ConstantExpr
>(V
)) {
551 Opcode
= C
->getOpcode();
558 case Instruction::BitCast
:
559 // Look past bitcasts if its operand is in the same BB.
560 return computeCallAddress(U
->getOperand(0), Addr
);
562 case Instruction::IntToPtr
:
563 // Look past no-op inttoptrs if its operand is in the same BB.
564 if (TLI
.getValueType(DL
, U
->getOperand(0)->getType()) ==
565 TLI
.getPointerTy(DL
))
566 return computeCallAddress(U
->getOperand(0), Addr
);
568 case Instruction::PtrToInt
:
569 // Look past no-op ptrtoints if its operand is in the same BB.
570 if (TLI
.getValueType(DL
, U
->getType()) == TLI
.getPointerTy(DL
))
571 return computeCallAddress(U
->getOperand(0), Addr
);
575 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
576 Addr
.setGlobalValue(GV
);
580 // If all else fails, try to materialize the value in a register.
581 if (!Addr
.getGlobalValue()) {
582 Addr
.setReg(getRegForValue(V
));
583 return Addr
.getReg() != 0;
589 bool MipsFastISel::isTypeLegal(Type
*Ty
, MVT
&VT
) {
590 EVT evt
= TLI
.getValueType(DL
, Ty
, true);
591 // Only handle simple types.
592 if (evt
== MVT::Other
|| !evt
.isSimple())
594 VT
= evt
.getSimpleVT();
596 // Handle all legal types, i.e. a register that will directly hold this
598 return TLI
.isTypeLegal(VT
);
601 bool MipsFastISel::isTypeSupported(Type
*Ty
, MVT
&VT
) {
602 if (Ty
->isVectorTy())
605 if (isTypeLegal(Ty
, VT
))
608 // If this is a type than can be sign or zero-extended to a basic operation
609 // go ahead and accept it now.
610 if (VT
== MVT::i1
|| VT
== MVT::i8
|| VT
== MVT::i16
)
616 bool MipsFastISel::isLoadTypeLegal(Type
*Ty
, MVT
&VT
) {
617 if (isTypeLegal(Ty
, VT
))
619 // We will extend this in a later patch:
620 // If this is a type than can be sign or zero-extended to a basic operation
621 // go ahead and accept it now.
622 if (VT
== MVT::i8
|| VT
== MVT::i16
)
627 // Because of how EmitCmp is called with fast-isel, you can
628 // end up with redundant "andi" instructions after the sequences emitted below.
629 // We should try and solve this issue in the future.
631 bool MipsFastISel::emitCmp(unsigned ResultReg
, const CmpInst
*CI
) {
632 const Value
*Left
= CI
->getOperand(0), *Right
= CI
->getOperand(1);
633 bool IsUnsigned
= CI
->isUnsigned();
634 unsigned LeftReg
= getRegEnsuringSimpleIntegerWidening(Left
, IsUnsigned
);
637 unsigned RightReg
= getRegEnsuringSimpleIntegerWidening(Right
, IsUnsigned
);
640 CmpInst::Predicate P
= CI
->getPredicate();
645 case CmpInst::ICMP_EQ
: {
646 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
647 emitInst(Mips::XOR
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
648 emitInst(Mips::SLTiu
, ResultReg
).addReg(TempReg
).addImm(1);
651 case CmpInst::ICMP_NE
: {
652 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
653 emitInst(Mips::XOR
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
654 emitInst(Mips::SLTu
, ResultReg
).addReg(Mips::ZERO
).addReg(TempReg
);
657 case CmpInst::ICMP_UGT
:
658 emitInst(Mips::SLTu
, ResultReg
).addReg(RightReg
).addReg(LeftReg
);
660 case CmpInst::ICMP_ULT
:
661 emitInst(Mips::SLTu
, ResultReg
).addReg(LeftReg
).addReg(RightReg
);
663 case CmpInst::ICMP_UGE
: {
664 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
665 emitInst(Mips::SLTu
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
666 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
669 case CmpInst::ICMP_ULE
: {
670 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
671 emitInst(Mips::SLTu
, TempReg
).addReg(RightReg
).addReg(LeftReg
);
672 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
675 case CmpInst::ICMP_SGT
:
676 emitInst(Mips::SLT
, ResultReg
).addReg(RightReg
).addReg(LeftReg
);
678 case CmpInst::ICMP_SLT
:
679 emitInst(Mips::SLT
, ResultReg
).addReg(LeftReg
).addReg(RightReg
);
681 case CmpInst::ICMP_SGE
: {
682 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
683 emitInst(Mips::SLT
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
684 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
687 case CmpInst::ICMP_SLE
: {
688 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
689 emitInst(Mips::SLT
, TempReg
).addReg(RightReg
).addReg(LeftReg
);
690 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
693 case CmpInst::FCMP_OEQ
:
694 case CmpInst::FCMP_UNE
:
695 case CmpInst::FCMP_OLT
:
696 case CmpInst::FCMP_OLE
:
697 case CmpInst::FCMP_OGT
:
698 case CmpInst::FCMP_OGE
: {
699 if (UnsupportedFPMode
)
701 bool IsFloat
= Left
->getType()->isFloatTy();
702 bool IsDouble
= Left
->getType()->isDoubleTy();
703 if (!IsFloat
&& !IsDouble
)
705 unsigned Opc
, CondMovOpc
;
707 case CmpInst::FCMP_OEQ
:
708 Opc
= IsFloat
? Mips::C_EQ_S
: Mips::C_EQ_D32
;
709 CondMovOpc
= Mips::MOVT_I
;
711 case CmpInst::FCMP_UNE
:
712 Opc
= IsFloat
? Mips::C_EQ_S
: Mips::C_EQ_D32
;
713 CondMovOpc
= Mips::MOVF_I
;
715 case CmpInst::FCMP_OLT
:
716 Opc
= IsFloat
? Mips::C_OLT_S
: Mips::C_OLT_D32
;
717 CondMovOpc
= Mips::MOVT_I
;
719 case CmpInst::FCMP_OLE
:
720 Opc
= IsFloat
? Mips::C_OLE_S
: Mips::C_OLE_D32
;
721 CondMovOpc
= Mips::MOVT_I
;
723 case CmpInst::FCMP_OGT
:
724 Opc
= IsFloat
? Mips::C_ULE_S
: Mips::C_ULE_D32
;
725 CondMovOpc
= Mips::MOVF_I
;
727 case CmpInst::FCMP_OGE
:
728 Opc
= IsFloat
? Mips::C_ULT_S
: Mips::C_ULT_D32
;
729 CondMovOpc
= Mips::MOVF_I
;
732 llvm_unreachable("Only switching of a subset of CCs.");
734 Register RegWithZero
= createResultReg(&Mips::GPR32RegClass
);
735 Register RegWithOne
= createResultReg(&Mips::GPR32RegClass
);
736 emitInst(Mips::ADDiu
, RegWithZero
).addReg(Mips::ZERO
).addImm(0);
737 emitInst(Mips::ADDiu
, RegWithOne
).addReg(Mips::ZERO
).addImm(1);
738 emitInst(Opc
).addReg(Mips::FCC0
, RegState::Define
).addReg(LeftReg
)
740 emitInst(CondMovOpc
, ResultReg
)
743 .addReg(RegWithZero
);
750 bool MipsFastISel::emitLoad(MVT VT
, unsigned &ResultReg
, Address
&Addr
) {
752 // more cases will be handled here in following patches.
755 switch (VT
.SimpleTy
) {
757 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
761 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
765 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
769 if (UnsupportedFPMode
)
771 ResultReg
= createResultReg(&Mips::FGR32RegClass
);
775 if (UnsupportedFPMode
)
777 ResultReg
= createResultReg(&Mips::AFGR64RegClass
);
783 if (Addr
.isRegBase()) {
784 simplifyAddress(Addr
);
785 emitInstLoad(Opc
, ResultReg
, Addr
.getReg(), Addr
.getOffset());
788 if (Addr
.isFIBase()) {
789 unsigned FI
= Addr
.getFI();
790 int64_t Offset
= Addr
.getOffset();
791 MachineFrameInfo
&MFI
= MF
->getFrameInfo();
792 MachineMemOperand
*MMO
= MF
->getMachineMemOperand(
793 MachinePointerInfo::getFixedStack(*MF
, FI
), MachineMemOperand::MOLoad
,
794 MFI
.getObjectSize(FI
), Align(4));
795 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Opc
), ResultReg
)
804 bool MipsFastISel::emitStore(MVT VT
, unsigned SrcReg
, Address
&Addr
) {
806 // more cases will be handled here in following patches.
809 switch (VT
.SimpleTy
) {
820 if (UnsupportedFPMode
)
825 if (UnsupportedFPMode
)
832 if (Addr
.isRegBase()) {
833 simplifyAddress(Addr
);
834 emitInstStore(Opc
, SrcReg
, Addr
.getReg(), Addr
.getOffset());
837 if (Addr
.isFIBase()) {
838 unsigned FI
= Addr
.getFI();
839 int64_t Offset
= Addr
.getOffset();
840 MachineFrameInfo
&MFI
= MF
->getFrameInfo();
841 MachineMemOperand
*MMO
= MF
->getMachineMemOperand(
842 MachinePointerInfo::getFixedStack(*MF
, FI
), MachineMemOperand::MOStore
,
843 MFI
.getObjectSize(FI
), Align(4));
844 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Opc
))
854 bool MipsFastISel::selectLogicalOp(const Instruction
*I
) {
856 if (!isTypeSupported(I
->getType(), VT
))
860 switch (I
->getOpcode()) {
862 llvm_unreachable("Unexpected instruction.");
863 case Instruction::And
:
864 ResultReg
= emitLogicalOp(ISD::AND
, VT
, I
->getOperand(0), I
->getOperand(1));
866 case Instruction::Or
:
867 ResultReg
= emitLogicalOp(ISD::OR
, VT
, I
->getOperand(0), I
->getOperand(1));
869 case Instruction::Xor
:
870 ResultReg
= emitLogicalOp(ISD::XOR
, VT
, I
->getOperand(0), I
->getOperand(1));
877 updateValueMap(I
, ResultReg
);
881 bool MipsFastISel::selectLoad(const Instruction
*I
) {
882 const LoadInst
*LI
= cast
<LoadInst
>(I
);
884 // Atomic loads need special handling.
888 // Verify we have a legal type before going any further.
890 if (!isLoadTypeLegal(LI
->getType(), VT
))
893 // Underaligned loads need special handling.
894 if (LI
->getAlign() < VT
.getFixedSizeInBits() / 8 &&
895 !Subtarget
->systemSupportsUnalignedAccess())
898 // See if we can handle this address.
900 if (!computeAddress(LI
->getOperand(0), Addr
))
904 if (!emitLoad(VT
, ResultReg
, Addr
))
906 updateValueMap(LI
, ResultReg
);
910 bool MipsFastISel::selectStore(const Instruction
*I
) {
911 const StoreInst
*SI
= cast
<StoreInst
>(I
);
913 Value
*Op0
= SI
->getOperand(0);
916 // Atomic stores need special handling.
920 // Verify we have a legal type before going any further.
922 if (!isLoadTypeLegal(SI
->getOperand(0)->getType(), VT
))
925 // Underaligned stores need special handling.
926 if (SI
->getAlign() < VT
.getFixedSizeInBits() / 8 &&
927 !Subtarget
->systemSupportsUnalignedAccess())
930 // Get the value to be stored into a register.
931 SrcReg
= getRegForValue(Op0
);
935 // See if we can handle this address.
937 if (!computeAddress(SI
->getOperand(1), Addr
))
940 if (!emitStore(VT
, SrcReg
, Addr
))
945 // This can cause a redundant sltiu to be generated.
946 // FIXME: try and eliminate this in a future patch.
947 bool MipsFastISel::selectBranch(const Instruction
*I
) {
948 const BranchInst
*BI
= cast
<BranchInst
>(I
);
949 MachineBasicBlock
*BrBB
= FuncInfo
.MBB
;
951 // TBB is the basic block for the case where the comparison is true.
952 // FBB is the basic block for the case where the comparison is false.
953 // if (cond) goto TBB
957 MachineBasicBlock
*TBB
= FuncInfo
.getMBB(BI
->getSuccessor(0));
958 MachineBasicBlock
*FBB
= FuncInfo
.getMBB(BI
->getSuccessor(1));
960 // Fold the common case of a conditional branch with a comparison
961 // in the same block.
962 unsigned ZExtCondReg
= 0;
963 if (const CmpInst
*CI
= dyn_cast
<CmpInst
>(BI
->getCondition())) {
964 if (CI
->hasOneUse() && CI
->getParent() == I
->getParent()) {
965 ZExtCondReg
= createResultReg(&Mips::GPR32RegClass
);
966 if (!emitCmp(ZExtCondReg
, CI
))
971 // For the general case, we need to mask with 1.
972 if (ZExtCondReg
== 0) {
973 Register CondReg
= getRegForValue(BI
->getCondition());
977 ZExtCondReg
= emitIntExt(MVT::i1
, CondReg
, MVT::i32
, true);
978 if (ZExtCondReg
== 0)
982 BuildMI(*BrBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Mips::BGTZ
))
985 finishCondBranch(BI
->getParent(), TBB
, FBB
);
989 bool MipsFastISel::selectCmp(const Instruction
*I
) {
990 const CmpInst
*CI
= cast
<CmpInst
>(I
);
991 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
992 if (!emitCmp(ResultReg
, CI
))
994 updateValueMap(I
, ResultReg
);
998 // Attempt to fast-select a floating-point extend instruction.
999 bool MipsFastISel::selectFPExt(const Instruction
*I
) {
1000 if (UnsupportedFPMode
)
1002 Value
*Src
= I
->getOperand(0);
1003 EVT SrcVT
= TLI
.getValueType(DL
, Src
->getType(), true);
1004 EVT DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1006 if (SrcVT
!= MVT::f32
|| DestVT
!= MVT::f64
)
1010 getRegForValue(Src
); // this must be a 32bit floating point register class
1011 // maybe we should handle this differently
1015 Register DestReg
= createResultReg(&Mips::AFGR64RegClass
);
1016 emitInst(Mips::CVT_D32_S
, DestReg
).addReg(SrcReg
);
1017 updateValueMap(I
, DestReg
);
1021 bool MipsFastISel::selectSelect(const Instruction
*I
) {
1022 assert(isa
<SelectInst
>(I
) && "Expected a select instruction.");
1024 LLVM_DEBUG(dbgs() << "selectSelect\n");
1027 if (!isTypeSupported(I
->getType(), VT
) || UnsupportedFPMode
) {
1029 dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
1033 unsigned CondMovOpc
;
1034 const TargetRegisterClass
*RC
;
1036 if (VT
.isInteger() && !VT
.isVector() && VT
.getSizeInBits() <= 32) {
1037 CondMovOpc
= Mips::MOVN_I_I
;
1038 RC
= &Mips::GPR32RegClass
;
1039 } else if (VT
== MVT::f32
) {
1040 CondMovOpc
= Mips::MOVN_I_S
;
1041 RC
= &Mips::FGR32RegClass
;
1042 } else if (VT
== MVT::f64
) {
1043 CondMovOpc
= Mips::MOVN_I_D32
;
1044 RC
= &Mips::AFGR64RegClass
;
1048 const SelectInst
*SI
= cast
<SelectInst
>(I
);
1049 const Value
*Cond
= SI
->getCondition();
1050 Register Src1Reg
= getRegForValue(SI
->getTrueValue());
1051 Register Src2Reg
= getRegForValue(SI
->getFalseValue());
1052 Register CondReg
= getRegForValue(Cond
);
1054 if (!Src1Reg
|| !Src2Reg
|| !CondReg
)
1057 Register ZExtCondReg
= createResultReg(&Mips::GPR32RegClass
);
1061 if (!emitIntExt(MVT::i1
, CondReg
, MVT::i32
, ZExtCondReg
, true))
1064 Register ResultReg
= createResultReg(RC
);
1065 Register TempReg
= createResultReg(RC
);
1067 if (!ResultReg
|| !TempReg
)
1070 emitInst(TargetOpcode::COPY
, TempReg
).addReg(Src2Reg
);
1071 emitInst(CondMovOpc
, ResultReg
)
1072 .addReg(Src1Reg
).addReg(ZExtCondReg
).addReg(TempReg
);
1073 updateValueMap(I
, ResultReg
);
1077 // Attempt to fast-select a floating-point truncate instruction.
1078 bool MipsFastISel::selectFPTrunc(const Instruction
*I
) {
1079 if (UnsupportedFPMode
)
1081 Value
*Src
= I
->getOperand(0);
1082 EVT SrcVT
= TLI
.getValueType(DL
, Src
->getType(), true);
1083 EVT DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1085 if (SrcVT
!= MVT::f64
|| DestVT
!= MVT::f32
)
1088 Register SrcReg
= getRegForValue(Src
);
1092 Register DestReg
= createResultReg(&Mips::FGR32RegClass
);
1096 emitInst(Mips::CVT_S_D32
, DestReg
).addReg(SrcReg
);
1097 updateValueMap(I
, DestReg
);
1101 // Attempt to fast-select a floating-point-to-integer conversion.
1102 bool MipsFastISel::selectFPToInt(const Instruction
*I
, bool IsSigned
) {
1103 if (UnsupportedFPMode
)
1107 return false; // We don't handle this case yet. There is no native
1108 // instruction for this but it can be synthesized.
1109 Type
*DstTy
= I
->getType();
1110 if (!isTypeLegal(DstTy
, DstVT
))
1113 if (DstVT
!= MVT::i32
)
1116 Value
*Src
= I
->getOperand(0);
1117 Type
*SrcTy
= Src
->getType();
1118 if (!isTypeLegal(SrcTy
, SrcVT
))
1121 if (SrcVT
!= MVT::f32
&& SrcVT
!= MVT::f64
)
1124 Register SrcReg
= getRegForValue(Src
);
1128 // Determine the opcode for the conversion, which takes place
1129 // entirely within FPRs.
1130 Register DestReg
= createResultReg(&Mips::GPR32RegClass
);
1131 Register TempReg
= createResultReg(&Mips::FGR32RegClass
);
1132 unsigned Opc
= (SrcVT
== MVT::f32
) ? Mips::TRUNC_W_S
: Mips::TRUNC_W_D32
;
1134 // Generate the convert.
1135 emitInst(Opc
, TempReg
).addReg(SrcReg
);
1136 emitInst(Mips::MFC1
, DestReg
).addReg(TempReg
);
1138 updateValueMap(I
, DestReg
);
1142 bool MipsFastISel::processCallArgs(CallLoweringInfo
&CLI
,
1143 SmallVectorImpl
<MVT
> &OutVTs
,
1144 unsigned &NumBytes
) {
1145 CallingConv::ID CC
= CLI
.CallConv
;
1146 SmallVector
<CCValAssign
, 16> ArgLocs
;
1147 CCState
CCInfo(CC
, false, *FuncInfo
.MF
, ArgLocs
, *Context
);
1148 CCInfo
.AnalyzeCallOperands(OutVTs
, CLI
.OutFlags
, CCAssignFnForCall(CC
));
1149 // Get a count of how many bytes are to be pushed on the stack.
1150 NumBytes
= CCInfo
.getStackSize();
1151 // This is the minimum argument area used for A0-A3.
1155 emitInst(Mips::ADJCALLSTACKDOWN
).addImm(16).addImm(0);
1156 // Process the args.
1158 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
1159 CCValAssign
&VA
= ArgLocs
[i
];
1160 const Value
*ArgVal
= CLI
.OutVals
[VA
.getValNo()];
1161 MVT ArgVT
= OutVTs
[VA
.getValNo()];
1165 if (ArgVT
== MVT::f32
) {
1166 VA
.convertToReg(Mips::F12
);
1167 } else if (ArgVT
== MVT::f64
) {
1168 if (Subtarget
->isFP64bit())
1169 VA
.convertToReg(Mips::D6_64
);
1171 VA
.convertToReg(Mips::D6
);
1173 } else if (i
== 1) {
1174 if ((firstMVT
== MVT::f32
) || (firstMVT
== MVT::f64
)) {
1175 if (ArgVT
== MVT::f32
) {
1176 VA
.convertToReg(Mips::F14
);
1177 } else if (ArgVT
== MVT::f64
) {
1178 if (Subtarget
->isFP64bit())
1179 VA
.convertToReg(Mips::D7_64
);
1181 VA
.convertToReg(Mips::D7
);
1185 if (((ArgVT
== MVT::i32
) || (ArgVT
== MVT::f32
) || (ArgVT
== MVT::i16
) ||
1186 (ArgVT
== MVT::i8
)) &&
1188 switch (VA
.getLocMemOffset()) {
1190 VA
.convertToReg(Mips::A0
);
1193 VA
.convertToReg(Mips::A1
);
1196 VA
.convertToReg(Mips::A2
);
1199 VA
.convertToReg(Mips::A3
);
1205 Register ArgReg
= getRegForValue(ArgVal
);
1209 // Handle arg promotion: SExt, ZExt, AExt.
1210 switch (VA
.getLocInfo()) {
1211 case CCValAssign::Full
:
1213 case CCValAssign::AExt
:
1214 case CCValAssign::SExt
: {
1215 MVT DestVT
= VA
.getLocVT();
1217 ArgReg
= emitIntExt(SrcVT
, ArgReg
, DestVT
, /*isZExt=*/false);
1222 case CCValAssign::ZExt
: {
1223 MVT DestVT
= VA
.getLocVT();
1225 ArgReg
= emitIntExt(SrcVT
, ArgReg
, DestVT
, /*isZExt=*/true);
1231 llvm_unreachable("Unknown arg promotion!");
1234 // Now copy/store arg to correct locations.
1235 if (VA
.isRegLoc() && !VA
.needsCustom()) {
1236 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
,
1237 TII
.get(TargetOpcode::COPY
), VA
.getLocReg()).addReg(ArgReg
);
1238 CLI
.OutRegs
.push_back(VA
.getLocReg());
1239 } else if (VA
.needsCustom()) {
1240 llvm_unreachable("Mips does not use custom args.");
1244 // FIXME: This path will currently return false. It was copied
1245 // from the AArch64 port and should be essentially fine for Mips too.
1246 // The work to finish up this path will be done in a follow-on patch.
1248 assert(VA
.isMemLoc() && "Assuming store on stack.");
1249 // Don't emit stores for undef values.
1250 if (isa
<UndefValue
>(ArgVal
))
1253 // Need to store on the stack.
1254 // FIXME: This alignment is incorrect but this path is disabled
1255 // for now (will return false). We need to determine the right alignment
1256 // based on the normal alignment for the underlying machine type.
1258 unsigned ArgSize
= alignTo(ArgVT
.getSizeInBits(), 4);
1260 unsigned BEAlign
= 0;
1261 if (ArgSize
< 8 && !Subtarget
->isLittle())
1262 BEAlign
= 8 - ArgSize
;
1265 Addr
.setKind(Address::RegBase
);
1266 Addr
.setReg(Mips::SP
);
1267 Addr
.setOffset(VA
.getLocMemOffset() + BEAlign
);
1269 Align Alignment
= DL
.getABITypeAlign(ArgVal
->getType());
1270 MachineMemOperand
*MMO
= FuncInfo
.MF
->getMachineMemOperand(
1271 MachinePointerInfo::getStack(*FuncInfo
.MF
, Addr
.getOffset()),
1272 MachineMemOperand::MOStore
, ArgVT
.getStoreSize(), Alignment
);
1274 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1275 return false; // can't store on the stack yet.
1282 bool MipsFastISel::finishCall(CallLoweringInfo
&CLI
, MVT RetVT
,
1283 unsigned NumBytes
) {
1284 CallingConv::ID CC
= CLI
.CallConv
;
1285 emitInst(Mips::ADJCALLSTACKUP
).addImm(16).addImm(0);
1286 if (RetVT
!= MVT::isVoid
) {
1287 SmallVector
<CCValAssign
, 16> RVLocs
;
1288 MipsCCState
CCInfo(CC
, false, *FuncInfo
.MF
, RVLocs
, *Context
);
1290 CCInfo
.AnalyzeCallResult(CLI
.Ins
, RetCC_Mips
, CLI
.RetTy
,
1291 CLI
.Symbol
? CLI
.Symbol
->getName().data()
1294 // Only handle a single return value.
1295 if (RVLocs
.size() != 1)
1297 // Copy all of the result registers out of their specified physreg.
1298 MVT CopyVT
= RVLocs
[0].getValVT();
1299 // Special handling for extended integers.
1300 if (RetVT
== MVT::i1
|| RetVT
== MVT::i8
|| RetVT
== MVT::i16
)
1303 Register ResultReg
= createResultReg(TLI
.getRegClassFor(CopyVT
));
1306 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
,
1307 TII
.get(TargetOpcode::COPY
),
1308 ResultReg
).addReg(RVLocs
[0].getLocReg());
1309 CLI
.InRegs
.push_back(RVLocs
[0].getLocReg());
1311 CLI
.ResultReg
= ResultReg
;
1312 CLI
.NumResultRegs
= 1;
1317 bool MipsFastISel::fastLowerArguments() {
1318 LLVM_DEBUG(dbgs() << "fastLowerArguments\n");
1320 if (!FuncInfo
.CanLowerReturn
) {
1321 LLVM_DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
1325 const Function
*F
= FuncInfo
.Fn
;
1326 if (F
->isVarArg()) {
1327 LLVM_DEBUG(dbgs() << ".. gave up (varargs)\n");
1331 CallingConv::ID CC
= F
->getCallingConv();
1332 if (CC
!= CallingConv::C
) {
1333 LLVM_DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
1337 std::array
<MCPhysReg
, 4> GPR32ArgRegs
= {{Mips::A0
, Mips::A1
, Mips::A2
,
1339 std::array
<MCPhysReg
, 2> FGR32ArgRegs
= {{Mips::F12
, Mips::F14
}};
1340 std::array
<MCPhysReg
, 2> AFGR64ArgRegs
= {{Mips::D6
, Mips::D7
}};
1341 auto NextGPR32
= GPR32ArgRegs
.begin();
1342 auto NextFGR32
= FGR32ArgRegs
.begin();
1343 auto NextAFGR64
= AFGR64ArgRegs
.begin();
1345 struct AllocatedReg
{
1346 const TargetRegisterClass
*RC
;
1348 AllocatedReg(const TargetRegisterClass
*RC
, unsigned Reg
)
1349 : RC(RC
), Reg(Reg
) {}
1352 // Only handle simple cases. i.e. All arguments are directly mapped to
1353 // registers of the appropriate type.
1354 SmallVector
<AllocatedReg
, 4> Allocation
;
1355 for (const auto &FormalArg
: F
->args()) {
1356 if (FormalArg
.hasAttribute(Attribute::InReg
) ||
1357 FormalArg
.hasAttribute(Attribute::StructRet
) ||
1358 FormalArg
.hasAttribute(Attribute::ByVal
)) {
1359 LLVM_DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
1363 Type
*ArgTy
= FormalArg
.getType();
1364 if (ArgTy
->isStructTy() || ArgTy
->isArrayTy() || ArgTy
->isVectorTy()) {
1365 LLVM_DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
1369 EVT ArgVT
= TLI
.getValueType(DL
, ArgTy
);
1370 LLVM_DEBUG(dbgs() << ".. " << FormalArg
.getArgNo() << ": "
1372 if (!ArgVT
.isSimple()) {
1373 LLVM_DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
1377 switch (ArgVT
.getSimpleVT().SimpleTy
) {
1381 if (!FormalArg
.hasAttribute(Attribute::SExt
) &&
1382 !FormalArg
.hasAttribute(Attribute::ZExt
)) {
1383 // It must be any extend, this shouldn't happen for clang-generated IR
1384 // so just fall back on SelectionDAG.
1385 LLVM_DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
1389 if (NextGPR32
== GPR32ArgRegs
.end()) {
1390 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1394 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32
<< ")\n");
1395 Allocation
.emplace_back(&Mips::GPR32RegClass
, *NextGPR32
++);
1397 // Allocating any GPR32 prohibits further use of floating point arguments.
1398 NextFGR32
= FGR32ArgRegs
.end();
1399 NextAFGR64
= AFGR64ArgRegs
.end();
1403 if (FormalArg
.hasAttribute(Attribute::ZExt
)) {
1404 // The O32 ABI does not permit a zero-extended i32.
1405 LLVM_DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
1409 if (NextGPR32
== GPR32ArgRegs
.end()) {
1410 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1414 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32
<< ")\n");
1415 Allocation
.emplace_back(&Mips::GPR32RegClass
, *NextGPR32
++);
1417 // Allocating any GPR32 prohibits further use of floating point arguments.
1418 NextFGR32
= FGR32ArgRegs
.end();
1419 NextAFGR64
= AFGR64ArgRegs
.end();
1423 if (UnsupportedFPMode
) {
1424 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1427 if (NextFGR32
== FGR32ArgRegs
.end()) {
1428 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
1431 LLVM_DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32
<< ")\n");
1432 Allocation
.emplace_back(&Mips::FGR32RegClass
, *NextFGR32
++);
1433 // Allocating an FGR32 also allocates the super-register AFGR64, and
1434 // ABI rules require us to skip the corresponding GPR32.
1435 if (NextGPR32
!= GPR32ArgRegs
.end())
1437 if (NextAFGR64
!= AFGR64ArgRegs
.end())
1442 if (UnsupportedFPMode
) {
1443 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1446 if (NextAFGR64
== AFGR64ArgRegs
.end()) {
1447 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
1450 LLVM_DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64
<< ")\n");
1451 Allocation
.emplace_back(&Mips::AFGR64RegClass
, *NextAFGR64
++);
1452 // Allocating an FGR32 also allocates the super-register AFGR64, and
1453 // ABI rules require us to skip the corresponding GPR32 pair.
1454 if (NextGPR32
!= GPR32ArgRegs
.end())
1456 if (NextGPR32
!= GPR32ArgRegs
.end())
1458 if (NextFGR32
!= FGR32ArgRegs
.end())
1463 LLVM_DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
1468 for (const auto &FormalArg
: F
->args()) {
1469 unsigned ArgNo
= FormalArg
.getArgNo();
1470 unsigned SrcReg
= Allocation
[ArgNo
].Reg
;
1471 Register DstReg
= FuncInfo
.MF
->addLiveIn(SrcReg
, Allocation
[ArgNo
].RC
);
1472 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1473 // Without this, EmitLiveInCopies may eliminate the livein if its only
1474 // use is a bitcast (which isn't turned into an instruction).
1475 Register ResultReg
= createResultReg(Allocation
[ArgNo
].RC
);
1476 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
,
1477 TII
.get(TargetOpcode::COPY
), ResultReg
)
1478 .addReg(DstReg
, getKillRegState(true));
1479 updateValueMap(&FormalArg
, ResultReg
);
1482 // Calculate the size of the incoming arguments area.
1483 // We currently reject all the cases where this would be non-zero.
1484 unsigned IncomingArgSizeInBytes
= 0;
1486 // Account for the reserved argument area on ABI's that have one (O32).
1487 // It seems strange to do this on the caller side but it's necessary in
1488 // SelectionDAG's implementation.
1489 IncomingArgSizeInBytes
= std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC
),
1490 IncomingArgSizeInBytes
);
1492 MF
->getInfo
<MipsFunctionInfo
>()->setFormalArgInfo(IncomingArgSizeInBytes
,
1498 bool MipsFastISel::fastLowerCall(CallLoweringInfo
&CLI
) {
1499 CallingConv::ID CC
= CLI
.CallConv
;
1500 bool IsTailCall
= CLI
.IsTailCall
;
1501 bool IsVarArg
= CLI
.IsVarArg
;
1502 const Value
*Callee
= CLI
.Callee
;
1503 MCSymbol
*Symbol
= CLI
.Symbol
;
1505 // Do not handle FastCC.
1506 if (CC
== CallingConv::Fast
)
1509 // Allow SelectionDAG isel to handle tail calls.
1513 // Let SDISel handle vararg functions.
1517 // FIXME: Only handle *simple* calls for now.
1519 if (CLI
.RetTy
->isVoidTy())
1520 RetVT
= MVT::isVoid
;
1521 else if (!isTypeSupported(CLI
.RetTy
, RetVT
))
1524 for (auto Flag
: CLI
.OutFlags
)
1525 if (Flag
.isInReg() || Flag
.isSRet() || Flag
.isNest() || Flag
.isByVal())
1528 // Set up the argument vectors.
1529 SmallVector
<MVT
, 16> OutVTs
;
1530 OutVTs
.reserve(CLI
.OutVals
.size());
1532 for (auto *Val
: CLI
.OutVals
) {
1534 if (!isTypeLegal(Val
->getType(), VT
) &&
1535 !(VT
== MVT::i1
|| VT
== MVT::i8
|| VT
== MVT::i16
))
1538 // We don't handle vector parameters yet.
1539 if (VT
.isVector() || VT
.getSizeInBits() > 64)
1542 OutVTs
.push_back(VT
);
1546 if (!computeCallAddress(Callee
, Addr
))
1549 // Handle the arguments now that we've gotten them.
1551 if (!processCallArgs(CLI
, OutVTs
, NumBytes
))
1554 if (!Addr
.getGlobalValue())
1558 unsigned DestAddress
;
1560 DestAddress
= materializeExternalCallSym(Symbol
);
1562 DestAddress
= materializeGV(Addr
.getGlobalValue(), MVT::i32
);
1563 emitInst(TargetOpcode::COPY
, Mips::T9
).addReg(DestAddress
);
1564 MachineInstrBuilder MIB
=
1565 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, TII
.get(Mips::JALR
),
1566 Mips::RA
).addReg(Mips::T9
);
1568 // Add implicit physical register uses to the call.
1569 for (auto Reg
: CLI
.OutRegs
)
1570 MIB
.addReg(Reg
, RegState::Implicit
);
1572 // Add a register mask with the call-preserved registers.
1573 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1574 MIB
.addRegMask(TRI
.getCallPreservedMask(*FuncInfo
.MF
, CC
));
1578 if (EmitJalrReloc
&& !Subtarget
->inMips16Mode()) {
1579 // Attach callee address to the instruction, let asm printer emit
1580 // .reloc R_MIPS_JALR.
1582 MIB
.addSym(Symbol
, MipsII::MO_JALR
);
1584 MIB
.addSym(FuncInfo
.MF
->getContext().getOrCreateSymbol(
1585 Addr
.getGlobalValue()->getName()), MipsII::MO_JALR
);
1588 // Finish off the call including any return values.
1589 return finishCall(CLI
, RetVT
, NumBytes
);
1592 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst
*II
) {
1593 switch (II
->getIntrinsicID()) {
1596 case Intrinsic::bswap
: {
1597 Type
*RetTy
= II
->getCalledFunction()->getReturnType();
1600 if (!isTypeSupported(RetTy
, VT
))
1603 Register SrcReg
= getRegForValue(II
->getOperand(0));
1606 Register DestReg
= createResultReg(&Mips::GPR32RegClass
);
1609 if (VT
== MVT::i16
) {
1610 if (Subtarget
->hasMips32r2()) {
1611 emitInst(Mips::WSBH
, DestReg
).addReg(SrcReg
);
1612 updateValueMap(II
, DestReg
);
1615 unsigned TempReg
[3];
1616 for (unsigned &R
: TempReg
) {
1617 R
= createResultReg(&Mips::GPR32RegClass
);
1621 emitInst(Mips::SLL
, TempReg
[0]).addReg(SrcReg
).addImm(8);
1622 emitInst(Mips::SRL
, TempReg
[1]).addReg(SrcReg
).addImm(8);
1623 emitInst(Mips::ANDi
, TempReg
[2]).addReg(TempReg
[1]).addImm(0xFF);
1624 emitInst(Mips::OR
, DestReg
).addReg(TempReg
[0]).addReg(TempReg
[2]);
1625 updateValueMap(II
, DestReg
);
1628 } else if (VT
== MVT::i32
) {
1629 if (Subtarget
->hasMips32r2()) {
1630 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
1631 emitInst(Mips::WSBH
, TempReg
).addReg(SrcReg
);
1632 emitInst(Mips::ROTR
, DestReg
).addReg(TempReg
).addImm(16);
1633 updateValueMap(II
, DestReg
);
1636 unsigned TempReg
[8];
1637 for (unsigned &R
: TempReg
) {
1638 R
= createResultReg(&Mips::GPR32RegClass
);
1643 emitInst(Mips::SRL
, TempReg
[0]).addReg(SrcReg
).addImm(8);
1644 emitInst(Mips::SRL
, TempReg
[1]).addReg(SrcReg
).addImm(24);
1645 emitInst(Mips::ANDi
, TempReg
[2]).addReg(TempReg
[0]).addImm(0xFF00);
1646 emitInst(Mips::OR
, TempReg
[3]).addReg(TempReg
[1]).addReg(TempReg
[2]);
1648 emitInst(Mips::ANDi
, TempReg
[4]).addReg(SrcReg
).addImm(0xFF00);
1649 emitInst(Mips::SLL
, TempReg
[5]).addReg(TempReg
[4]).addImm(8);
1651 emitInst(Mips::SLL
, TempReg
[6]).addReg(SrcReg
).addImm(24);
1652 emitInst(Mips::OR
, TempReg
[7]).addReg(TempReg
[3]).addReg(TempReg
[5]);
1653 emitInst(Mips::OR
, DestReg
).addReg(TempReg
[6]).addReg(TempReg
[7]);
1654 updateValueMap(II
, DestReg
);
1660 case Intrinsic::memcpy
:
1661 case Intrinsic::memmove
: {
1662 const auto *MTI
= cast
<MemTransferInst
>(II
);
1663 // Don't handle volatile.
1664 if (MTI
->isVolatile())
1666 if (!MTI
->getLength()->getType()->isIntegerTy(32))
1668 const char *IntrMemName
= isa
<MemCpyInst
>(II
) ? "memcpy" : "memmove";
1669 return lowerCallTo(II
, IntrMemName
, II
->arg_size() - 1);
1671 case Intrinsic::memset
: {
1672 const MemSetInst
*MSI
= cast
<MemSetInst
>(II
);
1673 // Don't handle volatile.
1674 if (MSI
->isVolatile())
1676 if (!MSI
->getLength()->getType()->isIntegerTy(32))
1678 return lowerCallTo(II
, "memset", II
->arg_size() - 1);
1684 bool MipsFastISel::selectRet(const Instruction
*I
) {
1685 const Function
&F
= *I
->getParent()->getParent();
1686 const ReturnInst
*Ret
= cast
<ReturnInst
>(I
);
1688 LLVM_DEBUG(dbgs() << "selectRet\n");
1690 if (!FuncInfo
.CanLowerReturn
)
1693 // Build a list of return value registers.
1694 SmallVector
<unsigned, 4> RetRegs
;
1696 if (Ret
->getNumOperands() > 0) {
1697 CallingConv::ID CC
= F
.getCallingConv();
1699 // Do not handle FastCC.
1700 if (CC
== CallingConv::Fast
)
1703 SmallVector
<ISD::OutputArg
, 4> Outs
;
1704 GetReturnInfo(CC
, F
.getReturnType(), F
.getAttributes(), Outs
, TLI
, DL
);
1706 // Analyze operands of the call, assigning locations to each operand.
1707 SmallVector
<CCValAssign
, 16> ValLocs
;
1708 MipsCCState
CCInfo(CC
, F
.isVarArg(), *FuncInfo
.MF
, ValLocs
,
1710 CCAssignFn
*RetCC
= RetCC_Mips
;
1711 CCInfo
.AnalyzeReturn(Outs
, RetCC
);
1713 // Only handle a single return value for now.
1714 if (ValLocs
.size() != 1)
1717 CCValAssign
&VA
= ValLocs
[0];
1718 const Value
*RV
= Ret
->getOperand(0);
1720 // Don't bother handling odd stuff for now.
1721 if ((VA
.getLocInfo() != CCValAssign::Full
) &&
1722 (VA
.getLocInfo() != CCValAssign::BCvt
))
1725 // Only handle register returns for now.
1729 Register Reg
= getRegForValue(RV
);
1733 unsigned SrcReg
= Reg
+ VA
.getValNo();
1734 Register DestReg
= VA
.getLocReg();
1735 // Avoid a cross-class copy. This is very unlikely.
1736 if (!MRI
.getRegClass(SrcReg
)->contains(DestReg
))
1739 EVT RVEVT
= TLI
.getValueType(DL
, RV
->getType());
1740 if (!RVEVT
.isSimple())
1743 if (RVEVT
.isVector())
1746 MVT RVVT
= RVEVT
.getSimpleVT();
1747 if (RVVT
== MVT::f128
)
1750 // Do not handle FGR64 returns for now.
1751 if (RVVT
== MVT::f64
&& UnsupportedFPMode
) {
1752 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1756 MVT DestVT
= VA
.getValVT();
1757 // Special handling for extended integers.
1758 if (RVVT
!= DestVT
) {
1759 if (RVVT
!= MVT::i1
&& RVVT
!= MVT::i8
&& RVVT
!= MVT::i16
)
1762 if (Outs
[0].Flags
.isZExt() || Outs
[0].Flags
.isSExt()) {
1763 bool IsZExt
= Outs
[0].Flags
.isZExt();
1764 SrcReg
= emitIntExt(RVVT
, SrcReg
, DestVT
, IsZExt
);
1771 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
,
1772 TII
.get(TargetOpcode::COPY
), DestReg
).addReg(SrcReg
);
1774 // Add register to return instruction.
1775 RetRegs
.push_back(VA
.getLocReg());
1777 MachineInstrBuilder MIB
= emitInst(Mips::RetRA
);
1778 for (unsigned Reg
: RetRegs
)
1779 MIB
.addReg(Reg
, RegState::Implicit
);
1783 bool MipsFastISel::selectTrunc(const Instruction
*I
) {
1784 // The high bits for a type smaller than the register size are assumed to be
1786 Value
*Op
= I
->getOperand(0);
1789 SrcVT
= TLI
.getValueType(DL
, Op
->getType(), true);
1790 DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1792 if (SrcVT
!= MVT::i32
&& SrcVT
!= MVT::i16
&& SrcVT
!= MVT::i8
)
1794 if (DestVT
!= MVT::i16
&& DestVT
!= MVT::i8
&& DestVT
!= MVT::i1
)
1797 Register SrcReg
= getRegForValue(Op
);
1801 // Because the high bits are undefined, a truncate doesn't generate
1803 updateValueMap(I
, SrcReg
);
1807 bool MipsFastISel::selectIntExt(const Instruction
*I
) {
1808 Type
*DestTy
= I
->getType();
1809 Value
*Src
= I
->getOperand(0);
1810 Type
*SrcTy
= Src
->getType();
1812 bool isZExt
= isa
<ZExtInst
>(I
);
1813 Register SrcReg
= getRegForValue(Src
);
1817 EVT SrcEVT
, DestEVT
;
1818 SrcEVT
= TLI
.getValueType(DL
, SrcTy
, true);
1819 DestEVT
= TLI
.getValueType(DL
, DestTy
, true);
1820 if (!SrcEVT
.isSimple())
1822 if (!DestEVT
.isSimple())
1825 MVT SrcVT
= SrcEVT
.getSimpleVT();
1826 MVT DestVT
= DestEVT
.getSimpleVT();
1827 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1829 if (!emitIntExt(SrcVT
, SrcReg
, DestVT
, ResultReg
, isZExt
))
1831 updateValueMap(I
, ResultReg
);
1835 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1838 switch (SrcVT
.SimpleTy
) {
1848 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
1849 emitInst(Mips::SLL
, TempReg
).addReg(SrcReg
).addImm(ShiftAmt
);
1850 emitInst(Mips::SRA
, DestReg
).addReg(TempReg
).addImm(ShiftAmt
);
1854 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1856 switch (SrcVT
.SimpleTy
) {
1860 emitInst(Mips::SEB
, DestReg
).addReg(SrcReg
);
1863 emitInst(Mips::SEH
, DestReg
).addReg(SrcReg
);
1869 bool MipsFastISel::emitIntSExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1871 if ((DestVT
!= MVT::i32
) && (DestVT
!= MVT::i16
))
1873 if (Subtarget
->hasMips32r2())
1874 return emitIntSExt32r2(SrcVT
, SrcReg
, DestVT
, DestReg
);
1875 return emitIntSExt32r1(SrcVT
, SrcReg
, DestVT
, DestReg
);
1878 bool MipsFastISel::emitIntZExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1882 switch (SrcVT
.SimpleTy
) {
1896 emitInst(Mips::ANDi
, DestReg
).addReg(SrcReg
).addImm(Imm
);
1900 bool MipsFastISel::emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1901 unsigned DestReg
, bool IsZExt
) {
1902 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1903 // DestVT are odd things, so test to make sure that they are both types we can
1904 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1905 // bail out to SelectionDAG.
1906 if (((DestVT
!= MVT::i8
) && (DestVT
!= MVT::i16
) && (DestVT
!= MVT::i32
)) ||
1907 ((SrcVT
!= MVT::i1
) && (SrcVT
!= MVT::i8
) && (SrcVT
!= MVT::i16
)))
1910 return emitIntZExt(SrcVT
, SrcReg
, DestVT
, DestReg
);
1911 return emitIntSExt(SrcVT
, SrcReg
, DestVT
, DestReg
);
1914 unsigned MipsFastISel::emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1916 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
1917 bool Success
= emitIntExt(SrcVT
, SrcReg
, DestVT
, DestReg
, isZExt
);
1918 return Success
? DestReg
: 0;
1921 bool MipsFastISel::selectDivRem(const Instruction
*I
, unsigned ISDOpcode
) {
1922 EVT DestEVT
= TLI
.getValueType(DL
, I
->getType(), true);
1923 if (!DestEVT
.isSimple())
1926 MVT DestVT
= DestEVT
.getSimpleVT();
1927 if (DestVT
!= MVT::i32
)
1931 switch (ISDOpcode
) {
1936 DivOpc
= Mips::SDIV
;
1940 DivOpc
= Mips::UDIV
;
1944 Register Src0Reg
= getRegForValue(I
->getOperand(0));
1945 Register Src1Reg
= getRegForValue(I
->getOperand(1));
1946 if (!Src0Reg
|| !Src1Reg
)
1949 emitInst(DivOpc
).addReg(Src0Reg
).addReg(Src1Reg
);
1950 emitInst(Mips::TEQ
).addReg(Src1Reg
).addReg(Mips::ZERO
).addImm(7);
1952 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1956 unsigned MFOpc
= (ISDOpcode
== ISD::SREM
|| ISDOpcode
== ISD::UREM
)
1959 emitInst(MFOpc
, ResultReg
);
1961 updateValueMap(I
, ResultReg
);
1965 bool MipsFastISel::selectShift(const Instruction
*I
) {
1968 if (!isTypeSupported(I
->getType(), RetVT
))
1971 Register ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1975 unsigned Opcode
= I
->getOpcode();
1976 const Value
*Op0
= I
->getOperand(0);
1977 Register Op0Reg
= getRegForValue(Op0
);
1981 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1982 if (Opcode
== Instruction::AShr
|| Opcode
== Instruction::LShr
) {
1983 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
1987 MVT Op0MVT
= TLI
.getValueType(DL
, Op0
->getType(), true).getSimpleVT();
1988 bool IsZExt
= Opcode
== Instruction::LShr
;
1989 if (!emitIntExt(Op0MVT
, Op0Reg
, MVT::i32
, TempReg
, IsZExt
))
1995 if (const auto *C
= dyn_cast
<ConstantInt
>(I
->getOperand(1))) {
1996 uint64_t ShiftVal
= C
->getZExtValue();
2000 llvm_unreachable("Unexpected instruction.");
2001 case Instruction::Shl
:
2004 case Instruction::AShr
:
2007 case Instruction::LShr
:
2012 emitInst(Opcode
, ResultReg
).addReg(Op0Reg
).addImm(ShiftVal
);
2013 updateValueMap(I
, ResultReg
);
2017 Register Op1Reg
= getRegForValue(I
->getOperand(1));
2023 llvm_unreachable("Unexpected instruction.");
2024 case Instruction::Shl
:
2025 Opcode
= Mips::SLLV
;
2027 case Instruction::AShr
:
2028 Opcode
= Mips::SRAV
;
2030 case Instruction::LShr
:
2031 Opcode
= Mips::SRLV
;
2035 emitInst(Opcode
, ResultReg
).addReg(Op0Reg
).addReg(Op1Reg
);
2036 updateValueMap(I
, ResultReg
);
2040 bool MipsFastISel::fastSelectInstruction(const Instruction
*I
) {
2041 switch (I
->getOpcode()) {
2044 case Instruction::Load
:
2045 return selectLoad(I
);
2046 case Instruction::Store
:
2047 return selectStore(I
);
2048 case Instruction::SDiv
:
2049 if (!selectBinaryOp(I
, ISD::SDIV
))
2050 return selectDivRem(I
, ISD::SDIV
);
2052 case Instruction::UDiv
:
2053 if (!selectBinaryOp(I
, ISD::UDIV
))
2054 return selectDivRem(I
, ISD::UDIV
);
2056 case Instruction::SRem
:
2057 if (!selectBinaryOp(I
, ISD::SREM
))
2058 return selectDivRem(I
, ISD::SREM
);
2060 case Instruction::URem
:
2061 if (!selectBinaryOp(I
, ISD::UREM
))
2062 return selectDivRem(I
, ISD::UREM
);
2064 case Instruction::Shl
:
2065 case Instruction::LShr
:
2066 case Instruction::AShr
:
2067 return selectShift(I
);
2068 case Instruction::And
:
2069 case Instruction::Or
:
2070 case Instruction::Xor
:
2071 return selectLogicalOp(I
);
2072 case Instruction::Br
:
2073 return selectBranch(I
);
2074 case Instruction::Ret
:
2075 return selectRet(I
);
2076 case Instruction::Trunc
:
2077 return selectTrunc(I
);
2078 case Instruction::ZExt
:
2079 case Instruction::SExt
:
2080 return selectIntExt(I
);
2081 case Instruction::FPTrunc
:
2082 return selectFPTrunc(I
);
2083 case Instruction::FPExt
:
2084 return selectFPExt(I
);
2085 case Instruction::FPToSI
:
2086 return selectFPToInt(I
, /*isSigned*/ true);
2087 case Instruction::FPToUI
:
2088 return selectFPToInt(I
, /*isSigned*/ false);
2089 case Instruction::ICmp
:
2090 case Instruction::FCmp
:
2091 return selectCmp(I
);
2092 case Instruction::Select
:
2093 return selectSelect(I
);
2098 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value
*V
,
2100 Register VReg
= getRegForValue(V
);
2103 MVT VMVT
= TLI
.getValueType(DL
, V
->getType(), true).getSimpleVT();
2105 if (VMVT
== MVT::i1
)
2108 if ((VMVT
== MVT::i8
) || (VMVT
== MVT::i16
)) {
2109 Register TempReg
= createResultReg(&Mips::GPR32RegClass
);
2110 if (!emitIntExt(VMVT
, VReg
, MVT::i32
, TempReg
, IsUnsigned
))
2117 void MipsFastISel::simplifyAddress(Address
&Addr
) {
2118 if (!isInt
<16>(Addr
.getOffset())) {
2120 materialize32BitInt(Addr
.getOffset(), &Mips::GPR32RegClass
);
2121 Register DestReg
= createResultReg(&Mips::GPR32RegClass
);
2122 emitInst(Mips::ADDu
, DestReg
).addReg(TempReg
).addReg(Addr
.getReg());
2123 Addr
.setReg(DestReg
);
2128 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode
,
2129 const TargetRegisterClass
*RC
,
2130 unsigned Op0
, unsigned Op1
) {
2131 // We treat the MUL instruction in a special way because it clobbers
2132 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2133 // mark these registers only as implicitly defined. As a result, the
2134 // register allocator runs out of registers when this instruction is
2135 // followed by another instruction that defines the same registers too.
2136 // We can fix this by explicitly marking those registers as dead.
2137 if (MachineInstOpcode
== Mips::MUL
) {
2138 Register ResultReg
= createResultReg(RC
);
2139 const MCInstrDesc
&II
= TII
.get(MachineInstOpcode
);
2140 Op0
= constrainOperandRegClass(II
, Op0
, II
.getNumDefs());
2141 Op1
= constrainOperandRegClass(II
, Op1
, II
.getNumDefs() + 1);
2142 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, MIMD
, II
, ResultReg
)
2145 .addReg(Mips::HI0
, RegState::ImplicitDefine
| RegState::Dead
)
2146 .addReg(Mips::LO0
, RegState::ImplicitDefine
| RegState::Dead
);
2150 return FastISel::fastEmitInst_rr(MachineInstOpcode
, RC
, Op0
, Op1
);
2155 FastISel
*Mips::createFastISel(FunctionLoweringInfo
&funcInfo
,
2156 const TargetLibraryInfo
*libInfo
) {
2157 return new MipsFastISel(funcInfo
, libInfo
);
2160 } // end namespace llvm