1 //===- MipsFastISel.cpp - Mips FastISel implementation --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file defines the MIPS-specific support for the FastISel class.
11 /// Some of the target-specific code is generated by tablegen in the file
12 /// MipsGenFastISel.inc, which is #included here.
14 //===----------------------------------------------------------------------===//
16 #include "MCTargetDesc/MipsABIInfo.h"
17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "MipsCCState.h"
19 #include "MipsISelLowering.h"
20 #include "MipsInstrInfo.h"
21 #include "MipsMachineFunction.h"
22 #include "MipsSubtarget.h"
23 #include "MipsTargetMachine.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/Analysis/TargetLibraryInfo.h"
29 #include "llvm/CodeGen/CallingConvLower.h"
30 #include "llvm/CodeGen/FastISel.h"
31 #include "llvm/CodeGen/FunctionLoweringInfo.h"
32 #include "llvm/CodeGen/ISDOpcodes.h"
33 #include "llvm/CodeGen/MachineBasicBlock.h"
34 #include "llvm/CodeGen/MachineFrameInfo.h"
35 #include "llvm/CodeGen/MachineInstrBuilder.h"
36 #include "llvm/CodeGen/MachineMemOperand.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/TargetInstrInfo.h"
39 #include "llvm/CodeGen/TargetLowering.h"
40 #include "llvm/CodeGen/ValueTypes.h"
41 #include "llvm/IR/Attributes.h"
42 #include "llvm/IR/CallingConv.h"
43 #include "llvm/IR/Constant.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/Function.h"
47 #include "llvm/IR/GetElementPtrTypeIterator.h"
48 #include "llvm/IR/GlobalValue.h"
49 #include "llvm/IR/GlobalVariable.h"
50 #include "llvm/IR/InstrTypes.h"
51 #include "llvm/IR/Instruction.h"
52 #include "llvm/IR/Instructions.h"
53 #include "llvm/IR/IntrinsicInst.h"
54 #include "llvm/IR/Operator.h"
55 #include "llvm/IR/Type.h"
56 #include "llvm/IR/User.h"
57 #include "llvm/IR/Value.h"
58 #include "llvm/MC/MCContext.h"
59 #include "llvm/MC/MCInstrDesc.h"
60 #include "llvm/MC/MCRegisterInfo.h"
61 #include "llvm/MC/MCSymbol.h"
62 #include "llvm/Support/Casting.h"
63 #include "llvm/Support/Compiler.h"
64 #include "llvm/Support/Debug.h"
65 #include "llvm/Support/ErrorHandling.h"
66 #include "llvm/Support/MachineValueType.h"
67 #include "llvm/Support/MathExtras.h"
68 #include "llvm/Support/raw_ostream.h"
74 #define DEBUG_TYPE "mips-fastisel"
78 extern cl::opt
<bool> EmitJalrReloc
;
82 class MipsFastISel final
: public FastISel
{
84 // All possible address modes.
87 using BaseKind
= enum { RegBase
, FrameIndexBase
};
90 BaseKind Kind
= RegBase
;
98 const GlobalValue
*GV
= nullptr;
101 // Innocuous defaults for our address.
102 Address() { Base
.Reg
= 0; }
104 void setKind(BaseKind K
) { Kind
= K
; }
105 BaseKind
getKind() const { return Kind
; }
106 bool isRegBase() const { return Kind
== RegBase
; }
107 bool isFIBase() const { return Kind
== FrameIndexBase
; }
109 void setReg(unsigned Reg
) {
110 assert(isRegBase() && "Invalid base register access!");
114 unsigned getReg() const {
115 assert(isRegBase() && "Invalid base register access!");
119 void setFI(unsigned FI
) {
120 assert(isFIBase() && "Invalid base frame index access!");
124 unsigned getFI() const {
125 assert(isFIBase() && "Invalid base frame index access!");
129 void setOffset(int64_t Offset_
) { Offset
= Offset_
; }
130 int64_t getOffset() const { return Offset
; }
131 void setGlobalValue(const GlobalValue
*G
) { GV
= G
; }
132 const GlobalValue
*getGlobalValue() { return GV
; }
135 /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can
136 /// make the right decision when generating code for different targets.
137 const TargetMachine
&TM
;
138 const MipsSubtarget
*Subtarget
;
139 const TargetInstrInfo
&TII
;
140 const TargetLowering
&TLI
;
141 MipsFunctionInfo
*MFI
;
143 // Convenience variables to avoid some queries.
144 LLVMContext
*Context
;
146 bool fastLowerArguments() override
;
147 bool fastLowerCall(CallLoweringInfo
&CLI
) override
;
148 bool fastLowerIntrinsicCall(const IntrinsicInst
*II
) override
;
150 bool UnsupportedFPMode
; // To allow fast-isel to proceed and just not handle
151 // floating point but not reject doing fast-isel in other
155 // Selection routines.
156 bool selectLogicalOp(const Instruction
*I
);
157 bool selectLoad(const Instruction
*I
);
158 bool selectStore(const Instruction
*I
);
159 bool selectBranch(const Instruction
*I
);
160 bool selectSelect(const Instruction
*I
);
161 bool selectCmp(const Instruction
*I
);
162 bool selectFPExt(const Instruction
*I
);
163 bool selectFPTrunc(const Instruction
*I
);
164 bool selectFPToInt(const Instruction
*I
, bool IsSigned
);
165 bool selectRet(const Instruction
*I
);
166 bool selectTrunc(const Instruction
*I
);
167 bool selectIntExt(const Instruction
*I
);
168 bool selectShift(const Instruction
*I
);
169 bool selectDivRem(const Instruction
*I
, unsigned ISDOpcode
);
171 // Utility helper routines.
172 bool isTypeLegal(Type
*Ty
, MVT
&VT
);
173 bool isTypeSupported(Type
*Ty
, MVT
&VT
);
174 bool isLoadTypeLegal(Type
*Ty
, MVT
&VT
);
175 bool computeAddress(const Value
*Obj
, Address
&Addr
);
176 bool computeCallAddress(const Value
*V
, Address
&Addr
);
177 void simplifyAddress(Address
&Addr
);
179 // Emit helper routines.
180 bool emitCmp(unsigned DestReg
, const CmpInst
*CI
);
181 bool emitLoad(MVT VT
, unsigned &ResultReg
, Address
&Addr
,
182 unsigned Alignment
= 0);
183 bool emitStore(MVT VT
, unsigned SrcReg
, Address Addr
,
184 MachineMemOperand
*MMO
= nullptr);
185 bool emitStore(MVT VT
, unsigned SrcReg
, Address
&Addr
,
186 unsigned Alignment
= 0);
187 unsigned emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, bool isZExt
);
188 bool emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
,
191 bool emitIntZExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
);
193 bool emitIntSExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
, unsigned DestReg
);
194 bool emitIntSExt32r1(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
196 bool emitIntSExt32r2(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
199 unsigned getRegEnsuringSimpleIntegerWidening(const Value
*, bool IsUnsigned
);
201 unsigned emitLogicalOp(unsigned ISDOpc
, MVT RetVT
, const Value
*LHS
,
204 unsigned materializeFP(const ConstantFP
*CFP
, MVT VT
);
205 unsigned materializeGV(const GlobalValue
*GV
, MVT VT
);
206 unsigned materializeInt(const Constant
*C
, MVT VT
);
207 unsigned materialize32BitInt(int64_t Imm
, const TargetRegisterClass
*RC
);
208 unsigned materializeExternalCallSym(MCSymbol
*Syn
);
210 MachineInstrBuilder
emitInst(unsigned Opc
) {
211 return BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Opc
));
214 MachineInstrBuilder
emitInst(unsigned Opc
, unsigned DstReg
) {
215 return BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Opc
),
219 MachineInstrBuilder
emitInstStore(unsigned Opc
, unsigned SrcReg
,
220 unsigned MemReg
, int64_t MemOffset
) {
221 return emitInst(Opc
).addReg(SrcReg
).addReg(MemReg
).addImm(MemOffset
);
224 MachineInstrBuilder
emitInstLoad(unsigned Opc
, unsigned DstReg
,
225 unsigned MemReg
, int64_t MemOffset
) {
226 return emitInst(Opc
, DstReg
).addReg(MemReg
).addImm(MemOffset
);
229 unsigned fastEmitInst_rr(unsigned MachineInstOpcode
,
230 const TargetRegisterClass
*RC
,
231 unsigned Op0
, unsigned Op1
);
233 // for some reason, this default is not generated by tablegen
234 // so we explicitly generate it here.
235 unsigned fastEmitInst_riir(uint64_t inst
, const TargetRegisterClass
*RC
,
236 unsigned Op0
, uint64_t imm1
, uint64_t imm2
,
241 // Call handling routines.
243 CCAssignFn
*CCAssignFnForCall(CallingConv::ID CC
) const;
244 bool processCallArgs(CallLoweringInfo
&CLI
, SmallVectorImpl
<MVT
> &ArgVTs
,
246 bool finishCall(CallLoweringInfo
&CLI
, MVT RetVT
, unsigned NumBytes
);
248 const MipsABIInfo
&getABI() const {
249 return static_cast<const MipsTargetMachine
&>(TM
).getABI();
253 // Backend specific FastISel code.
254 explicit MipsFastISel(FunctionLoweringInfo
&funcInfo
,
255 const TargetLibraryInfo
*libInfo
)
256 : FastISel(funcInfo
, libInfo
), TM(funcInfo
.MF
->getTarget()),
257 Subtarget(&funcInfo
.MF
->getSubtarget
<MipsSubtarget
>()),
258 TII(*Subtarget
->getInstrInfo()), TLI(*Subtarget
->getTargetLowering()) {
259 MFI
= funcInfo
.MF
->getInfo
<MipsFunctionInfo
>();
260 Context
= &funcInfo
.Fn
->getContext();
261 UnsupportedFPMode
= Subtarget
->isFP64bit() || Subtarget
->useSoftFloat();
264 unsigned fastMaterializeAlloca(const AllocaInst
*AI
) override
;
265 unsigned fastMaterializeConstant(const Constant
*C
) override
;
266 bool fastSelectInstruction(const Instruction
*I
) override
;
268 #include "MipsGenFastISel.inc"
271 } // end anonymous namespace
273 static bool CC_Mips(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
274 CCValAssign::LocInfo LocInfo
, ISD::ArgFlagsTy ArgFlags
,
275 CCState
&State
) LLVM_ATTRIBUTE_UNUSED
;
277 static bool CC_MipsO32_FP32(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
278 CCValAssign::LocInfo LocInfo
,
279 ISD::ArgFlagsTy ArgFlags
, CCState
&State
) {
280 llvm_unreachable("should not be called");
283 static bool CC_MipsO32_FP64(unsigned ValNo
, MVT ValVT
, MVT LocVT
,
284 CCValAssign::LocInfo LocInfo
,
285 ISD::ArgFlagsTy ArgFlags
, CCState
&State
) {
286 llvm_unreachable("should not be called");
289 #include "MipsGenCallingConv.inc"
291 CCAssignFn
*MipsFastISel::CCAssignFnForCall(CallingConv::ID CC
) const {
295 unsigned MipsFastISel::emitLogicalOp(unsigned ISDOpc
, MVT RetVT
,
296 const Value
*LHS
, const Value
*RHS
) {
297 // Canonicalize immediates to the RHS first.
298 if (isa
<ConstantInt
>(LHS
) && !isa
<ConstantInt
>(RHS
))
313 llvm_unreachable("unexpected opcode");
316 unsigned LHSReg
= getRegForValue(LHS
);
321 if (const auto *C
= dyn_cast
<ConstantInt
>(RHS
))
322 RHSReg
= materializeInt(C
, MVT::i32
);
324 RHSReg
= getRegForValue(RHS
);
328 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
332 emitInst(Opc
, ResultReg
).addReg(LHSReg
).addReg(RHSReg
);
336 unsigned MipsFastISel::fastMaterializeAlloca(const AllocaInst
*AI
) {
337 assert(TLI
.getValueType(DL
, AI
->getType(), true) == MVT::i32
&&
338 "Alloca should always return a pointer.");
340 DenseMap
<const AllocaInst
*, int>::iterator SI
=
341 FuncInfo
.StaticAllocaMap
.find(AI
);
343 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
344 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
345 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Mips::LEA_ADDiu
),
347 .addFrameIndex(SI
->second
)
355 unsigned MipsFastISel::materializeInt(const Constant
*C
, MVT VT
) {
356 if (VT
!= MVT::i32
&& VT
!= MVT::i16
&& VT
!= MVT::i8
&& VT
!= MVT::i1
)
358 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
359 const ConstantInt
*CI
= cast
<ConstantInt
>(C
);
360 return materialize32BitInt(CI
->getZExtValue(), RC
);
363 unsigned MipsFastISel::materialize32BitInt(int64_t Imm
,
364 const TargetRegisterClass
*RC
) {
365 unsigned ResultReg
= createResultReg(RC
);
367 if (isInt
<16>(Imm
)) {
368 unsigned Opc
= Mips::ADDiu
;
369 emitInst(Opc
, ResultReg
).addReg(Mips::ZERO
).addImm(Imm
);
371 } else if (isUInt
<16>(Imm
)) {
372 emitInst(Mips::ORi
, ResultReg
).addReg(Mips::ZERO
).addImm(Imm
);
375 unsigned Lo
= Imm
& 0xFFFF;
376 unsigned Hi
= (Imm
>> 16) & 0xFFFF;
378 // Both Lo and Hi have nonzero bits.
379 unsigned TmpReg
= createResultReg(RC
);
380 emitInst(Mips::LUi
, TmpReg
).addImm(Hi
);
381 emitInst(Mips::ORi
, ResultReg
).addReg(TmpReg
).addImm(Lo
);
383 emitInst(Mips::LUi
, ResultReg
).addImm(Hi
);
388 unsigned MipsFastISel::materializeFP(const ConstantFP
*CFP
, MVT VT
) {
389 if (UnsupportedFPMode
)
391 int64_t Imm
= CFP
->getValueAPF().bitcastToAPInt().getZExtValue();
392 if (VT
== MVT::f32
) {
393 const TargetRegisterClass
*RC
= &Mips::FGR32RegClass
;
394 unsigned DestReg
= createResultReg(RC
);
395 unsigned TempReg
= materialize32BitInt(Imm
, &Mips::GPR32RegClass
);
396 emitInst(Mips::MTC1
, DestReg
).addReg(TempReg
);
398 } else if (VT
== MVT::f64
) {
399 const TargetRegisterClass
*RC
= &Mips::AFGR64RegClass
;
400 unsigned DestReg
= createResultReg(RC
);
401 unsigned TempReg1
= materialize32BitInt(Imm
>> 32, &Mips::GPR32RegClass
);
403 materialize32BitInt(Imm
& 0xFFFFFFFF, &Mips::GPR32RegClass
);
404 emitInst(Mips::BuildPairF64
, DestReg
).addReg(TempReg2
).addReg(TempReg1
);
410 unsigned MipsFastISel::materializeGV(const GlobalValue
*GV
, MVT VT
) {
411 // For now 32-bit only.
414 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
415 unsigned DestReg
= createResultReg(RC
);
416 const GlobalVariable
*GVar
= dyn_cast
<GlobalVariable
>(GV
);
417 bool IsThreadLocal
= GVar
&& GVar
->isThreadLocal();
418 // TLS not supported at this time.
421 emitInst(Mips::LW
, DestReg
)
422 .addReg(MFI
->getGlobalBaseReg(*MF
))
423 .addGlobalAddress(GV
, 0, MipsII::MO_GOT
);
424 if ((GV
->hasInternalLinkage() ||
425 (GV
->hasLocalLinkage() && !isa
<Function
>(GV
)))) {
426 unsigned TempReg
= createResultReg(RC
);
427 emitInst(Mips::ADDiu
, TempReg
)
429 .addGlobalAddress(GV
, 0, MipsII::MO_ABS_LO
);
435 unsigned MipsFastISel::materializeExternalCallSym(MCSymbol
*Sym
) {
436 const TargetRegisterClass
*RC
= &Mips::GPR32RegClass
;
437 unsigned DestReg
= createResultReg(RC
);
438 emitInst(Mips::LW
, DestReg
)
439 .addReg(MFI
->getGlobalBaseReg(*MF
))
440 .addSym(Sym
, MipsII::MO_GOT
);
444 // Materialize a constant into a register, and return the register
445 // number (or zero if we failed to handle it).
446 unsigned MipsFastISel::fastMaterializeConstant(const Constant
*C
) {
447 EVT CEVT
= TLI
.getValueType(DL
, C
->getType(), true);
449 // Only handle simple types.
450 if (!CEVT
.isSimple())
452 MVT VT
= CEVT
.getSimpleVT();
454 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(C
))
455 return (UnsupportedFPMode
) ? 0 : materializeFP(CFP
, VT
);
456 else if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(C
))
457 return materializeGV(GV
, VT
);
458 else if (isa
<ConstantInt
>(C
))
459 return materializeInt(C
, VT
);
464 bool MipsFastISel::computeAddress(const Value
*Obj
, Address
&Addr
) {
465 const User
*U
= nullptr;
466 unsigned Opcode
= Instruction::UserOp1
;
467 if (const Instruction
*I
= dyn_cast
<Instruction
>(Obj
)) {
468 // Don't walk into other basic blocks unless the object is an alloca from
469 // another block, otherwise it may not have a virtual register assigned.
470 if (FuncInfo
.StaticAllocaMap
.count(static_cast<const AllocaInst
*>(Obj
)) ||
471 FuncInfo
.MBBMap
[I
->getParent()] == FuncInfo
.MBB
) {
472 Opcode
= I
->getOpcode();
475 } else if (const ConstantExpr
*C
= dyn_cast
<ConstantExpr
>(Obj
)) {
476 Opcode
= C
->getOpcode();
482 case Instruction::BitCast
:
483 // Look through bitcasts.
484 return computeAddress(U
->getOperand(0), Addr
);
485 case Instruction::GetElementPtr
: {
486 Address SavedAddr
= Addr
;
487 int64_t TmpOffset
= Addr
.getOffset();
488 // Iterate through the GEP folding the constants into offsets where
490 gep_type_iterator GTI
= gep_type_begin(U
);
491 for (User::const_op_iterator i
= U
->op_begin() + 1, e
= U
->op_end(); i
!= e
;
493 const Value
*Op
= *i
;
494 if (StructType
*STy
= GTI
.getStructTypeOrNull()) {
495 const StructLayout
*SL
= DL
.getStructLayout(STy
);
496 unsigned Idx
= cast
<ConstantInt
>(Op
)->getZExtValue();
497 TmpOffset
+= SL
->getElementOffset(Idx
);
499 uint64_t S
= DL
.getTypeAllocSize(GTI
.getIndexedType());
501 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(Op
)) {
502 // Constant-offset addressing.
503 TmpOffset
+= CI
->getSExtValue() * S
;
506 if (canFoldAddIntoGEP(U
, Op
)) {
507 // A compatible add with a constant operand. Fold the constant.
509 cast
<ConstantInt
>(cast
<AddOperator
>(Op
)->getOperand(1));
510 TmpOffset
+= CI
->getSExtValue() * S
;
511 // Iterate on the other operand.
512 Op
= cast
<AddOperator
>(Op
)->getOperand(0);
516 goto unsupported_gep
;
520 // Try to grab the base operand now.
521 Addr
.setOffset(TmpOffset
);
522 if (computeAddress(U
->getOperand(0), Addr
))
524 // We failed, restore everything and try the other options.
529 case Instruction::Alloca
: {
530 const AllocaInst
*AI
= cast
<AllocaInst
>(Obj
);
531 DenseMap
<const AllocaInst
*, int>::iterator SI
=
532 FuncInfo
.StaticAllocaMap
.find(AI
);
533 if (SI
!= FuncInfo
.StaticAllocaMap
.end()) {
534 Addr
.setKind(Address::FrameIndexBase
);
535 Addr
.setFI(SI
->second
);
541 Addr
.setReg(getRegForValue(Obj
));
542 return Addr
.getReg() != 0;
545 bool MipsFastISel::computeCallAddress(const Value
*V
, Address
&Addr
) {
546 const User
*U
= nullptr;
547 unsigned Opcode
= Instruction::UserOp1
;
549 if (const auto *I
= dyn_cast
<Instruction
>(V
)) {
550 // Check if the value is defined in the same basic block. This information
551 // is crucial to know whether or not folding an operand is valid.
552 if (I
->getParent() == FuncInfo
.MBB
->getBasicBlock()) {
553 Opcode
= I
->getOpcode();
556 } else if (const auto *C
= dyn_cast
<ConstantExpr
>(V
)) {
557 Opcode
= C
->getOpcode();
564 case Instruction::BitCast
:
565 // Look past bitcasts if its operand is in the same BB.
566 return computeCallAddress(U
->getOperand(0), Addr
);
568 case Instruction::IntToPtr
:
569 // Look past no-op inttoptrs if its operand is in the same BB.
570 if (TLI
.getValueType(DL
, U
->getOperand(0)->getType()) ==
571 TLI
.getPointerTy(DL
))
572 return computeCallAddress(U
->getOperand(0), Addr
);
574 case Instruction::PtrToInt
:
575 // Look past no-op ptrtoints if its operand is in the same BB.
576 if (TLI
.getValueType(DL
, U
->getType()) == TLI
.getPointerTy(DL
))
577 return computeCallAddress(U
->getOperand(0), Addr
);
581 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
582 Addr
.setGlobalValue(GV
);
586 // If all else fails, try to materialize the value in a register.
587 if (!Addr
.getGlobalValue()) {
588 Addr
.setReg(getRegForValue(V
));
589 return Addr
.getReg() != 0;
595 bool MipsFastISel::isTypeLegal(Type
*Ty
, MVT
&VT
) {
596 EVT evt
= TLI
.getValueType(DL
, Ty
, true);
597 // Only handle simple types.
598 if (evt
== MVT::Other
|| !evt
.isSimple())
600 VT
= evt
.getSimpleVT();
602 // Handle all legal types, i.e. a register that will directly hold this
604 return TLI
.isTypeLegal(VT
);
607 bool MipsFastISel::isTypeSupported(Type
*Ty
, MVT
&VT
) {
608 if (Ty
->isVectorTy())
611 if (isTypeLegal(Ty
, VT
))
614 // If this is a type than can be sign or zero-extended to a basic operation
615 // go ahead and accept it now.
616 if (VT
== MVT::i1
|| VT
== MVT::i8
|| VT
== MVT::i16
)
622 bool MipsFastISel::isLoadTypeLegal(Type
*Ty
, MVT
&VT
) {
623 if (isTypeLegal(Ty
, VT
))
625 // We will extend this in a later patch:
626 // If this is a type than can be sign or zero-extended to a basic operation
627 // go ahead and accept it now.
628 if (VT
== MVT::i8
|| VT
== MVT::i16
)
633 // Because of how EmitCmp is called with fast-isel, you can
634 // end up with redundant "andi" instructions after the sequences emitted below.
635 // We should try and solve this issue in the future.
637 bool MipsFastISel::emitCmp(unsigned ResultReg
, const CmpInst
*CI
) {
638 const Value
*Left
= CI
->getOperand(0), *Right
= CI
->getOperand(1);
639 bool IsUnsigned
= CI
->isUnsigned();
640 unsigned LeftReg
= getRegEnsuringSimpleIntegerWidening(Left
, IsUnsigned
);
643 unsigned RightReg
= getRegEnsuringSimpleIntegerWidening(Right
, IsUnsigned
);
646 CmpInst::Predicate P
= CI
->getPredicate();
651 case CmpInst::ICMP_EQ
: {
652 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
653 emitInst(Mips::XOR
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
654 emitInst(Mips::SLTiu
, ResultReg
).addReg(TempReg
).addImm(1);
657 case CmpInst::ICMP_NE
: {
658 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
659 emitInst(Mips::XOR
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
660 emitInst(Mips::SLTu
, ResultReg
).addReg(Mips::ZERO
).addReg(TempReg
);
663 case CmpInst::ICMP_UGT
:
664 emitInst(Mips::SLTu
, ResultReg
).addReg(RightReg
).addReg(LeftReg
);
666 case CmpInst::ICMP_ULT
:
667 emitInst(Mips::SLTu
, ResultReg
).addReg(LeftReg
).addReg(RightReg
);
669 case CmpInst::ICMP_UGE
: {
670 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
671 emitInst(Mips::SLTu
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
672 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
675 case CmpInst::ICMP_ULE
: {
676 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
677 emitInst(Mips::SLTu
, TempReg
).addReg(RightReg
).addReg(LeftReg
);
678 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
681 case CmpInst::ICMP_SGT
:
682 emitInst(Mips::SLT
, ResultReg
).addReg(RightReg
).addReg(LeftReg
);
684 case CmpInst::ICMP_SLT
:
685 emitInst(Mips::SLT
, ResultReg
).addReg(LeftReg
).addReg(RightReg
);
687 case CmpInst::ICMP_SGE
: {
688 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
689 emitInst(Mips::SLT
, TempReg
).addReg(LeftReg
).addReg(RightReg
);
690 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
693 case CmpInst::ICMP_SLE
: {
694 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
695 emitInst(Mips::SLT
, TempReg
).addReg(RightReg
).addReg(LeftReg
);
696 emitInst(Mips::XORi
, ResultReg
).addReg(TempReg
).addImm(1);
699 case CmpInst::FCMP_OEQ
:
700 case CmpInst::FCMP_UNE
:
701 case CmpInst::FCMP_OLT
:
702 case CmpInst::FCMP_OLE
:
703 case CmpInst::FCMP_OGT
:
704 case CmpInst::FCMP_OGE
: {
705 if (UnsupportedFPMode
)
707 bool IsFloat
= Left
->getType()->isFloatTy();
708 bool IsDouble
= Left
->getType()->isDoubleTy();
709 if (!IsFloat
&& !IsDouble
)
711 unsigned Opc
, CondMovOpc
;
713 case CmpInst::FCMP_OEQ
:
714 Opc
= IsFloat
? Mips::C_EQ_S
: Mips::C_EQ_D32
;
715 CondMovOpc
= Mips::MOVT_I
;
717 case CmpInst::FCMP_UNE
:
718 Opc
= IsFloat
? Mips::C_EQ_S
: Mips::C_EQ_D32
;
719 CondMovOpc
= Mips::MOVF_I
;
721 case CmpInst::FCMP_OLT
:
722 Opc
= IsFloat
? Mips::C_OLT_S
: Mips::C_OLT_D32
;
723 CondMovOpc
= Mips::MOVT_I
;
725 case CmpInst::FCMP_OLE
:
726 Opc
= IsFloat
? Mips::C_OLE_S
: Mips::C_OLE_D32
;
727 CondMovOpc
= Mips::MOVT_I
;
729 case CmpInst::FCMP_OGT
:
730 Opc
= IsFloat
? Mips::C_ULE_S
: Mips::C_ULE_D32
;
731 CondMovOpc
= Mips::MOVF_I
;
733 case CmpInst::FCMP_OGE
:
734 Opc
= IsFloat
? Mips::C_ULT_S
: Mips::C_ULT_D32
;
735 CondMovOpc
= Mips::MOVF_I
;
738 llvm_unreachable("Only switching of a subset of CCs.");
740 unsigned RegWithZero
= createResultReg(&Mips::GPR32RegClass
);
741 unsigned RegWithOne
= createResultReg(&Mips::GPR32RegClass
);
742 emitInst(Mips::ADDiu
, RegWithZero
).addReg(Mips::ZERO
).addImm(0);
743 emitInst(Mips::ADDiu
, RegWithOne
).addReg(Mips::ZERO
).addImm(1);
744 emitInst(Opc
).addReg(Mips::FCC0
, RegState::Define
).addReg(LeftReg
)
746 emitInst(CondMovOpc
, ResultReg
)
749 .addReg(RegWithZero
);
756 bool MipsFastISel::emitLoad(MVT VT
, unsigned &ResultReg
, Address
&Addr
,
757 unsigned Alignment
) {
759 // more cases will be handled here in following patches.
762 switch (VT
.SimpleTy
) {
764 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
768 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
772 ResultReg
= createResultReg(&Mips::GPR32RegClass
);
776 if (UnsupportedFPMode
)
778 ResultReg
= createResultReg(&Mips::FGR32RegClass
);
782 if (UnsupportedFPMode
)
784 ResultReg
= createResultReg(&Mips::AFGR64RegClass
);
790 if (Addr
.isRegBase()) {
791 simplifyAddress(Addr
);
792 emitInstLoad(Opc
, ResultReg
, Addr
.getReg(), Addr
.getOffset());
795 if (Addr
.isFIBase()) {
796 unsigned FI
= Addr
.getFI();
797 int64_t Offset
= Addr
.getOffset();
798 MachineFrameInfo
&MFI
= MF
->getFrameInfo();
799 MachineMemOperand
*MMO
= MF
->getMachineMemOperand(
800 MachinePointerInfo::getFixedStack(*MF
, FI
), MachineMemOperand::MOLoad
,
801 MFI
.getObjectSize(FI
), Align(4));
802 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Opc
), ResultReg
)
811 bool MipsFastISel::emitStore(MVT VT
, unsigned SrcReg
, Address
&Addr
,
812 unsigned Alignment
) {
814 // more cases will be handled here in following patches.
817 switch (VT
.SimpleTy
) {
828 if (UnsupportedFPMode
)
833 if (UnsupportedFPMode
)
840 if (Addr
.isRegBase()) {
841 simplifyAddress(Addr
);
842 emitInstStore(Opc
, SrcReg
, Addr
.getReg(), Addr
.getOffset());
845 if (Addr
.isFIBase()) {
846 unsigned FI
= Addr
.getFI();
847 int64_t Offset
= Addr
.getOffset();
848 MachineFrameInfo
&MFI
= MF
->getFrameInfo();
849 MachineMemOperand
*MMO
= MF
->getMachineMemOperand(
850 MachinePointerInfo::getFixedStack(*MF
, FI
), MachineMemOperand::MOStore
,
851 MFI
.getObjectSize(FI
), Align(4));
852 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Opc
))
862 bool MipsFastISel::selectLogicalOp(const Instruction
*I
) {
864 if (!isTypeSupported(I
->getType(), VT
))
868 switch (I
->getOpcode()) {
870 llvm_unreachable("Unexpected instruction.");
871 case Instruction::And
:
872 ResultReg
= emitLogicalOp(ISD::AND
, VT
, I
->getOperand(0), I
->getOperand(1));
874 case Instruction::Or
:
875 ResultReg
= emitLogicalOp(ISD::OR
, VT
, I
->getOperand(0), I
->getOperand(1));
877 case Instruction::Xor
:
878 ResultReg
= emitLogicalOp(ISD::XOR
, VT
, I
->getOperand(0), I
->getOperand(1));
885 updateValueMap(I
, ResultReg
);
889 bool MipsFastISel::selectLoad(const Instruction
*I
) {
890 // Atomic loads need special handling.
891 if (cast
<LoadInst
>(I
)->isAtomic())
894 // Verify we have a legal type before going any further.
896 if (!isLoadTypeLegal(I
->getType(), VT
))
899 // See if we can handle this address.
901 if (!computeAddress(I
->getOperand(0), Addr
))
905 if (!emitLoad(VT
, ResultReg
, Addr
, cast
<LoadInst
>(I
)->getAlignment()))
907 updateValueMap(I
, ResultReg
);
911 bool MipsFastISel::selectStore(const Instruction
*I
) {
912 Value
*Op0
= I
->getOperand(0);
915 // Atomic stores need special handling.
916 if (cast
<StoreInst
>(I
)->isAtomic())
919 // Verify we have a legal type before going any further.
921 if (!isLoadTypeLegal(I
->getOperand(0)->getType(), VT
))
924 // Get the value to be stored into a register.
925 SrcReg
= getRegForValue(Op0
);
929 // See if we can handle this address.
931 if (!computeAddress(I
->getOperand(1), Addr
))
934 if (!emitStore(VT
, SrcReg
, Addr
, cast
<StoreInst
>(I
)->getAlignment()))
939 // This can cause a redundant sltiu to be generated.
940 // FIXME: try and eliminate this in a future patch.
941 bool MipsFastISel::selectBranch(const Instruction
*I
) {
942 const BranchInst
*BI
= cast
<BranchInst
>(I
);
943 MachineBasicBlock
*BrBB
= FuncInfo
.MBB
;
945 // TBB is the basic block for the case where the comparison is true.
946 // FBB is the basic block for the case where the comparison is false.
947 // if (cond) goto TBB
951 MachineBasicBlock
*TBB
= FuncInfo
.MBBMap
[BI
->getSuccessor(0)];
952 MachineBasicBlock
*FBB
= FuncInfo
.MBBMap
[BI
->getSuccessor(1)];
954 // Fold the common case of a conditional branch with a comparison
955 // in the same block.
956 unsigned ZExtCondReg
= 0;
957 if (const CmpInst
*CI
= dyn_cast
<CmpInst
>(BI
->getCondition())) {
958 if (CI
->hasOneUse() && CI
->getParent() == I
->getParent()) {
959 ZExtCondReg
= createResultReg(&Mips::GPR32RegClass
);
960 if (!emitCmp(ZExtCondReg
, CI
))
965 // For the general case, we need to mask with 1.
966 if (ZExtCondReg
== 0) {
967 unsigned CondReg
= getRegForValue(BI
->getCondition());
971 ZExtCondReg
= emitIntExt(MVT::i1
, CondReg
, MVT::i32
, true);
972 if (ZExtCondReg
== 0)
976 BuildMI(*BrBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Mips::BGTZ
))
979 finishCondBranch(BI
->getParent(), TBB
, FBB
);
983 bool MipsFastISel::selectCmp(const Instruction
*I
) {
984 const CmpInst
*CI
= cast
<CmpInst
>(I
);
985 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
986 if (!emitCmp(ResultReg
, CI
))
988 updateValueMap(I
, ResultReg
);
992 // Attempt to fast-select a floating-point extend instruction.
993 bool MipsFastISel::selectFPExt(const Instruction
*I
) {
994 if (UnsupportedFPMode
)
996 Value
*Src
= I
->getOperand(0);
997 EVT SrcVT
= TLI
.getValueType(DL
, Src
->getType(), true);
998 EVT DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1000 if (SrcVT
!= MVT::f32
|| DestVT
!= MVT::f64
)
1004 getRegForValue(Src
); // this must be a 32bit floating point register class
1005 // maybe we should handle this differently
1009 unsigned DestReg
= createResultReg(&Mips::AFGR64RegClass
);
1010 emitInst(Mips::CVT_D32_S
, DestReg
).addReg(SrcReg
);
1011 updateValueMap(I
, DestReg
);
1015 bool MipsFastISel::selectSelect(const Instruction
*I
) {
1016 assert(isa
<SelectInst
>(I
) && "Expected a select instruction.");
1018 LLVM_DEBUG(dbgs() << "selectSelect\n");
1021 if (!isTypeSupported(I
->getType(), VT
) || UnsupportedFPMode
) {
1023 dbgs() << ".. .. gave up (!isTypeSupported || UnsupportedFPMode)\n");
1027 unsigned CondMovOpc
;
1028 const TargetRegisterClass
*RC
;
1030 if (VT
.isInteger() && !VT
.isVector() && VT
.getSizeInBits() <= 32) {
1031 CondMovOpc
= Mips::MOVN_I_I
;
1032 RC
= &Mips::GPR32RegClass
;
1033 } else if (VT
== MVT::f32
) {
1034 CondMovOpc
= Mips::MOVN_I_S
;
1035 RC
= &Mips::FGR32RegClass
;
1036 } else if (VT
== MVT::f64
) {
1037 CondMovOpc
= Mips::MOVN_I_D32
;
1038 RC
= &Mips::AFGR64RegClass
;
1042 const SelectInst
*SI
= cast
<SelectInst
>(I
);
1043 const Value
*Cond
= SI
->getCondition();
1044 unsigned Src1Reg
= getRegForValue(SI
->getTrueValue());
1045 unsigned Src2Reg
= getRegForValue(SI
->getFalseValue());
1046 unsigned CondReg
= getRegForValue(Cond
);
1048 if (!Src1Reg
|| !Src2Reg
|| !CondReg
)
1051 unsigned ZExtCondReg
= createResultReg(&Mips::GPR32RegClass
);
1055 if (!emitIntExt(MVT::i1
, CondReg
, MVT::i32
, ZExtCondReg
, true))
1058 unsigned ResultReg
= createResultReg(RC
);
1059 unsigned TempReg
= createResultReg(RC
);
1061 if (!ResultReg
|| !TempReg
)
1064 emitInst(TargetOpcode::COPY
, TempReg
).addReg(Src2Reg
);
1065 emitInst(CondMovOpc
, ResultReg
)
1066 .addReg(Src1Reg
).addReg(ZExtCondReg
).addReg(TempReg
);
1067 updateValueMap(I
, ResultReg
);
1071 // Attempt to fast-select a floating-point truncate instruction.
1072 bool MipsFastISel::selectFPTrunc(const Instruction
*I
) {
1073 if (UnsupportedFPMode
)
1075 Value
*Src
= I
->getOperand(0);
1076 EVT SrcVT
= TLI
.getValueType(DL
, Src
->getType(), true);
1077 EVT DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1079 if (SrcVT
!= MVT::f64
|| DestVT
!= MVT::f32
)
1082 unsigned SrcReg
= getRegForValue(Src
);
1086 unsigned DestReg
= createResultReg(&Mips::FGR32RegClass
);
1090 emitInst(Mips::CVT_S_D32
, DestReg
).addReg(SrcReg
);
1091 updateValueMap(I
, DestReg
);
1095 // Attempt to fast-select a floating-point-to-integer conversion.
1096 bool MipsFastISel::selectFPToInt(const Instruction
*I
, bool IsSigned
) {
1097 if (UnsupportedFPMode
)
1101 return false; // We don't handle this case yet. There is no native
1102 // instruction for this but it can be synthesized.
1103 Type
*DstTy
= I
->getType();
1104 if (!isTypeLegal(DstTy
, DstVT
))
1107 if (DstVT
!= MVT::i32
)
1110 Value
*Src
= I
->getOperand(0);
1111 Type
*SrcTy
= Src
->getType();
1112 if (!isTypeLegal(SrcTy
, SrcVT
))
1115 if (SrcVT
!= MVT::f32
&& SrcVT
!= MVT::f64
)
1118 unsigned SrcReg
= getRegForValue(Src
);
1122 // Determine the opcode for the conversion, which takes place
1123 // entirely within FPRs.
1124 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
1125 unsigned TempReg
= createResultReg(&Mips::FGR32RegClass
);
1126 unsigned Opc
= (SrcVT
== MVT::f32
) ? Mips::TRUNC_W_S
: Mips::TRUNC_W_D32
;
1128 // Generate the convert.
1129 emitInst(Opc
, TempReg
).addReg(SrcReg
);
1130 emitInst(Mips::MFC1
, DestReg
).addReg(TempReg
);
1132 updateValueMap(I
, DestReg
);
1136 bool MipsFastISel::processCallArgs(CallLoweringInfo
&CLI
,
1137 SmallVectorImpl
<MVT
> &OutVTs
,
1138 unsigned &NumBytes
) {
1139 CallingConv::ID CC
= CLI
.CallConv
;
1140 SmallVector
<CCValAssign
, 16> ArgLocs
;
1141 CCState
CCInfo(CC
, false, *FuncInfo
.MF
, ArgLocs
, *Context
);
1142 CCInfo
.AnalyzeCallOperands(OutVTs
, CLI
.OutFlags
, CCAssignFnForCall(CC
));
1143 // Get a count of how many bytes are to be pushed on the stack.
1144 NumBytes
= CCInfo
.getNextStackOffset();
1145 // This is the minimum argument area used for A0-A3.
1149 emitInst(Mips::ADJCALLSTACKDOWN
).addImm(16).addImm(0);
1150 // Process the args.
1152 for (unsigned i
= 0, e
= ArgLocs
.size(); i
!= e
; ++i
) {
1153 CCValAssign
&VA
= ArgLocs
[i
];
1154 const Value
*ArgVal
= CLI
.OutVals
[VA
.getValNo()];
1155 MVT ArgVT
= OutVTs
[VA
.getValNo()];
1159 if (ArgVT
== MVT::f32
) {
1160 VA
.convertToReg(Mips::F12
);
1161 } else if (ArgVT
== MVT::f64
) {
1162 if (Subtarget
->isFP64bit())
1163 VA
.convertToReg(Mips::D6_64
);
1165 VA
.convertToReg(Mips::D6
);
1167 } else if (i
== 1) {
1168 if ((firstMVT
== MVT::f32
) || (firstMVT
== MVT::f64
)) {
1169 if (ArgVT
== MVT::f32
) {
1170 VA
.convertToReg(Mips::F14
);
1171 } else if (ArgVT
== MVT::f64
) {
1172 if (Subtarget
->isFP64bit())
1173 VA
.convertToReg(Mips::D7_64
);
1175 VA
.convertToReg(Mips::D7
);
1179 if (((ArgVT
== MVT::i32
) || (ArgVT
== MVT::f32
) || (ArgVT
== MVT::i16
) ||
1180 (ArgVT
== MVT::i8
)) &&
1182 switch (VA
.getLocMemOffset()) {
1184 VA
.convertToReg(Mips::A0
);
1187 VA
.convertToReg(Mips::A1
);
1190 VA
.convertToReg(Mips::A2
);
1193 VA
.convertToReg(Mips::A3
);
1199 unsigned ArgReg
= getRegForValue(ArgVal
);
1203 // Handle arg promotion: SExt, ZExt, AExt.
1204 switch (VA
.getLocInfo()) {
1205 case CCValAssign::Full
:
1207 case CCValAssign::AExt
:
1208 case CCValAssign::SExt
: {
1209 MVT DestVT
= VA
.getLocVT();
1211 ArgReg
= emitIntExt(SrcVT
, ArgReg
, DestVT
, /*isZExt=*/false);
1216 case CCValAssign::ZExt
: {
1217 MVT DestVT
= VA
.getLocVT();
1219 ArgReg
= emitIntExt(SrcVT
, ArgReg
, DestVT
, /*isZExt=*/true);
1225 llvm_unreachable("Unknown arg promotion!");
1228 // Now copy/store arg to correct locations.
1229 if (VA
.isRegLoc() && !VA
.needsCustom()) {
1230 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
,
1231 TII
.get(TargetOpcode::COPY
), VA
.getLocReg()).addReg(ArgReg
);
1232 CLI
.OutRegs
.push_back(VA
.getLocReg());
1233 } else if (VA
.needsCustom()) {
1234 llvm_unreachable("Mips does not use custom args.");
1238 // FIXME: This path will currently return false. It was copied
1239 // from the AArch64 port and should be essentially fine for Mips too.
1240 // The work to finish up this path will be done in a follow-on patch.
1242 assert(VA
.isMemLoc() && "Assuming store on stack.");
1243 // Don't emit stores for undef values.
1244 if (isa
<UndefValue
>(ArgVal
))
1247 // Need to store on the stack.
1248 // FIXME: This alignment is incorrect but this path is disabled
1249 // for now (will return false). We need to determine the right alignment
1250 // based on the normal alignment for the underlying machine type.
1252 unsigned ArgSize
= alignTo(ArgVT
.getSizeInBits(), 4);
1254 unsigned BEAlign
= 0;
1255 if (ArgSize
< 8 && !Subtarget
->isLittle())
1256 BEAlign
= 8 - ArgSize
;
1259 Addr
.setKind(Address::RegBase
);
1260 Addr
.setReg(Mips::SP
);
1261 Addr
.setOffset(VA
.getLocMemOffset() + BEAlign
);
1263 Align Alignment
= DL
.getABITypeAlign(ArgVal
->getType());
1264 MachineMemOperand
*MMO
= FuncInfo
.MF
->getMachineMemOperand(
1265 MachinePointerInfo::getStack(*FuncInfo
.MF
, Addr
.getOffset()),
1266 MachineMemOperand::MOStore
, ArgVT
.getStoreSize(), Alignment
);
1268 // if (!emitStore(ArgVT, ArgReg, Addr, MMO))
1269 return false; // can't store on the stack yet.
1276 bool MipsFastISel::finishCall(CallLoweringInfo
&CLI
, MVT RetVT
,
1277 unsigned NumBytes
) {
1278 CallingConv::ID CC
= CLI
.CallConv
;
1279 emitInst(Mips::ADJCALLSTACKUP
).addImm(16).addImm(0);
1280 if (RetVT
!= MVT::isVoid
) {
1281 SmallVector
<CCValAssign
, 16> RVLocs
;
1282 MipsCCState
CCInfo(CC
, false, *FuncInfo
.MF
, RVLocs
, *Context
);
1284 CCInfo
.AnalyzeCallResult(CLI
.Ins
, RetCC_Mips
, CLI
.RetTy
,
1285 CLI
.Symbol
? CLI
.Symbol
->getName().data()
1288 // Only handle a single return value.
1289 if (RVLocs
.size() != 1)
1291 // Copy all of the result registers out of their specified physreg.
1292 MVT CopyVT
= RVLocs
[0].getValVT();
1293 // Special handling for extended integers.
1294 if (RetVT
== MVT::i1
|| RetVT
== MVT::i8
|| RetVT
== MVT::i16
)
1297 unsigned ResultReg
= createResultReg(TLI
.getRegClassFor(CopyVT
));
1300 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
,
1301 TII
.get(TargetOpcode::COPY
),
1302 ResultReg
).addReg(RVLocs
[0].getLocReg());
1303 CLI
.InRegs
.push_back(RVLocs
[0].getLocReg());
1305 CLI
.ResultReg
= ResultReg
;
1306 CLI
.NumResultRegs
= 1;
1311 bool MipsFastISel::fastLowerArguments() {
1312 LLVM_DEBUG(dbgs() << "fastLowerArguments\n");
1314 if (!FuncInfo
.CanLowerReturn
) {
1315 LLVM_DEBUG(dbgs() << ".. gave up (!CanLowerReturn)\n");
1319 const Function
*F
= FuncInfo
.Fn
;
1320 if (F
->isVarArg()) {
1321 LLVM_DEBUG(dbgs() << ".. gave up (varargs)\n");
1325 CallingConv::ID CC
= F
->getCallingConv();
1326 if (CC
!= CallingConv::C
) {
1327 LLVM_DEBUG(dbgs() << ".. gave up (calling convention is not C)\n");
1331 std::array
<MCPhysReg
, 4> GPR32ArgRegs
= {{Mips::A0
, Mips::A1
, Mips::A2
,
1333 std::array
<MCPhysReg
, 2> FGR32ArgRegs
= {{Mips::F12
, Mips::F14
}};
1334 std::array
<MCPhysReg
, 2> AFGR64ArgRegs
= {{Mips::D6
, Mips::D7
}};
1335 auto NextGPR32
= GPR32ArgRegs
.begin();
1336 auto NextFGR32
= FGR32ArgRegs
.begin();
1337 auto NextAFGR64
= AFGR64ArgRegs
.begin();
1339 struct AllocatedReg
{
1340 const TargetRegisterClass
*RC
;
1342 AllocatedReg(const TargetRegisterClass
*RC
, unsigned Reg
)
1343 : RC(RC
), Reg(Reg
) {}
1346 // Only handle simple cases. i.e. All arguments are directly mapped to
1347 // registers of the appropriate type.
1348 SmallVector
<AllocatedReg
, 4> Allocation
;
1349 for (const auto &FormalArg
: F
->args()) {
1350 if (FormalArg
.hasAttribute(Attribute::InReg
) ||
1351 FormalArg
.hasAttribute(Attribute::StructRet
) ||
1352 FormalArg
.hasAttribute(Attribute::ByVal
)) {
1353 LLVM_DEBUG(dbgs() << ".. gave up (inreg, structret, byval)\n");
1357 Type
*ArgTy
= FormalArg
.getType();
1358 if (ArgTy
->isStructTy() || ArgTy
->isArrayTy() || ArgTy
->isVectorTy()) {
1359 LLVM_DEBUG(dbgs() << ".. gave up (struct, array, or vector)\n");
1363 EVT ArgVT
= TLI
.getValueType(DL
, ArgTy
);
1364 LLVM_DEBUG(dbgs() << ".. " << FormalArg
.getArgNo() << ": "
1365 << ArgVT
.getEVTString() << "\n");
1366 if (!ArgVT
.isSimple()) {
1367 LLVM_DEBUG(dbgs() << ".. .. gave up (not a simple type)\n");
1371 switch (ArgVT
.getSimpleVT().SimpleTy
) {
1375 if (!FormalArg
.hasAttribute(Attribute::SExt
) &&
1376 !FormalArg
.hasAttribute(Attribute::ZExt
)) {
1377 // It must be any extend, this shouldn't happen for clang-generated IR
1378 // so just fall back on SelectionDAG.
1379 LLVM_DEBUG(dbgs() << ".. .. gave up (i8/i16 arg is not extended)\n");
1383 if (NextGPR32
== GPR32ArgRegs
.end()) {
1384 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1388 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32
<< ")\n");
1389 Allocation
.emplace_back(&Mips::GPR32RegClass
, *NextGPR32
++);
1391 // Allocating any GPR32 prohibits further use of floating point arguments.
1392 NextFGR32
= FGR32ArgRegs
.end();
1393 NextAFGR64
= AFGR64ArgRegs
.end();
1397 if (FormalArg
.hasAttribute(Attribute::ZExt
)) {
1398 // The O32 ABI does not permit a zero-extended i32.
1399 LLVM_DEBUG(dbgs() << ".. .. gave up (i32 arg is zero extended)\n");
1403 if (NextGPR32
== GPR32ArgRegs
.end()) {
1404 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of GPR32 arguments)\n");
1408 LLVM_DEBUG(dbgs() << ".. .. GPR32(" << *NextGPR32
<< ")\n");
1409 Allocation
.emplace_back(&Mips::GPR32RegClass
, *NextGPR32
++);
1411 // Allocating any GPR32 prohibits further use of floating point arguments.
1412 NextFGR32
= FGR32ArgRegs
.end();
1413 NextAFGR64
= AFGR64ArgRegs
.end();
1417 if (UnsupportedFPMode
) {
1418 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1421 if (NextFGR32
== FGR32ArgRegs
.end()) {
1422 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of FGR32 arguments)\n");
1425 LLVM_DEBUG(dbgs() << ".. .. FGR32(" << *NextFGR32
<< ")\n");
1426 Allocation
.emplace_back(&Mips::FGR32RegClass
, *NextFGR32
++);
1427 // Allocating an FGR32 also allocates the super-register AFGR64, and
1428 // ABI rules require us to skip the corresponding GPR32.
1429 if (NextGPR32
!= GPR32ArgRegs
.end())
1431 if (NextAFGR64
!= AFGR64ArgRegs
.end())
1436 if (UnsupportedFPMode
) {
1437 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode)\n");
1440 if (NextAFGR64
== AFGR64ArgRegs
.end()) {
1441 LLVM_DEBUG(dbgs() << ".. .. gave up (ran out of AFGR64 arguments)\n");
1444 LLVM_DEBUG(dbgs() << ".. .. AFGR64(" << *NextAFGR64
<< ")\n");
1445 Allocation
.emplace_back(&Mips::AFGR64RegClass
, *NextAFGR64
++);
1446 // Allocating an FGR32 also allocates the super-register AFGR64, and
1447 // ABI rules require us to skip the corresponding GPR32 pair.
1448 if (NextGPR32
!= GPR32ArgRegs
.end())
1450 if (NextGPR32
!= GPR32ArgRegs
.end())
1452 if (NextFGR32
!= FGR32ArgRegs
.end())
1457 LLVM_DEBUG(dbgs() << ".. .. gave up (unknown type)\n");
1462 for (const auto &FormalArg
: F
->args()) {
1463 unsigned ArgNo
= FormalArg
.getArgNo();
1464 unsigned SrcReg
= Allocation
[ArgNo
].Reg
;
1465 unsigned DstReg
= FuncInfo
.MF
->addLiveIn(SrcReg
, Allocation
[ArgNo
].RC
);
1466 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1467 // Without this, EmitLiveInCopies may eliminate the livein if its only
1468 // use is a bitcast (which isn't turned into an instruction).
1469 unsigned ResultReg
= createResultReg(Allocation
[ArgNo
].RC
);
1470 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
,
1471 TII
.get(TargetOpcode::COPY
), ResultReg
)
1472 .addReg(DstReg
, getKillRegState(true));
1473 updateValueMap(&FormalArg
, ResultReg
);
1476 // Calculate the size of the incoming arguments area.
1477 // We currently reject all the cases where this would be non-zero.
1478 unsigned IncomingArgSizeInBytes
= 0;
1480 // Account for the reserved argument area on ABI's that have one (O32).
1481 // It seems strange to do this on the caller side but it's necessary in
1482 // SelectionDAG's implementation.
1483 IncomingArgSizeInBytes
= std::min(getABI().GetCalleeAllocdArgSizeInBytes(CC
),
1484 IncomingArgSizeInBytes
);
1486 MF
->getInfo
<MipsFunctionInfo
>()->setFormalArgInfo(IncomingArgSizeInBytes
,
1492 bool MipsFastISel::fastLowerCall(CallLoweringInfo
&CLI
) {
1493 CallingConv::ID CC
= CLI
.CallConv
;
1494 bool IsTailCall
= CLI
.IsTailCall
;
1495 bool IsVarArg
= CLI
.IsVarArg
;
1496 const Value
*Callee
= CLI
.Callee
;
1497 MCSymbol
*Symbol
= CLI
.Symbol
;
1499 // Do not handle FastCC.
1500 if (CC
== CallingConv::Fast
)
1503 // Allow SelectionDAG isel to handle tail calls.
1507 // Let SDISel handle vararg functions.
1511 // FIXME: Only handle *simple* calls for now.
1513 if (CLI
.RetTy
->isVoidTy())
1514 RetVT
= MVT::isVoid
;
1515 else if (!isTypeSupported(CLI
.RetTy
, RetVT
))
1518 for (auto Flag
: CLI
.OutFlags
)
1519 if (Flag
.isInReg() || Flag
.isSRet() || Flag
.isNest() || Flag
.isByVal())
1522 // Set up the argument vectors.
1523 SmallVector
<MVT
, 16> OutVTs
;
1524 OutVTs
.reserve(CLI
.OutVals
.size());
1526 for (auto *Val
: CLI
.OutVals
) {
1528 if (!isTypeLegal(Val
->getType(), VT
) &&
1529 !(VT
== MVT::i1
|| VT
== MVT::i8
|| VT
== MVT::i16
))
1532 // We don't handle vector parameters yet.
1533 if (VT
.isVector() || VT
.getSizeInBits() > 64)
1536 OutVTs
.push_back(VT
);
1540 if (!computeCallAddress(Callee
, Addr
))
1543 // Handle the arguments now that we've gotten them.
1545 if (!processCallArgs(CLI
, OutVTs
, NumBytes
))
1548 if (!Addr
.getGlobalValue())
1552 unsigned DestAddress
;
1554 DestAddress
= materializeExternalCallSym(Symbol
);
1556 DestAddress
= materializeGV(Addr
.getGlobalValue(), MVT::i32
);
1557 emitInst(TargetOpcode::COPY
, Mips::T9
).addReg(DestAddress
);
1558 MachineInstrBuilder MIB
=
1559 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, TII
.get(Mips::JALR
),
1560 Mips::RA
).addReg(Mips::T9
);
1562 // Add implicit physical register uses to the call.
1563 for (auto Reg
: CLI
.OutRegs
)
1564 MIB
.addReg(Reg
, RegState::Implicit
);
1566 // Add a register mask with the call-preserved registers.
1567 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1568 MIB
.addRegMask(TRI
.getCallPreservedMask(*FuncInfo
.MF
, CC
));
1572 if (EmitJalrReloc
&& !Subtarget
->inMips16Mode()) {
1573 // Attach callee address to the instruction, let asm printer emit
1574 // .reloc R_MIPS_JALR.
1576 MIB
.addSym(Symbol
, MipsII::MO_JALR
);
1578 MIB
.addSym(FuncInfo
.MF
->getContext().getOrCreateSymbol(
1579 Addr
.getGlobalValue()->getName()), MipsII::MO_JALR
);
1582 // Finish off the call including any return values.
1583 return finishCall(CLI
, RetVT
, NumBytes
);
1586 bool MipsFastISel::fastLowerIntrinsicCall(const IntrinsicInst
*II
) {
1587 switch (II
->getIntrinsicID()) {
1590 case Intrinsic::bswap
: {
1591 Type
*RetTy
= II
->getCalledFunction()->getReturnType();
1594 if (!isTypeSupported(RetTy
, VT
))
1597 unsigned SrcReg
= getRegForValue(II
->getOperand(0));
1600 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
1603 if (VT
== MVT::i16
) {
1604 if (Subtarget
->hasMips32r2()) {
1605 emitInst(Mips::WSBH
, DestReg
).addReg(SrcReg
);
1606 updateValueMap(II
, DestReg
);
1609 unsigned TempReg
[3];
1610 for (int i
= 0; i
< 3; i
++) {
1611 TempReg
[i
] = createResultReg(&Mips::GPR32RegClass
);
1612 if (TempReg
[i
] == 0)
1615 emitInst(Mips::SLL
, TempReg
[0]).addReg(SrcReg
).addImm(8);
1616 emitInst(Mips::SRL
, TempReg
[1]).addReg(SrcReg
).addImm(8);
1617 emitInst(Mips::OR
, TempReg
[2]).addReg(TempReg
[0]).addReg(TempReg
[1]);
1618 emitInst(Mips::ANDi
, DestReg
).addReg(TempReg
[2]).addImm(0xFFFF);
1619 updateValueMap(II
, DestReg
);
1622 } else if (VT
== MVT::i32
) {
1623 if (Subtarget
->hasMips32r2()) {
1624 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
1625 emitInst(Mips::WSBH
, TempReg
).addReg(SrcReg
);
1626 emitInst(Mips::ROTR
, DestReg
).addReg(TempReg
).addImm(16);
1627 updateValueMap(II
, DestReg
);
1630 unsigned TempReg
[8];
1631 for (int i
= 0; i
< 8; i
++) {
1632 TempReg
[i
] = createResultReg(&Mips::GPR32RegClass
);
1633 if (TempReg
[i
] == 0)
1637 emitInst(Mips::SRL
, TempReg
[0]).addReg(SrcReg
).addImm(8);
1638 emitInst(Mips::SRL
, TempReg
[1]).addReg(SrcReg
).addImm(24);
1639 emitInst(Mips::ANDi
, TempReg
[2]).addReg(TempReg
[0]).addImm(0xFF00);
1640 emitInst(Mips::OR
, TempReg
[3]).addReg(TempReg
[1]).addReg(TempReg
[2]);
1642 emitInst(Mips::ANDi
, TempReg
[4]).addReg(SrcReg
).addImm(0xFF00);
1643 emitInst(Mips::SLL
, TempReg
[5]).addReg(TempReg
[4]).addImm(8);
1645 emitInst(Mips::SLL
, TempReg
[6]).addReg(SrcReg
).addImm(24);
1646 emitInst(Mips::OR
, TempReg
[7]).addReg(TempReg
[3]).addReg(TempReg
[5]);
1647 emitInst(Mips::OR
, DestReg
).addReg(TempReg
[6]).addReg(TempReg
[7]);
1648 updateValueMap(II
, DestReg
);
1654 case Intrinsic::memcpy
:
1655 case Intrinsic::memmove
: {
1656 const auto *MTI
= cast
<MemTransferInst
>(II
);
1657 // Don't handle volatile.
1658 if (MTI
->isVolatile())
1660 if (!MTI
->getLength()->getType()->isIntegerTy(32))
1662 const char *IntrMemName
= isa
<MemCpyInst
>(II
) ? "memcpy" : "memmove";
1663 return lowerCallTo(II
, IntrMemName
, II
->getNumArgOperands() - 1);
1665 case Intrinsic::memset
: {
1666 const MemSetInst
*MSI
= cast
<MemSetInst
>(II
);
1667 // Don't handle volatile.
1668 if (MSI
->isVolatile())
1670 if (!MSI
->getLength()->getType()->isIntegerTy(32))
1672 return lowerCallTo(II
, "memset", II
->getNumArgOperands() - 1);
1678 bool MipsFastISel::selectRet(const Instruction
*I
) {
1679 const Function
&F
= *I
->getParent()->getParent();
1680 const ReturnInst
*Ret
= cast
<ReturnInst
>(I
);
1682 LLVM_DEBUG(dbgs() << "selectRet\n");
1684 if (!FuncInfo
.CanLowerReturn
)
1687 // Build a list of return value registers.
1688 SmallVector
<unsigned, 4> RetRegs
;
1690 if (Ret
->getNumOperands() > 0) {
1691 CallingConv::ID CC
= F
.getCallingConv();
1693 // Do not handle FastCC.
1694 if (CC
== CallingConv::Fast
)
1697 SmallVector
<ISD::OutputArg
, 4> Outs
;
1698 GetReturnInfo(CC
, F
.getReturnType(), F
.getAttributes(), Outs
, TLI
, DL
);
1700 // Analyze operands of the call, assigning locations to each operand.
1701 SmallVector
<CCValAssign
, 16> ValLocs
;
1702 MipsCCState
CCInfo(CC
, F
.isVarArg(), *FuncInfo
.MF
, ValLocs
,
1704 CCAssignFn
*RetCC
= RetCC_Mips
;
1705 CCInfo
.AnalyzeReturn(Outs
, RetCC
);
1707 // Only handle a single return value for now.
1708 if (ValLocs
.size() != 1)
1711 CCValAssign
&VA
= ValLocs
[0];
1712 const Value
*RV
= Ret
->getOperand(0);
1714 // Don't bother handling odd stuff for now.
1715 if ((VA
.getLocInfo() != CCValAssign::Full
) &&
1716 (VA
.getLocInfo() != CCValAssign::BCvt
))
1719 // Only handle register returns for now.
1723 unsigned Reg
= getRegForValue(RV
);
1727 unsigned SrcReg
= Reg
+ VA
.getValNo();
1728 Register DestReg
= VA
.getLocReg();
1729 // Avoid a cross-class copy. This is very unlikely.
1730 if (!MRI
.getRegClass(SrcReg
)->contains(DestReg
))
1733 EVT RVEVT
= TLI
.getValueType(DL
, RV
->getType());
1734 if (!RVEVT
.isSimple())
1737 if (RVEVT
.isVector())
1740 MVT RVVT
= RVEVT
.getSimpleVT();
1741 if (RVVT
== MVT::f128
)
1744 // Do not handle FGR64 returns for now.
1745 if (RVVT
== MVT::f64
&& UnsupportedFPMode
) {
1746 LLVM_DEBUG(dbgs() << ".. .. gave up (UnsupportedFPMode\n");
1750 MVT DestVT
= VA
.getValVT();
1751 // Special handling for extended integers.
1752 if (RVVT
!= DestVT
) {
1753 if (RVVT
!= MVT::i1
&& RVVT
!= MVT::i8
&& RVVT
!= MVT::i16
)
1756 if (Outs
[0].Flags
.isZExt() || Outs
[0].Flags
.isSExt()) {
1757 bool IsZExt
= Outs
[0].Flags
.isZExt();
1758 SrcReg
= emitIntExt(RVVT
, SrcReg
, DestVT
, IsZExt
);
1765 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
,
1766 TII
.get(TargetOpcode::COPY
), DestReg
).addReg(SrcReg
);
1768 // Add register to return instruction.
1769 RetRegs
.push_back(VA
.getLocReg());
1771 MachineInstrBuilder MIB
= emitInst(Mips::RetRA
);
1772 for (unsigned i
= 0, e
= RetRegs
.size(); i
!= e
; ++i
)
1773 MIB
.addReg(RetRegs
[i
], RegState::Implicit
);
1777 bool MipsFastISel::selectTrunc(const Instruction
*I
) {
1778 // The high bits for a type smaller than the register size are assumed to be
1780 Value
*Op
= I
->getOperand(0);
1783 SrcVT
= TLI
.getValueType(DL
, Op
->getType(), true);
1784 DestVT
= TLI
.getValueType(DL
, I
->getType(), true);
1786 if (SrcVT
!= MVT::i32
&& SrcVT
!= MVT::i16
&& SrcVT
!= MVT::i8
)
1788 if (DestVT
!= MVT::i16
&& DestVT
!= MVT::i8
&& DestVT
!= MVT::i1
)
1791 unsigned SrcReg
= getRegForValue(Op
);
1795 // Because the high bits are undefined, a truncate doesn't generate
1797 updateValueMap(I
, SrcReg
);
1801 bool MipsFastISel::selectIntExt(const Instruction
*I
) {
1802 Type
*DestTy
= I
->getType();
1803 Value
*Src
= I
->getOperand(0);
1804 Type
*SrcTy
= Src
->getType();
1806 bool isZExt
= isa
<ZExtInst
>(I
);
1807 unsigned SrcReg
= getRegForValue(Src
);
1811 EVT SrcEVT
, DestEVT
;
1812 SrcEVT
= TLI
.getValueType(DL
, SrcTy
, true);
1813 DestEVT
= TLI
.getValueType(DL
, DestTy
, true);
1814 if (!SrcEVT
.isSimple())
1816 if (!DestEVT
.isSimple())
1819 MVT SrcVT
= SrcEVT
.getSimpleVT();
1820 MVT DestVT
= DestEVT
.getSimpleVT();
1821 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1823 if (!emitIntExt(SrcVT
, SrcReg
, DestVT
, ResultReg
, isZExt
))
1825 updateValueMap(I
, ResultReg
);
1829 bool MipsFastISel::emitIntSExt32r1(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1832 switch (SrcVT
.SimpleTy
) {
1842 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
1843 emitInst(Mips::SLL
, TempReg
).addReg(SrcReg
).addImm(ShiftAmt
);
1844 emitInst(Mips::SRA
, DestReg
).addReg(TempReg
).addImm(ShiftAmt
);
1848 bool MipsFastISel::emitIntSExt32r2(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1850 switch (SrcVT
.SimpleTy
) {
1854 emitInst(Mips::SEB
, DestReg
).addReg(SrcReg
);
1857 emitInst(Mips::SEH
, DestReg
).addReg(SrcReg
);
1863 bool MipsFastISel::emitIntSExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1865 if ((DestVT
!= MVT::i32
) && (DestVT
!= MVT::i16
))
1867 if (Subtarget
->hasMips32r2())
1868 return emitIntSExt32r2(SrcVT
, SrcReg
, DestVT
, DestReg
);
1869 return emitIntSExt32r1(SrcVT
, SrcReg
, DestVT
, DestReg
);
1872 bool MipsFastISel::emitIntZExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1876 switch (SrcVT
.SimpleTy
) {
1890 emitInst(Mips::ANDi
, DestReg
).addReg(SrcReg
).addImm(Imm
);
1894 bool MipsFastISel::emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1895 unsigned DestReg
, bool IsZExt
) {
1896 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1897 // DestVT are odd things, so test to make sure that they are both types we can
1898 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1899 // bail out to SelectionDAG.
1900 if (((DestVT
!= MVT::i8
) && (DestVT
!= MVT::i16
) && (DestVT
!= MVT::i32
)) ||
1901 ((SrcVT
!= MVT::i1
) && (SrcVT
!= MVT::i8
) && (SrcVT
!= MVT::i16
)))
1904 return emitIntZExt(SrcVT
, SrcReg
, DestVT
, DestReg
);
1905 return emitIntSExt(SrcVT
, SrcReg
, DestVT
, DestReg
);
1908 unsigned MipsFastISel::emitIntExt(MVT SrcVT
, unsigned SrcReg
, MVT DestVT
,
1910 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
1911 bool Success
= emitIntExt(SrcVT
, SrcReg
, DestVT
, DestReg
, isZExt
);
1912 return Success
? DestReg
: 0;
1915 bool MipsFastISel::selectDivRem(const Instruction
*I
, unsigned ISDOpcode
) {
1916 EVT DestEVT
= TLI
.getValueType(DL
, I
->getType(), true);
1917 if (!DestEVT
.isSimple())
1920 MVT DestVT
= DestEVT
.getSimpleVT();
1921 if (DestVT
!= MVT::i32
)
1925 switch (ISDOpcode
) {
1930 DivOpc
= Mips::SDIV
;
1934 DivOpc
= Mips::UDIV
;
1938 unsigned Src0Reg
= getRegForValue(I
->getOperand(0));
1939 unsigned Src1Reg
= getRegForValue(I
->getOperand(1));
1940 if (!Src0Reg
|| !Src1Reg
)
1943 emitInst(DivOpc
).addReg(Src0Reg
).addReg(Src1Reg
);
1944 emitInst(Mips::TEQ
).addReg(Src1Reg
).addReg(Mips::ZERO
).addImm(7);
1946 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1950 unsigned MFOpc
= (ISDOpcode
== ISD::SREM
|| ISDOpcode
== ISD::UREM
)
1953 emitInst(MFOpc
, ResultReg
);
1955 updateValueMap(I
, ResultReg
);
1959 bool MipsFastISel::selectShift(const Instruction
*I
) {
1962 if (!isTypeSupported(I
->getType(), RetVT
))
1965 unsigned ResultReg
= createResultReg(&Mips::GPR32RegClass
);
1969 unsigned Opcode
= I
->getOpcode();
1970 const Value
*Op0
= I
->getOperand(0);
1971 unsigned Op0Reg
= getRegForValue(Op0
);
1975 // If AShr or LShr, then we need to make sure the operand0 is sign extended.
1976 if (Opcode
== Instruction::AShr
|| Opcode
== Instruction::LShr
) {
1977 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
1981 MVT Op0MVT
= TLI
.getValueType(DL
, Op0
->getType(), true).getSimpleVT();
1982 bool IsZExt
= Opcode
== Instruction::LShr
;
1983 if (!emitIntExt(Op0MVT
, Op0Reg
, MVT::i32
, TempReg
, IsZExt
))
1989 if (const auto *C
= dyn_cast
<ConstantInt
>(I
->getOperand(1))) {
1990 uint64_t ShiftVal
= C
->getZExtValue();
1994 llvm_unreachable("Unexpected instruction.");
1995 case Instruction::Shl
:
1998 case Instruction::AShr
:
2001 case Instruction::LShr
:
2006 emitInst(Opcode
, ResultReg
).addReg(Op0Reg
).addImm(ShiftVal
);
2007 updateValueMap(I
, ResultReg
);
2011 unsigned Op1Reg
= getRegForValue(I
->getOperand(1));
2017 llvm_unreachable("Unexpected instruction.");
2018 case Instruction::Shl
:
2019 Opcode
= Mips::SLLV
;
2021 case Instruction::AShr
:
2022 Opcode
= Mips::SRAV
;
2024 case Instruction::LShr
:
2025 Opcode
= Mips::SRLV
;
2029 emitInst(Opcode
, ResultReg
).addReg(Op0Reg
).addReg(Op1Reg
);
2030 updateValueMap(I
, ResultReg
);
2034 bool MipsFastISel::fastSelectInstruction(const Instruction
*I
) {
2035 switch (I
->getOpcode()) {
2038 case Instruction::Load
:
2039 return selectLoad(I
);
2040 case Instruction::Store
:
2041 return selectStore(I
);
2042 case Instruction::SDiv
:
2043 if (!selectBinaryOp(I
, ISD::SDIV
))
2044 return selectDivRem(I
, ISD::SDIV
);
2046 case Instruction::UDiv
:
2047 if (!selectBinaryOp(I
, ISD::UDIV
))
2048 return selectDivRem(I
, ISD::UDIV
);
2050 case Instruction::SRem
:
2051 if (!selectBinaryOp(I
, ISD::SREM
))
2052 return selectDivRem(I
, ISD::SREM
);
2054 case Instruction::URem
:
2055 if (!selectBinaryOp(I
, ISD::UREM
))
2056 return selectDivRem(I
, ISD::UREM
);
2058 case Instruction::Shl
:
2059 case Instruction::LShr
:
2060 case Instruction::AShr
:
2061 return selectShift(I
);
2062 case Instruction::And
:
2063 case Instruction::Or
:
2064 case Instruction::Xor
:
2065 return selectLogicalOp(I
);
2066 case Instruction::Br
:
2067 return selectBranch(I
);
2068 case Instruction::Ret
:
2069 return selectRet(I
);
2070 case Instruction::Trunc
:
2071 return selectTrunc(I
);
2072 case Instruction::ZExt
:
2073 case Instruction::SExt
:
2074 return selectIntExt(I
);
2075 case Instruction::FPTrunc
:
2076 return selectFPTrunc(I
);
2077 case Instruction::FPExt
:
2078 return selectFPExt(I
);
2079 case Instruction::FPToSI
:
2080 return selectFPToInt(I
, /*isSigned*/ true);
2081 case Instruction::FPToUI
:
2082 return selectFPToInt(I
, /*isSigned*/ false);
2083 case Instruction::ICmp
:
2084 case Instruction::FCmp
:
2085 return selectCmp(I
);
2086 case Instruction::Select
:
2087 return selectSelect(I
);
2092 unsigned MipsFastISel::getRegEnsuringSimpleIntegerWidening(const Value
*V
,
2094 unsigned VReg
= getRegForValue(V
);
2097 MVT VMVT
= TLI
.getValueType(DL
, V
->getType(), true).getSimpleVT();
2099 if (VMVT
== MVT::i1
)
2102 if ((VMVT
== MVT::i8
) || (VMVT
== MVT::i16
)) {
2103 unsigned TempReg
= createResultReg(&Mips::GPR32RegClass
);
2104 if (!emitIntExt(VMVT
, VReg
, MVT::i32
, TempReg
, IsUnsigned
))
2111 void MipsFastISel::simplifyAddress(Address
&Addr
) {
2112 if (!isInt
<16>(Addr
.getOffset())) {
2114 materialize32BitInt(Addr
.getOffset(), &Mips::GPR32RegClass
);
2115 unsigned DestReg
= createResultReg(&Mips::GPR32RegClass
);
2116 emitInst(Mips::ADDu
, DestReg
).addReg(TempReg
).addReg(Addr
.getReg());
2117 Addr
.setReg(DestReg
);
2122 unsigned MipsFastISel::fastEmitInst_rr(unsigned MachineInstOpcode
,
2123 const TargetRegisterClass
*RC
,
2124 unsigned Op0
, unsigned Op1
) {
2125 // We treat the MUL instruction in a special way because it clobbers
2126 // the HI0 & LO0 registers. The TableGen definition of this instruction can
2127 // mark these registers only as implicitly defined. As a result, the
2128 // register allocator runs out of registers when this instruction is
2129 // followed by another instruction that defines the same registers too.
2130 // We can fix this by explicitly marking those registers as dead.
2131 if (MachineInstOpcode
== Mips::MUL
) {
2132 unsigned ResultReg
= createResultReg(RC
);
2133 const MCInstrDesc
&II
= TII
.get(MachineInstOpcode
);
2134 Op0
= constrainOperandRegClass(II
, Op0
, II
.getNumDefs());
2135 Op1
= constrainOperandRegClass(II
, Op1
, II
.getNumDefs() + 1);
2136 BuildMI(*FuncInfo
.MBB
, FuncInfo
.InsertPt
, DbgLoc
, II
, ResultReg
)
2139 .addReg(Mips::HI0
, RegState::ImplicitDefine
| RegState::Dead
)
2140 .addReg(Mips::LO0
, RegState::ImplicitDefine
| RegState::Dead
);
2144 return FastISel::fastEmitInst_rr(MachineInstOpcode
, RC
, Op0
, Op1
);
2149 FastISel
*Mips::createFastISel(FunctionLoweringInfo
&funcInfo
,
2150 const TargetLibraryInfo
*libInfo
) {
2151 return new MipsFastISel(funcInfo
, libInfo
);
2154 } // end namespace llvm